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 "Abridged XSS Prevention Cheat Sheet"

From OWASP
Jump to: navigation, search
 
(101 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= DRAFT CHEAT SHEET - WORK IN PROGRESS =
+
#redirect [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet]]
= Introduction =
 
 
 
Cross site scripting is the most common web vulnerability.  It represents a serious threat because cross site scripting allows evil attacker code to run in a victim’s browser.  More details about XSS can be found here:  [https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29 https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29]
 
 
 
= XSS Prevention Overview =
 
 
 
{| class="wikitable"
 
|-
 
! Data Type
 
! Context
 
! Code Sample
 
! Defense
 
|-
 
| String
 
| HTML Body
 
| &lt;span><span style="color:red;">UNTRUSTED DATA</span>&lt;/span>
 
| <ul><li>[https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.231_-_HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content HTML Entity Encoding]</li></ul>
 
|-
 
| String
 
| HTML Attribute, quoted
 
| &lt;span id="<span style="color:red;">UNTRUSTED DATA</span>">&lt;/span>
 
| <ul><li>HTML Entity Encode single and double quotes</li></ul>
 
|-
 
| String
 
| HTML Attribute, unquoted
 
| &lt;span id=<span style="color:red;">UNTRUSTED DATA</span>>&lt;/span>
 
| <ul><li>[https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.232_-_Attribute_Escape_Before_Inserting_Untrusted_Data_into_HTML_Common_Attributes Aggressive HTML Entity Encoding]</li></ul>
 
|-
 
| String
 
| GET Parameter
 
| &lt;a href="/site/search?value=<span style="color:red;">UNTRUSTED DATA</span>">clickme&lt;/a>
 
| <ul><li>[https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.235_-_URL_Escape_Before_Inserting_Untrusted_Data_into_HTML_URL_Parameter_Values URL Encoding]</li></ul>
 
|-
 
| String
 
| Untrusted URL rendered in an HREF tag<br/>(or other HTML link contexts)
 
| &lt;a href="<span style="color:red;">UNTRUSTED DATA</span>">clickme&lt;/a>
 
| <ul><li>URL Validation<li>reject javascript: URL’s<li>Whitelist http, https and other safe URL types<li>Attribute encoding<li>safe URL verification</ul>
 
|-
 
| String
 
| CSS
 
| &lt;div style="width: <span style="color:red;">UNTRUSTED DATA</span>;">Selection&lt;/div>
 
| <ul><li>[https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.234_-_CSS_Escape_And_Strictly_Validate_Before_Inserting_Untrusted_Data_into_HTML_Style_Property_Values Strict structural validation]<li>CSS Hex encoding<li>good design of CSS Features</ul>
 
|-
 
| String
 
| JavaScript
 
| &lt;script>var currentValue='<span style="color:red;">UNTRUSTED DATA</span>';&lt;/script>
 
| <ul><li>Ensure JavaScript variables are quoted</li><li>JavaScript Hex Encoding</li><li>JavaScript Unicode Encoding</li></ul>
 
|-
 
| String
 
| JavaScript Event Handler Attribute
 
| <input type="button" onclick="UNTRUSTED DATA" .../>
 
| <ul><li>Ensure UNTRUSTED DATA is quoted</li><li>JavaScript Hex or Unicode Encoding but NO backslash encoding (\" or \' or \\) </li></ul>
 
|-
 
| HTML Text
 
| HTML Body
 
| &lt;span><span style="color:red;">UNTRUSTED HTML</span>&lt;/span>
 
| <ul><li>[https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet#RULE_.236_-_Use_an_HTML_Policy_engine_to_validate_or_clean_user-driven_HTML_in_an_outbound_way HTML Validation (JSoup, AntiSamy, HTML Sanitizer)]</li></ul>
 
|-
 
| String
 
| DOM XSS
 
|
 
| <ul><li>[[DOM based XSS Prevention Cheat Sheet]]</li></ul>
 
|}
 
 
 
= Output Encoding Types =
 
 
 
{| class="wikitable"
 
|-
 
! Encoding Type
 
! Encoding Mechanism
 
|-
 
| HTML Entity Encoding
 
|  & --> &amp;amp;<br/>< --> &amp;lt;<br/>> --> &amp;gt;<br/>" --> &amp;quot;<br/>' --> &amp;#x27;    &apos; is not recommended<br/>/ --> &amp;#x2F;    forward slash is included as it helps end an HTML entity
 
|-
 
| HTML Attribute Encoding
 
| TODO
 
|-
 
| URL Encoding
 
| TODO
 
|-
 
| JavaScript HEX Encoding
 
| TODO
 
|-
 
| CSS Hex Encoding
 
| TODO
 
|}
 
 
 
= Related Articles =
 
 
 
{{Cheatsheet_Navigation}}
 
 
 
= Authors and Primary Editors  =
 
 
 
Jim Manico - jim [at] owasp.org<br/>
 
Jeff Williams - jeff [at] aspectsecurity.com
 
 
 
[[Category:Cheatsheets]]
 

Latest revision as of 17:48, 16 September 2012