Skip to content
Business logic Disclosed report Advanced CVE-2022-34468

CSP sandbox header without `allow-scripts` can be bypassed via retargeted javascript: URI

A document sandboxed by a CSP header without allow-scripts could still execute script by clicking a javascript: link that targets a script-capable parent frame. The script then runs in the parent's origin, bypassing the sandbox restriction.

Armin Ebert Follow

Originally disclosed on Mozilla

  • Fixed in 24 days
  • Paid in 29 days
  • Disclosed after 24 days

Under a CSP sandbox header directive without allow-scripts, the document shouldn't be able to run scripts. However, this can be bypassed using a javascript: URI with a suitable (i.e. script-capable, same-origin) target document.

Proof of concept

parent.html:

HTML
<iframe src="child.html"></iframe>

child.html:

HTML
Content-Security-Policy: sandbox allow-same-origin allow-top-navigation

<noscript>no scripts allowed here</noscript>
<a href="javascript:alert(`origin=${origin} location=${location}`)" target="_parent">click me</a>

When the link is clicked, the script runs in the context of parent.html. As per allow-top-navigation, the child is allowed to navigate the parent, but not to execute scripts since its sandbox doesn't allow-scripts.

Details

Since the target (parent.html) is itself allowed to run scripts, the relevant security check here is the verification that the sandbox flags of the triggering context allow scripts, too:

nsJSThunk::EvaluateScript() at nsJSProtocolHandler.cpp:238

C++
  // Sandboxed document check: javascript: URI execution is disabled in a
  // sandboxed document unless 'allow-scripts' was specified.
  if ((targetDoc && !targetDoc->IsScriptEnabled()) ||
      (loadInfo->GetTriggeringSandboxFlags() & SANDBOXED_SCRIPTS)) {
  ...

In the example, loadInfo->GetTriggeringSandboxFlags() == 0, thus the script is allowed.

My understanding is that the triggering sandbox flags are taken from the triggering BC (not the document):

nsDocShell::OnLinkClickSync() at nsDocShell.cpp:13004

C++
  uint32_t triggeringSandboxFlags = 0;
  if (mBrowsingContext) {
    triggeringSandboxFlags = mBrowsingContext->GetSandboxFlags();
  }
  ...
  aLoadState->SetTriggeringSandboxFlags(triggeringSandboxFlags);
  ...
  nsresult rv = InternalLoad(aLoadState);

However, the CSP sandbox header didn't change the sandbox flags of the BC:

C++
(gdb) b nsDocShell.cpp:13100
Thread 1 "Isolated Web Co" hit Breakpoint 1, nsDocShell::OnLinkClickSync (this=0x7f514773d400, aContent=0x7f5142609030, aLoadState=0x7f516e69b480, aNoOpenerImplied=false, aTriggeringPrincipal=0x7f514284d880) at /m-c/docshell/base/nsDocShell.cpp:13100
13100     aLoadState->SetTriggeringSandboxFlags(triggeringSandboxFlags);
(gdb) p GetDocument()->GetSandboxFlags()
$1 = 1048555
(gdb) p mBrowsingContext->GetSandboxFlags()
$2 = (const uint32_t &) @0x7f514a905b64: 0
(gdb) p triggeringSandboxFlags
$3 = 0

Note that if the sandbox isn't set by CSP header, but <iframe sandbox="allow-same-origin allow-top-navigation"> instead, the issue doesn't occur. In that case, the sandbox flags on the document and BC are aligned, as expected:

C++
(gdb) b nsDocShell.cpp:13100
Thread 1 "Isolated Web Co" hit Breakpoint 1, nsDocShell::OnLinkClickSync (this=0x7f5147740000, aContent=0x7f5142609190, aLoadState=0x7f516e69b480, aNoOpenerImplied=false, aTriggeringPrincipal=0x7f514284d1a0) at /m-c/docshell/base/nsDocShell.cpp:13100
13100     aLoadState->SetTriggeringSandboxFlags(triggeringSandboxFlags);
(gdb) p GetDocument()->GetSandboxFlags()
$1 = 1048555
(gdb) p mBrowsingContext->GetSandboxFlags()
$2 = (const uint32_t &) @0x7f516e6d3164: 1048555
(gdb) p triggeringSandboxFlags
$3 = 1048555

(Here, the BC's sandbox flags were updated from nsFrameLoader::ApplySandboxFlags().)

The original report as published on Mozilla's Bugzilla, by Armin Ebert. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The browser checked the sandbox flags of the browsing context rather than the document when deciding whether to allow a javascript: URI, and the CSP sandbox header did not update the browsing context's flags.

Impact

An attacker could execute script in a more privileged context, bypassing the intended sandbox restriction, but only if the victim clicks a link and the target frame is script-capable and same-origin.

What the attacker needed

Victim clicks a link; target frame is script-capable; target frame is same-origin

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A sandbox is a browser security feature that restricts what a web page can do, such as running scripts. This report shows that a page sandboxed by a special HTTP header could still run script if it contained a link that, when clicked, made a script run in a different page that was not sandboxed.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by Armin Ebert

View all

18 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.