How to safeguard your site with HTML5 sandbox
Todays web applications are often integrating various resources to mesh up a new experience. This can be Twitter widgets to show up the latest tweets about a product, this can be Facebook comments to discuss a specific article or just plain integrated web pages through an IFRAME element. Latter is a concern and there is a new kid on the block to help you out here: The HTML5 Sandbox.
The IFRAME element is useful to integrate complete HTML web pages into your own web page. Using the IFRAME element is straightforward. You specify the tag and link to the source of content you want to embed into your webpage.
<iframe src="somecontent.html"></iframe>
A lot of today's websites are using the IFRAME tag to integrate content. This can be as simple as hosting advertisement as a seperate entity and as complex as providing an email service showing user generated HTML mails.
Whatever your scenario is to use the IFRAME element tag, you should know that there are some valid security concerns to host content this way.
A black box
Embedding content with an IFRAME is like hosting an party announced publicly on Facebook. You think you know who you have shared your annoucement, but you have no idea who reshared and who will come. The same is true for framing content. You know what you are referencing but you have no idea how the site will evolve in the future. This is true for the content of the page you include as well as for the functionality of the page.
Security concerns using iframe
The source of content is a regular HTML page with all options you have in HTML. The browser handles the page like any other regular web page. Forms can be used to retrieve user input, scripts can be executed, the page can navigate within the browser window, browser plugins can be executed - you have no control what the hosted content will do. At least there is one mechanism in place by default which prevents some kind of attacks. The cross-domain policy.
Rehosting content from another domain
If the hosted content is coming from another domain, cross-domain policy comes into play and is prohibiting the foreign content to access the parents document object model.
The embedded page is not able to read for instance cookies or the browser's local storage for the hosted domain. But there are still risks left. Such hosted content can still renavigate on the top level and display the same website like the user would expect, trying to phish some confidential information from the user. Or try to retrieve more information from the user through an form which is styled as it would belong to the same page. It is still a big risk.
Rehosting content from the same domain
The case for rehosting content from the same domain is even worse. No security restrictions are applied as the content is coming from the same domain. Embedded content is able to access the complete loaded browser DOM and manipulate everything. Also content on the same domain should be safe, but there is still the risk of user generated content if such will be rehosted in the IFRAME
A sandboxed approach
All those valid concerns haven't been addressed for a long time by standards body. Therefore Microsoft provided an proprietary implementation of IFRAME security in Internet Explorer 8. This implementation was due the fact of a missing W3C standard and showed the necessity of securing the host from framed content. Others picked up Internet Exporers implementation and discussed it as baseline for their browsers as well. Today's Internet Explorer implementation is based on the W3C IFrame Sandbox Attribute.
The sandbox attribute was introduced with HTML5. Its purpose is to secure the content host of the embedded resource. To apply it just add it as empty attribute to the IFRAME element.
<iframe sandbox src="http://somewebsite.com/default.html"></iframe>
That's it. The IFRAME sandboxed content is rehosted in the browser with the following restrictions:
- Plugins are disabled. Any kind of ActiveX, Flash or Silverlight Plugin will not be executed.
- Forms are disabled. The hosted content is not allowed to make form post back to any target.
- Scripts are disabled. JavaScript is disabled and will not execute.
- Links to other browsing contexts are disabled. An anchor tag targeting different browser levels will not execute.
- Unique origin treatment. All content is treated under an unique origin. The content is not able to traverse the DOM or read Cookie information.
This means that even content coming from the same domain is treated with the cross-domain policy, as each IFRAME content will be viewed as an unique origin.
Embedded content is just able to display information. No further actions can be done inside the iframe which could compromise the hosting website.
Checking for the sandbox attribute
We know that an IFRAME is an open gate. We know that the sandbox attribute is locking down security of hosted content. The decision is clear: Use IFRAME elements just with the sandbox attribute! You can check if the browser supports the IFRAME sandbox attribute with a simple check in JavaScript:
if( "sandbox" in document.createElement( "IFRAME" ) ) {
// render the iframe element...
} else {
// embed content through other ways,
// as the browser does not support the sandbox
}
If it is the case just use it. If not try to embed the content through other ways or just display the user a message that he should upgrade to a modern browser.
Customizing the Sandbox
There are cases where we need some level of customization on the restrictions. This is possible with the sandbox attribute. Several attribute values relax the standard sandbox policy.
allow-forms
If you want to enable forms post back within the IFRAME element you just specify the allow-forms value for the sandbox attribute.
<iframe sandbox="allow-forms" src="xyz.html"></iframe>
If this value is present the embedded page is allowed to post back using a form submit within the frame.
allow-scripts
JavaScript is a powerful language and is often used to have dynamic interactions on the client side without resending information to the server. But that power brings also risks when rehosting foreign web pages. Therefore, think if you really want to enable JavaScript in IFRAME scenarios. Especially when the content is from an unknown source.
Enabling JavaScript is done through the allow-scripts value.
<iframe sandbox="allow-scripts" src="xyz.html"></iframe>
allow-same-origin
Per default an IFRAME page from the same domain has the possibility to access the parent's document object model. With the sandbox attribute in place the page will be treated not under the same origin. This page has no access to the resources even when coming from the same domain.
To reenable same origin treatment in an sandboxed scenario you have to specify the allow-same-orign attribute.
<iframe sandbox="allow-same-origin" src="xyz.html"></iframe>
The value itself is not very helpful as you need some script capabilities to make use of it. Here is an example. If you want to access the local storage of the current domain like in this code snippet
function loadFromStorage( key ) {
if( localStorage ) {
return localStorage.getItem( key );
}
});
you also need the allow-scripts value.
<iframe sandbox="allow-scripts allow-same-origin" src="xyz.html"></iframe>
Now access works. But be warned:
Allowing scripts and same origin in the same sandbox enables the hosted content to manipulate the attributes of the sandbox and remove further restrictions.
allow-top-navigation
Per default anchor targeting other browsing contexts are ignored and not executed. This protects the website hosting the IFRAME content from being replaced by the hosted content. The following anchor link will not be executed in the default sandbox
<a href="xyz.html" target="_top">Click me</a>
as the target would replace the complete web page.
Therefore relaxing this policy is also just recommended if you trust the content you host.
<iframe sandbox="allow-top-navigation" src="xyz.html"></iframe>
ms-allow-popups
Sometimes it is useful to allow embedded content opening up new popup windows. A perfect example is using mapping services like Bing Maps. When embedding bing map some more functionality like directions or information about the place shown can be looked up in popup windows. Due to the fact that the sandbox is prohibiting this, there is a setting in Internet Explorer 10 which enables popups without compromising the sandbox. The following code shows how to setup ms-allow-popups.
<iframe sandbox="ms-allow-popups" src="xyz.html"></iframe>
When setting this value embedded sites are able to display information in a new window
<a href="xyz.html" target="_new">Show Info</a>
Putting it all together
You can combine several attribute values on one sandbox, for instance, you want to enable forms postback, top-level navigation and JavaScript, just specify
<iframe sandbox="allow-forms allow-top-navigation allow-scripts" src="xyz.html"></iframe>
It is also good to know that the sandbox behaves correctly when used in hierachical situations - several nested iframes with different sandbox attribute values. The top level sandbox always dominates down the hierachy. For instance if the top IFRAME sandbox is just allowing forms you can't extend the sandbox in an IFRAME below to allow scripts.
You can play around with the HTML sandbox in this demo.
You can also download a copy of this demo from Github to play around with it. To enable the form postback demo just open the project folder in WebMatrix and start the project from there.
Conclusion
The sandbox attribute is the key to use the IFRAME element in a secure way on your website. You should check if the browser is supporting the HTML sandbox and display the content just in that case. Start today, download a modern browser like Internet Explorer 10 Platform Preview 2 and get familiar with the sandbox by reading the IE developer guide. This is a step towards a more secure web and modern browsers are ready to sandbox embedded content.