Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent an iframe with srcdoc and defined csp from inheriting the parent page's CSP policies #700

Open
JuanRojasC opened this issue Dec 7, 2024 · 1 comment
Labels
wontfix This proposal or request will not be implemented

Comments

@JuanRojasC
Copy link

I am using an iframe to load dynamic content through the srcdoc attribute. This iframe is configured with its own CSP policy using the csp attribute, as shown in the following code:

<iframe
    csp="default-src * data: 'unsafe-inline'; script-src * 'unsafe-inline'"
    sandbox="allow-scripts"
    srcdoc="...content..."
></iframe>

The iframe content includes dynamically provided scripts, styles, and other resources. While the iframe is correctly loading the CSP configuration specified in the csp attribute, it also inherits and applies the CSP policies from the parent page. The parent page's CSP is much stricter, and because of this, it overrides the iframe's custom CSP configuration. As a result, the iframe's content is restricted, which prevents it from functioning as intended.

How can I ensure that the iframe respects only the CSP defined in its csp attribute, without inheriting or applying the parent page's CSP?

@arturjanc
Copy link

This unfortunately isn't possible - if a srcdoc iframe allowed relaxing the page's CSP then any HTML injection could add srcdoc iframes with arbitrary data/scripts and bypass the protections offered by the parent document's policy.

As a workaround, you could load a shim empty iframe from the network, postMessage to it and document.write() the data into the frame. This will let you set an arbitrary CSP (or no policy at all) in the HTTP response returning the iframe.

@ciaramcmullin ciaramcmullin added the wontfix This proposal or request will not be implemented label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This proposal or request will not be implemented
Projects
None yet
Development

No branches or pull requests

3 participants