r/javascript • Front-End Engineer • Aug 09 '12

A JavaScript Security Flaw

http://blog.alexmaccaw.com/a-javascript-security-flaw
0 Upvotes

13 comments sorted by

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.

-5

u/ctrldavid Aug 10 '12

I think you're wrong here. This has nothing to do with XSS. If you set the string via url parameters or an input it will work fine, because the string will contain the correct characters.

The problem is that the author isn't escaping his < and > characters as you should be when you don't want them to be interpreted as a tag.

4

u/[deleted] Aug 10 '12

It is XSS. If the 'string' that has the malicious script tag in it isn't escaped (the fault of the person who created the site) then it could be used to load javascript from another website - this is one way to achieve Cross-Site-Scripting. Take a look at r/xss for numerous other examples.

-3

u/ctrldavid Aug 10 '12

That's exactly it though, the 'string' is created by the person who created the site. You can't XSS your own site. If the string is sourced from user input then it doesn't exist while the HTML is being parsed, and there is no problem.

The article says that the problem arises when you add user controlled strings directly into the page, but that's simply because he isn't escaping the user input. You don't need to sanitise it. Infact you don't even need to escape it if you're using CDATA sections as you should be, or if you use an external JS file.

4

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

u/[deleted] 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

u/[deleted] 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

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 = "&lt;/script&gt;&lt;script&gt;alert('Pwned');&lt;/script&gt;";

1

u/kenman Aug 10 '12

Wow, someone should write that down in a guide somewhere.