19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 n Attacking Users: Cross-Site Scripting 495<br />

output validation are subject to bypasses. By employing both techniques, <strong>the</strong><br />

<strong>application</strong> gains some additional assurance that an attacker will be defeated<br />

even if one of its two filters is found to be defective. Of <strong>the</strong> two defenses, <strong>the</strong><br />

output validation is <strong>the</strong> most important and is mandatory. Performing strict<br />

input validation should be viewed as a secondary failover.<br />

Of course, when devising <strong>the</strong> input and output validation logic itself, great<br />

care should be taken to avoid any vulnerabilities that lead to bypasses. In particular,<br />

filtering and encoding should be carried out after any relevant canonicalization,<br />

and <strong>the</strong> data should not be fur<strong>the</strong>r canonicalized afterwards. The<br />

<strong>application</strong> should also ensure that <strong>the</strong> presence of any NULL bytes does not<br />

interfere with its validation.<br />

Eliminate Dangerous Insertion Points<br />

There are some locations within <strong>the</strong> <strong>application</strong> page where it is just too inherently<br />

dangerous to insert user-supplied input, and developers should look for<br />

an alternative means of implementing <strong>the</strong> desired functionality.<br />

Inserting user-controllable data directly into existing script code should be<br />

avoided wherever possible. This applies to code within tags, and also<br />

code within event handlers. When <strong>application</strong>s attempt to do this safely, it is<br />

frequently possible to bypass <strong>the</strong>ir defensive filters. And once an attacker has<br />

taken control of <strong>the</strong> context of <strong>the</strong> data he controls, he typically needs to perform<br />

minimal work to inject arbitrary script commands and <strong>the</strong>refore perform<br />

malicious actions.<br />

Where a tag attribute may take a URL as its value, <strong>application</strong>s should generally<br />

avoid embedding user input, because various techniques may be used to<br />

introduce script code, including <strong>the</strong> use of scripting pseudo-protocols.<br />

A fur<strong>the</strong>r pitfall to avoid is situations where an attacker can manipulate <strong>the</strong><br />

character set of <strong>the</strong> <strong>application</strong>’s response, ei<strong>the</strong>r by injecting into a relevant<br />

directive or because <strong>the</strong> <strong>application</strong> uses a request parameter to specify <strong>the</strong><br />

preferred character set. In this situation, input and output filters that are well<br />

designed in o<strong>the</strong>r respects may fail because <strong>the</strong> attacker’s input is encoded<br />

in an unusual form that <strong>the</strong> filters do not recognize as potentially malicious.<br />

Wherever possible, <strong>the</strong> <strong>application</strong> should explicitly specify an encoding type<br />

in its response headers, disallow any means of modifying this, and ensure that<br />

its XSS filters are compatible with it. For example:<br />

Content-Type: text/html; charset=ISO-8859-1<br />

Allowing Limited HTML<br />

Some <strong>application</strong>s need to let users submit data in HTML format that will be<br />

inserted into <strong>application</strong> responses. For example, a blogging <strong>application</strong> may

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!