Difference between revisions of "HTML5 Security Cheat Sheet"

From OWASP
Jump to: navigation, search
(filled-out webworkers section)
(added some points)
Line 1: Line 1:
= DRAFT CHEAT SHEET - WORK IN PROGRESS =
+
= DRAFT CHEAT SHEET - WORK IN PROGRESS =
  
 
= Introduction  =
 
= Introduction  =
Line 11: Line 11:
 
== Cross Origin Resource Sharing  ==
 
== Cross Origin Resource Sharing  ==
  
* Validate URLs passed to <tt>XMLHttpRequest.open</tt>, current browsers allow these URLs to be cross domain.  
+
*Validate URLs passed to <tt>XMLHttpRequest.open</tt>, current browsers allow these URLs to be cross domain.  
* Ensure that URLs responding with <tt>Access-Control-Allow-Origin: *</tt> do not include any sensitive content or information that might aid attacker in further attacks. Use <tt>Access-Control-Allow-Origin</tt> header only on chosen URLs that need to be accessed cross-domain. Don't use the header for the whole domain.  
+
*Ensure that URLs responding with <tt>Access-Control-Allow-Origin: *</tt> do not include any sensitive content or information that might aid attacker in further attacks. Use <tt>Access-Control-Allow-Origin</tt> header only on chosen URLs that need to be accessed cross-domain. Don't use the header for the whole domain.  
* Take special care when using <tt>Access-Control-Allow-Credentials: true</tt> response header. Whitelist the allowed Origins and never echo back the <tt>Origin</tt> request header in <tt>Access-Control-Allow-Origin</tt>.
+
*Take special care when using <tt>Access-Control-Allow-Credentials: true</tt> response header. Whitelist the allowed Origins and never echo back the <tt>Origin</tt> request header in <tt>Access-Control-Allow-Origin</tt>.  
* Allow only selected, trusted domains in <tt>Access-Control-Allow-Origin</tt> header. Prefer whitelisting domains over blacklisting or allowing any domain (either through <tt>*</tt> wildcard or echoing the <tt>Origin</tt> header content).
+
*Allow only selected, trusted domains in <tt>Access-Control-Allow-Origin</tt> header. Prefer whitelisting domains over blacklisting or allowing any domain (either through <tt>*</tt> wildcard or echoing the <tt>Origin</tt> header content).  
* Keep in mind that CORS does not prevent the requested data from going to an un-authenticated location - it's still important for the server to perform usual [[Cross-Site Request Forgery (CSRF)|CSRF]] prevention.
+
*Keep in mind that CORS does not prevent the requested data from going to an un-authenticated location - it's still important for the server to perform usual [[Cross-Site Request Forgery (CSRF)|CSRF]] prevention.  
* While the RFC recommends a pre-flight request with the <tt>OPTIONS</tt> verb, current implementations might not perform this request, so it's important that &quot;ordinary&quot; (<tt>GET</tt> and <tt>POST</tt>) requests perform any access control necessary.
+
*While the RFC recommends a pre-flight request with the <tt>OPTIONS</tt> verb, current implementations might not perform this request, so it's important that "ordinary" (<tt>GET</tt> and <tt>POST</tt>) requests perform any access control necessary.
  
 
== Local Storage (a.k.a. Offline Storage, Web Storage)  ==
 
== Local Storage (a.k.a. Offline Storage, Web Storage)  ==
* Underlying storage mechanism may vary from one user agent to the next. In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored. Therefore, it's recommended not to store any sensitive information in local storage.
+
 
* Pay extra attention to “localStorage.getItem” and “setItem” calls implemented in HTML5 page. It helps in detecting sensitive information storage and their usage at run-time.
+
*Underlying storage mechanism may vary from one user agent to the next. In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored. Therefore, it's recommended not to store any sensitive information in local storage.  
 +
*Pay extra attention to “localStorage.getItem” and “setItem” calls implemented in HTML5 page. It helps in detecting sensitive information storage and their usage at run-time.
  
 
== WebDatabase  ==
 
== WebDatabase  ==
* Underlying storage mechanism may vary from one user agent to the next.  In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored.  Therefore, it's recommended not to store any sensitive information in local storage.
 
