4

Ref: html() vs innerHTML jquery/javascript & XSS attacks

From this, I can infer that, Jquery extracts the <script> tags and execute separately in DOM, it doesn't appear in DOM.

Consider the following HTML code:

a = <iframe><iframe //><script>alert(1)</script>

b = <iframe><iframe> //<script>alert(1)</script>

As of the code in a, body.innerHTML = a; doesn't execute the script, but $("body").html(a); does.

Why? Jquery's .html() execute the content after // but .innerHTML = doesn't?

If it is so, why b inside either .innerHTML = or .html() doesn't get executed?

Update: For a demo, open up console, and execute this:

  1. document.body.innerHTML = "<iframe><iframe //><script>alert(1)</script>"
  2. $("body").html("<iframe><iframe //><script>alert(1)</script>");

1 will not execute alert(), but 2 will. Replace the HTML values with b. Neither will get executed.

Update 2: From what I can determine that the HTML code will get executed in Jquery's body() but not in .innerHTML=?

Community
  • 1
  • 1
  • html = hypertext markup language. so i mean your html. – Tommy Schmidt Nov 14 '16 at 08:34
  • @TommySchmidt I get that. I understand this markup is invalid. I just want to know why this different behaviour with `.html()`. – verstappen_doodle Nov 14 '16 at 08:36
  • this could be the reason why the behavior is different. i dont know exactly how jquerry handles that string. maybe it is parsing it when it is inserted and the markup error is causing your issue. normally i would test it before posting such a comment but i am on my phone right now. so maybe you should try that out. i could be totally wrong tho. – Tommy Schmidt Nov 14 '16 at 08:40
  • @Netham I have removed `c` now. Some error in my console due to XSS filters I use, seems `c` executes in .html(). And, `b` **doesn't** seem to execute when put inside `
    `.
    – verstappen_doodle Nov 14 '16 at 08:46