<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Abraham+Kang</id>
		<title>OWASP - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.owasp.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Abraham+Kang"/>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php/Special:Contributions/Abraham_Kang"/>
		<updated>2026-04-22T05:03:39Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.2</generator>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Abridged_XSS_Prevention_Cheat_Sheet&amp;diff=120253</id>
		<title>Abridged XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Abridged_XSS_Prevention_Cheat_Sheet&amp;diff=120253"/>
				<updated>2011-11-16T06:39:39Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT CHEAT SHEET - WORK IN PROGRESS =&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
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]&lt;br /&gt;
&lt;br /&gt;
= XSS Prevention Overview =&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Data Type&lt;br /&gt;
! Context&lt;br /&gt;
! Code Sample&lt;br /&gt;
! Defense&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Body&lt;br /&gt;
| &amp;amp;lt;span&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Attribute, quoted&lt;br /&gt;
| &amp;amp;lt;span id=&amp;quot;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;HTML Entity Encode single and double quotes&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Attribute, unquoted&lt;br /&gt;
| &amp;amp;lt;span id=&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| GET Parameter&lt;br /&gt;
| &amp;amp;lt;a href=&amp;quot;/site/search?value=&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;clickme&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| Untrusted URL rendered in an HREF tag&amp;lt;br/&amp;gt;(or other HTML link contexts)&lt;br /&gt;
| &amp;amp;lt;a href=&amp;quot;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;clickme&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;URL Validation&amp;lt;li&amp;gt;reject javascript: URL’s&amp;lt;li&amp;gt;Whitelist http, https and other safe URL types&amp;lt;li&amp;gt;Attribute encoding&amp;lt;li&amp;gt;safe URL verification&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| CSS&lt;br /&gt;
| &amp;amp;lt;div style=&amp;quot;width: &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;;&amp;quot;&amp;gt;Selection&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;li&amp;gt;CSS Hex encoding&amp;lt;li&amp;gt;good design of CSS Features&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| JavaScript&lt;br /&gt;
| &amp;amp;lt;script&amp;gt;var currentValue='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;';&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;Ensure JavaScript variables are quoted&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Hex Encoding&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Unicode Encoding&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| JavaScript Event Handler Attribute&lt;br /&gt;
| &amp;lt;input type=&amp;quot;button&amp;quot; onclick=&amp;quot;UNTRUSTED DATA&amp;quot; .../&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;Ensure UNTRUSTED DATA is quoted&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Hex or Unicode Encoding but NO backslash encoding (\&amp;quot; or \' or \\) &amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| HTML Text&lt;br /&gt;
| HTML Body&lt;br /&gt;
| &amp;amp;lt;span&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED HTML&amp;lt;/span&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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)]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| DOM XSS&lt;br /&gt;
|&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[[DOM based XSS Prevention Cheat Sheet]]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Output Encoding Types =&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Encoding Type&lt;br /&gt;
! Encoding Mechanism&lt;br /&gt;
|-&lt;br /&gt;
| HTML Entity Encoding&lt;br /&gt;
|   &amp;amp; --&amp;gt; &amp;amp;amp;amp;&amp;lt;br/&amp;gt;&amp;lt; --&amp;gt; &amp;amp;amp;lt;&amp;lt;br/&amp;gt;&amp;gt; --&amp;gt; &amp;amp;amp;gt;&amp;lt;br/&amp;gt;&amp;quot; --&amp;gt; &amp;amp;amp;quot;&amp;lt;br/&amp;gt;' --&amp;gt; &amp;amp;amp;#x27;     &amp;amp;apos; is not recommended&amp;lt;br/&amp;gt;/ --&amp;gt; &amp;amp;amp;#x2F;     forward slash is included as it helps end an HTML entity&lt;br /&gt;
|-&lt;br /&gt;
| HTML Attribute Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| URL Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| JavaScript HEX Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| CSS Hex Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Related Articles =&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim [at] owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
Jeff Williams - jeff [at] aspectsecurity.com&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Abridged_XSS_Prevention_Cheat_Sheet&amp;diff=120252</id>
		<title>Abridged XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Abridged_XSS_Prevention_Cheat_Sheet&amp;diff=120252"/>
				<updated>2011-11-16T06:36:11Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= DRAFT CHEAT SHEET - WORK IN PROGRESS =&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
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]&lt;br /&gt;
&lt;br /&gt;
= XSS Prevention Overview =&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Data Type&lt;br /&gt;
! Context&lt;br /&gt;
! Code Sample&lt;br /&gt;
! Defense&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Body&lt;br /&gt;
| &amp;amp;lt;span&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Attribute, quoted&lt;br /&gt;
| &amp;amp;lt;span id=&amp;quot;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;HTML Entity Encode single and double quotes&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| HTML Attribute, unquoted&lt;br /&gt;
| &amp;amp;lt;span id=&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| GET Parameter&lt;br /&gt;
| &amp;amp;lt;a href=&amp;quot;/site/search?value=&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;clickme&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| Untrusted URL rendered in an HREF tag&amp;lt;br/&amp;gt;(or other HTML link contexts)&lt;br /&gt;
| &amp;amp;lt;a href=&amp;quot;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;&amp;quot;&amp;gt;clickme&amp;amp;lt;/a&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;URL Validation&amp;lt;li&amp;gt;reject javascript: URL’s&amp;lt;li&amp;gt;Whitelist http, https and other safe URL types&amp;lt;li&amp;gt;Attribute encoding&amp;lt;li&amp;gt;safe URL verification&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| CSS&lt;br /&gt;
| &amp;amp;lt;div style=&amp;quot;width: &amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;;&amp;quot;&amp;gt;Selection&amp;amp;lt;/div&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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]&amp;lt;li&amp;gt;CSS Hex encoding&amp;lt;li&amp;gt;good design of CSS Features&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| JavaScript&lt;br /&gt;
| &amp;amp;lt;script&amp;gt;var currentValue='&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED DATA&amp;lt;/span&amp;gt;';&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;Ensure JavaScript variables are quoted&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Hex Encoding&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Unicode Encoding&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| JavaScript Event Handler Attribute&lt;br /&gt;
| &amp;lt;input type=&amp;quot;button&amp;quot; onclick=&amp;quot;UNTRUSTED DATA&amp;quot; .../&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;Ensure JavaScript variables are quoted&amp;lt;/li&amp;gt;&amp;lt;li&amp;gt;JavaScript Hex or Unicode Encoding but NO backslash encoding (\&amp;quot; or \' or \\) &amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| HTML Text&lt;br /&gt;
| HTML Body&lt;br /&gt;
| &amp;amp;lt;span&amp;gt;&amp;lt;span style=&amp;quot;color:red;&amp;quot;&amp;gt;UNTRUSTED HTML&amp;lt;/span&amp;gt;&amp;amp;lt;/span&amp;gt;&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[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)]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt; &lt;br /&gt;
|-&lt;br /&gt;
| String&lt;br /&gt;
| DOM XSS&lt;br /&gt;
|&lt;br /&gt;
| &amp;lt;ul&amp;gt;&amp;lt;li&amp;gt;[[DOM based XSS Prevention Cheat Sheet]]&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Output Encoding Types =&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Encoding Type&lt;br /&gt;
! Encoding Mechanism&lt;br /&gt;
|-&lt;br /&gt;
| HTML Entity Encoding&lt;br /&gt;
|   &amp;amp; --&amp;gt; &amp;amp;amp;amp;&amp;lt;br/&amp;gt;&amp;lt; --&amp;gt; &amp;amp;amp;lt;&amp;lt;br/&amp;gt;&amp;gt; --&amp;gt; &amp;amp;amp;gt;&amp;lt;br/&amp;gt;&amp;quot; --&amp;gt; &amp;amp;amp;quot;&amp;lt;br/&amp;gt;' --&amp;gt; &amp;amp;amp;#x27;     &amp;amp;apos; is not recommended&amp;lt;br/&amp;gt;/ --&amp;gt; &amp;amp;amp;#x2F;     forward slash is included as it helps end an HTML entity&lt;br /&gt;
|-&lt;br /&gt;
| HTML Attribute Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| URL Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| JavaScript HEX Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|-&lt;br /&gt;
| CSS Hex Encoding&lt;br /&gt;
| TODO&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= Related Articles =&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
= Authors and Primary Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim [at] owasp.org&amp;lt;br/&amp;gt;&lt;br /&gt;
Jeff Williams - jeff [at] aspectsecurity.com&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Connections_Committee_-_Application_7&amp;diff=118205</id>
		<title>OWASP Connections Committee - Application 7</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Connections_Committee_-_Application_7&amp;diff=118205"/>
				<updated>2011-09-29T13:14:45Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[How to Join a Committee|Click here to return to 'How to Join a Committee' page]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
 ! colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot;|&amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE APPLICATION FORM''' &lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;|'''Applicant's Name'''&lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;Jerry Hoff&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;| '''Current and past OWASP Roles''' &lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|OWASP Video Series&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;| '''Committee Applying for''' &lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|OWASP Connection Committee&lt;br /&gt;
 |}&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
Please be aware that for an application to be considered by the board, '''you MUST have 5 recommendations'''.  &lt;br /&gt;
An incomplete application will not be considered for vote.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
 ! colspan=&amp;quot;8&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot;|&amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE RECOMMENDATIONS''' &lt;br /&gt;
 |- &lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:white; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;&lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Who Recommends/Name''' &lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Role in OWASP'''&lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Recommendation Content''' &lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''1'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Simon Bennetts&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| ZAP Project Lead, Manchester UK Chapter Leader&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| I strongly support Jerry's candidacy for the GCC. His AppSec tutorial videos are a great way to spread the OWASP message.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''2'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Ludovic Petit&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Chapter leader OWASP France, Connections Committee Member &lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| We need guys like Jerry. His work is both a must and serves the Owasp voice, definitely!&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''3'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jack Mannino&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Mobile Security Project&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry has great ideas on how to promote application security, and the drive to get it done.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''4'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Chris Schmidt&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| GPC / ESAPI &lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry has great vision and ideas on how to make AppSec visible and accessible - he would be a great addition to the GCC&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''5'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Sherif Koussa&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| GIC / Ottawa CL / CS Project Co-Leader&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| You know from the moment you start talking to Jerry, that he has the kind of vision and leadership that we need at OWASP. Most importantly, he knows how to communicate them in a way that could be easily accepted by the listener. Definitely recommended.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''6'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Abraham Kang&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| DOM XSS Cheat Sheet / OWASP Guide&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry is not only an outstanding and knowledgeable security practitioner but also understands how to communicate to developers.  His application security video series is a testament to this.  Jerry is hard working and takes responsibility of the projects that he is involved with.  I am sure that Jerry will be an important contributor to the Connections Committee.  &lt;br /&gt;
 |}&lt;br /&gt;
----&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=OWASP_Connections_Committee_-_Application_7&amp;diff=118204</id>
		<title>OWASP Connections Committee - Application 7</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=OWASP_Connections_Committee_-_Application_7&amp;diff=118204"/>
				<updated>2011-09-29T13:14:19Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[How to Join a Committee|Click here to return to 'How to Join a Committee' page]]&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
 ! colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot;|&amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE APPLICATION FORM''' &lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;|'''Applicant's Name'''&lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;Jerry Hoff&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;| '''Current and past OWASP Roles''' &lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|OWASP Video Series&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot;| '''Committee Applying for''' &lt;br /&gt;
 | colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot;|OWASP Connection Committee&lt;br /&gt;
 |}&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
