Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  175] [ 2]  / answers: 1 / hits: 25105  / 13 Years ago, tue, november 29, 2011, 12:00:00

I'm testing xss attacks on my own code. The example beneath is a simple box where an user can type whatever he wants. After pressing test! button, JS will show the input string into two divs.This is an example I made to explain better my question:



<html>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script>
<script type=text/javascript>
function testIt(){
var input = document.getElementById('input-test').value;
var testHtml = document.getElementById('test-html');
var testInnerHTML = document.getElementById('test-innerHTML');
$(testHtml).html(input);
testInnerHTML.innerHTML = input;
}
</script>
<head>this is a test</head>
<body>
<input id=input-test type=text name=foo />
<input type=button onClick=testIt(); value=test!/>
<div id=test-html>
</div>
<div id=test-innerHTML>
</div>
</body>




if you try to copy it into a .html file and run it, it will work fine, but if you try to input <script>alert('xss')</script>, only one alert box will be thrown: the one inside `test-html' div (with html() function).



I really can't understand why this is happening, and also, inspecting the code with firebug gives me this result (after injecting the script)



<body>
this is a test
<input id=input-test type=text name=foo>
<input type=button value=test! onclick=testIt();>
<div id=test-html> </div>
<div id=test-innerHTML>
<script>
alert('xss')
</script>
</div>
</body>


as you can see test-html div is empty, and test-innerhtml div contans the script. Can someone tell me why? Is because html() is more secure against scripts injection or something similar?



Thanks in advance, best regards.


More From » jquery

 Answers
66

JQuery strips out the script tags, which is why you aren't seeing it append to the dom let alone executing.



To see an explanation of why jquery strips it out, you can see John Resig's reply here: https://forum.jquery.com/topic/jquery-dommanip-script-tag-will-be-removed



Hope this helps


[#88837] Monday, November 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsleyashlynnh

Total Points: 64
Total Questions: 119
Total Answers: 98

Location: Burundi
Member since Sat, Aug 21, 2021
3 Years ago
;