Firefox sandbox iframe can execute scripts without allow-scripts directive
A sandboxed iframe in Firefox could run script in the parent page's context when a user clicked a javascript: link, even though the sandbox attribute lacked allow-scripts. An attacker could use this to escape the sandbox protection and act as if the iframe were not sandboxed at all.
Originally disclosed on Mozilla
- Fixed in 9 days
- Paid in 14 days
- Disclosed after 9 days
When an iframe with the sandbox attribute set to allow-same-origin and allow-top-navigation/allow-top-navigation-by-user-activation (the below snippet), the embedded document can execute the script in the context of the parent document when users click on a javascript URL even if the sandbox attribute does not contain the allow-scripts directive.
'''
<iframe sandbox="allow-same-origin allow-top-navigation-by-user-activation" srcdoc="<a target='_top' href='javascript:alert(location)'>alert</a>">
'''
While I understand that it's logical to allow the embedded document to change the top.location to a javascript URL in this case, it would break the sandbox protection and make it no more secure than not using the sandbox attribute at all (e.g the embedded document can remove the sandbox attribute or reload itself in a new iframe).
It seems Chromium and Safari don't allow this behavior. I also think that is a more reasonable approach because normal users wouldn't expect an iframe without the allow-scripts directive to execute arbitrary scripts.
I noticed the behavior when trying the live demo of a markdown library (https://github.com/markedjs/marked).
The untrusted HTML is sandboxed inside an iframe with the sandbox attribute set to "allow-same-origin allow-top-navigation-by-user-activation": https://marked.js.org/demo/?text=[click me](javascript%3Aalert(location))
The application basically does the following:
'''
<iframe id="previewIframe" sandbox="allow-same-origin allow-top-navigation" srcdoc="<base target='_parent'>"></iframe>
<script>
var parsedMarkdown = "<a href=javascript:alert(location)>click me</a>";
previewIframe.addEventListener("load", function(){
previewIframe.contentDocument.body.innerHTML = parsedMarkdown;
});
</script>
'''
This issue might be related to https://bugzilla.mozilla.org/show_bug.cgi?id=785310
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
Likely the browser allowed a top-level navigation to a javascript: URL from a sandboxed frame that had allow-same-origin and top-navigation but not allow-scripts, without re-checking the script permission.
Impact
An attacker could run script in the parent page's context, defeating the sandbox and potentially removing the sandbox attribute or reloading the frame, though this affected Firefox only and required a user click.
What the attacker needed
Firefox browser; victim clicks a javascript: link; iframe sandbox lacks allow-scripts
How it was found
- Create an iframe with sandbox set to allow-same-origin and allow-top-navigation-by-user-activation, with srcdoc containing a link targeting _top with a javascript: URL.
- Load the page in Firefox and click the link.
- Observe that the script runs in the parent document's context despite allow-scripts being absent.
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 attribute on an iframe is meant to restrict what the embedded page can do. One restriction, allow-scripts, is supposed to stop any script from running. Here, clicking a special link that starts with javascript: still ran code, so the restriction was bypassed.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.