Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing DOM XSS

Tags:

javascript

xss

We recently on-boarded someone else's code which has since been tested, and failed, for DOM XSS attacks. Basically the url fragments are being passed directly into jQuery selectors and enabling JavaScript to be injected, like so:

"http://website.com/#%3Cimg%20src=x%20onerror=alert%28/XSSed/%29%3E)"
$(".selector [thing="+window.location.hash.substr(1)+"]");

The problem is that this is occurring throughout their scripts and would need a lot of regression testing to fix e.g. if we escape the data if statements won't return true any more as the data won't match.

The JavaScript file in question is concatenated at build time from many smaller files so this becomes even more difficult to fix.

Is there a way to prevent these DOM XSS attacks with some global code without having to go through and debug each instance.


I proposed that we add a little regular expression at the top of the script to detect common chars used in XSS attacks and to simply kill the script if it returns true.

 var xss = window.location.href.match(/(javascript|src|onerror|%|<|>)/g);

if(xss != null) return;

This appears to work but I'm not 100% happy with the solution. Does anyone have a better solution or any useful insight they can offer?

like image 843
sidonaldson Avatar asked Nov 08 '12 10:11

sidonaldson


People also ask

What security control should be used to prevent DOM XSS?

The best way to fix DOM based cross-site scripting is to use the right output method (sink). For example if you want to use user input to write in a div tag element don't use innerHtml , instead use innerText or textContent .

How can XSS be prevented?

To prevent XSS attacks, your application must validate all the input data, make sure that only the allowlisted data is allowed, and ensure that all variable output in a page is encoded before it is returned to the user.

Can CSP prevent DOM XSS?

CSP is a browser security mechanism that aims to mitigate XSS and some other attacks. It works by restricting the resources (such as scripts and images) that a page can load and restricting whether a page can be framed by other pages.

What can you do with DOM XSS?

DOM-based XSS is a cross-site scripting vulnerability that enables attackers to inject a malicious payload into a web page by manipulating the client's browser environment. Since these attacks rely on the Document Object Model, they are orchestrated on the client-side after loading the page.


1 Answers

If you stick to the regular expression solution, which is far from ideal but may be the best choice given your constraints:

Rather than defining a regular expression matching malicious hashes (/(javascript|src|onerror|%|<|>)/g), I would define a regular expression matching sound hashes (e.g. /^[\w_-]*$/).

It will avoid false-positive errors (e.g. src_records), make it clear what is authorized and what isn't, and block more complex injection mechanisms.

like image 130
Julien Royer Avatar answered Nov 09 '22 03:11

Julien Royer