This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org

Difference between revisions of "XSS (Cross Site Scripting) Prevention Cheat Sheet"

From OWASP
Jump to: navigation, search
(New page: == Introduction == This article provides a simple positive model for preventing XSS using encoding properly. While there are a huge number of XSS attack vectors, following a few simpl...)
 
Line 17: Line 17:
  
  
== RULE #0: Only Put User Data in Allowed Locations ==
+
== RULE #0 - Only Put User Data in Allowed Locations ==
  
 
The first rule is that you should adopt a positive model for putting data into HTML. None of the following locations are good places for untrusted data.
 
The first rule is that you should adopt a positive model for putting data into HTML. None of the following locations are good places for untrusted data.
Line 32: Line 32:
  
  
== RULE #1: Escape HTML Element Content ==
+
== RULE #1 - Escape HTML Element Content ==
  
 
Rule #1 is for when you want to put untrusted input directly into the HTML body somewhere. This includes inside normal tags like div, p, b, td, etc...
 
Rule #1 is for when you want to put untrusted input directly into the HTML body somewhere. This includes inside normal tags like div, p, b, td, etc...
Line 44: Line 44:
 
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.
 
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.
  
&
+
  &
<
+
  <
>
+
  >
"
+
  "
'
+
  '
&#x2F
+
  &#x2F
  
  
Line 56: Line 56:
 
Rule #2 is for putting untrusted data input typical attribute values like width, name, value, etc... It is extremely important that event handler attributes like onmouseover should use Rule #3 for HTML JavaScript Data Values.
 
Rule #2 is for putting untrusted data input typical attribute values like width, name, value, etc... It is extremely important that event handler attributes like onmouseover should use Rule #3 for HTML JavaScript Data Values.
  
   &lt;div attr=...USER DATA HERE...>content</div>
+
   &lt;div attr=...USER DATA HERE...>content</div>     inside UNquoted attribute
  
Escape all characters less than 256 except alphanumeric characters with the &amp;#xHH; format (or a named entity in 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 % * + , - / ; < = > ^ | could break out.
+
  &lt;div attr='...USER DATA HERE...'>content</div>   inside single quoted attribute
  
 +
  &lt;div attr="...USER DATA HERE...">content</div>  inside double quoted attribute
  
 +
Escape all characters less than 256 except alphanumeric characters with the &amp;#xHH; format (or a named entity in 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 % * + , - / ; < = > ^ | could break out.
  
  
3) HTML JavaScript Data Value
+
3) Rule #3 - HTML JavaScript Data Value
  
Example: <script>alert('...USER DATA HERE...')</script>
+
Rule #3 concerns the JavaScript event handlers that are specified on various HTML elements. The only safe place to put untrusted data into these event handlers is into a "data value."  Including user data inside these little code blocks is quite dangerous, as it is very easy to switch into an execution context, so use with caution.
  
Example: <script>x=...USER DATA HERE...</script>
+
  &lt;script>alert('...USER DATA HERE...')&lt;/script>    inside a quoted string
 
+
 
Example: <div onmouseover=...USER DATA HERE...</div>
+
  &lt;script>x=...USER DATA HERE...&lt;/script>           one side of an expression
 
+
 
Example: <div onmouseover="...USER DATA HERE..."</div>
+
  &lt;div onmouseover=...USER DATA HERE...&lt;/div>       inside UNquoted event handler
 
+
 
Note: All other event handlers are similar
+
  &lt;div onmouseover='...USER DATA HERE...'&lt;/div>      inside quoted event handler
 +
 
 +
  &lt;div onmouseover="...USER DATA HERE..."&lt;/div>     inside quoted event handler
  
 
Escape all characters less than 256 except alphanumeric characters with the \xHH format to prevent switching out of the data value into the script context or into another attribute. If 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 % * + , - / ; < = > ^ | could break out.
 
Escape all characters less than 256 except alphanumeric characters with the \xHH format to prevent switching out of the data value into the script context or into another attribute. If 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 % * + , - / ; < = > ^ | could break out.
Line 79: Line 83:
  
  
== Escape HTML Style Property Values ==
+
== Rule #4 - Escape HTML Style Property Values ==
  
 +
Rule #4 is for when you want to put untrusted data into a stylesheet or a style tag. CSS is surprisingly powerful, and can be used for numerous attacks. Therefore, it's important that you only use untrusted data in a property '''value''' and not into other places in style data. You should stay away from putting untrusted data into complex properties like url, behavior, and custom (-moz-binding). You should also not put user data into IE’s expression property value which allows JavaScript.
  
 +
  &lt;style>selector { property : ...USER DATA HERE...; } &lt;/style>
 +
 
 +
  &lt;span style=property : ...USER DATA HERE...;>text&lt;/style>
  
Example: <style>selector { property : ...USER DATA HERE...; } </style>
+
Use \HH for all characters less than 256 except alphanumeric. Do not use any shortcuts like \" because they are significant in enclosing contexts. For example,
 
 
Example: <span style=property : ...USER DATA HERE...;>text</style>
 
 
 
Note: Beware complex properties like url, behavior, and custom (-moz-binding)
 
 
 
Note: Also beware using user input in IE’s expression property value which contains javascript.
 
 
 
