Web Development

What is cross-site scripting?

Cross-site scripting, also known as "XSS", is a class of security exploit that has gotten a fair bit of attention in the last few years. Many users, and even Web developers, aren't entirely clear on what the term means, however. I'll explain cross-site scripting for you, so you will know where the dangers lie.

Cross-site scripting, also known as "XSS," is a class of security exploit that has gotten a fair bit of attention in the last few years. Many users, and even Web developers, aren't entirely clear on what the term means, however. I'll explain cross-site scripting for you, so you will know where the dangers lie.

Defining cross-site scripting

JavaScript is a powerful tool for developing rich Web applications. Without client-side execution of code embedded in HTML and XHTML pages, the dynamic nature of Web applications like Google Maps, Try Ruby! and Zoho Office would not be possible. Unfortunately, any time you add complexity to a system, you increase the potential for security issues -- and adding JavaScript to a Web page is no exception.

Among the problems introduced by JavaScript are:

  1. A malicious website might employ JavaScript to make changes to the local system, such as copying or deleting files.
  2. A malicious website might employ JavaScript to monitor activity on the local system, such as with keystroke logging.
  3. A malicious website might employ JavaScript to interact with other Websites the user has open in other browser windows or tabs.

The first and second problems in the above list can be mitigated by turning the browser into a sort of "sandbox" that limits the way JavaScript is allowed to behave so that it only works within the browser's little world. The third can be limited somewhat as well, but it is all too easy to get around that limitation because whether a particular webpage can interact with another webpage in a given manner may not be something that can be controlled by the software employed by the end user. Sometimes, the ability of one website's JavaScript to steal data meant for another Website can only be limited by the due diligence of the other website's developers.

The key to defining cross-site scripting is in the fact that vulnerabilities in a given website's use of dynamic Web design elements may give someone the opportunity to use JavaScript for security compromises. It's called "cross-site" because  it involves interactions between two separate websites to achieve its goals. In many cases, however, even though the exploit involves the use of JavaScript, the website that's vulnerable to cross-site scripting exploits does not have to employ JavaScript itself at all. Only in the case of local cross-site scripting exploits does the vulnerability have to exist in JavaScript sent to the browser by a legitimate website.

Types of cross-site scripting

There are currently three major categories of cross-site scripting. Others may be discovered in the future, however, so don't think this sort of misuse of Web page vulnerability is necessarily limited to these three types.

  • Reflected: Probably the most common type of cross-site scripting exploit is the reflected exploit. It targets vulnerabilities that occur in some websites when data submitted by the client is immediately processed by the server to generate results that are then sent back to the browser on the client system. An exploit is successful if it can send code to the server that is included in the Web page results sent back to the browser, and when those results are sent the code is not encoded using HTML special character encoding -- thus being interpreted by the browser rather than being displayed as inert visible text. The most common way to make use of this exploit probably involves a link using a malformed URL, such that a variable passed in a URL to be displayed on the page contains malicious code. Something as simple as another URL used by the server-side code to produce links on the page, or even a user's name to be included in the text page so that the user can be greeted by name, can become a vulnerability employed in a reflected cross-site scripting exploit.
  • Stored: Also known as HTML injection attacks, stored cross-site scripting exploits are those where some data sent to the server is stored (typically in a database) to be used in the creation of pages that will be served to other users later. This form of cross-site scripting exploit can affect any visitor to your website, if your site is subject to a stored cross-site scripting vulnerability. The classic example of this sort of vulnerability is content management software such as forums and bulletin boards where users are allowed to use raw HTML and XHTML to format their posts. As with preventing reflected exploits, the key to securing your site against stored exploits is ensuring that all submitted data is translated to display entities before display so that it will not be interpreted by the browser as code.
  • Local: A local cross-site scripting exploit targets vulnerabilities within the code of a webpage itself. These vulnerabilities are the result of incautious use of the Document Object Model in JavaScript so that opening another Web page with malicious JavaScript code in it at the same time might actually alter the code in the first page on the local system. In older versions of Internet Explorer (before IE 6 on MS Windows XP Service Pack 2), in fact, this could even be used on local Web pages (stored on the local computer rather than retrieved from the World Wide Web), and through those pages break out of the browser "sandbox" to affect the local system with the user privileges used to run the browser. Because most MS Windows users have tended to run everything as the Administrator account, this effectively meant that local cross-site scripting exploits on MS Windows before XP Service Pack 2 could do just about anything. In a local cross-site scripting exploit, unlike reflected and stored exploits, no malicious code is sent to the server at all. The behavior of the exploit takes place entirely on the local client system, but it alters the pages provided by the otherwise benign Website before they are interpreted by the browser so that they behave as though they carried the malicious payload to the client from the server. This means that server-side protections that filter out or block malicious cross-site scripting will not work with this sort of exploit. For more about local cross-site scripting, see the explanation at DOM Based Cross Site Scripting.

