Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have CSP only apply to the parent frame, not any iframes?

If I have a webpage with CSP set to:

default-src 'self'; img-src *

Or similar, and I have an iframe like such:

some legal content
<iframe sandbox="allow-scripts" srcdoc="&lt;script>alert('arbitrary code')&lt;/script>"></iframe>

Is it possible to allow the code in the iframe to disobey the parent frame's CSP and allow inline scripts/styles, content from other domains, or any other arbitrary HTML thing that doesn't violate the sandbox restrictions?

Currently this will give:

[Error] Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback. (about:srcdoc, line 1)

The CSP spec confirms this is correct behavior:

Whenever a user agent creates an iframe srcdoc document in a browsing context nested in the protected resource, if the user agent is enforcing any policies for the protected resource, the user agent MUST enforce those policies on the iframe srcdoc document as well.

like image 609
bjb568 Avatar asked May 24 '15 21:05

bjb568


People also ask

Does CSP apply iframe?

The HTTP Content-Security-Policy (CSP) frame-src directive specifies valid sources for nested browsing contexts loading using elements such as <frame> and <iframe> .

What is iframe Srcdoc?

The srcdoc attribute specifies the HTML content of the page to show in the inline frame. Tip: This attribute is expected to be used together with the sandbox and seamless attributes. If a browser supports the srcdoc attribute, it will override the content specified in the src attribute (if present).

What is a frame ancestor?

The frame-ancestors directive allows you to specify which parent URLs can frame the current resource. Using the frame-ancestors CSP directive we can block or allow a page from being placed within a frame or iframe.

How do I use iframe tags?

Definition and UsageThe <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document. Tip: Use CSS to style the <iframe> (see example below). Tip: It is a good practice to always include a title attribute for the <iframe> .


1 Answers

It is not possible.

The are only two ways to accomplish what you're after:

  1. Alter the CSP rules of the parent page to whitelist your arbitrary code (I would suggest using a CSP nonce or hash for your arbitrary content rather than unsafe-inline).
  2. Point your iframe to an external (sub)domain with rules you can control and whitelist it with frame-src & child-src (see point 1).
like image 69
anthonyryan1 Avatar answered Sep 29 '22 00:09

anthonyryan1