This site is the archived OWASP Foundation Wiki and is no longer accepting Account Requests.
To view the new OWASP Foundation website, please visit https://owasp.org
Difference between revisions of "HTML5 Security Cheat Sheet"
From OWASP
Line 10: | Line 10: | ||
The following cheat sheet serves as a guide for implementing HTML 5 in a secure fashion. | The following cheat sheet serves as a guide for implementing HTML 5 in a secure fashion. | ||
− | = | + | = Communication APIs = |
− | + | == Web Messaging == | |
− | |||
− | |||
Web Messaging (also known as Cross Domain Messaging) provides a means of messaging between documents from different origins in a way that is generally safer than the multiple hacks used in the past to accomplish this task. However, there are still some recommendations to keep in mind: | Web Messaging (also known as Cross Domain Messaging) provides a means of messaging between documents from different origins in a way that is generally safer than the multiple hacks used in the past to accomplish this task. However, there are still some recommendations to keep in mind: | ||
Line 28: | Line 26: | ||
* If you need to embed external content/untrusted gadgets and allow user-controlled scripts (which is highly discouraged), consider using a JavaScript rewriting framework such as [http://code.google.com/p/google-caja/ Google Caja] or check the information on [[#Sandboxed frames|sandboxed frames]]. | * If you need to embed external content/untrusted gadgets and allow user-controlled scripts (which is highly discouraged), consider using a JavaScript rewriting framework such as [http://code.google.com/p/google-caja/ Google Caja] or check the information on [[#Sandboxed frames|sandboxed frames]]. | ||
− | + | == Cross Origin Resource Sharing == | |
* Validate URLs passed to <tt>XMLHttpRequest.open</tt>. Current browsers allow these URLs to be cross domain; this behavior can lead to code injection by a remote attacker. Pay extra attention to absolute URLs. | * Validate URLs passed to <tt>XMLHttpRequest.open</tt>. Current browsers allow these URLs to be cross domain; this behavior can lead to code injection by a remote attacker. Pay extra attention to absolute URLs. | ||
Line 38: | Line 36: | ||
* Don't rely only on the Origin header for Access Control checks. Browser always sends this header in CORS requests, but may be spoofed outside the browser. Application-level protocols should be used to protect sensitive data. | * Don't rely only on the Origin header for Access Control checks. Browser always sends this header in CORS requests, but may be spoofed outside the browser. Application-level protocols should be used to protect sensitive data. | ||
− | + | == WebSockets == | |
* Drop backward compatibility in implemented client/servers and use only protocol versions above hybi-00. Popular Hixie-76 version (hiby-00) and older are outdated and insecure. | * Drop backward compatibility in implemented client/servers and use only protocol versions above hybi-00. Popular Hixie-76 version (hiby-00) and older are outdated and insecure. | ||
Line 50: | Line 48: | ||
* As a WebSockets client in a 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 a WebSockets connection. | * As a WebSockets client in a 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 a WebSockets connection. | ||
− | + | == Server-Sent Events == | |
* Validate URLs passed to the <tt>EventSource</tt> constructor, even though only same-origin URLs are allowed. | * Validate URLs passed to the <tt>EventSource</tt> constructor, even though only same-origin URLs are allowed. | ||
Line 56: | Line 54: | ||
* Always check the origin attribute of the message (<tt>event.origin</tt>) to ensure the message is coming from a trusted domain. Use a whitelist approach. | * Always check the origin attribute of the message (<tt>event.origin</tt>) to ensure the message is coming from a trusted domain. Use a whitelist approach. | ||
− | + | = Storage APIs = | |
− | + | == Local Storage == | |
*Also known as 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. | *Also known as 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. | ||
Line 68: | Line 66: | ||
*There is no way to restrict the visibility of an object to a specific path like with the attribute path of HTTP Cookies, every object is shared within an origin and protected with the Same Origin Policy. Avoid host multiple applications on the same origin, all of them would share the same localStorage object, use different subdomains instead. | *There is no way to restrict the visibility of an object to a specific path like with the attribute path of HTTP Cookies, every object is shared within an origin and protected with the Same Origin Policy. Avoid host multiple applications on the same origin, all of them would share the same localStorage object, use different subdomains instead. | ||
− | + | == Client-side databases == | |
* On November 2010, the W3C announced Web SQL Database (relational SQL database) as a deprecated specification. A new standard Indexed Database API or IndexedDB (formerly WebSimpleDB) is actively developed, which provides key/value database storage and methods for performing advanced queries. | * On November 2010, the W3C announced Web SQL Database (relational SQL database) as a deprecated specification. A new standard Indexed Database API or IndexedDB (formerly WebSimpleDB) is actively developed, which provides key/value database storage and methods for performing advanced queries. | ||
Line 75: | Line 73: | ||
* Like Local Storage, a single [[Cross-site_Scripting_(XSS)|Cross Site Scripting]] can be used to load malicious data into a web database as well. Don't consider data in these to be trusted. | * Like Local Storage, a single [[Cross-site_Scripting_(XSS)|Cross Site Scripting]] can be used to load malicious data into a web database as well. Don't consider data in these to be trusted. | ||
− | + | = Geolocation = | |
* The Geolocation RFC recommends that the user agent ask the user's permission before calculating location. 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. 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>. | ||
− | + | = 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. | * 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. | ||
Line 85: | Line 83: | ||
* 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|DOM Based XSS]] vulnerability. | * 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|DOM Based XSS]] vulnerability. | ||
− | + | = Sandboxed frames = | |
* Use the <tt>sandbox</tt> attribute of an <tt>iframe</tt> for untrusted content. | * Use the <tt>sandbox</tt> attribute of an <tt>iframe</tt> for untrusted content. | ||
Line 100: | Line 98: | ||
* Apart from this attribute, to prevent Clickjacking attacks and unsolicited framing it is encouraged to use the header <tt>X-Frame-Options</tt> which supports the <tt>deny</tt> and <tt>same-origin</tt> values. Other solutions like framebusting <tt>if(window!== window.top) { window.top.location = location; }</tt> are not recommended. | * Apart from this attribute, to prevent Clickjacking attacks and unsolicited framing it is encouraged to use the header <tt>X-Frame-Options</tt> which supports the <tt>deny</tt> and <tt>same-origin</tt> values. Other solutions like framebusting <tt>if(window!== window.top) { window.top.location = location; }</tt> are not recommended. | ||
− | + | = Offline Applications = | |
* Whether the user agent requests permission to the user to store data for offline browsing and when this cache is deleted varies from one browser to the next. Cache poisoning is an issue if a user connects through insecure networks, so for privacy reasons it is encouraged to require user input before sending any <tt>manifest</tt> file. | * Whether the user agent requests permission to the user to store data for offline browsing and when this cache is deleted varies from one browser to the next. Cache poisoning is an issue if a user connects through insecure networks, so for privacy reasons it is encouraged to require user input before sending any <tt>manifest</tt> file. | ||
* Users should only cache trusted websites and clean the cache after browsing through open or insecure networks. | * Users should only cache trusted websites and clean the cache after browsing through open or insecure networks. | ||
− | + | = 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. | * 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. | ||
− | + | = HTTP Headers to enhance security = | |
− | + | == X-Frame-Options == | |
− | * This header can be used to prevent ClickJacking in modern browsers | + | * This header can be used to prevent ClickJacking in modern browsers. |
* Use the <tt>same-origin</tt> attribute to allow being framed from urls of the same origin or <tt>deny</tt> to block all. Example: <tt>X-Frame-Options: DENY</tt> | * Use the <tt>same-origin</tt> attribute to allow being framed from urls of the same origin or <tt>deny</tt> to block all. Example: <tt>X-Frame-Options: DENY</tt> | ||
+ | * For more information on Clickjacking Defense please see the [[Clickjacking Defense Cheat Sheet]]. | ||
− | + | == X-XSS-Protection == | |
* Enable XSS filter (only works for Reflected XSS). | * Enable XSS filter (only works for Reflected XSS). | ||
* Example: <tt>X-XSS-Protection: 1; mode=block</tt> | * Example: <tt>X-XSS-Protection: 1; mode=block</tt> | ||
− | + | == Strict Transport Security == | |
* Force every browser request to be sent over TLS/SSL (this can prevent SSL strip attacks). | * Force every browser request to be sent over TLS/SSL (this can prevent SSL strip attacks). | ||
Line 127: | Line 126: | ||
* Example: Strict-Transport-Security: max-age=8640000; includeSubDomains | * Example: Strict-Transport-Security: max-age=8640000; includeSubDomains | ||
− | + | == Content Security Policy == | |
* Policy to define a set of content restrictions for web resources which aims to mitigate web application vulnerabilities such as Cross Site Scripting. | * Policy to define a set of content restrictions for web resources which aims to mitigate web application vulnerabilities such as Cross Site Scripting. | ||
* Example: X-Content-Security-Policy: allow 'self'; img-src *; object-src media.example.com; script-src js.example.com | * Example: X-Content-Security-Policy: allow 'self'; img-src *; object-src media.example.com; script-src js.example.com | ||
− | + | == Origin == | |
* Sent by CORS/WebSockets requests. | * Sent by CORS/WebSockets requests. |
Revision as of 03:20, 8 April 2014
Last revision (mm/dd/yy): 04/8/2014 IntroductionThe following cheat sheet serves as a guide for implementing HTML 5 in a secure fashion. Communication APIsWeb MessagingWeb Messaging (also known as Cross Domain Messaging) provides a means of messaging between documents from different origins in a way that is generally safer than the multiple hacks used in the past to accomplish this task. However, there are still some recommendations to keep in mind:
Cross Origin Resource Sharing
WebSockets
Server-Sent Events
Storage APIsLocal Storage
Client-side databases
Geolocation
Web Workers
Sandboxed frames
It is possible to have a fine-grained control over iframe capabilities using the value of the sandbox attribute.
Offline Applications
Progressive Enhancements and Graceful Degradation Risks
HTTP Headers to enhance securityX-Frame-Options
X-XSS-Protection
Strict Transport Security
Content Security Policy
Origin
Authors and Primary Editors
|
Other Cheatsheets |