Protection Against Cross-Site Scripting

The most comprehensive way to protect your Web design from being exploited by cross-site scripting is to translate any and all special characters in user-provided input -- even in URLs -- into display entities, such as HTML entities. This applies not only to server-side code like PHP, Perl, and ASP.NET code, but also JavaScript that works with any user-provided input as well. This may interfere with the operation of Websites where users expect to be able to use HTML and XHTML in their input, such as for Website design helper applications -- in which case more complex code may be needed to protect against malicious code. Such fine-grained filtering is just one side of an arms race against malicious security crackers, however, and cannot reasonably be 100% effective.

Another way to protect your Website from cross-site scripting exploits is to never directly use any user-provided input in your pages. Accepting a limited number of values in user-provided input that are each used as "keys," for lack of a better term, to choose from among certain predefined options is an example of how user-provided input can be used to define output, but obviously greatly limits the dynamic nature of Web applications. If your website does not need greater dynamism than this provides, however, this may be your safest option for generating output based on user input.

Similarly, input validation that simply strips out all characters unauthorized for specific, limited input types (such as removing everything but dashes, parentheses, periods, and digits from input expected to contain telephone numbers), or that rejects input containing unauthorized characters entirely, can be used. This is a useful technique for many forms of input, but not all. Such validation techniques should be used whenever possible, because they not only provide some protection against cross-site scripting, but also against direct attempts to compromise the server itself through buffer overflows, SQL injection, and other attempts to exceed the bounds of the system.

Cookies are often used to provide some form of security against cross-site scripting. Many cross-site scripting exploits are designed to "steal" session cookies, but a cookie can be "tied" to a particular IP address so that hijacked cookies fail validation when employed by cross-site scripting exploits. There are potential work-arounds for this sort of security, such as when the legitimate user of a given cookie and a cross-site scripting exploit both originate from behind the same proxy server or NAT device, of course. Internet Explorer implements an HTTPOnly flag that prevents local scripts from affecting a cookie to try to guard against this sort of cookie abuse, but it is ineffective against cross-site request forgery attacks, where unintended requests may be sent via cross-site scripting exploits alongside a cookie used to authorize the requests at the server.

The single most effective means of avoiding cross-site scripting in Web development, however, is to design your website so that it does not require client-side code at all. That way, if your users want to turn off the JavaScript interpreters in their browsers, they can do so without losing the ability to make use of your Website. This does not protect against all forms of potential malicious input to your server, of course, and it does not actually limit the vulnerability of your website all by itself -- but it does give visitors to your website the option of protecting themselves.

About

Chad Perrin is an IT consultant, developer, and freelance professional writer. He holds both Microsoft and CompTIA certifications and is a graduate of two IT industry trade schools.

15 comments
stacey7165
stacey7165

The Tomcat 7 project introduced a new way to force cookies to HttpOnly by setting a property called useHttpOnly as a context element in the server config. This essentially prevents the browser from reading cookies and getting pertinent information like Session ID and reduces the possibilities of security vulnerabilities of this nature substantially. For more information on this check out Mark Thomas', Tomcat 7 release manager and Apache Security Committee Member, article on the subject: http://www.tomcatexpert.com/blog/2011/01/26/cross-site-scripting-xss-prevention-tomcat-7