Please be aware that for an application to be considered by the board, '''you MUST have 5 recommendations'''.  &lt;br /&gt;
An incomplete application will not be considered for vote.&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
 ! colspan=&amp;quot;8&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot;|&amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE RECOMMENDATIONS''' &lt;br /&gt;
 |- &lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:white; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;&lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Who Recommends/Name''' &lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Role in OWASP'''&lt;br /&gt;
 ! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot;|&amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Recommendation Content''' &lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''1'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Simon Bennetts&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| ZAP Project Lead, Manchester UK Chapter Leader&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| I strongly support Jerry's candidacy for the GCC. His AppSec tutorial videos are a great way to spread the OWASP message.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''2'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Ludovic Petit&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Chapter leader OWASP France, Connections Committee Member &lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| We need guys like Jerry. His work is both a must and serves the Owasp voice, definitely!&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''3'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jack Mannino&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Mobile Security Project&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry has great ideas on how to promote application security, and the drive to get it done.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''4'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Chris Schmidt&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| GPC / ESAPI &lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry has great vision and ideas on how to make AppSec visible and accessible - he would be a great addition to the GCC&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''5'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Sherif Koussa&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| GIC / Ottawa CL / CS Project Co-Leader&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| You know from the moment you start talking to Jerry, that he has the kind of vision and leadership that we need at OWASP. Most importantly, he knows how to communicate them in a way that could be easily accepted by the listener. Definitely recommended.&lt;br /&gt;
 |-&lt;br /&gt;
 | style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;|'''6'''&lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| &lt;br /&gt;
 | style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| DOM XSS Cheat Sheet / OWASP Guide&lt;br /&gt;
 | style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot;| Jerry is not only an outstanding and knowledgeable security practitioner but also understands how to communicate to developers.  His application security video series is a testament to this.  Jerry is hard working and takes responsibility of the projects that he is involved with.  I am sure that Jerry will be an important contributor to the Connections Committee.  &lt;br /&gt;
 |}&lt;br /&gt;