* If utilized, WebDatabase content on client side can be vulnerable to SQLInjection and needs to have proper validation and parametrization.
 
  
== Web Workers ==
+
*Underlying storage mechanism may vary from one user agent to the next. In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored. Therefore, it's recommended not to store any sensitive information in local storage.
* Web Workers are allowed to use <tt>XMLHttpRequest</tt> object to perform in-domain and Cross Origin Resource Sharing requests. See relevant section of this Cheat Sheet to ensure CORS security.
+
*If utilized, WebDatabase content on client side can be vulnerable to SQLInjection and needs to have proper validation and parametrization.
* While Web Workers don't have access to DOM of the calling page, malicious Web Workers can use excessive CPU for computation, leading to Denial of Service condition or abuse Cross Origin Resource Sharing for further exploitation. Ensure code in all Web Workers scripts is not malevolent. Don't allow creating Web Worker scripts from user supplied input.
+
 
* Validate messages exchanged with a Web Worker. Do not try to exchange snippets of Javascript for evaluation e.g. via eval()
+
== Web Workers ==
 +
 
 +
*Web Workers are allowed to use <tt>XMLHttpRequest</tt> object to perform in-domain and Cross Origin Resource Sharing requests. See relevant section of this Cheat Sheet to ensure CORS security.  
 +
*While Web Workers don't have access to DOM of the calling page, malicious Web Workers can use excessive CPU for computation, leading to Denial of Service condition or abuse Cross Origin Resource Sharing for further exploitation. Ensure code in all Web Workers scripts is not malevolent. Don't allow creating Web Worker scripts from user supplied input.  
 +
*Validate messages exchanged with a Web Worker. Do not try to exchange snippets of Javascript for evaluation e.g. via eval() as that could introduce a&nbsp;[[DOM_Based_XSS|DOM Based XSS]]&nbsp;vulnerability.
  
 
== WebSockets  ==
 
== WebSockets  ==
  
* Drop backward compatibility in implemented client/servers and use only protocol versions above hybi-00. Popular Hixie-76 version and olders are outdated and insecure.  
+
*Drop backward compatibility in implemented client/servers and use only protocol versions above hybi-00. Popular Hixie-76 version and olders are outdated and insecure.  
* While it is relatively easy to tunnel TCP services through WebSockets (e.g. VNC, FTP), doing so enables access to these tunneled services for the in-browser attacker in case of a Cross-Site-Scripting attack. These services might also be called directly from a malicious page or program.  
+
*While it is relatively easy to tunnel TCP services through WebSockets (e.g. VNC, FTP), doing so enables access to these tunneled services for the in-browser attacker in case of a Cross-Site-Scripting attack. These services might also be called directly from a malicious page or program.  
* The protocol doesn't handle authorization and/or authentication. Application-level protocols should handle that separately in case sensitive data is being transferred.  
+
*The protocol doesn't handle authorization and/or authentication. Application-level protocols should handle that separately in case sensitive data is being transferred.  
* Endpoints exposed through <tt>ws://</tt> protocol are easily reversible to plaintext. Only <tt>wss://</tt> (WebSockets over SSH) should be used for protection against Man-In-The-Middle attacks  
+
*Endpoints exposed through <tt>ws://</tt> protocol are easily reversible to plaintext. Only <tt>wss://</tt> (WebSockets over SSH) should be used for protection against Man-In-The-Middle attacks  
* Spoofing the client is possible outside browser, so WebSockets server should be able to handle incorrect/malicious input. Always validate input coming from the remote site, as it might have been altered.  
+
*Spoofing the client is possible outside browser, so WebSockets server should be able to handle incorrect/malicious input. Always validate input coming from the remote site, as it might have been altered.  
* When implementing servers, check the <tt>Origin:</tt> header in Websockets handshake. Though it might be spoofed outside browser, browsers always add the Origin of the page which initiated Websockets connection.  
+
*When implementing servers, check the <tt>Origin:</tt> header in Websockets handshake. Though it might be spoofed outside browser, browsers always add the Origin of the page which initiated Websockets connection.  
* As WebSockets client in browser is accessible through Javascript calls, all Websockets communication can be spoofed or hijacked through [[Cross Site Scripting Flaw|Cross-Site-Scripting]]. Always validate data coming through WebSockets connection.
+
*As WebSockets client in browser is accessible through Javascript calls, all Websockets communication can be spoofed or hijacked through [[Cross Site Scripting Flaw|Cross-Site-Scripting]]. Always validate data coming through WebSockets connection.
  