garfield.sicard
garfield.sicard

Fantastic explanation of XSS. I run a Linux server that hosts 73 sites that mostly use CMS, Forums, or Gallery Scripts. It was recently compromised by an unknown method so I am researching to figure out how the perp got in. Thank you for this well written explanation of XSS.

aureolin
aureolin

>>The single most effective means of avoiding cross-site >>scripting in Web development, however, is to design >>your website so that it does not require client-side >>code at all. It's a noble sentiment and all, but that's sort of like saying the best way to avoid car accidents is to lock yourself in the bathroom.

catseverywhere
catseverywhere

Thank you, I have a much clearer idea of what's going on here. And I add this is very well written. I had no foundation upon which to understand any of this, I'm not a programmer or web developer. But your explanations are entirely comprehensible nevertheless. The mark of a good writer.

BALTHOR
BALTHOR

Who does the law enforcement here?Surely the designers of computers and the Internet saw the potential for abuse problem.Would they settle for always getting attacked with little or no defense?You'd have to be a doctor in an area where no doctor yet exists just to operate a web site.I think that we are way off the mark,far away from the engineer's design idea.

apotheon
apotheon

In the article [url=http://blogs.techrepublic.com.com/security/?p=426]What is cross-site scripting?[/url] I explained what XSS (cross-site scripting) is, the three major types of XSS, and what we can do to mitigate the risks. Have I covered everything? Let me know what I've forgotten, or what you'd like to know more about.

apotheon
apotheon

"[i]It's a noble sentiment and all, but that's sort of like saying the best way to avoid car accidents is to lock yourself in the bathroom.[/i]" No . . . what I'm saying is that the most effective (not necessarily the best) way to avoid your customers getting into car accidents on the way to your store is to make it easy for them to get there by bus or on foot.

Sterling chip Camden
Sterling chip Camden

I have to agree that going sans JS is often not an option. But apotheon's article reminds us that when we do get behind the wheel, we need to drive defensively.

apotheon
apotheon

Thanks for the compliment. I do what I can to make my articles informative and comprehensible to my audience.

Sterling chip Camden
Sterling chip Camden

"Surely the designers of computers and the Internet saw the potential for abuse problem." ...not very effectively.

robo_dev
robo_dev

A couple of suggestions: Maybe for a future article: The threat of blended attacks like XSS with Cache poisoning or DNS hijacking. Mitigation/Detection: Firefox with extensions like tamperdata and chickenfoot makes an awesome web app vuln test platforms. Mitigation Tools: Commercial Tools Acunetix WVS (http://www.acunetix.com) AppScan(http://watchfire.com) HailStorm (http://www.cenzic.com) N-Stealth(http://nstalker.com) NTOSpider(http://www.ntobjectives.com) WebInspect(http://www.spidynamics.com) WebKing (http://parasoft.com) Free/OpenSource Tools * Pantera (http://www.owasp.org/index.php/Category:OWASP_Pantera_Web_Assessment_Studio_Project) * Paros (http://parosproxy.org/index.shtml) c * Spike Proxy (http://www.immunitysec.com/resources-freesoftware.shtml) * TestMaker (http://www.pushtotest.com/Downloads/features.html) * WebScarab (http://www.owasp.org/index.php/Category:OWASP_WebScarab_Project) * Wapiti (http://wapiti.sourceforge.net) * W3AF (http://w3af.sourceforge.net) * Grabber (http://rgaucher.info/beta/grabber)

seanferd
seanferd

Thanks for the refresher. Yet another good article to point to for users who have need of a primer in the subject.

JCitizen
JCitizen

of web-developers that work for some of my clients. Very good information apotheon!

$$$$$$$$$$
$$$$$$$$$$

When the DARPANET gradually became more public, nobody said "I didn't design this to be used by people without security clearance, who have not taken oaths to uphold the Constitution, and it simply isn't a good idea to make a global WAN"?

apotheon
apotheon

Thanks for the suggestions. I might make use of some of them. I have probably a dozen ideas on the back burner already, though, so I can't guarantee I'll write articles about any of those topics soon.