Use \HH for all characters less than 256 except alphanumeric. Do not use any shortcuts like \"
 
  
 
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. Unquoted attributes can be broken out of with many characters including space % * + , - / ; < = > ^ | could break out.
 
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. Unquoted attributes can be broken out of with many characters including space % * + , - / ; < = > ^ | could break out.

Revision as of 16:56, 16 January 2009

Introduction

This article provides a simple positive model for preventing XSS using encoding properly. While there are a huge number of XSS attack vectors, following a few simple rules can completely defend against this serious attack.

For a great cheatsheet on the attack vectors related to XSS, please refer to the excellent XSS Cheat Sheet by RSnake.


Model

This article treats an HTML page like a template, with slots where a developer might put user input. There are several different types of slots, and they each have slightly different security rules. When you put untrusted data into these slots, you need to take certain steps to make sure that the data does not "escape" that slot and break into a context that allows code execution.

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 often frequently untrusted from a security perspective. That is, it might not have been perfectly validated. Therefore, it is best to always escape/encode this data to make sure it can't be used to convey an attack.

This document sets out the most common types of slots and the rules for putting untrusted data into them safely. Based on the various specifications, known XSS vectors, and a great deal of manual testing with all the popular browsers, we have determined that the rule proposed here are safe.

The slots are defined and a few examples of each are provided. Developers SHOULD NOT put data into any other slots without a very careful analysis to ensure that what they are doing is safe. Browser parsing reference to Zalinsky is extremely tricky and many innocuous looking characters can be significant in the right context.


RULE #0 - Only Put User Data in Allowed Locations

The first rule is that you should adopt a positive model for putting data into HTML. None of the following locations are good places for untrusted data.

 <script>...NO USER DATA HERE...</script>   directly in a script
 
 <!--...NO USER DATA HERE...-->             inside an HTML comment
 
 <div ...NO USER DATA HERE...=test />       in an attribute name
 
 <...NO USER DATA HERE... href="/test" />   in a tag name

Using user input in HTML anywhere except as specifically discussed above is NOT ALLOWED. Most importantly, never accept code from a user and then run it. No amount of encoding can fix that. There’s no good reason to put user data in these contexts.


RULE #1 - Escape HTML Element Content

Rule #1 is for when you want to put untrusted input directly into the HTML body somewhere. This includes inside normal tags like div, p, b, td, etc...

 <body>...USER DATA HERE...</body>
 
 <div>...USER DATA HERE...</div>
 any other normal HTML elements

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.

 &amp;
 &lt;
 &gt;
 &quot;
 &apos;
 &#x2F


Rule #2 - Escape HTML Common Attributes

Rule #2 is for putting untrusted data input typical attribute values like width, name, value, etc... It is extremely important that event handler attributes like onmouseover should use Rule #3 for HTML JavaScript Data Values.

 <div attr=...USER DATA HERE...>content</div>     inside UNquoted attribute
 <div attr='...USER DATA HERE...'>content</div>   inside single quoted attribute
 <div attr="...USER DATA HERE...">content</div>   inside double quoted attribute

Escape all characters less than 256 except alphanumeric characters with the &#xHH; format (or a named entity in 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 % * + , - / ; < = > ^ | could break out.


3) Rule #3 - HTML JavaScript Data Value

Rule #3 concerns the JavaScript event handlers that are specified on various HTML elements. The only safe place to put untrusted data into these event handlers is into a "data value." Including user data inside these little code blocks is quite dangerous, as it is very easy to switch into an execution context, so use with caution.

 <script>alert('...USER DATA HERE...')</script>     inside a quoted string
 
 <script>x=...USER DATA HERE...</script>            one side of an expression
 
 <div onmouseover=...USER DATA HERE...</div>        inside UNquoted event handler
 
 <div onmouseover='...USER DATA HERE...'</div>      inside quoted event handler
 
 <div onmouseover="...USER DATA HERE..."</div>      inside quoted event handler

Escape all characters less than 256 except alphanumeric characters with the \xHH format to prevent switching out of the data value into the script context or into another attribute. If 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 % * + , - / ; < = > ^ | could break out.


Rule #4 - Escape HTML Style Property Values

Rule #4 is for when you want to put untrusted data into a stylesheet or a style tag. CSS is surprisingly powerful, and can be used for numerous attacks. Therefore, it's important that you only use untrusted data in a property value and not into other places in style data. You should stay away from putting untrusted data into complex properties like url, behavior, and custom (-moz-binding). You should also not put user data into IE’s expression property value which allows JavaScript.

 <style>selector { property : ...USER DATA HERE...; } </style>
 
 <span style=property : ...USER DATA HERE...;>text</style>

Use \HH for all characters less than 256 except alphanumeric. Do not use any shortcuts like \" because they are significant in enclosing contexts. For example,

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. Unquoted attributes can be broken out of with many characters including space % * + , - / ; < = > ^ | could break out.


5) HTML URL Attributes

Example: <a href=http://...USER DATA HERE...>link</a >

Example: <img src=http://...USER DATA HERE... />

Example: <script src="http://...USER DATA HERE..." />

Note: Using user data in javascript: urls is a bad idea, but you could possibly use the HTML JavaScript Data Value rule above

Use %HH for all characters less than 256 except alphanumeric. Including user data in data: urls should not be allowed as there is no good way to disable attacks with encoding to prevent switching out of the url. All attributes should be quoted. Unquoted attributes can be broken out of with many characters including space % * + , - / ; < = > ^ | could break out. Note that entity encoding is useless in this context.