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.

524 Chapter 13 n Attacking Users: O<strong>the</strong>r Techniques<br />

Fur<strong>the</strong>rmore, in a technique similar to <strong>the</strong> CSS injection attack described<br />

previously, it was sometimes possible to inject text at appropriate points within<br />

a target <strong>application</strong>’s HTML response to wrap an arbitrary {...} block around<br />

sensitive data contained within that response. The whole response could <strong>the</strong>n<br />

be included cross-domain as a script to capture <strong>the</strong> wrapped data.<br />

Nei<strong>the</strong>r of <strong>the</strong> attacks just described works on current browsers. As this process<br />

continues, and browser support for novel syntactic constructs is fur<strong>the</strong>r<br />

extended, it is likely that new kinds of cross-domain data capture will become<br />

possible, targeting <strong>application</strong>s that were not vulnerable to <strong>the</strong>se attacks before<br />

<strong>the</strong> new browser features were introduced.<br />

Preventing JavaScript Hijacking<br />

Several preconditions must be in place before a JavaScript hijacking attack can<br />

be performed. To prevent such attacks, it is necessary to violate at least one<br />

of <strong>the</strong>se preconditions. To provide defense-in-depth, it is recommended that<br />

multiple precautions be implemented jointly:<br />

n As for requests that perform sensitive actions, <strong>the</strong> <strong>application</strong> should<br />

use standard anti-CSRF defenses to prevent cross-domain requests from<br />

returning any responses containing sensitive data.<br />

n When an <strong>application</strong> dynamically executes JavaScript code from its own<br />

domain, it is not restricted to using tags to include <strong>the</strong> script.<br />

Because <strong>the</strong> request is on-site, client-side code can use XMLHttpRequest to<br />

retrieve <strong>the</strong> raw response and perform additional processing on it before it<br />

is executed as script. This means that <strong>the</strong> <strong>application</strong> can insert invalid or<br />

problematic JavaScript at <strong>the</strong> start of <strong>the</strong> response, which <strong>the</strong> client <strong>application</strong><br />

removes before it is processed. For example, <strong>the</strong> following code causes<br />

an infinite loop when executed using a script include but can be stripped<br />

before execution when <strong>the</strong> script is accessed using XMLHttpRequest:<br />

for(;;);<br />

n Because <strong>the</strong> <strong>application</strong> can use XMLHttpRequest to retrieve dynamic script<br />

code, it can use POST requests to do so. If <strong>the</strong> <strong>application</strong> accepts only POST<br />

requests for potentially vulnerable script code, it prevents third-party<br />

sites from including <strong>the</strong>m using tags.<br />

The Same-Origin Policy Revisited<br />

This chapter and <strong>the</strong> preceding one have described numerous examples of<br />

how <strong>the</strong> same-origin policy is applied to HTML and JavaScript, and ways<br />

in which it can be circumvented via <strong>application</strong> bugs and browser quirks.

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

Saved successfully!

Ooh no, something went wrong!