== Geolocation ==
+
== Geolocation ==
* The Geolocation RFC recommends that the user agent ask the user's permission before calculating location, but whether or how this decision is remembered varies from browser to browser. Some user agents require the user to visit the page again in order to turn off the ability to get the user's location without asking, so for privacy reasons, it's recommended to require user input before calling <tt>getCurrentPosition</tt> or <tt>watchPosition</tt>.
+
 
 +
*The Geolocation RFC recommends that the user agent ask the user's permission before calculating location, but whether or how this decision is remembered varies from browser to browser. Some user agents require the user to visit the page again in order to turn off the ability to get the user's location without asking, so for privacy reasons, it's recommended to require user input before calling <tt>getCurrentPosition</tt> or <tt>watchPosition</tt>.
  
 
== Use the <tt>sandbox</tt> attribute for untrusted content (iFrame)  ==
 
== Use the <tt>sandbox</tt> attribute for untrusted content (iFrame)  ==
Line 48: Line 52:
 
[[http://blog.whatwg.org/whats-next-in-html-episode-2-sandbox]]  
 
[[http://blog.whatwg.org/whats-next-in-html-episode-2-sandbox]]  
  
== Web Messaging ==
+
== Web Messaging ==
Web Messaging provides a means of messaging between documents from different origins in a way which is generally safer than JSON-P, however, there are still some recommendations to keep in mind:
+
 
* When posting a message, explicitly state the expected origin as the second argument to <tt>postMessage</tt> rather than <tt>*</tt> in order to prevent sending the message to an unknown origin after a redirect or some other means of the target window's origin changing.
+
Web Messaging provides a means of messaging between documents from different origins in a way which is generally safer than JSON-P, however, there are still some recommendations to keep in mind:  
* The receiving page should '''always''':
+
 
** Check the <tt>origin</tt> attribute of the sender to verify the data is originating from the expected location, and
+
*When posting a message, explicitly state the expected origin as the second argument to <tt>postMessage</tt> rather than <tt>*</tt> in order to prevent sending the message to an unknown origin after a redirect or some other means of the target window's origin changing.  
** Perform input validation on the <tt>data</tt> attribute of the event to ensure it's in the desired format
+
*The receiving page should '''always''':  
 +
**Check the <tt>origin</tt> attribute of the sender to verify the data is originating from the expected location, and  
 +
**Perform input validation on the <tt>data</tt> attribute of the event to ensure it's in the desired format.
 +
**Don't assume you have control over data attribute. Single&nbsp;[[Cross-Site_Scripting|Cross Site Scripting]] flaw in sending page allows attacker to send messages of any given format.<br>
 +
*Both pages should only interpret the exchanged messages as '''data'''. Never evaluate passed messages as code (e.g. via eval() )&nbsp;or insert it to a page DOM (e.g. via&nbsp;innerHTML) as that would create a DOM based XSS vulnerability. For more information see [[DOM_based_XSS_Prevention_Cheat_Sheet|DOM based XSS Prevention Cheat Sheet]].<span class="Apple-tab-span" style="white-space:pre" />
  
== XHR and DOM abuses ==
+
== XHR and DOM abuses ==
  
* XHR calls allow dynamic loading of streams into existing DOM under HTML5 framework, it can open up vulnerabilities like DOM based XSS and DOM driven open redirect and forwards. It is imperative to secure calls and streams. For more information see [[DOM based XSS Prevention Cheat Sheet]].
+
*XHR calls allow dynamic loading of streams into existing DOM under HTML5 framework, it can open up vulnerabilities like DOM based XSS and DOM driven open redirect and forwards. It is imperative to secure calls and streams. For more information see [[DOM based XSS Prevention Cheat Sheet]].
  
== HTML5 Widgets ==
+
== HTML5 Widgets ==
  
* If the application allows loading of widgets within the HTML5 page structure, then it is important to make sure that they are not sharing the same DOM. Each widget should be running in its own sandbox/DOM running in separate iframe.
+
*If the application allows loading of widgets within the HTML5 page structure, then it is important to make sure that they are not sharing the same DOM. Each widget should be running in its own sandbox/DOM running in separate iframe.
  
 
== Progressive Enhancements and Graceful Degradation Risks  ==
 
== Progressive Enhancements and Graceful Degradation Risks  ==
  
* The best practice now is to determine the capabilities that a browser supports and augment with some type of substitute for capabilities that are not directly supported. This may mean an onion-like element, e.g. falling through to a Flash Player if the &lt;video&gt; tag is unsupported, or it may mean additional scripting code from various sources that should be code reviewed.  
+
*The best practice now is to determine the capabilities that a browser supports and augment with some type of substitute for capabilities that are not directly supported. This may mean an onion-like element, e.g. falling through to a Flash Player if the &lt;video&gt; tag is unsupported, or it may mean additional scripting code from various sources that should be code reviewed.
  
 
= Related Cheat Sheets  =
 
= Related Cheat Sheets  =
Line 73: Line 81:
 
= Authors and Primary Editors  =
 
= Authors and Primary Editors  =
  
Mark Roxbury - mark.roxberry [at] owasp.org
+
Mark Roxbury - mark.roxberry [at] owasp.org  
  
Krzysztof Kotowicz - krzysztof [at] kotowicz.net
+
Krzysztof Kotowicz - krzysztof [at] kotowicz.net  
  
Will Stranathan - will [at] cltnc.us
+
Will Stranathan - will [at] cltnc.us  
  
Shreeraj Shah - shreeraj.shah [at] blueinfy.net
+
Shreeraj Shah - shreeraj.shah [at] blueinfy.net  
  
 
[[Category:How_To]] [[Category:Cheatsheets]]
 
[[Category:How_To]] [[Category:Cheatsheets]]

Revision as of 04:31, 15 October 2011

DRAFT CHEAT SHEET - WORK IN PROGRESS

Introduction

HTML 5

Browser Securability Chart

There are a few sites charting browser capabilities as they relate to the HTML 5 / CSS 3 standard. I have not seen any that mention security. There may not be a need for it, but the sandbox attribute of iframe's will be ignored in browsers except those HTML 5 compliant browsers which support it. If there are differences in implementations, there will be differences in security configuration / settings.

Cross Origin Resource Sharing

  • Validate URLs passed to XMLHttpRequest.open, current browsers allow these URLs to be cross domain.
  • Ensure that URLs responding with Access-Control-Allow-Origin: * do not include any sensitive content or information that might aid attacker in further attacks. Use Access-Control-Allow-Origin header only on chosen URLs that need to be accessed cross-domain. Don't use the header for the whole domain.
  • Take special care when using Access-Control-Allow-Credentials: true response header. Whitelist the allowed Origins and never echo back the Origin request header in Access-Control-Allow-Origin.
  • Allow only selected, trusted domains in Access-Control-Allow-Origin header. Prefer whitelisting domains over blacklisting or allowing any domain (either through * wildcard or echoing the Origin header content).
  • Keep in mind that CORS does not prevent the requested data from going to an un-authenticated location - it's still important for the server to perform usual CSRF prevention.
  • While the RFC recommends a pre-flight request with the OPTIONS verb, current implementations might not perform this request, so it's important that "ordinary" (GET and POST) requests perform any access control necessary.

Local Storage (a.k.a. Offline Storage, Web Storage)

  • Underlying storage mechanism may vary from one user agent to the next. In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored. Therefore, it's recommended not to store any sensitive information in local storage.
  • Pay extra attention to “localStorage.getItem” and “setItem” calls implemented in HTML5 page. It helps in detecting sensitive information storage and their usage at run-time.

WebDatabase

  • Underlying storage mechanism may vary from one user agent to the next. In other words, any authentication your application requires can be bypassed by a user with local privileges to the machine on which the data is stored. Therefore, it's recommended not to store any sensitive information in local storage.
  • If utilized, WebDatabase content on client side can be vulnerable to SQLInjection and needs to have proper validation and parametrization.

Web Workers

  • Web Workers are allowed to use XMLHttpRequest object to perform in-domain and Cross Origin Resource Sharing requests. See relevant section of this Cheat Sheet to ensure CORS security.
  • While Web Workers don't have access to DOM of the calling page, malicious Web Workers can use excessive CPU for computation, leading to Denial of Service condition or abuse Cross Origin Resource Sharing for further exploitation. Ensure code in all Web Workers scripts is not malevolent. Don't allow creating Web Worker scripts from user supplied input.
  • Validate messages exchanged with a Web Worker. Do not try to exchange snippets of Javascript for evaluation e.g. via eval() as that could introduce a DOM Based XSS vulnerability.

WebSockets

  • Drop backward compatibility in implemented client/servers and use only protocol versions above hybi-00. Popular Hixie-76 version and olders are outdated and insecure.
  • While it is relatively easy to tunnel TCP services through WebSockets (e.g. VNC, FTP), doing so enables access to these tunneled services for the in-browser attacker in case of a Cross-Site-Scripting attack. These services might also be called directly from a malicious page or program.
  • The protocol doesn't handle authorization and/or authentication. Application-level protocols should handle that separately in case sensitive data is being transferred.
  • Endpoints exposed through ws:// protocol are easily reversible to plaintext. Only wss:// (WebSockets over SSH) should be used for protection against Man-In-The-Middle attacks
  • Spoofing the client is possible outside browser, so WebSockets server should be able to handle incorrect/malicious input. Always validate input coming from the remote site, as it might have been altered.
  • When implementing servers, check the Origin: header in Websockets handshake. Though it might be spoofed outside browser, browsers always add the Origin of the page which initiated Websockets connection.
  • As WebSockets client in browser is accessible through Javascript calls, all Websockets communication can be spoofed or hijacked through Cross-Site-Scripting. Always validate data coming through WebSockets connection.

Geolocation

  • The Geolocation RFC recommends that the user agent ask the user's permission before calculating location, but whether or how this decision is remembered varies from browser to browser. Some user agents require the user to visit the page again in order to turn off the ability to get the user's location without asking, so for privacy reasons, it's recommended to require user input before calling getCurrentPosition or watchPosition.

Use the sandbox attribute for untrusted content (iFrame)

[[1]]

Web Messaging

Web Messaging provides a means of messaging between documents from different origins in a way which is generally safer than JSON-P, however, there are still some recommendations to keep in mind:

  • When posting a message, explicitly state the expected origin as the second argument to postMessage rather than * in order to prevent sending the message to an unknown origin after a redirect or some other means of the target window's origin changing.
  • The receiving page should always:
    • Check the origin attribute of the sender to verify the data is originating from the expected location, and
    • Perform input validation on the data attribute of the event to ensure it's in the desired format.
    • Don't assume you have control over data attribute. Single Cross Site Scripting flaw in sending page allows attacker to send messages of any given format.
  • Both pages should only interpret the exchanged messages as data. Never evaluate passed messages as code (e.g. via eval() ) or insert it to a page DOM (e.g. via innerHTML) as that would create a DOM based XSS vulnerability. For more information see DOM based XSS Prevention Cheat Sheet.<span class="Apple-tab-span" style="white-space:pre" />

XHR and DOM abuses

  • XHR calls allow dynamic loading of streams into existing DOM under HTML5 framework, it can open up vulnerabilities like DOM based XSS and DOM driven open redirect and forwards. It is imperative to secure calls and streams. For more information see DOM based XSS Prevention Cheat Sheet.

HTML5 Widgets

  • If the application allows loading of widgets within the HTML5 page structure, then it is important to make sure that they are not sharing the same DOM. Each widget should be running in its own sandbox/DOM running in separate iframe.

Progressive Enhancements and Graceful Degradation Risks

  • The best practice now is to determine the capabilities that a browser supports and augment with some type of substitute for capabilities that are not directly supported. This may mean an onion-like element, e.g. falling through to a Flash Player if the <video> tag is unsupported, or it may mean additional scripting code from various sources that should be code reviewed.

Related Cheat Sheets

OWASP Cheat Sheets Project Homepage

Developer Cheat Sheets (Builder)

Assessment Cheat Sheets (Breaker)

Mobile Cheat Sheets

OpSec Cheat Sheets (Defender)

Draft Cheat Sheets

Authors and Primary Editors

Mark Roxbury - mark.roxberry [at] owasp.org

Krzysztof Kotowicz - krzysztof [at] kotowicz.net

Will Stranathan - will [at] cltnc.us

Shreeraj Shah - shreeraj.shah [at] blueinfy.net