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.

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

Preventing XSS Attacks<br />

Despite <strong>the</strong> various manifestations of XSS, and <strong>the</strong> different possibilities for<br />

exploitation, preventing <strong>the</strong> vulnerability itself is in fact conceptually straightforward.<br />

What makes it problematic in practice is <strong>the</strong> difficulty of identifying every<br />

instance in which user-controllable data is handled in a potentially dangerous<br />

way. Any given page of an <strong>application</strong> may process and display dozens of items<br />

of user data. In addition to <strong>the</strong> core functionality, vulnerabilities may arise in<br />

error messages and o<strong>the</strong>r locations. It is hardly surprising, <strong>the</strong>refore, that XSS<br />

flaws are so hugely prevalent, even in <strong>the</strong> most security-critical <strong>application</strong>s.<br />

Different types of defense are applicable to reflected and stored XSS on <strong>the</strong> one<br />

hand, and to DOM-based XSS on <strong>the</strong> o<strong>the</strong>r, because of <strong>the</strong>ir different root causes.<br />

Preventing Reflected and Stored XSS<br />

The root cause of both reflected and stored XSS is that user-controllable data is<br />

copied into <strong>application</strong> responses without adequate validation and sanitization.<br />

Because <strong>the</strong> data is being inserted into <strong>the</strong> raw source code of an HTML page,<br />

malicious data can interfere with that page, modifying not only its content but<br />

also its structure — breaking out of quoted strings, opening and closing tags,<br />

injecting scripts, and so on.<br />

To eliminate reflected and stored XSS vulnerabilities, <strong>the</strong> first step is to identify<br />

every instance within <strong>the</strong> <strong>application</strong> where user-controllable data is being<br />

copied into responses. This includes data that is copied from <strong>the</strong> immediate<br />

request and also any stored data that originated from any user at any prior time,<br />

including via out-of-band channels. To ensure that every instance is identified,<br />

<strong>the</strong>re is no real substitute for a close review of all <strong>application</strong> source code.<br />

Having identified all <strong>the</strong> operations that are potentially at risk of XSS and<br />

that need to be suitably defended, you should follow a threefold approach to<br />

prevent any actual vulnerabilities from arising:<br />

n Validate input.<br />

n Validate output.<br />

n Eliminate dangerous insertion points.<br />

One caveat to this approach arises where an <strong>application</strong> needs to let users<br />

author content in HTML format, such as a blogging <strong>application</strong> that allows<br />

HTML in comments. Some specific considerations relating to this situation are<br />

discussed after general defensive techniques have been described.<br />

Validate Input<br />

At <strong>the</strong> point where <strong>the</strong> <strong>application</strong> receives user-supplied data that may be copied<br />

into one of its responses at any future point, <strong>the</strong> <strong>application</strong> should perform

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

Saved successfully!

Ooh no, something went wrong!