Search code examples
javascripthtmlcssiframesandbox

Disallow Network Connections in IFrame


I have a frame that looks like this: <iframe srcdoc="*insert HTML here*"></iframe>.

The frame may have some javascript in it and that's okay.

How could I prevent the contents of that frame from connecting to the network?

This includes: - Javascript's HTTP requests and WebSocket connections etc - Remote resources referenced in CSS - External files in the HTML code

Is there some kind of sandbox rule to disable remote connections or do I have to regex all of that out? If so, what should I watch out for when applying the regex?


Solution

  • There is currently no reliable way of accomplishing this.

    The sandbox attribute cannot apply the type of restriction you are trying to apply here. A Content-Security-Policy can (with some difficulty), but there is currently no way of reliably applying such a policy to an <iframe> that has its contents set by the srcdoc attribute, as there is no way of simulating HTTP headers for such a document. Indeed, an iframe with srcdoc is simply treated as part of the page which embeds it, and inherits any Content-Security-Policy from that page!

    The W3C draft specification "Content Security Policy: Embedded Enforcement" has proposed a csp attribute. In the future, this might be usable to apply restrictions to such a document.

    In the meantime, however, you will probably need to serve this content through a sandbox domain, or rethink your design.