----&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=115313</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=115313"/>
				<updated>2011-08-08T21:09:39Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross-Site Scripting), there are three generally recognized forms of [[XSS]].  [[XSS#Stored_and_Reflected_XSS_Attacks | Reflected, Stored]], and [[DOM Based XSS]].  The [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]] does an excellent job of addressing Reflected and Stored XSS. This cheatsheet addresses DOM (Document Object Model) based XSS and is an extension (and assumes comprehension of) the [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]].&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS, one needs to see the fundamental difference between reflected and stored XSS when compared to DOM based XSS.  Reflected and Stored XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each subcontext (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code, the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods, we refer to the other contexts besides the JavaScript context as subcontexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example of an attack which occurs in the JavaScript context and HTML subcontext:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= taintedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the subcontext in the execution context.  Let’s look at the individual subcontexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Subcontext within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Subcontext within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Subcontext within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing &amp;lt;code&amp;gt;javascript:attackCode()&amp;lt;/code&amp;gt; to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Subcontexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The setAttribute(&amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;,&amp;lt;i&amp;gt;value_string&amp;lt;/i&amp;gt;) method is dangerous because it implicitly coerces the  &amp;lt;i&amp;gt;string_value&amp;lt;/i&amp;gt; into the DOM attribute datatype of &amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;.  In the case above, the attribute name is an JavaScript event handler, so the attribute value is implicitly converted to JavaScript code and evaluated.  In the case above, JavaScript encoding does not mitigate against DOM based XSS.  Other JavaScript methods which take code as a string types will have a similar problem as outline above (setTimeout, setInterval, new Function, etc.).  This is in stark contrast to JavaScript encoding in the event handler attribute of a HTML tag (HTML parser) where JavaScript encoding mitigates against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!-- Does NOT work  --&amp;gt;&lt;br /&gt;
&amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An alternative to using Element.setAttribute(...) to set DOM attributes is to set the attribute directly.  Directly setting event handler attributes will allow JavaScript encoding to mitigate against DOM based XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    &amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.  &amp;quot;alert(7)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onclick = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0029&amp;quot;;&lt;br /&gt;
            &lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = &amp;quot;testIt&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;(&amp;quot; and &amp;quot;)&amp;quot;. &amp;quot;alert(77)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0037\u0029;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;;&amp;quot;. &amp;quot;testIt;testIt&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074\u003b\u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
      &lt;br /&gt;
            //The following DOES WORK because the encoded value is a valid variable name or function reference.  &amp;quot;testIt&amp;quot; is JavaScript encoded&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
&lt;br /&gt;
            function testIt() {&lt;br /&gt;
                &lt;br /&gt;
                alert(&amp;quot;I was called.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed (upon execution) in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual subcontext (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //server-side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL subcontext (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL subcontext the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS( &amp;amp;#x21a9;&lt;br /&gt;
               Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
&lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrieved the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
[[User:Achim| Achim Hoffmann]] [mailto:achim@owasp.org| achim_@_owasp.org]&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Erlend Oftedal&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=110051</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=110051"/>
				<updated>2011-05-06T07:04:07Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross-Site Scripting), there are three generally recognized forms of [[XSS]].  [[XSS#Stored_and_Reflected_XSS_Attacks | Reflected, Stored]], and [[DOM Based XSS]].  The [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]] does an excellent job of addressing Reflected and Stored XSS. This cheatsheet addresses DOM (Document Object Model) based XSS and is an extension (and assumes comprehension of) the [[XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet | XSS Prevention Cheatsheet]].&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS, one needs to see the fundamental difference between reflected and stored XSS when compared to DOM based XSS.  Reflected and Stored XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each subcontext (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code, the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods, we refer to the other contexts besides the JavaScript context as subcontexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example of an attack which occurs in the JavaScript context and HTML subcontext:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the subcontext in the execution context.  Let’s look at the individual subcontexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Subcontext within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Subcontext within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Subcontext within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Subcontext within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing &amp;lt;code&amp;gt;javascript:attackCode()&amp;lt;/code&amp;gt; to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Subcontexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The setAttribute(&amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;,&amp;lt;i&amp;gt;value_string&amp;lt;/i&amp;gt;) method is dangerous because it implicitly coerces the  &amp;lt;i&amp;gt;string_value&amp;lt;/i&amp;gt; into the DOM attribute datatype of &amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;.  In the case above, the attribute name is an JavaScript event handler, so the attribute value is implicitly converted to JavaScript code and evaluated.  In the case above, JavaScript encoding does not mitigate against DOM based XSS.  Other JavaScript methods which take code as a string types will have a similar problem as outline above (setTimeout, setInterval, new Function, etc.).  This is in stark contrast to JavaScript encoding in the event handler attribute of a HTML tag (HTML parser) where JavaScript encoding mitigates against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!-- Does NOT work  --&amp;gt;&lt;br /&gt;
&amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An alternative to using Element.setAttribute(...) to set DOM attributes is to set the attribute directly.  Directly setting event handler attributes will allow JavaScript encoding to mitigate against DOM based XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    &amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.  &amp;quot;alert(7)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onclick = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0029&amp;quot;;&lt;br /&gt;
            &lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = &amp;quot;testIt&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;(&amp;quot; and &amp;quot;)&amp;quot;. &amp;quot;alert(77)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0037\u0029;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;;&amp;quot;. &amp;quot;testIt;testIt&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074\u003b\u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
      &lt;br /&gt;
            //The following DOES WORK because the encoded value is a valid variable name or function reference.  &amp;quot;testIt&amp;quot; is JavaScript encoded&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
&lt;br /&gt;
            function testIt() {&lt;br /&gt;
                &lt;br /&gt;
                alert(&amp;quot;I was called.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed (upon execution) in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual subcontext (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //server-side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL subcontext (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL subcontext the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS( &amp;amp;#x21a9;&lt;br /&gt;
               Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
&lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrieved the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
[[User:Achim| Achim Hoffmann]] [mailto:achim@owasp.org| achim_@_owasp.org]&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Erlend Oftedal&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=106647</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=106647"/>
				<updated>2011-03-11T07:23:16Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing &amp;lt;code&amp;gt;javascript:attackCode()&amp;lt;/code&amp;gt; to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The setAttribute(&amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;,&amp;lt;i&amp;gt;value_string&amp;lt;/i&amp;gt;) method is dangerous because it implicitly coerces the  &amp;lt;i&amp;gt;string_value&amp;lt;/i&amp;gt; into the DOM attribute datatype of &amp;lt;i&amp;gt;name_string&amp;lt;/i&amp;gt;.  In the case above, the attribute name is an JavaScript event handler, so the attribute value is implicitly converted to JavaScript code and evaluated.  In the case above, JavaScript encoding does not mitigate against DOM based XSS.  Other JavaScript methods which take code as a string types will have a similar problem as outline above (setTimeout, setInterval, new Function, etc.).  This is in stark contrast to JavaScript encoding in the event handler attribute of a HTML tag (HTML parser) where JavaScript encoding mitigates against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!-- Does NOT work  --&amp;gt;&lt;br /&gt;
&amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot; onclick=&amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0029&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An alternative to using Element.setAttribute(...) to set DOM attributes is to set the attribute directly.  Directly setting event handler attributes will allow JavaScript encoding to mitigate against DOM based XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
    &amp;lt;a id=&amp;quot;bb&amp;quot; href=&amp;quot;#&amp;quot;&amp;gt; Test Me&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.  &amp;quot;alert(7)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onclick = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0029&amp;quot;;&lt;br /&gt;
            &lt;br /&gt;
            //The following does NOT work because the event handler is being set to a string.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = &amp;quot;testIt&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;(&amp;quot; and &amp;quot;)&amp;quot;. &amp;quot;alert(77)&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0061\u006c\u0065\u0072\u0074\u0028\u0037\u0037\u0029;&lt;br /&gt;
&lt;br /&gt;
            //The following does NOT work because of the encoded &amp;quot;;&amp;quot;. &amp;quot;testIt;testIt&amp;quot; is JavaScript encoded.&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074\u003b\u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
      &lt;br /&gt;
            //The following DOES WORK because the encoded value is a valid variable name or function reference.  &amp;quot;testIt&amp;quot; is JavaScript encoded&lt;br /&gt;
            document.getElementById(&amp;quot;bb&amp;quot;).onmouseover = \u0074\u0065\u0073\u0074\u0049\u0074;&lt;br /&gt;
&lt;br /&gt;
            function testIt() {&lt;br /&gt;
                &lt;br /&gt;
                alert(&amp;quot;I was called.&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //server-side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS( &amp;amp;#x21a9;&lt;br /&gt;
               Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
[[User:Achim| Achim Hoffmann]] [mailto:achim@owasp.org| achim_@_owasp.org]&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Erlend Oftedal&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105670</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105670"/>
				<updated>2011-02-24T02:15:13Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 } &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&amp;lt;&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
Achim Hoffman&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Erlend Oftedal&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105669</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105669"/>
				<updated>2011-02-24T02:14:01Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 } &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&amp;lt;&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
Achim Hoffman&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105668</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105668"/>
				<updated>2011-02-24T02:13:12Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 } &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&amp;lt;&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
Achim Hoffman&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105667</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105667"/>
				<updated>2011-02-24T02:11:34Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 } &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&amp;lt;&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Authors and Contributing Editors  =&lt;br /&gt;
&lt;br /&gt;
Jim Manico - jim[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
Abraham Kang - abraham.kang[at]owasp.org&lt;br /&gt;
&lt;br /&gt;
Gareth (Gaz) Heyes&lt;br /&gt;
&lt;br /&gt;
Stefano Di Paola&lt;br /&gt;
&lt;br /&gt;
Achim Hoffman&lt;br /&gt;
&lt;br /&gt;
Robert (RSnake) Hansen&lt;br /&gt;
&lt;br /&gt;
Mario Heiderich&lt;br /&gt;
&lt;br /&gt;
John Stevens&lt;br /&gt;
&lt;br /&gt;
Chris (Chris BEF) Schmidt&lt;br /&gt;
&lt;br /&gt;
Mike Samuel&lt;br /&gt;
&lt;br /&gt;
Jeremy Long&lt;br /&gt;
&lt;br /&gt;
Edwardo (SirDarkCat) Alberto Vela Nava&lt;br /&gt;
&lt;br /&gt;
Jeff Williams - jeff.williams[at]owasp.org &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105666</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105666"/>
				<updated>2011-02-24T01:58:51Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&lt;br /&gt;
 var d = document.createElement(‘div’);&lt;br /&gt;
 d.innerHTML = x;&lt;br /&gt;
 document.body.appendChild(d);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
 document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
 element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
 document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(“input”);&lt;br /&gt;
 x.setAttribute(“name”, “company_name”);&lt;br /&gt;
 x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&lt;br /&gt;
 var form1 = document.forms[0];&lt;br /&gt;
 form1.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = document.createElement(&amp;quot;a&amp;quot;);&lt;br /&gt;
 x.href=&amp;quot;#”;&lt;br /&gt;
 x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&lt;br /&gt;
 var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;);&lt;br /&gt;
 x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 document.body.appendChild(x);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&lt;br /&gt;
     \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074                  &lt;br /&gt;
     .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&lt;br /&gt;
     (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&lt;br /&gt;
 } &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 \u0077\u0069\u006e\u0064\u006f\u0077&lt;br /&gt;
 .\u0065\u0076\u0061\u006c&lt;br /&gt;
 \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&lt;br /&gt;
 .\u0077\u0072\u0069\u0074\u0065(111111111));&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;;&lt;br /&gt;
 var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&lt;br /&gt;
 window[s](t);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”…” &amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treated as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
3. Use &amp;lt;code&amp;gt;document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…)&amp;lt;/code&amp;gt;, etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout((function(param) { return function() {&lt;br /&gt;
          customFunction(param);&lt;br /&gt;
          }&lt;br /&gt;
 })(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&lt;br /&gt;
 function customFunction (firstName, lastName)&lt;br /&gt;
       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;doubleJavaScriptEncodedData&amp;lt;/code&amp;gt; has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;setTimeout()&amp;lt;/code&amp;gt; reverses another layer of JavaScript encoding to pass the correct value to &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt;.  The reason why you only need to double JavaScript encode is that the &amp;lt;code&amp;gt;customFunction&amp;lt;/code&amp;gt; function did not itself pass the input to another method which implicitly or explicitly called &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;.  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;lt;code&amp;gt;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt; above would need to be changed to &amp;lt;code&amp;gt;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&lt;br /&gt;
 if (x == &amp;quot;A&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;x is A&amp;quot;);&lt;br /&gt;
 } else if (x == &amp;quot;\u0041&amp;quot;) {&lt;br /&gt;
    alert(&amp;quot;This is what pops&amp;quot;);&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server-side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library such as ESAPI4JS) for the individual sub context (DOM methods) which untrusted data is passed to.  ESAPI4JS (located at http://bit.ly/9hRTLH) and jQuery Encoder (located at https://github.com/chrisisbeef/jquery-encoder/blob/master/src/main/javascript/org/owasp/esapi/jquery/encoder.js) are two client side encoding libraries developed by Chris Schmidt.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here are some examples of how they are used:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happening in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 function escapeHTML(str) {&lt;br /&gt;
      str = str + &amp;quot;''&amp;quot;;&lt;br /&gt;
      var out = &amp;quot;''&amp;quot;;&lt;br /&gt;
      for(var i=0; i&amp;lt;str.length; i++) {&lt;br /&gt;
          if(str[i] === '&amp;lt;') {&lt;br /&gt;
              out += '&amp;amp;amp;lt;';&lt;br /&gt;
          } else if(str[i] === '&amp;gt;') {&lt;br /&gt;
              out += '&amp;amp;amp;gt;';&lt;br /&gt;
          } else if(str[i] === &amp;quot;'&amp;quot;) {&lt;br /&gt;
              out += '&amp;amp;amp;#39;'; &lt;br /&gt;
          } else if(str[i] === '&amp;quot;') {&lt;br /&gt;
              out += '&amp;amp;amp;quot;';                        &lt;br /&gt;
          } else {&lt;br /&gt;
              out += str[i];&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
      return out;                    &lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg. &amp;lt;code&amp;gt;location&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt;).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 window[userData] = “moreUserData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 if (userData===”location”) {&lt;br /&gt;
    window.location = “static/path/or/properly/url/encoded/value”;&lt;br /&gt;
 }&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 var myMapType = {};&lt;br /&gt;
 myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.  This could be used by an attacker to subvert internal and external attributes of the &amp;lt;code&amp;gt;myMapType&amp;lt;/code&amp;gt; object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t &amp;lt;code&amp;gt;eval()&amp;lt;/code&amp;gt; JSON to convert it to native JavaScript objects.  Instead use &amp;lt;code&amp;gt;JSON.toJSON()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;JSON.parse()&amp;lt;/code&amp;gt; (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&amp;lt;&lt;br /&gt;
     window.location = url;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (&amp;lt;code&amp;gt;href&amp;lt;/code&amp;gt; attribute of an &amp;lt;code&amp;gt;&amp;lt;a&amp;gt;&amp;lt;/code&amp;gt; tag) then changed to a JavaScript execution context (&amp;lt;code&amp;gt;javascript:&amp;lt;/code&amp;gt; protocol handler) which passed the untrusted data to an execution URL sub context (&amp;lt;code&amp;gt;window.location&amp;lt;/code&amp;gt; of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server-side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&lt;br /&gt;
 &amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, 'test');”&amp;gt;Click Me&amp;lt;/a&amp;gt;&lt;br /&gt;
 ...&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 Function myFunction (url,name) {&lt;br /&gt;
     var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&lt;br /&gt;
     window.location = encodedURL;&lt;br /&gt;
 }&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 &amp;amp;amp;#x61;lert(1);&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;form name=”myForm” …&amp;gt;&lt;br /&gt;
   &amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&lt;br /&gt;
 …&lt;br /&gt;
 &amp;lt;/form&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&lt;br /&gt;
 document.writeln(x);  //any code passed into lName is now executable.&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
 &amp;lt;script&amp;gt;&lt;br /&gt;
 var tag = document.createElement(“script”);&lt;br /&gt;
 tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&lt;br /&gt;
 &amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105572</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105572"/>
				<updated>2011-02-23T01:35:28Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code, or escaping to HTML and opening a new script &lt;br /&gt;
&amp;gt; tag.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout((function(param) { return function() {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;          customFunction(param);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;          }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;})(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105489</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105489"/>
				<updated>2011-02-21T04:01:55Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters and enclosed within a closure or JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Utilizing an Enclosure (as suggested by Gaz)&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The example that follows illustrates using closures to avoid double JavaScript encoding.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout((function(param) { return function() {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;          customFunction(param);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;          }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;})(&amp;quot;&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;&amp;quot;), y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The other alternative is using N-levels of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105487</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105487"/>
				<updated>2011-02-21T03:47:33Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = &amp;quot;''&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105485</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105485"/>
				<updated>2011-02-21T03:29:34Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105476</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105476"/>
				<updated>2011-02-20T20:44:58Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Mitigating DOM based XSS (Remediation)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;Chris Schmidt has put together another implementation of a JavaScript encoder at http://yet-another-dev.blogspot.com/2011/02/client-side-contextual-encoding-for.html.  &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Global_Industry_Committee_-_Application_10&amp;diff=105431</id>
		<title>Global Industry Committee - Application 10</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Global_Industry_Committee_-_Application_10&amp;diff=105431"/>
				<updated>2011-02-18T17:14:12Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[How to Join a Committee|Click here to return to 'How to Join a Committee' page]] &lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;2&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot; | &amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE APPLICATION FORM'''&amp;lt;/font&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot; | '''Applicant's Name''' &lt;br /&gt;
| colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot; | &amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;Sherif Koussa&amp;lt;/font&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot; | '''Current and past OWASP Roles''' &lt;br /&gt;
| colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot; | OWASP Ottawa Chapter Leader - WebGoat 5.0&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:25%; background:#7B8ABD&amp;quot; align=&amp;quot;center&amp;quot; | '''Committee Applying for''' &lt;br /&gt;
| colspan=&amp;quot;1&amp;quot; style=&amp;quot;width:85%; background:#cccccc&amp;quot; align=&amp;quot;left&amp;quot; | Global Industry Committee&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
Please be aware that for an application to be considered by the board, '''you MUST have 5 recommendations'''. An incomplete application will not be considered for vote. &lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
{| style=&amp;quot;width:100%&amp;quot; border=&amp;quot;0&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! colspan=&amp;quot;8&amp;quot; align=&amp;quot;center&amp;quot; style=&amp;quot;background:#4058A0; color:white&amp;quot; | &amp;lt;font color=&amp;quot;white&amp;quot;&amp;gt;'''COMMITTEE RECOMMENDATIONS'''&amp;lt;/font&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! align=&amp;quot;center&amp;quot; style=&amp;quot;background:white; color:white&amp;quot; | &amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;&amp;lt;/font&amp;gt; &lt;br /&gt;
! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot; | &amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Who Recommends/Name'''&amp;lt;/font&amp;gt; &lt;br /&gt;
! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot; | &amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Role in OWASP'''&amp;lt;/font&amp;gt; &lt;br /&gt;
! align=&amp;quot;center&amp;quot; style=&amp;quot;background:#7B8ABD; color:white&amp;quot; | &amp;lt;font color=&amp;quot;black&amp;quot;&amp;gt;'''Recommendation Content'''&amp;lt;/font&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | '''1''' &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Dinis Cruz &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | [[O2 Platform]] Project Leader &lt;br /&gt;
| style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Sherif is an active OWASP leader and in the past has already made a number of bridges and connections with the Industry. I think he will be a great addition to this Committee&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | '''2''' &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Bruce Mayhew &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | [[WebGoat]] Project Leader &lt;br /&gt;
| style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Sherif is active in the security community, active in OWASP, and has made major contributions to WebGoat. Sherif will be an asset to this committee.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | '''3''' &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Karim Nathoo &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | OWASP Ottawa Chapter Co-Leader &lt;br /&gt;
| style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Sherif is the driving force behind the Ottawa Chapter. &amp;amp;nbsp;He is both a very passionate and motivated volunteer and also a talented application security professional. &amp;amp;nbsp;I am confident he will be an asset to the Global Industry Committee.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | '''4''' &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Joe Bernik&amp;lt;span class=&amp;quot;Apple-tab-span&amp;quot; style=&amp;quot;white-space:pre&amp;quot;&amp;gt;	&amp;lt;/span&amp;gt;&lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Committee member&amp;lt;span class=&amp;quot;Apple-tab-span&amp;quot; style=&amp;quot;white-space:pre&amp;quot;&amp;gt;	&amp;lt;/span&amp;gt;&lt;br /&gt;
| style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Sherif is a driven leader in Canada and will make a great contribution to the Committee.&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;width:3%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | '''5''' &lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Abraham Kang&lt;br /&gt;
| style=&amp;quot;width:20%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | DOM based XSS Cheatsheet Co-Leader&lt;br /&gt;
| style=&amp;quot;width:57%; background:#cccccc&amp;quot; align=&amp;quot;center&amp;quot; | Sherif is a talented application security engineer.  He has worked in many industrusties and his experience would be a great asset to the committee.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
----&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105430</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105430"/>
				<updated>2011-02-18T17:08:27Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105408</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105408"/>
				<updated>2011-02-18T05:02:33Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(userRelativePath))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Because JavaScript is based on an international standard (ECMAScript), JavaScript encoding enables the support of international characters in programming constructs and variables in addition to alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.  HTML tag elements are well defined and do not support alternate representations of the same tag.  So HTML encoding cannot be used to allow the developer to have alternate representations of the &amp;lt;a&amp;gt; tag for example.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example (no HTML encoding):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”…” &amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications (Web 2.0) such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
1. Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &amp;lt;br/&amp;gt;&lt;br /&gt;
2. Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
3. Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&amp;lt;br/&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
4. Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&amp;lt;br/&amp;gt;&lt;br /&gt;
5. There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;N-Levels of Encoding&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your code looked like the following, you would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;       alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval() of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implicitly or explicitly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implicitly or explicitly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through before being passed to the if comparison and the number of times the value was JavaScript encoded.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
This brings up an interesting design point.  Ideally, the correct way to apply encoding and avoid the problem stated above is to server side encode for the output context where data is introduced into the application.  Then client-side encode (using a JavaScript encoding library) for the individual sub context (DOM methods) which untrusted data is passed to.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var input = “&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;”;  //Server side encoding&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
window.location = ESAPI4JS.encodeForURL(input);  //URL encoding is happing in JavaScript&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
document.writeln(ESAPI4JS.encodeForHTML(input));  //HTML encoding is happing in JavaScript&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
It has been well noted by the group that any kind of reliance on a JavaScript library for encoding would be problematic as the JavaScript library could be subverted by attackers.  One option is to wait till ECMAScript 5 so the JavaScript library could support immutable properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Another option provided by Gaz (Gareth) was to use a specific code construct to limit mutability with anonymous clousures.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An example follows:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function escapeHTML(str) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    str = str + '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    var out = '';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    for(var i=0;i&amp;lt;str.length;i++) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        if(str[i] === '&amp;lt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;lt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;gt;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;gt;';&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === &amp;quot;'&amp;quot;) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;#39;';            &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        } else if(str[i] === '&amp;quot;') {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += '&amp;amp;quot;';    &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                        &lt;br /&gt;
&amp;lt;code&amp;gt;                        } else {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                            out += str[i];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                        }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;                    return out;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                    &lt;br /&gt;
&amp;lt;code&amp;gt;                }&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
6. Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you want to change different object attributes based on user input use a level of indirection.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Instead of: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Do the following instead:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
7. When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
8. Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&amp;lt;br/&amp;gt;&lt;br /&gt;
Here is an example of the problem when using map types:&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
Var myMapType = {};&lt;br /&gt;
myMapType[&amp;lt;%=untrustedData%&amp;gt;] = “moreUntrustedData”;&lt;br /&gt;
&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Although the developer writing the code above was trying to add additional keyed elements to the “myMapType” object.  This could be used by an attacker to subvert internal and external attributes of the “myMapType” object.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
9. Run your JavaScript in a ECMAScript 5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
10. Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
=  Common Problems Associated with Mitgating DOM Based XSS =&lt;br /&gt;
==Complex Contexts==&lt;br /&gt;
In many cases the context isn’t always strait forward to discern.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=untrustedData%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = url;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, untrusted data started in the rendering URL context (href attribute of an &amp;lt;a&amp;gt; tag) then changed to a JavaScript execution context (javascript: protocol handler) which passed the untrusted data to an execution URL sub context (window.location of myFunction).  Because the data was introduced in JavaScript code and passed to a URL sub context the appropriate server side encoding would be the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(untrustedData))%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or if you were using ECMAScript 5 with an immutable JavaScript client-side encoding libraries you could do the following:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;!--server side URL encoding has been removed.  Now only JavaScript encoding on server side. --&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=”javascript:myFunction(‘&amp;lt;%=Encoder.encodeForJS(untrustedData)%&amp;gt;’, “test”);”&amp;gt;Click Me&amp;lt;/a&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;Function myFunction (url,name) {&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    var encodedURL = ESAPI4JS.encodeForURL(url);  //URL encoding using client-side scripts&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    window.location = encodedURL;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Insonsistencies of Encoding Libraries ==&lt;br /&gt;
There are a number of open source encoding libraries out there:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#ESAPI&lt;br /&gt;
#Apache Commons String Utils&lt;br /&gt;
#Jtidy&lt;br /&gt;
#Your company’s custom implementation.&lt;br /&gt;
&lt;br /&gt;
Some work on a black list others ignore important characters like “&amp;lt;” and “&amp;gt;”.  ESAPI is one of the few which work on a whitelist and encode all non-alpha numeric characters.&lt;br /&gt;
It is important to use an encoding library which understands which characters can be used to exploit vulnerabilies in their respective contexts.&lt;br /&gt;
But there are misconceptions abound related to proper encoding. &lt;br /&gt;
==Encoding Misconceptions==&lt;br /&gt;
Many security training curriculums and papers advocate the blind usage of HTML encoding to resolve XSS.  This logically seems to be prudent advice as the JavaScript parser does not understand HTML encoding.  However, if the pages returned from your web application utilize a content type of “text/xhtml” or the file type extension of “*.xhtml” then HML encoding may not work to mitigate against XSS.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For example: &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
&amp;amp;#x61;lert(1);&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
The HTML encoded value above is still executable.  If that isn’t enough to keep in mind, you have to remember that encodings are lost when you retrieve them using the value attribute of a DOM element. &amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Let’s look at the sample page and script:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;form name=”myForm” …&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;input type=”text” name=”lName” value=”&amp;lt;%=Encoder.encodeForHTML(last_name)%&amp;gt;”&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
…&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/form&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.myForm.lName.value;  //when the value is retrived the encoding is reversed&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(x);  //any code passed into lName is now executable.&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finally there is the problem that certain methods in JavaScript which are usually safe can be unsafe in certain contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Usually Safe Methods==&lt;br /&gt;
&lt;br /&gt;
One example of an attribute which is usually safe is innerText.  Some papers or guides advocate its use as an alternative to innerHTML to mitigate against XSS in innerHTML.  However, depending on the tag which innerText is applied, code can be executed.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var tag = document.createElement(“script”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;tag.innerText = “&amp;lt;%=untrustedData%&amp;gt;”;  //executes code&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105311</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105311"/>
				<updated>2011-02-17T05:56:34Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&lt;br /&gt;
&lt;br /&gt;
N-Levels of Encoding&lt;br /&gt;
&lt;br /&gt;
If you code looked like the following would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;    alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implictly or explictly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implictly or explictly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through and the number of times the value was JavaScript encoded.&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&lt;br /&gt;
##&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105310</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105310"/>
				<updated>2011-02-17T05:54:48Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, JavaScript encoded to N-levels based on usage, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function or handle it by adding an extra layer of encoding.&lt;br /&gt;
&lt;br /&gt;
N-Levels of Encoding&lt;br /&gt;
&lt;br /&gt;
If you code looked like the following would need to only double JavaScript encode input data.&lt;br /&gt;
&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;function customFunction (firstName, lastName) {&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;    alert(&amp;quot;Hello&amp;quot; + firstName + &amp;quot; &amp;quot; + lastNam);&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The doubleJavaScriptEncodedData has its first layer of JavaScript encoding reversed in the single quotes.  Then the implicit eval of setTimeout reverses another layer of JavaScript encoding to pass the correct value to &amp;quot;customFunction&amp;quot;.  The reason why you only need to double JavaScript encode is that the &amp;quot;customFunction&amp;quot; function did not itself pass the input to another method which implictly or explictly called eval().  If &amp;quot;firstName&amp;quot; was passed to another JavaScript method which implictly or explictly called eval() then &amp;quot;&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;&amp;quot; above would need to be changed to &amp;quot;&amp;lt;%=tripleJavaScriptEncodedData%&amp;gt;&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
An important implementation note is that if the JavaScript code tries to utilize the double or triple encoded data in string comparisons, the value may be interpreted as different values based on the number of evals() the data has passed through and the number of times the value was JavaScript encoded.&lt;br /&gt;
&lt;br /&gt;
If &amp;quot;A&amp;quot; is double JavaScript encoded then the following if check will return false.&lt;br /&gt;
##&amp;lt;code&amp;gt;var x = &amp;quot;doubleJavaScriptEncodedA&amp;quot;;  //\u005c\u0075\u0030\u0030\u0034\u0031&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;if (x == &amp;quot;A&amp;quot;) { &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;   alert(&amp;quot;x is A&amp;quot;); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;} else if (x == &amp;quot;\u0041&amp;quot;) { &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;   alert(&amp;quot;This is what pops&amp;quot;); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike Samuel).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike Samuel).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gareth Heyes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105309</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105309"/>
				<updated>2011-02-17T05:22:08Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Untrusted data should only be treatedas displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105308</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105308"/>
				<updated>2011-02-17T05:19:53Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If HTML encoding followed the same semantics as JavaScript encoding.  The line above could have possibily worked to render a link.  This difference makes JavaScript encoding a less viable weapon in our fight against XSS.&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105307</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105307"/>
				<updated>2011-02-17T05:16:38Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding serves to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105306</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105306"/>
				<updated>2011-02-17T05:13:08Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105305</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105305"/>
				<updated>2011-02-17T05:09:46Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These methods constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105304</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105304"/>
				<updated>2011-02-17T05:08:57Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML content within JavaScript.  These method constitute the HTML Sub Context within the Execution Context.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105303</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105303"/>
				<updated>2011-02-17T05:06:47Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in addition to the encoding required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML execution content within JavaScript.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Cheatsheet_Navigation}}&lt;br /&gt;
&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105301</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105301"/>
				<updated>2011-02-17T04:59:59Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in additional encodings required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML execution content within JavaScript.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, Jeff Williams, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Application Security Verification Standard Project]]&lt;br /&gt;
[[Category:OWASP Enterprise Security API]]&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105298</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105298"/>
				<updated>2011-02-17T04:54:35Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in additional encodings required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML execution content within JavaScript.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= HTML Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
= URL Attribute Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Jim Manico, Gareth Heyes, Stefano Di Paola, Robert Hansen, Mario Heiderich, Achim Hoffmann, John Stevens, Edwardo (SirDarkCat) Alberto Vela Nava, Chris Schmidt, Mike Samuel, Jeremy Long, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Application Security Verification Standard Project]]&lt;br /&gt;
[[Category:OWASP Enterprise Security API]]&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105109</id>
		<title>DOM based XSS Prevention Cheat Sheet</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=DOM_based_XSS_Prevention_Cheat_Sheet&amp;diff=105109"/>
				<updated>2011-02-15T06:07:13Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: DOM based XSS Cheatsheet&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Introduction =&lt;br /&gt;
&lt;br /&gt;
When looking at XSS (Cross Site Scripting) there are three generally recognized forms of XSS.  Reflected, Persisted, and DOM based XSS.  The [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] has done an excellent job of addressing Reflected and Persisted XSS. This cheatsheet will address DOM (Document Object Model) based XSS and is an extension (as assumes comprehension of) the the [http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet XSS Prevention Cheatsheet] .&lt;br /&gt;
&lt;br /&gt;
In order to understand DOM based XSS one needs to see the fundamental difference between reflected and persisted XSS when compared to DOM based XSS.  Reflected and persisted XSS exist in a higher level rendering context and DOM based XSS is primarily found in a lower level execution context.  A rendering context is associated with the parsing of HTML tags and their attributes.  The HTML parser of the rendering context dictates how data is presented and laid out on the page and can be further broken down into the standard contexts of HTML, HTML attribute, URL, and CSS.  The JavaScript or VBScript parser of an execution context is associated with the parsing and execution of script code.  Each parser has distinct and separate semantics in the way they can possibly execute script code (XSS) which make creating consistent rules for mitigating both rendering and execution based contexts difficult.   The complication is compounded by the differing meanings and treatment of encoded values within each sub context (HTML, HTML attribute, URL, and CSS) within the execution context.  &lt;br /&gt;
&lt;br /&gt;
This paper refers to the HTML, HTML attribute, URL, and CSS Cheatsheet contexts as subcontexts because each of these contexts can be reached and set within a JavaScript execution context.  In JavaScript code the main context is JavaScript but since an attacker can try to attack the other 4 contexts using equivalent JavaScript DOM methods we refer to the other contexts besides the JavaScript context as sub contexts.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following example of an attack which occurs in the JavaScript context and HTML sub context:&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
var x = ‘&amp;lt;%= htmlAndJavaScriptEncodedVar %&amp;gt;’;&amp;lt;br/&amp;gt;&lt;br /&gt;
var d = document.createElement(‘div’);&amp;lt;br/&amp;gt;&lt;br /&gt;
d.innerHTML = x;&amp;lt;br/&amp;gt;&lt;br /&gt;
document.body.appendChild(d);&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/script&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One consistency, however, is the need to JavaScript encode in additional encodings required for the sub context in the execution context.  Let’s look at the individual sub contexts of the execution context in turn.&lt;br /&gt;
&lt;br /&gt;
= HTML Sub Context within the Execution Context =&lt;br /&gt;
&lt;br /&gt;
There are several methods and attributes which can be used to directly render HTML execution content within JavaScript.&lt;br /&gt;
&lt;br /&gt;
==Attributes==&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;HTML&amp;gt; Tags and markup”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;HTML&amp;gt; Tags and markup”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Guideline==&lt;br /&gt;
In a pure HTML execution context (not HTML Attribute) use HTML and JavaScript encoding to mitigate against attacks.&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.innerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;element.outerHTML = “&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Methods==&lt;br /&gt;
&amp;lt;code&amp;gt;document.write(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.writeln(“&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTML(untrustedData))%&amp;gt;”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=HTML Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
The HTML attribute Sub Context within the Execution context is divergent from the standard encoding rules.  This is because the rule to HTML attribute encode in an HTML attribute rendering context is mitigating attacks which try to exit out of the attribute to add additional attributes and/or tags which could have executable code.  When you are in a DOM execution context you only need to JavaScript encode HTML attributes which do not execute code (attributes other than event handler, CSS, and URL attributes).&lt;br /&gt;
&lt;br /&gt;
For example, the general rule is to HTML Attribute encode untrusted data (data from the database, http request, user, backend system, etc.) placed in an HTML Attribute.  This is the appropriate step to take when outputting data in a rendering context, however using HTML Attribute encoding in an execution context will break the application display of data.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForHTMLAttr(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The problem is that if companyName had the value “Johnson &amp;amp; Johnson”.  What would be displayed in the input text field would be “Johnson &amp;amp;amp; Johnson”.  The appropriate encoding to use in the above case would be only JavaScript encoding to disallow an attacker from closing out the single quotes and in-lining code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“input”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“name”, “company_name”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“value”, ‘&amp;lt;%=Encoder.encodeForJS(companyName)%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var form1 = document.forms[0];&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;form1.appendChild(x);&lt;br /&gt;
&lt;br /&gt;
It is important to note that when setting an HTML attribute which does not execute code the value is set directly within the object attribute of the HTML element so there is no concerns with injecting up.&lt;br /&gt;
&lt;br /&gt;
=URL Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
The logic which parses URLs in both execution and rendering contexts looks to be the same.  Therefore there is little change in the encoding rules for URL attributes in an execution (DOM) context.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(“a”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(“href”, ‘&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;’);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextElement(“Click Me To Test”);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you utilize fully qualified URLs then this will break the links as the colon in the protocol identifier (“http:” or “javascript:”) will be URL encoded preventing the “http” and “javascript” protocols from being invoked.&lt;br /&gt;
&lt;br /&gt;
=CSS Attribute Sub Context within the Execution Context=&lt;br /&gt;
&lt;br /&gt;
Normally executing JavaScript from a CSS context required either passing “javascript:attackCode()” to the CSS url() method or invoking the CSS expression() method passing JavaScript code to be directly executed.  From my experience, calling the expression() function from an execution context (JavaScript) has been disabled.  In order to mitigate against the CSS url() method ensure that you are URL encoding the data passed to the CSS url() method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.style.backgroundImage = &amp;quot;url(&amp;lt;%=Encoder.encodeForJS(Encoder.encodeForURL(companyName))%&amp;gt;)&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
TODO: We have not been able to get the expression() function working from DOM JavaScript code.  Need some help.&lt;br /&gt;
&lt;br /&gt;
=Event Handler and JavaScript code Sub Contexts within an Execution Context=&lt;br /&gt;
&lt;br /&gt;
Putting dynamic data within JavaScript code is especially dangerous because JavaScript encoding has different semantics for JavaScript encoded data when compared to other encodings.  In many cases, JavaScript encoding does not stop attacks within an execution context.  For example, a JavaScript encoded string will execute even though it is JavaScript encoded. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var x = document.createElement(&amp;quot;a&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.href=&amp;quot;#”;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.setAttribute(&amp;quot;onclick&amp;quot;, &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0032\u0032\u0029&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var y = document.createTextNode(&amp;quot;Click To Test&amp;quot;); &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;x.appendChild(y);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;document.body.appendChild(x);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript event handler methods are dangerous because they implicitly do an eval() on the data passed to the DOM attribute.  There are other places in JavaScript where JavaScript encoding is accepted as valid executable code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;for ( var \u0062=0; \u0062 &amp;lt; 10; \u0062++){&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    \u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074   &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;                   &lt;br /&gt;
&amp;lt;code&amp;gt;    .\u0077\u0072\u0069\u0074\u0065\u006c\u006e&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;    (&amp;quot;\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064&amp;quot;);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;} &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0077\u0069\u006e\u0064\u006f\u0077&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0065\u0076\u0061\u006c (&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;\u0064\u006f\u0063\u0075\u006d\u0065\u006e\u0074&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;.\u0077\u0072\u0069\u0074\u0065(111111111));&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var s = &amp;quot;\u0065\u0076\u0061\u006c&amp;quot;; &amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;var t = &amp;quot;\u0061\u006c\u0065\u0072\u0074\u0028\u0031\u0031\u0029&amp;quot;;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;window[s](t);&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JavaScript encoding enables the support of international characters in programming constructs and variables as well as well as alternate string representations (string escapes).  &lt;br /&gt;
&lt;br /&gt;
However the opposite is the case with HTML encoding.&lt;br /&gt;
&lt;br /&gt;
==HTML Encoding’s Disarming Nature==&lt;br /&gt;
&lt;br /&gt;
In general, HTML encoding servers to castrate HTML tags which are placed in HTML and HTML attribute contexts.&lt;br /&gt;
Working example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;a href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally encoded example (Does Not Work – DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;amp;#x3c;a href=… &amp;amp;amp;#x3e;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
HTML encoded example to highlight a fundamental difference with JavaScript encoded values (DNW):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;&amp;amp;amp;#x61; href=…&amp;gt;&amp;lt;/code&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Guidelines for Developing Secure Applications Utilizing JavaScript=&lt;br /&gt;
&lt;br /&gt;
DOM based XSS is extremely difficult to mitigate against because of its large attack surface and lack of standardization across browsers.  The guidelines below are an attempt to provide guidelines for developers when developing Web based JavaScript applications such that they can avoid XSS.&lt;br /&gt;
&lt;br /&gt;
# Data should only be displayed as displayable text.  Never treat untrusted data as code or markup within JavaScript code. &lt;br /&gt;
# Always JavaScript encode and delimit untrusted data as quoted strings when entering the application (Jim Manico and Robert Hansen) &lt;br /&gt;
##&amp;lt;code&amp;gt;var x = “&amp;lt;%=encodedJavaScriptData%&amp;gt;”;&amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
# Use document.createElement(“…”), element.setAttribute(“…”,”value”), element.appendChild(…), etc. to build dynamic interfaces.  Avoid use of HTML rendering methods:&lt;br /&gt;
##&amp;lt;code&amp;gt; &amp;lt;code&amp;gt;element.innerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt; &amp;lt;code&amp;gt;element.outerHTML = “…”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt; &amp;lt;code&amp;gt;document.write(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
##&amp;lt;code&amp;gt; &amp;lt;code&amp;gt;document.writeln(…); &amp;lt;/code&amp;gt;&lt;br /&gt;
#Understand the dataflow of untrusted data through your JavaScript code.  If you do have to use the methods above remember to HTML and them JavaScript encode the untrusted data (Stefano Di Paola).&lt;br /&gt;
#There are numerous methods which implicitly eval() data passed to it.  Make sure that any untrusted data passed to these methods is delimited with string delimiters, double JavaScript encoded, and wrapped in a custom function.  Ensure to follow step 4 above to make sure that the untrusted data is not sent to dangerous methods within the custom function.&lt;br /&gt;
##&amp;lt;code&amp;gt;setTimeout(“customFunction(‘&amp;lt;%=doubleJavaScriptEncodedData%&amp;gt;’, y)”);&amp;lt;/code&amp;gt;&lt;br /&gt;
#Limit the usage of dynamic untrusted data to right side operations.  And be aware of data which may be passed to the application which look like code (eg.  “location”, “eval”).  (Achim)#&lt;br /&gt;
##	&amp;lt;code&amp;gt;var x = “&amp;lt;%=properly encoded data for flow%&amp;gt;”; &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;if you want to change different object attributes based on user input instead of: &amp;lt;/code&amp;gt;&lt;br /&gt;
##	&amp;lt;code&amp;gt;window[userData] = “moreUserData”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#Use the following methods when placing dynamic untrusted data in right side operations.  &lt;br /&gt;
## &amp;lt;code&amp;gt;if (userData===”location”) { &amp;lt;/code&amp;gt; &lt;br /&gt;
##&amp;lt;code&amp;gt;window.location = “static/path/or/properly/url/encoded/value”; &amp;lt;/code&amp;gt;&lt;br /&gt;
#When URL encoding in DOM be aware of character set issues as the character set in JavaScript DOM is not clearly defined (Mike from Google).&lt;br /&gt;
#Limit access to properties objects when using object[x] accessors. (Mike from Google).  In other words use a level of indirection between untrusted input and specified object properties.&lt;br /&gt;
#Run your JavaScript in a ECAM5 canopy or sand box to make it harder for your JavaScript API to be compromised (Gaz Hayzes and John Stevens).&lt;br /&gt;
#Don’t eval() JSON to convert it to native JavaScript objects.  Instead use JSON.toJSON() and JSON.parse() (Chris Schmidt).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;b&amp;gt;Special thanks to Gaz Hayes, Stefano di Paola, Robert Hansen, Mario H, Achim, John Stevens, Edwardo (SirDarkCat), Chris Schmidt, Mike from Google, and many others who help make this guide possible&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:OWASP Application Security Verification Standard Project]]&lt;br /&gt;
[[Category:OWASP Enterprise Security API]]&lt;br /&gt;
[[Category:How To]]&lt;br /&gt;
[[Category:Cheatsheets]]&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_5&amp;diff=105015</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 5</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_5&amp;diff=105015"/>
				<updated>2011-02-14T06:20:52Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OWASP Licensing FAQ (Frequently Asked Questions) -- Deliverable from OWASP Summit meeting in Lisbon 2011&amp;lt;br&amp;gt;Disclaimer: The following is not legal advice. It is highly recommended for a licensed lawyer to review your specific situation and ascertain all relevant issues when selecting and understanding license agreements.&amp;lt;br&amp;gt;1. Who own the content and code submitted to OWASP?&amp;lt;br&amp;gt;a. The author of the submitted code or content owns the code. However, the author submitting the creative work agrees to open source their work under an approved open source license. The author also has the option of completely assigning all rights in his work to OWASP. Under copyright law the author of a creative work gains copyright protection once the creative work is put into a tangible form.&amp;lt;br&amp;gt;2. Can I take back from OWASP the code/documentation I had previously submitted?&amp;lt;br&amp;gt;a. Technically no, because you open sourced your code/documentation. However, you can fork your documentation/code and close source your additional changes as the owner of the original documentation/code.&amp;lt;br&amp;gt;3. Does OWASP require you to share back your changes? &amp;lt;br&amp;gt;a. It depends on the license associated with the code/documentation you are modifying. Some licenses require you to share back any code changes the instant you make then. Other licenses require you to use the same license as the parent code/document which you used. Some are triggered upon distribution and others are triggered on modification or use.&amp;lt;br&amp;gt;4. What is the default license for information posted on the OWASP wiki?&amp;lt;br&amp;gt;a. Creative Commons 3.0 SA Attribution&amp;lt;br&amp;gt;5. Can you override the default license which OWASP runs under?&amp;lt;br&amp;gt;a. Yes, but you have to follow the directions in the license you are selecting to abide by the selected license. If the license you selected for your code/document does not include placement directions. Add a license section in the header comments of a code file or the appendix of a document.&amp;lt;br&amp;gt;6. Which license should I use if I want to give enterprises free will to build products on top of your submitted code or make and use changes to your submitted documents?&amp;lt;br&amp;gt;a. BSD&amp;lt;br&amp;gt;7. Which license should I use to control distribution, sale or modification of the submitted code/documentation?&amp;lt;br&amp;gt;a. It depends on the limitations you want to enforce in your submitted code/documentation. So read the license and talk to an attorney to understand what you are getting into. Remember that the more restrictive the license then the less likely that an enterprise will want to use it.&amp;lt;br&amp;gt;8. Is it possible to change my license after my document/code is released to the public?&amp;lt;br&amp;gt;a. If you are the sole contributor for the document/code then you can make changes to the license at any time. If there are multiple contributors to a document or code base you will need go get agreement for the license change from all contributors.&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_5&amp;diff=105014</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 5</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_5&amp;diff=105014"/>
				<updated>2011-02-14T06:20:18Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Created page with &amp;quot;OWASP Licensing FAQ (Frequently Asked Questions) -- Deliverable from OWASP Summit meeting in Lisbon 2011 Disclaimer:  The following is not legal advice.  It is highly recommended...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OWASP Licensing FAQ (Frequently Asked Questions) -- Deliverable from OWASP Summit meeting in Lisbon 2011&lt;br /&gt;
Disclaimer:  The following is not legal advice.  It is highly recommended for a licensed lawyer to review your specific situation and ascertain all relevant issues when selecting and understanding license agreements.&lt;br /&gt;
1.	 Who own the content and code submitted to OWASP?&lt;br /&gt;
a.	The author of the submitted code or content owns the code.  However, the author submitting the creative work agrees to open source their work under an approved open source license.  The author also has the option of completely assigning all rights in his work to OWASP.  Under copyright law the author of a creative work gains copyright protection once the creative work is put into a tangible form.&lt;br /&gt;
2.	Can I take back from OWASP the code/documentation I had previously submitted?&lt;br /&gt;
a.	Technically no, because you open sourced your code/documentation.  However, you can fork your documentation/code and close source your additional changes as the owner of the original documentation/code.&lt;br /&gt;
3.	Does OWASP require you to share back your changes?  &lt;br /&gt;
a.	It depends on the license associated with the code/documentation you are modifying.  Some licenses require you to share back any code changes the instant you make then.  Other licenses require you to use the same license as the parent code/document which you used.  Some are triggered upon distribution and others are triggered on modification or use.&lt;br /&gt;
4.	What is the default license for information posted on the OWASP wiki?&lt;br /&gt;
a.	Creative Commons 3.0 SA Attribution&lt;br /&gt;
5.	Can you override the default license which OWASP runs under?&lt;br /&gt;
a.	Yes, but you have to follow the directions in the license you are selecting to abide by the selected license.  If the license you selected for your code/document does not include placement directions.  Add a license section in the header comments of a code file or the appendix of a document.&lt;br /&gt;
6.	Which license should I use if I want to give enterprises free will to build products on top of your submitted code or make and use changes to your submitted documents?&lt;br /&gt;
a.	BSD&lt;br /&gt;
7.	Which license should I use to control distribution, sale or modification of the submitted code/documentation?&lt;br /&gt;
a.	It depends on the limitations you want to enforce in your submitted code/documentation.  So read the license and talk to an attorney to understand what you are getting into.  Remember that the more restrictive the license then the less likely that an enterprise will want to use it.&lt;br /&gt;
8.	Is it possible to change my license after my document/code is released to the public?&lt;br /&gt;
a.	If you are the sole contributor for the document/code then you can make changes to the license at any time.  If there are multiple contributors to a document or code base you will need go get agreement for the license change from all contributors.&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105013</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105013"/>
				<updated>2011-02-14T06:19:16Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_email2 = alexandre.aniceto@sekirite.org&lt;br /&gt;
| summit_session_attendee_username2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_company2= Willway&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies. OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = OWASP: Licensing FAQs&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = See the &amp;quot;discussion&amp;quot; page for deliverables&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt;Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt;Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&amp;amp;nbsp;Test&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105012</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105012"/>
				<updated>2011-02-14T06:13:03Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;IMG class=FCK__MWIncludeonly src=&amp;quot;http://www.owasp.org/extensions/FCKeditor/fckeditor/editor/images/spacer.gif&amp;quot; width=1 height=1 _fckfakelement=&amp;quot;true&amp;quot; _fckrealelement=&amp;quot;3&amp;quot; _fck_mw_includeonly=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;IMG class=FCK__MWNoinclude src=&amp;quot;http://www.owasp.org/extensions/FCKeditor/fckeditor/editor/images/spacer.gif&amp;quot; width=1 height=1 _fckfakelement=&amp;quot;true&amp;quot; _fckrealelement=&amp;quot;2&amp;quot; _fck_mw_noinclude=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_email2 = alexandre.aniceto@sekirite.org&lt;br /&gt;
| summit_session_attendee_username2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_company2= Willway&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies. OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = OWASP: Licensing FAQs&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = See the &amp;quot;discussion&amp;quot; page for deliverables&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt;Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt;Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Summit_2011_Working_Sessions/Session090&amp;diff=105011</id>
		<title>Talk:Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Summit_2011_Working_Sessions/Session090&amp;diff=105011"/>
				<updated>2011-02-14T06:10:27Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''OWASP Licensing FAQ (Frequently Asked Questions) -- Deliverable 1&amp;amp;nbsp;from OWASP Summit meeting in Lisbon 2011''' &lt;br /&gt;
&lt;br /&gt;
Disclaimer: The following is not legal advice. It is highly recommended for a licensed lawyer to review your specific situation and ascertain all relevant issues when selecting and understanding license agreements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;1. Who own the content and code submitted to OWASP? a. The author of the submitted code or content owns the code. However, the author submitting the creative work agrees to open source their work under an approved open source license. The author also has the option of completely assigning all rights in his work to OWASP. Under copyright law the author of a creative work gains copyright protection once the creative work is put into a tangible form. &lt;br /&gt;
&lt;br /&gt;
2. Can I take back from OWASP the code/documentation I had previously submitted? a. Technically no, because you open sourced your code/documentation. However, you can fork your documentation/code and close source your additional changes as the owner of the original documentation/code. &lt;br /&gt;
&lt;br /&gt;
3. Does OWASP require you to share back your changes? a. It depends on the license associated with the code/documentation you are modifying. Some licenses require you to share back any code changes the instant you make then. Other licenses require you to use the same license as the parent code/document which you used. Some are triggered upon distribution and others are triggered on modification or use. &lt;br /&gt;
&lt;br /&gt;
4. What is the default license for information posted on the OWASP wiki? a. Creative Commons 3.0 SA Attribution &lt;br /&gt;
&lt;br /&gt;
5. Can you override the default license which OWASP runs under? a. Yes, but you have to follow the directions in the license you are selecting to abide by the selected license. If the license you selected for your code/document does not include placement directions. Add a license section in the header comments of a code file or the appendix of a document. &lt;br /&gt;
&lt;br /&gt;
6. Which license should I use if I want to give enterprises free will to build products on top of your submitted code or make and use changes to your submitted documents? a. BSD &lt;br /&gt;
&lt;br /&gt;
7. Which license should I use to control distribution, sale or modification of the submitted code/documentation? a. It depends on the limitations you want to enforce in your submitted code/documentation. So read the license and talk to an attorney to understand what you are getting into. Remember that the more restrictive the license then the less likely that an enterprise will want to use it. &lt;br /&gt;
&lt;br /&gt;
8. Is it possible to change my license after my document/code is released to the public? a. If you are the sole contributor for the document/code then you can make changes to the license at any time. If there are multiple contributors to a document or code base you will need go get agreement for the license change from all contributors. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''OWASP Licensing Issues to Consider -- Deliverable 2&amp;amp;nbsp;from OWASP Summit meeting in Lisbon 2011''' &lt;br /&gt;
&lt;br /&gt;
1. Should we require contributors to completely assign their rights to OWASP. During the meeting it was mentioned that Apache does this and it makes changing licenses and other management functions easier. &lt;br /&gt;
&lt;br /&gt;
2. How should we deal with a multi-contributor document or code base where there is a lack of agreement for license types or other things requiring a unanimous agreement. &lt;br /&gt;
&lt;br /&gt;
3. I thought that the choice of licensing by owner was open to any open source license. Should we limit the options? &lt;br /&gt;
&lt;br /&gt;
4. It was determined that the major issue with enterprise adoption of OWASP documents was the requirement to open source/share back any derivative documents upon use (older licenses) or utilize the same or similar open source license upon distribution (Creative Commons 3.0 SA Attribution). Can we clarify the meaning of “distribution” such that the passing of derived works to partners or affiliates does not constitute public “distribution” under Creative Commons 3.0 SA Attribution)? &lt;br /&gt;
&lt;br /&gt;
5. Can we have a licensed attorney review the licenses in question to ensure that there are no hidden issues with OWASP licensing? For example, to discuss if C.C. Share Alike 3.0 SA Attribution is the correct license for OWASP documentation. Also to see if there is a process which can be used to update older document licenses (such as giving contributors notice and opportunity to object to license changes) &lt;br /&gt;
&lt;br /&gt;
6. Is there any difference with international contributors as to copyright and ownership rules? &lt;br /&gt;
&lt;br /&gt;
7.&amp;amp;nbsp;The OWASP:Copyright page needs to be updated to Creative Commons 3.0 SA Attribution.&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_4&amp;diff=105010</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 4</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_4&amp;diff=105010"/>
				<updated>2011-02-14T06:08:05Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Created page with &amp;quot;Clarify the term &amp;quot;distribution&amp;quot; so that it does not include affiliates and partners of enterprises.&amp;amp;nbsp; This would help enterprises&amp;amp;nbsp;who modify OWASP documents to use them ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Clarify the term &amp;quot;distribution&amp;quot; so that it does not include affiliates and partners of enterprises.&amp;amp;nbsp; This would help enterprises&amp;amp;nbsp;who modify OWASP documents to use them for internal operations with occasional distribution to affiliates and partners.&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_3&amp;diff=105009</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 3</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_3&amp;diff=105009"/>
				<updated>2011-02-14T06:01:35Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Created page with &amp;quot;4. It was determined that the major issue with enterprise adoption of OWASP documents was the requirement to open source/share back any derivative documents upon use (older licen...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;4. It was determined that the major issue with enterprise adoption of OWASP documents was the requirement to open source/share back any derivative documents upon use (older licenses) or utilize the same or similar open source license upon distribution (Creative Commons 3.0 SA Attribution). Can we clarify the meaning of “distribution” such that the passing of derived works to partners or affiliates does not constitute public “distribution” under Creative Commons 3.0 SA Attribution)?&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_2&amp;diff=105008</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 2</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_2&amp;diff=105008"/>
				<updated>2011-02-14T05:59:43Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The most popular licenses used at OWASP are &lt;br /&gt;
&lt;br /&gt;
GNU Free Documentation License &lt;br /&gt;
&lt;br /&gt;
LGPL ang GPL &lt;br /&gt;
&lt;br /&gt;
Creative Commons 3.0 SA Attribution (all wiki content is this)&lt;br /&gt;
&lt;br /&gt;
BSD&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_2&amp;diff=105007</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 2</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_2&amp;diff=105007"/>
				<updated>2011-02-14T05:53:05Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Created page with &amp;quot;The most popular licenses used at OWASP are  GNU Free Documentation License LGPL ang GPL Creative Commons 3.0 SA Attribution BSD&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The most popular licenses used at OWASP are &lt;br /&gt;
GNU Free Documentation License&lt;br /&gt;
LGPL ang GPL&lt;br /&gt;
Creative Commons 3.0 SA Attribution&lt;br /&gt;
BSD&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105006</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105006"/>
				<updated>2011-02-14T05:44:32Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_email2 = alexandre.aniceto@sekirite.org&lt;br /&gt;
| summit_session_attendee_username2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_company2= Willway&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies.  OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = See the &amp;quot;discussion&amp;quot; page for deliverables&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105005</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105005"/>
				<updated>2011-02-14T05:42:42Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_email2 = alexandre.aniceto@sekirite.org&lt;br /&gt;
| summit_session_attendee_username2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_company2= Willway&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies.  OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List is existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = See the &amp;quot;discussion&amp;quot; page for deliverables&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105004</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=105004"/>
				<updated>2011-02-14T05:41:07Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_email2 = alexandre.aniceto@sekirite.org&lt;br /&gt;
| summit_session_attendee_username2 = Alexandre Miguel Aniceto&lt;br /&gt;
| summit_session_attendee_company2= Willway&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies.  OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List is existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = See the &amp;quot;discussion&amp;quot; page for diliverables&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_1&amp;diff=105003</id>
		<title>Summit 2011 Working Sessions/Session090/Deliverable 1</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090/Deliverable_1&amp;diff=105003"/>
				<updated>2011-02-14T05:31:25Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: Created page with &amp;quot;OWASP only asks that contributors utilize an approved Open Source License.  However, the preferred license for wiki content is the Creative Commons 3.0 SA Attribution.&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;OWASP only asks that contributors utilize an approved Open Source License.&lt;br /&gt;
&lt;br /&gt;
However, the preferred license for wiki content is the Creative Commons 3.0 SA Attribution.&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Talk:Summit_2011_Working_Sessions/Session090&amp;diff=105002</id>
		<title>Talk:Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Talk:Summit_2011_Working_Sessions/Session090&amp;diff=105002"/>
				<updated>2011-02-14T05:19:52Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: 2 Deliverables from Session Workshop&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''OWASP Licensing FAQ (Frequently Asked Questions) -- Deliverable 1&amp;amp;nbsp;from OWASP Summit meeting in Lisbon 2011''' &lt;br /&gt;
&lt;br /&gt;
Disclaimer: The following is not legal advice. It is highly recommended for a licensed lawyer to review your specific situation and ascertain all relevant issues when selecting and understanding license agreements. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;1. Who own the content and code submitted to OWASP? a. The author of the submitted code or content owns the code. However, the author submitting the creative work agrees to open source their work under an approved open source license. The author also has the option of completely assigning all rights in his work to OWASP. Under copyright law the author of a creative work gains copyright protection once the creative work is put into a tangible form. &lt;br /&gt;
&lt;br /&gt;
2. Can I take back from OWASP the code/documentation I had previously submitted? a. Technically no, because you open sourced your code/documentation. However, you can fork your documentation/code and close source your additional changes as the owner of the original documentation/code. &lt;br /&gt;
&lt;br /&gt;
3. Does OWASP require you to share back your changes? a. It depends on the license associated with the code/documentation you are modifying. Some licenses require you to share back any code changes the instant you make then. Other licenses require you to use the same license as the parent code/document which you used. Some are triggered upon distribution and others are triggered on modification or use. &lt;br /&gt;
&lt;br /&gt;
4. What is the default license for information posted on the OWASP wiki? a. Creative Commons 3.0 SA Attribution &lt;br /&gt;
&lt;br /&gt;
5. Can you override the default license which OWASP runs under? a. Yes, but you have to follow the directions in the license you are selecting to abide by the selected license. If the license you selected for your code/document does not include placement directions. Add a license section in the header comments of a code file or the appendix of a document. &lt;br /&gt;
&lt;br /&gt;
6. Which license should I use if I want to give enterprises free will to build products on top of your submitted code or make and use changes to your submitted documents? a. BSD &lt;br /&gt;
&lt;br /&gt;
7. Which license should I use to control distribution, sale or modification of the submitted code/documentation? a. It depends on the limitations you want to enforce in your submitted code/documentation. So read the license and talk to an attorney to understand what you are getting into. Remember that the more restrictive the license then the less likely that an enterprise will want to use it. &lt;br /&gt;
&lt;br /&gt;
8. Is it possible to change my license after my document/code is released to the public? a. If you are the sole contributor for the document/code then you can make changes to the license at any time. If there are multiple contributors to a document or code base you will need go get agreement for the license change from all contributors. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''OWASP Licensing Issues to Consider -- Deliverable 2&amp;amp;nbsp;from OWASP Summit meeting in Lisbon 2011''' &lt;br /&gt;
&lt;br /&gt;
1. Should we require contributors to completely assign their rights to OWASP. During the meeting it was mentioned that Apache does this and it makes changing licenses and other management functions easier. &lt;br /&gt;
&lt;br /&gt;
2. How should we deal with a multi-contributor document or code base where there is a lack of agreement for license types or other things requiring a unanimous agreement. &lt;br /&gt;
&lt;br /&gt;
3. I thought that the choice of licensing by owner was open to any open source license. Should we limit the options? &lt;br /&gt;
&lt;br /&gt;
4. It was determined that the major issue with enterprise adoption of OWASP documents was the requirement to open source/share back any derivative documents upon use (older licenses) or utilize the same or similar open source license upon distribution (Creative Commons 3.0 SA Attribution). Can we clarify the meaning of “distribution” such that the passing of derived works to partners or affiliates does not constitute public “distribution” under Creative Commons 3.0 SA Attribution)? &lt;br /&gt;
&lt;br /&gt;
5. Can we have a licensed attorney review the licenses in question to ensure that there are no hidden issues with OWASP licensing? For example, to discuss if C.C. Share Alike 3.0 SA Attribution is the correct license for OWASP documentation. Also to see if there is a process which can be used to update older document licenses (such as giving contributors notice and opportunity to object to license changes) &lt;br /&gt;
&lt;br /&gt;
6. Is there any difference with international contributors as to copyright and ownership rules?&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session086&amp;diff=103396</id>
		<title>Summit 2011 Working Sessions/Session086</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session086&amp;diff=103396"/>
				<updated>2011-02-04T21:44:07Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = &lt;br /&gt;
| summit_session_attendee_email2 = &lt;br /&gt;
| summit_session_attendee_username2 = &lt;br /&gt;
| summit_session_attendee_company2=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = [[Image:T._metrics.jpg]]&lt;br /&gt;
| summit_ws_logo = [[Image:WS._metrics.jpg]]&lt;br /&gt;
| summit_session_name = Creating a unified &amp;quot;finding&amp;quot;&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session086&lt;br /&gt;
| mailing_list =&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description=&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = &lt;br /&gt;
| related_project_url_1 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = &lt;br /&gt;
| related_project_url_2 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = &lt;br /&gt;
| related_project_url_3 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Dinis Cruz&lt;br /&gt;
| summit_session_leader_email1 = dinis.cruz@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = Dinis.cruz &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 =&lt;br /&gt;
| operational_leader_email1 =&lt;br /&gt;
| operational_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| meeting_notes = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session086&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session086&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session002&amp;diff=103395</id>
		<title>Summit 2011 Working Sessions/Session002</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session002&amp;diff=103395"/>
				<updated>2011-02-04T21:42:24Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = John Wilander&lt;br /&gt;
| summit_session_attendee_email1 = john.wilander@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = John.wilander&lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = Michael Coates&lt;br /&gt;
| summit_session_attendee_email2 = Michael.Coates@owasp.org&lt;br /&gt;
| summit_session_attendee_username2 = MichaelCoates&lt;br /&gt;
| summit_session_attendee_company2=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = Tony UcedaVelez&lt;br /&gt;
| summit_session_attendee_email3 = tonyuv@owasp.org&lt;br /&gt;
| summit_session_attendee_username3 = Tony UcedaVelez&lt;br /&gt;
| summit_session_attendee_company3= VerSprite&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = Stefano Di Paola&lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 =&lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = Isaac Dawson&lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 =&lt;br /&gt;
| summit_session_attendee_company5= Veracode&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = Chris Eng&lt;br /&gt;
| summit_session_attendee_email6 = ceng@veracode.com&lt;br /&gt;
| summit_session_attendee_username6= &lt;br /&gt;
| summit_session_attendee_company6= Veracode&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = Nishi Kumar&lt;br /&gt;
| summit_session_attendee_email7 = nishi.kumar@owasp.org&lt;br /&gt;
| summit_session_attendee_username7= &lt;br /&gt;
| summit_session_attendee_company7= FIS&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = Elke Roth-Mandutz&lt;br /&gt;
| summit_session_attendee_email8 = elke.roth-mandutz@ohm-hochschule.de&lt;br /&gt;
| summit_session_attendee_username8= &lt;br /&gt;
| summit_session_attendee_company8=GSO-University of Applied Science&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = Giorgio Fedon&lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9= gfedon&lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = Paolo Perego&lt;br /&gt;
| summit_session_attendee_email10 = thesp0nge@owasp.org&lt;br /&gt;
| summit_session_attendee_username10= thesp0nge&lt;br /&gt;
| summit_session_attendee_company10= Armoredcode.com&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = Eduardo Vela&lt;br /&gt;
| summit_session_attendee_email11 = evn@google.com&lt;br /&gt;
| summit_session_attendee_username11= EduardoVela&lt;br /&gt;
| summit_session_attendee_company11= Google&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email12 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username12= Abraham Kang&lt;br /&gt;
| summit_session_attendee_company12 =&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14= &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15= &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16= &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17= &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18= &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19= &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20= &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = [[Image:T._browser_security.jpg]]&lt;br /&gt;
| summit_ws_logo = [[Image:WS._browser_security.jpg]]&lt;br /&gt;
| summit_session_name = HTML5 Security&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session002&lt;br /&gt;
| mailing_list = https://groups.google.com/group/owasp-summit-browsersec&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = Browser Security Track - main page&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/Category:Summit_2011_Browser_Security_Track&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = Google Group for the Browser Security Track&lt;br /&gt;
| related_project_url_2 = https://groups.google.com/group/owasp-summit-browsersec&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = &lt;br /&gt;
| related_project_url_3 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= '''Handle autofocus in a unified and secure way'''.&amp;lt;noinclude&amp;gt; Make sure SOP applies for autofocus usage in frame/iframe'd websites. Re-discuss necessity for (future) attributes like this.&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = '''Discuss necessity and capability for the HTML5 form controls'''.&amp;lt;noinclude&amp;gt; Do we need a non-SOP formaction attribute and why? &amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = &amp;lt;noinclude&amp;gt;'''Goal I''':&amp;lt;/noinclude&amp;gt;  Initiate and create documentation and references for developers that address security issues. &amp;lt;noinclude&amp;gt;Html5sec.org is a start but impossible to continue or extend large scale without vendor help&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &amp;lt;noinclude&amp;gt;'''Goal II''':&amp;lt;/noinclude&amp;gt;Discuss and heavily restrict SVG capabilities - especially when deployed in CSS backgrounds and &amp;lt;img&amp;gt; tags. &amp;lt;noinclude&amp;gt;Mainly Opera and Mozilla are addressed here.&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =   '''Long Term Goal(s)''': Provide a working and easy to use as well as vendor supported HTML5 compliant filter software such as HTMLPurifier. &amp;lt;noinclude&amp;gt;Browser vendors should participate in creating security software and filters - not undermine them as we could experience in the last decade.&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = Tuesday, 09 February &amp;lt;br&amp;gt; Time: TBA&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = The working form will most probably be short presentations to frame the topic and then round table discussions. Depending on number of attendees we'll break into groups.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = &amp;lt;br&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
[[Image:Html5_mario_hackvertor.jpg‎‎]]&lt;br /&gt;
&lt;br /&gt;
===Co-chair Mario Heiderich===&lt;br /&gt;
Mario Heiderich works as a researcher for the Ruhr-University in Bochum, Germany and currently focuses on HTML5, SVG security and security implications of the ES5 specification draft. Mario invoked the [http://html5sec.org/ HTML5 security cheat-sheet] and maintains the [http://php-ids.org/ PHPIDS filter rules]. In his spare time he delivers trainings and security consultancy for larger German and international companies. He is also one of the co-authors of [http://www.amazon.com/Web-Application-Obfuscation-WAFs-Evasion-Filters-alert/dp/1597496049 Web Application Obfuscation: '-/WAFs..Evasion..Filters//alert(/Obfuscation/)-'] – a book on how an attacker would bypass different types of security controls including IDS/IPS.&lt;br /&gt;
&lt;br /&gt;
===Co-chair Gareth Heyes===&lt;br /&gt;
Gareth &amp;quot;Gaz&amp;quot; Heyes calls himself Chief Conspiracy theorist and is affiliated with Microsoft. He is the designer and developer behind [http://www.owasp.org/index.php/OWASP_JavaScript_Sandboxes#tab=JSReg JSReg] – a Javascript sandbox which converts code using regular expressions; [http://www.owasp.org/index.php/OWASP_JavaScript_Sandboxes#tab=HTMLReg HTMLReg] &amp;amp; [http://www.owasp.org/index.php/OWASP_JavaScript_Sandboxes#tab=CSSReg CSSReg] – converters of malicious HTML/CSS into a safe form of HTML. He is also one of the co-authors of [http://www.amazon.com/Web-Application-Obfuscation-WAFs-Evasion-Filters-alert/dp/1597496049 Web Application Obfuscation: '-/WAFs..Evasion..Filters//alert(/Obfuscation/)-'] – a book on how an attacker would bypass different types of security controls including IDS/IPS.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 =  Browser Security Report&lt;br /&gt;
|summit_session_deliverable_url_1 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = Browser Security Priority Report&lt;br /&gt;
|summit_session_deliverable_url_2 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = &lt;br /&gt;
|summit_session_deliverable_url_3 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = &lt;br /&gt;
|summit_session_deliverable_url_4 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
|summit_session_deliverable_url_5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
|summit_session_deliverable_url_6 =&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
|summit_session_deliverable_url_7 =&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
|summit_session_deliverable_url_8 =&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Mario Heiderich&lt;br /&gt;
| summit_session_leader_email1 = &lt;br /&gt;
| summit_session_leader_username1 =&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = Gareth Heyes&lt;br /&gt;
| summit_session_leader_email2 = gazheyes@gmail.com&lt;br /&gt;
| summit_session_leader_username2 = Gareth Heyes&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 =&lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 =&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = John Wilander&lt;br /&gt;
| operational_leader_email1 = john.wilander@owasp.org&lt;br /&gt;
| operational_leader_username1 = John.wilander&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| meeting_notes = &lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session002&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session002&lt;br /&gt;
}}&lt;br /&gt;
&amp;lt;/includeonly&amp;gt;&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=103296</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=103296"/>
				<updated>2011-02-03T22:10:41Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = &lt;br /&gt;
| summit_session_attendee_email2 = &lt;br /&gt;
| summit_session_attendee_username2 = &lt;br /&gt;
| summit_session_attendee_company2=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = OWASP Guide&lt;br /&gt;
| related_project_url_1 = http://www.owasp.org/index.php/OWASP_Guide_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = ESAPI&lt;br /&gt;
| related_project_url_2 = http://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API&lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = ASVS&lt;br /&gt;
| related_project_url_3 = http://www.owasp.org/index.php/Category:OWASP_Application_Security_Verification_Standard_Project&lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies.  OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List is existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	<entry>
		<id>https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=103294</id>
		<title>Summit 2011 Working Sessions/Session090</title>
		<link rel="alternate" type="text/html" href="https://wiki.owasp.org/index.php?title=Summit_2011_Working_Sessions/Session090&amp;diff=103294"/>
				<updated>2011-02-03T22:04:42Z</updated>
		
		<summary type="html">&lt;p&gt;Abraham Kang: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Template:&amp;lt;includeonly&amp;gt;{{{1}}}&amp;lt;/includeonly&amp;gt;&amp;lt;noinclude&amp;gt;Summit 2011 Working Sessions test tab&amp;lt;/noinclude&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name1 = Abraham Kang&lt;br /&gt;
| summit_session_attendee_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_attendee_username1 = &lt;br /&gt;
| summit_session_attendee_company1=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed1=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name2 = &lt;br /&gt;
| summit_session_attendee_email2 = &lt;br /&gt;
| summit_session_attendee_username2 = &lt;br /&gt;
| summit_session_attendee_company2=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed2=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name3 = &lt;br /&gt;
| summit_session_attendee_email3 = &lt;br /&gt;
| summit_session_attendee_username3 = &lt;br /&gt;
| summit_session_attendee_company3=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed3=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name4 = &lt;br /&gt;
| summit_session_attendee_email4 = &lt;br /&gt;
| summit_session_attendee_username4 = &lt;br /&gt;
| summit_session_attendee_company4=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed4=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name5 = &lt;br /&gt;
| summit_session_attendee_email5 = &lt;br /&gt;
| summit_session_attendee_username5 = &lt;br /&gt;
| summit_session_attendee_company5=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed5=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name6 = &lt;br /&gt;
| summit_session_attendee_email6 = &lt;br /&gt;
| summit_session_attendee_username6 = &lt;br /&gt;
| summit_session_attendee_company6=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed6=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name7 = &lt;br /&gt;
| summit_session_attendee_email7 = &lt;br /&gt;
| summit_session_attendee_username7 = &lt;br /&gt;
| summit_session_attendee_company7=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed7=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name8 = &lt;br /&gt;
| summit_session_attendee_email8 = &lt;br /&gt;
| summit_session_attendee_username8 = &lt;br /&gt;
| summit_session_attendee_company8=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed8=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name9 = &lt;br /&gt;
| summit_session_attendee_email9 = &lt;br /&gt;
| summit_session_attendee_username9 = &lt;br /&gt;
| summit_session_attendee_company9=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed9=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name10 = &lt;br /&gt;
| summit_session_attendee_email10 = &lt;br /&gt;
| summit_session_attendee_username10 = &lt;br /&gt;
| summit_session_attendee_company10=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed10=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name11 = &lt;br /&gt;
| summit_session_attendee_email11 = &lt;br /&gt;
| summit_session_attendee_username11 = &lt;br /&gt;
| summit_session_attendee_company11=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed11=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name12 = &lt;br /&gt;
| summit_session_attendee_email12 = &lt;br /&gt;
| summit_session_attendee_username12 = &lt;br /&gt;
| summit_session_attendee_company12=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed12=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name13 = &lt;br /&gt;
| summit_session_attendee_email13 = &lt;br /&gt;
| summit_session_attendee_username13 = &lt;br /&gt;
| summit_session_attendee_company13=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed13=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name14 = &lt;br /&gt;
| summit_session_attendee_email14 = &lt;br /&gt;
| summit_session_attendee_username14 = &lt;br /&gt;
| summit_session_attendee_company14=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed14= &lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name15 = &lt;br /&gt;
| summit_session_attendee_email15 = &lt;br /&gt;
| summit_session_attendee_username15 = &lt;br /&gt;
| summit_session_attendee_company15=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed15=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name16 = &lt;br /&gt;
| summit_session_attendee_email16 = &lt;br /&gt;
| summit_session_attendee_username16 = &lt;br /&gt;
| summit_session_attendee_company16=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed16=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name17 = &lt;br /&gt;
| summit_session_attendee_email17 = &lt;br /&gt;
| summit_session_attendee_username17 = &lt;br /&gt;
| summit_session_attendee_company17=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed17=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name18 = &lt;br /&gt;
| summit_session_attendee_email18 = &lt;br /&gt;
| summit_session_attendee_username18 = &lt;br /&gt;
| summit_session_attendee_company18=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed18=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name19 = &lt;br /&gt;
| summit_session_attendee_email19 = &lt;br /&gt;
| summit_session_attendee_username19 = &lt;br /&gt;
| summit_session_attendee_company19=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed19=&lt;br /&gt;
&lt;br /&gt;
| summit_session_attendee_name20 = &lt;br /&gt;
| summit_session_attendee_email20 = &lt;br /&gt;
| summit_session_attendee_username20 = &lt;br /&gt;
| summit_session_attendee_company20=&lt;br /&gt;
| summit_session_attendee_notes,_reason_for_participating_and_issues_to_be discussed20=&lt;br /&gt;
|-&lt;br /&gt;
| summit_track_logo = &lt;br /&gt;
| summit_ws_logo = &lt;br /&gt;
| summit_session_name = OWASP Licensing&lt;br /&gt;
| summit_session_url = http://www.owasp.org/index.php/Summit_2011_Working_Sessions/Session090&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| short_working_session_description= Go over the OWASP Licenses related to OWASP documentation to discuss if the is a way to make the license more amiable to corporate users thereby spurring sales of corporate sponsorships. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| related_project_name1 = &lt;br /&gt;
| related_project_url_1 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name2 = &lt;br /&gt;
| related_project_url_2 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name3 = &lt;br /&gt;
| related_project_url_3 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name4 = &lt;br /&gt;
| related_project_url_4 = &lt;br /&gt;
&lt;br /&gt;
| related_project_name5 = &lt;br /&gt;
| related_project_url_5 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name1= Discuss the goals of the OWASP Licensing model for OWASP documents and informative materials.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name2 = Understand better the corporate use cases for OWASP documentation and artifacts.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name3 = Identify possible options for licensing changes to spur growth in corporate sponsorships.&lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name4 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_objective_name5 =  &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_date_and_time = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| discussion_model = participants and attendees&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_resources = Projector, whiteboards, markers, Internet connectivity, power&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| working_session_additional_details = Corporations face similar problems related to educating, creating, and implementing security policies and measures. Corporations require update-to-date security material to supplement their policies.  OWASP has this information but licensing constraints may be limiting use of OWASP material in enterprises.&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name1 = Licensing Requirements for OWASP documentation&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name2 = List is existing Licenses used by OWASP projects.&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name3 = Problem corporations face with adopting and utilizing OWASP materials and code&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name4 = Recommendations for changes in the OWASP License&lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name5 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name6 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name7 = &lt;br /&gt;
&lt;br /&gt;
|summit_session_deliverable_name8 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name1 = Abraham Kang&lt;br /&gt;
| summit_session_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| summit_session_leader_username1 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name2 = &lt;br /&gt;
| summit_session_leader_email2 = &lt;br /&gt;
| summit_session_leader_username2 = &lt;br /&gt;
&lt;br /&gt;
| summit_session_leader_name3 = &lt;br /&gt;
| summit_session_leader_email3 = &lt;br /&gt;
| summit_session_leader_username3 = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
| operational_leader_name1 = Abraham Kang&lt;br /&gt;
| operational_leader_email1 = abraham.kang@owasp.org&lt;br /&gt;
| operational_leader_username1 = Abraham Kang&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
| meeting_notes = &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| session_name_mask = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Session090&lt;br /&gt;
| session_home_page = &amp;lt;!--Please replace DO NOT EDIT this string --&amp;gt; Summit_2011_Working_Sessions/Session090&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Abraham Kang</name></author>	</entry>

	</feed>