XSS (Cross Site Scripting) Prevention Cheat Sheet
XSS (Cross Site Scripting) Prevention Cheat Sheet
https://www.owasp.org/index.php?title=XSS_(Cros...
Contents
1 Introduction
1.1 Untrusted Data
1.2 Escaping (aka Output Encoding)
1.3 Injection Theory
1.4 A Positive XSS Prevention Model
1.5 Why Can't I Just HTML Entity Encode Untrusted Data?
1.6 You Need a Security Encoding Library
2 XSS Prevention Rules
2.1 RULE #0 - Never Insert Untrusted Data Except in Allowed Locations
2.2 RULE #1 - HTML Escape Before Inserting Untrusted Data into HTML
Element Content
2.3 RULE #2 - Attribute Escape Before Inserting Untrusted Data into HTML
Common Attributes
2.4 RULE #3 - JavaScript Escape Before Inserting Untrusted Data into HTML
JavaScript Data Values
2.5 RULE #4 - CSS Escape Before Inserting Untrusted Data into HTML Style
Property Values
2.6 RULE #5 - URL Escape Before Inserting Untrusted Data into HTML URL
Parameter Values
2.7 RULE #6 - Use an HTML Policy engine to validate or clean user-driven
HTML in an outbound way
2.8 RULE #7 - Prevent DOM-based XSS
3 Encoding Information
4 Additional XSS Defense (HTTPOnly cookie flag)
5 Related Articles
6 Authors and Primary Editors
Introduction
This article provides a simple positive model for preventing XSS using output
escaping/encoding properly. While there are a huge number of XSS attack vectors,
following a few simple rules can completely defend against this serious attack. This
article does not explore the technical or business impact of XSS. Suffice it to say that it
1 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
can lead to an attacker gaining the ability to do anything a victim can do through their
browser.
These rules apply to all the different varieties of XSS. Both reflected and stored XSS can
be addressed by performing the appropriate escaping on the server-side. The use of an
escaping/encoding library like the one in ESAPI is strongly recommended as there are
many special cases. DOM Based XSS can be addressed by applying these rules on the
client on untrusted data.
For a great cheatsheet on the attack vectors related to XSS, please refer to the excellent
XSS Cheat Sheet (http://ha.ckers.org/xss.html) by RSnake. More background on browser
security and the various browsers can be found in the Browser Security Handbook
(http://code.google.com/p/browsersec/) .
Untrusted Data
Untrusted data is most often data that comes from the HTTP request, in the form of URL
parameters, form fields, headers, or cookies. But data that comes from databases, web
services, and other sources is frequently untrusted from a security perspective. That is,
it might not have been perfectly validated. The OWASP Code Review Guide has a decent
list of methods that return untrusted data in various languages, but you should be
careful about your own methods as well.
Untrusted data should always be treated as though it contains an attack. That means you
should not send it anywhere without taking steps to make sure that any attacks are
detected and neutralized. As applications get more and more interconnected, the
likelihood of a buried attack being decoded or executed by a downstream interpreter
increases rapidly.
Traditionally, input validation has been the preferred approach for handling untrusted
data. However, input validation is not a great solution for injection attacks. First, input
validation is typically done when the data is received, before the destination is known.
That means that we don't know which characters might be significant in the target
interpreter. Second, and possibly even more importantly, applications must allow
potentially harmful characters in. For example, should poor Mr. O'Malley be prevented
from registering in the database simply because SQL considers ' a special character?
While input validation is important and should always be performed, it is not a complete
solution for injection attacks. It's better to think of input validation as defense in depth
and use escaping as described below as the primary defense.
2 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
called output "encoding." Some of these techniques define a special "escape" character,
and other techniques have a more sophisticated syntax that involves several characters.
Do not confuse output escaping with the notion of Unicode character encoding
(http://www.w3.org/TR/charmod/#sec-Digital) , which involves mapping a Unicode
character to a sequence of bits. This level of encoding is automatically decoded, and does
not defuse attacks. However, if there are misunderstandings about the intended charset
between the server and browser, it may cause unintended characters to be
communicated, possibly enabling XSS attacks. This is why it is still important to specify
(http://www.w3.org/TR/charmod/#sec-Encodings) the Unicode character encoding
(charset), such as UTF-8, for all communications.
Escaping is the primary means to make sure that untrusted data can't be used to convey
an injection attack. There is no harm in escaping data properly - it will still render in
the browser properly. Escaping simply lets the interpreter know that the data is not
intended to be executed, and therefore prevents attacks from working.
Injection Theory
Injection is an attack that involves breaking out of a data context and switching into a
code context through the use of special characters that are significant in the interpreter
being used. A data context is like <div>data context</div>. If the attacker's data gets
placed into the data context, they might break out like this <div>data <
script>alert("attack")</script> context</div>.
XSS is a form of injection where the interpreter is the browser and attacks are buried in
an HTML document. HTML is easily the worst mashup of code and data of all time, as
there are so many possible places to put code and so many different valid encodings.
HTML is particularly difficult because it is not only hierarchical, but also contains many
different parsers (XML, HTML, JavaScript, VBScript, CSS, URL, etc...).
To really understand what's going on with XSS, you have to consider injection into the
hierarchical structure of the HTML DOM (http://www.w3schools.com/HTMLDOM
/default.asp) . Given a place to insert data into an HTML document (that is, a place
where a developer has allowed untrusted data to be included in the DOM), there are two
ways to inject code:
Injecting UP
The most common way is to close the current context and start a new code context.
For example, this is what you do when you close an HTML attribute with a "> and
start a new <script> tag. This attack closes the original context (going up in the
hierarchy) and then starts a new tag that will allow script code to execute.
Remember that you may be able to skip many layers up in the hierarchy when trying
to break out of your current context. For example, a </script> tag may be able to
terminate a script block even if it is injected inside a quoted string inside a method
call inside the script. This happens because the HTML parser runs before the
JavaScript parser.
3 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
Injecting DOWN
The less common way to perform XSS injection is to introduce a code subcontext
without closing the current context. For example, if the attacker is able to change
<img src="...UNTRUSTED DATA HERE..." /> into < img
src="javascript:alert(document.cookie)" /> they do not have to break out of the
HTML attribute context. Instead, they introduce a subcontext that allows scripting
within the src attribute (in this case a javascript url). Another example is the
expression() functionality in CSS properties. Even though you may not be able to
escape a quoted CSS property to inject up, you may be able to introduce something
like xss:expression(document.write(document.cookie)) without ever leaving the
current context.
There's also the possibility of injecting directly in the current context. For example, if
you take untrusted input and put it directly into a JavaScript context. While insane,
accepting code from an attacker is more common than you might think in modern
applications. Generally it is impossible to secure untrusted code with escaping (or
anything else). If you do this, your application is just a conduit for attacker code to get
running in your users' browsers.
The rules in this document have been designed to prevent both UP and DOWN varieties
of XSS injection. To prevent injecting up, you must escape the characters that would
allow you to close the current context and start a new one. To prevent attacks that jump
up several levels in the DOM hierarchy, you must also escape all the characters that are
significant in all enclosing contexts. To prevent injecting down, you must escape any
characters that can be used to introduce a new sub-context within the current context.
4 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
doing is safe. Browser parsing is extremely tricky and many innocuous looking
characters can be significant in the right context.
5 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
directly in a script
in an attribute name
in a tag name
Most importantly, never accept actual JavaScript code from an untrusted source and
then run it. For example, a parameter named "callback" that contains a JavaScript code
snippet. No amount of escaping can fix that.
Escape the following characters with HTML entity encoding to prevent switching into
any execution context, such as script, style, or event handlers. Using hex entities is
recommended in the spec. In addition to the 5 characters significant in XML (&, <, >, ",
'), the forward slash is included as it helps to end an HTML entity.
&
<
>
"
'
/
-->
-->
-->
-->
-->
-->
&
<
>
"
'
/
6 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
java/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/HTMLEntityCodec.java)
of HTML entity escaping and unescaping.
String safe = ESAPI.encoder().encodeForHTML( request.getParameter( "input" ) );
Except for alphanumeric characters, escape all characters with ASCII values less than
256 with the &#xHH; format (or a named entity if available) to prevent switching out of
the attribute. The reason this rule is so broad is that developers frequently leave
attributes unquoted. Properly quoted attributes can only be escaped with the
corresponding quote. Unquoted attributes can be broken out of with many characters,
including [space] % * + , - / ; < = > ^ and |.
See the ESAPI reference implementation (http://code.google.com/p/owasp-esapijava/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/HTMLEntityCodec.java)
of HTML entity escaping and unescaping.
String safe = ESAPI.encoder().encodeForHTMLAttribute( request.getParameter( "input" ) );
7 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
Please note there are some JavaScript functions that can never safely use untrusted data
as input - EVEN IF JAVASCRIPT ESCAPED!
For example:
<script>
window.setInterval('...EVEN IF YOU ESCAPE UNTRUSTED DATA YOU ARE XSSED HERE...');
</script>
Except for alphanumeric characters, escape all characters less than 256 with the \xHH
format to prevent switching out of the data value into the script context or into another
attribute. Do not use any escaping shortcuts like \" because the quote character may be
matched by the HTML attribute parser which runs first. If an event handler is quoted,
breaking out requires the corresponding quote. The reason this rule is so broad is that
developers frequently leave event handler attributes unquoted. Properly quoted
attributes can only be escaped with the corresponding quote. Unquoted attributes can
be broken out of with many characters including [space] % * + , - / ; < = > ^ and |. Also,
a </script> closing tag will close a script block even though it is inside a quoted string
because the HTML parser runs before the JavaScript parser.
See the ESAPI reference implementation (http://code.google.com/p/owasp-esapijava/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/JavaScriptCodec.java) of
JavaScript escaping and unescaping.
String safe = ESAPI.encoder().encodeForJavaScript( request.getParameter( "input" ) );
property value
property value
property value
Except for alphanumeric characters, escape all characters with ASCII values less than
256 with the \HH escaping format. Do not use any escaping shortcuts like \" because the
8 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
quote character may be matched by the HTML attribute parser which runs first. Prevent
switching out of the property value and into another property or attribute. Also prevent
switching into an expression or other property value that allows scripting. If attribute is
quoted, breaking out requires the corresponding quote. All attributes should be quoted
but your encoding should be strong enough to prevent XSS when untrusted data is
placed in unquoted contexts. Unquoted attributes can be broken out of with many
characters including [space] % * + , - / ; < = > ^ and |. Also, the </style> tag will close
the style block even though it is inside a quoted string because the HTML parser runs
before the JavaScript parser. Please note that we recommend aggressive CSS encoding to
prevent XSS attacks for both quoted and unquoted attributes.
See the ESAPI reference implementation (http://code.google.com/p/owasp-esapijava/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/CSSCodec.java) of CSS
escaping and unescaping.
String safe = ESAPI.encoder().encodeForCSS( request.getParameter( "input" ) );
Except for alphanumeric characters, escape all characters with ASCII values less than
256 with the %HH escaping format. Including untrusted data in data: URLs should not
be allowed as there is no good way to disable attacks with escaping to prevent switching
out of the URL. All attributes should be quoted. Unquoted attributes can be broken out
of with many characters including [space] % * + , - / ; < = > ^ and |. Note that entity
encoding is useless in this context.
See the ESAPI reference implementation (http://code.google.com/p/owasp-esapijava/source/browse/trunk/src/main/java/org/owasp/esapi/codecs/PercentCodec.java) of
URL escaping and unescaping.
String safe = ESAPI.encoder().encodeForURL( request.getParameter( "input" ) );
WARNING: Do not encode complete or relative URL's with URL encoding! If untrusted
input is meant to be placed into href, src or other URL-based attributes, it should be
validated to make sure it does not point to an unexpected protocol, especially Javascript
links. URL's should then be encoded based on the context of display like any other piece
of data. For example, user driven URL's in HREF links should be attribute encoded. For
example:
9 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
Encoding Information
OWASP Development Guide (http://code.google.com/p/owasp-development-guide/wiki
/WebAppSecDesignGuide_D6)
Related Articles
XSS Attack Cheat Sheet
The following article describes how to exploit different kinds of XSS Vulnerabilities that
this article was created to help you avoid:
RSnake: "XSS Cheat Sheet" - http://ha.ckers.org/xss.html
10 of 11
05/28/2011 06:26 PM
https://www.owasp.org/index.php?title=XSS_(Cros...
11 of 11
05/28/2011 06:26 PM