r/javascript • u/davemo Front-End Engineer • Aug 09 '12
A JavaScript Security Flaw
http://blog.alexmaccaw.com/a-javascript-security-flaw4
u/bonafidebob Aug 09 '12
I think the description confuses the render tree with DOM tree, which made me re-read it several times. This is 'just' a HTML parsing problem.
The first occurrence of the sequence </script inside a <script> element ends the element. The HTML parser is not even supposed to try to parse javascript, it does not and should not know about JS syntax.
But, yeah, inserting arbitrary strings into <script> elements is a problem.
4
Aug 10 '12
I don't understand, what is the security flaw here? Considering anyone can run arbitrary JavaScript through the developer console or the address bar, I don't understand why someone being able to run JavaScript in this instance is any different.
1
Aug 12 '12
After thinking about this more, I think I understand the security risk. The idea isn't to get the site to run your JavaScript on your machine, but rather on someone else's machine. For example, let's say I could do this trick on my facebook profile, and the code would run anytime someone looked at my profile. That would give me a hook to view their cookies or do something else malicious.
2
2
u/mythril no(fun).at(parties); Aug 10 '12
Sorry to be nitpicky, but can you show me what part of the Ecmascript spec causes this? Or what section of V8 or SpiderMonkey have this flaw?
If you find yourself scratching your head, think about what sits between your server and the JavaScript in this case.
If you still don't have it see if the same problem persists if you extract the JavaScript into a separate file and include it with a src="" attribute.
2
u/kenman Aug 10 '12
Showing my age perhaps, but this was a common pattern 'back in the day':
document.write('<scr' + 'ipt src="foo.js"></scr' + 'ipt>');You kids and your fancy loaders and whatnot!
1
u/ctrldavid Aug 09 '12
Note: The "flaw" doesn't happen in an xml serialised page when the code is wrapped in a CDATA section.
2
u/ctrldavid Aug 09 '12
Otherwise you should be escaping your
<and>characters:
var str = "</script><script>alert('Pwned');</script>";1
11
u/[deleted] Aug 10 '12
This is basic, ground-level XSS. As basic as it gets. It's not a 'security flaw' in javascript so much as it is the expected behavior of embedding javascript in an HTML page. And if you are trusting user input without filtering, you are doing it wrong.
Also, the title is misleading.