Other CSP rules ignored when specifying sandbox 'allow-scripts'
When a Content-Security-Policy header includes the sandbox directive with allow-scripts, Firefox ignores other script-blocking directives such as script-src 'none', allowing scripts to execute. An attacker could exploit this to run scripts that should have been blocked by the policy.
- Fixed in 11 days
- Paid in 17 days
- Disclosed after 11 days
Created attachment 8882535
firefox-csp-bug.zip
User Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Steps to reproduce:
I'm using a CSP rule (provided as a header, not meta tag) on a page or iframe, where the page contains a script, as follows:
Content-Security-Policy: sandbox allow-scripts; script-src 'none';
I tested this in Firefox 54 and the 55 beta on Linux 64 bit.
Actual results:
The script executes.
Expected results:
The script should not execute. If you remove the sandbox flag entirely from the CSP rule the script is blocked by the script-src 'none' rule.
If you swap the identical sandbox rule to the iframe (if using an iframe) as an attribute on the iframe HTML (rather than via CSP) then the script is correctly blocked. My expectation would be for the behaviour here to be the same regardless of how you specify the sandboxing.
The example may seem contradictory (to specify allow-scripts to only then block all scripts) but I actually noticed this when attempting to both sandbox and limit scripts to a particular domain (rather than block off all scripts) but noticed that any script was being allowed through whenever the sandbox rule was present via CSP.
The scope of this issue may actually be wider than only allowing through scripts.
I've attached a zip that should hopefully illustrate the basic issue (it includes a node/npm http server wrapper for convenience).
What the report shows
The target is Firefox's handling of the Content Security Policy sandbox directive. The researcher, Rhys, tested a page or iframe that delivers CSP as an HTTP header (not a meta tag) and contains a script. The policy under test was:
Content-Security-Policy: sandbox allow-scripts; script-src 'none';
The test environment was Firefox 54 and the 55 beta on 64-bit Linux. The user agent string recorded in the report was:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Steps, as described:
- Serve a page or iframe with the CSP header above.
- Include a script in the page.
- Load it in Firefox 54 and in the 55 beta on Linux 64-bit.
Actual result: the script executes. Expected result: the script should not execute, because script-src 'none' blocks all scripts.
The researcher established two controls that isolate the cause:
- Removing the
sandboxflag entirely from the CSP rule means the script is blocked byscript-src 'none'. - Moving the identical sandbox rule to the iframe's
sandboxHTML attribute (when an iframe is used) means the script is correctly blocked.
The expectation stated is that behaviour should be the same regardless of how sandboxing is specified. The example looks contradictory, allowing scripts in order to then block all of them, but the researcher explains the real motivation: sandboxing while limiting scripts to a particular domain rather than blocking all scripts. In that setup, any script was allowed through whenever the sandbox rule was present via CSP.
The report notes that the scope may be wider than allowing scripts through, but does not demonstrate anything beyond that. A zip file, firefox-csp-bug.zip, was attached, containing a node/npm HTTP server wrapper for convenience. The platform records the report as medium severity, in the Core: DOM: Security component, disclosed on 11 July 2017. The text gives no bounty amount and no fix details.
Root cause
The report points to the interaction between the CSP sandbox directive and the other directives in the same policy. When sandbox allow-scripts is present, the script-src 'none' restriction is not enforced, while the same sandbox value applied as an iframe attribute leaves script-src enforcement intact. The report does not identify the faulty function or the exact mechanism.
Impact
A site that applies a policy containing sandbox allow-scripts alongside a script restriction finds that the intended script restriction is not applied. The researcher's own scenario is a site that sandboxes a page and limits scripts to a particular domain: in that case any script origin was allowed through. The report hedges that the scope may be wider than scripts, but shows no other effect. Where a site relies on script-src to restrict scripts, the restriction is not applied to the sandboxed document.
How this kind of bug works
CSP is enforced by several cooperating checks. script-src decides which script sources may load and execute. The sandbox directive applies the same restrictions as the iframe sandbox attribute: it puts the document in an opaque origin, disables forms, plugins and so on, unless tokens such as allow-scripts or allow-same-origin are listed. Both are supposed to apply at once, with the most restrictive combination winning.
Bugs in this class arise when one enforcement layer is implemented in a way that skips, overwrites or short-circuits another. Typical causes are a sandbox flag being applied after the script checks have already been evaluated, a code path that treats the presence of a sandbox directive as a reason to relax other checks, or a mismatch between how header-delivered and attribute-delivered sandboxing is processed. The result is a policy that looks restrictive in configuration but is not enforced in the browser.
Defences are structural: each directive should be evaluated independently and combined with a logical AND, so that adding a directive can only ever restrict, never relax. Tests should cover every delivery mechanism (header, meta tag, iframe attribute) and every combination of sandbox tokens with source directives.
How to test for it
- Identify where CSP is delivered: response headers, meta tags, or iframe
sandboxattributes. Note every directive and token. - Build a minimal page that loads a script from a source the policy should block, and serve it with the exact policy under test.
- Compare delivery mechanisms: the same sandbox value as a header, as a meta tag, and as an iframe attribute. A difference in whether the script runs is the signal.
- Vary the combination:
sandbox allow-scriptswithscript-src 'none', with a host allowlist, and with nonces or hashes. Also try other sandbox tokens alongside restrictive directives. - Check whether the effect extends beyond scripts: inline event handlers,
javascript:URLs, form submission, and plugin or frame loading, since the report suspected a wider scope. - Confirm in more than one browser and version, and record the exact user agent.
A positive result is a script executing, or another restricted action succeeding, under a policy that should block it, with a matching control showing the block works when the sandbox directive is removed or moved to the attribute. Common false positives: the script was already cached or allowed by another directive; the policy was delivered by a mechanism the browser ignores; a nonce or hash matched; the test page was not actually covered by the policy; or an extension altered the response.
How it is fixed
The report gives no fix details. Regression tests should cover the combinations above across delivery mechanisms. Site-side, defence in depth helps: do not rely on a single directive, keep script-src strict with nonces or hashes, and avoid policies whose sandbox tokens and source directives pull in opposite directions.
Hunting notes
This class hides wherever policies are assembled dynamically: applications that build CSP headers from configuration, frameworks that merge a default policy with per-route overrides, and pages embedded in iframes with their own sandbox attributes. Recon signals include CSP headers with many directives, sandbox tokens combined with source restrictions, policies that differ between routes or between header and meta delivery, and applications that let users influence headers or embed content.
To get a report accepted, show a minimal reproduction with the exact policy, the exact browser and version, a control proving the restriction works without the sandbox directive, and a clear statement of what an attacker gains, for example script execution in a context the site believed was locked down. Keep claims to what you demonstrated; if you suspect wider scope, say so as a hypothesis rather than a finding.
What happened, in plain words
Websites can send instructions to your browser along with each page. One of these instruction sets is called Content Security Policy, or CSP. Think of it as a list of house rules that a website hands to the browser: which scripts may run, which images may load, and so on. The browser is supposed to follow those rules strictly.
One of the rules in that list is sandbox. A sandbox is like putting a page in a playpen: it can still do things, but it is cut off from the rest of the world. The sandbox rule can be loosened with small permission words called tokens. One token is allow-scripts, which says scripts are allowed to run inside the playpen.
Rhys, the researcher, set up a page (or an iframe, which is a page embedded inside another page) with this rule sent as an HTTP header, meaning it travels with the page response rather than sitting inside the page's HTML:
Content-Security-Policy: sandbox allow-scripts; script-src 'none';
Line by line, this says:
Content-Security-Policy:is the name of the header. It is how the website hands its rule list to the browser.sandbox allow-scriptsputs the page in a sandbox but permits scripts to run inside it.script-src 'none'says no script from any source may run at all.
The two parts seem to contradict each other, and that is the point. The researcher wanted to show that when both are present, the browser gets it wrong.
The test was run in Firefox 54 and the 55 beta on 64-bit Linux. The user agent string recorded in the report was:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
A user agent string is the short text a browser sends to identify itself. The steps were:
- Serve a page or iframe with the CSP header above.
- Include a script in the page.
- Load it in Firefox 54 and in the 55 beta on Linux 64-bit.
What actually happened: the script ran. What should have happened: the script should have been blocked, because script-src 'none' forbids every script.
To find the cause, the researcher ran two comparison tests, called controls. A control is a version of the test where you change one thing, so you can see what made the difference.
- Removing the
sandboxpart entirely meant the script was blocked byscript-src 'none'. So the blocking rule works on its own. - Moving the same sandbox setting onto the iframe as an HTML attribute, rather than through the CSP header, meant the script was correctly blocked. So the sandbox works when it is written the other way.
The expectation is that the result should be the same however the sandbox is specified. The example looks odd, allowing scripts only to then block all of them, but the researcher explains the real motivation: sandboxing a page while limiting scripts to one particular domain instead of blocking all scripts. In that setup, any script was let through whenever the sandbox rule was present via CSP.
The report says the problem might be wider than scripts, but does not show anything beyond that. A zip file, firefox-csp-bug.zip, was attached, containing a node/npm HTTP server wrapper for convenience. The platform records the report as medium severity, in the Core: DOM: Security component, disclosed on 11 July 2017. The text gives no bounty amount and no fix details.
In general, how this kind of bug works
CSP is enforced by several checks working together. script-src decides which script sources may load and run. The sandbox rule applies the same restrictions as the iframe sandbox attribute: it puts the page in an isolated origin, blocks forms and plugins, and so on, unless tokens such as allow-scripts or allow-same-origin are listed. Both are supposed to apply at once, and the stricter combination should win.
Bugs in this class appear when one layer of enforcement skips, overwrites or short-circuits another. Typical causes are a sandbox flag being applied after the script checks have already run, a code path that treats a sandbox rule as a reason to relax other checks, or a mismatch between how header-delivered and attribute-delivered sandboxing is processed. The result is a policy that looks strict on paper but is not enforced in the browser.
Defences are structural: each rule should be checked on its own and combined so that adding a rule can only ever restrict, never relax. Tests should cover every delivery mechanism (header, meta tag, iframe attribute) and every combination of sandbox tokens with source rules.
How to test for it
- Find where CSP is delivered: response headers, meta tags, or iframe
sandboxattributes. Note every rule and token. - Build a minimal page that loads a script from a source the policy should block, and serve it with the exact policy under test.
- Compare delivery mechanisms: the same sandbox value as a header, as a meta tag, and as an iframe attribute. A difference in whether the script runs is the signal.
- Vary the combination:
sandbox allow-scriptswithscript-src 'none', with a host allowlist, and with nonces or hashes. Also try other sandbox tokens alongside restrictive rules. - Check whether the effect goes beyond scripts: inline event handlers,
javascript:URLs, form submission, and plugin or frame loading, since the report suspected a wider scope. - Confirm in more than one browser and version, and record the exact user agent.
A positive result is a script running, or another restricted action succeeding, under a policy that should block it, with a matching control showing the block works when the sandbox rule is removed or moved to the attribute. Common false positives: the script was already cached or allowed by another rule; the policy was delivered by a mechanism the browser ignores; a nonce or hash matched; the test page was not actually covered by the policy; or an extension altered the response.
How it is fixed
The report gives no fix details. Regression tests should cover the combinations above across delivery mechanisms. On the site side, defence in depth helps: do not rely on a single rule, keep script-src strict with nonces or hashes, and avoid policies whose sandbox tokens and source rules pull in opposite directions.
Hunting notes
This class hides wherever policies are assembled dynamically: applications that build CSP headers from configuration, frameworks that merge a default policy with per-route overrides, and pages embedded in iframes with their own sandbox attributes. Recon signals include CSP headers with many rules, sandbox tokens combined with source restrictions, policies that differ between routes or between header and meta delivery, and applications that let users influence headers or embed content.
To get a report accepted, show a minimal reproduction with the exact policy, the exact browser and version, a control proving the restriction works without the sandbox rule, and a clear statement of what an attacker gains, for example script execution in a context the site believed was locked down. Keep claims to what you demonstrated; if you suspect wider scope, say so as a hypothesis rather than a finding.
Glossary
- CSP (Content Security Policy): a list of rules a website sends to the browser saying what the page may load and run.
- HTTP header: extra information sent alongside a web page response, separate from the page content itself.
- Meta tag: an HTML tag inside the page that can carry settings, an alternative to sending them as a header.
- Sandbox: an isolation setting that puts a page in a restricted playpen, cut off from the rest of the browser.
- Token: a small permission word added to a rule, such as
allow-scripts, that loosens it in a specific way. allow-scripts: a sandbox token that permits scripts to run inside the sandbox.script-src: the CSP rule that decides which script sources may load and run.'none': a value meaning nothing at all is allowed.- Script: a small program that runs inside a web page.
- Iframe: a web page embedded inside another web page.
- Attribute: a setting written directly on an HTML element, such as
sandboxon an iframe. - User agent string: the short text a browser sends to identify itself and its version.
- Control: a version of a test where one thing is changed, used to confirm what caused the result.
- Origin: the combination of scheme, host and port that identifies where content came from.
- Nonce: a one-time random value that lets a specific script through a CSP rule.
- Hash: a fingerprint of a script's content that lets that exact script through a CSP rule.
- False positive: a test result that looks like a bug but has an innocent explanation.
- Defence in depth: using several independent protections so that one failing does not leave everything open.
- Regression test: a test kept in place to make sure a fixed bug does not come back.