21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.10 Preventing Cross-Site Scripting<br />

<strong>Problem</strong><br />

You are developing a web-based application, and you want to ensure that an attacker<br />

cannot exploit it in an effort to steal information from the browsers of other people<br />

visiting the same site.<br />

Solution<br />

When you are generating HTMLthat must contain external input, be sure to escape<br />

that input so that if it contains embedded HTMLtags, the tags are not treated as<br />

HTML by the browser.<br />

Discussion<br />

Cross-site scripting attacks (often called CSS, but more frequently XSS in an effort to<br />

avoid confusion with cascading style sheets) are a general class of attacks with a common<br />

root cause: insufficient input validation. The goal of many cross-site scripting<br />

attacks is to steal information (usually the contents of some specific cookie) from<br />

unsuspecting users. Other times, the goal is to get an unsuspecting user to launch an<br />

attack on himself. These attacks are especially a problem for sites that store sensitive<br />

information, such as login data or session IDs, in cookies. Cookie theft could allow<br />

an attacker to hijack a session or glean other information that is intended to be private.<br />

Consider, for example, a web-based message board, where many different people<br />

visit the site to read the messages that other people have posted, and to post messages<br />

themselves. When someone posts a new message to the board, if the message<br />

board software does not properly validate the input, the message could contain malicious<br />

HTMLthat, when viewed by other people, performs some unexpected action.<br />

Usually an attacker will attempt to embed some JavaScript code that steals cookies,<br />

or something similar.<br />

Often, an attacker has to go to greater lengths to exploit a cross-site script vulnerability;<br />

the example described above is simplistic. An attacker can exploit any page that<br />

will include unescaped user input, but usually the attacker has to trick the user into<br />

displaying that page somehow. Attackers use many methods to accomplish this goal,<br />

such as fake pages that look like part of the site from which the attacker wishes to<br />

steal cookies, or embedded links in innocent-looking email messages.<br />

It is not generally a good idea to allow users to embed HTMLin any input accepted<br />

from them, but many sites allow simple tags in some input, such as those that enable<br />

bold or italics on text. Disallowing HTMLaltogether is the right solution in most<br />

Preventing Cross-Site Scripting | 103<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!