CSP strict-dynamic bypass via shimmed Facebook SDK + DOM Clobbering
Firefox's shim for the Facebook SDK uses document.currentScript.src to load a script, but DOM clobbering can make currentScript point to an attacker-controlled element, causing the shim to load an attacker-supplied URL. This allows bypassing a strict-dynamic CSP and executing arbitrary JavaScript when the page calls FB.login().
Originally disclosed on Mozilla
- Fixed in 4 days
- Paid in 29 days
- Disclosed after 4 days
Created attachment 9414144
fx_csp_bypass_strict-dynamic_fb.html
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Steps to reproduce:
Summary:
When Firefox's Strict Enhanced Tracking Protection is enabled, certain tracking contents are shimmed. Here, Facebook JS SDK shimmed by Firefox can be used as script gadgets to bypass Content Security Policy `strict-dynamic`. It allows an attacker to execute arbitrary JavaScript even if the page has strict CSP rules.
Note: In order to use this bypass, the page vulnerable to XSS must have the Facebook SDK loaded and additonally the existing legitimate code has to call `FB.login()` API. Otherwise, the vulnerable code will not be reached.
Steps to reproduce:
1. Open https://vulnerabledoma.in/fx_csp_bypass_strict-dynamic_fb.html with Strict Enhanced Tracking Protection enabled. This page contains strict CSP rules:
'''html
<meta http-equiv="Content-Security-Policy" content="default-src 'none';script-src 'strict-dynamic' 'nonce-random'">
'''
2. Click on the "Login with Facebook" button to call `FB.login()`. An alert dialog will pop up.
Root cause:
This is caused by DOM clobbering resulting in a reference to the wrong element.
The DOM clobbering occurs in the document.currentScript.src access at: https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/facebook-sdk.js#29
Here, `<img name="currentScript" src="data:,alert(document.domain)">` is accessed wrongly.
And then if the `FB.login()` is called, the wrong URL will be loaded via a <script> tag at https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/facebook-sdk.js#314
Note that the same references exist in other shims, which can be also potentially used for the bypass:
https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/rambler-authenticator.js#8
https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/vidible.js#18
The following seems to be test code, but just in case:
https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/live-test-shim.js#10
https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/mochitest-shim-1.js#10
https://searchfox.org/mozilla-central/rev/5756c5a3dea4f2896cdb3c8bb15d0ced5e2bf690/browser/extensions/webcompat/shims/mochitest-shim-2.js#10
Actual results:
CSP strict-dynamic is bypassed.
Expected results:
CSP strict-dynamic should not be bypassed.
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 shim reads document.currentScript.src without accounting for DOM clobbering, so an element with name="currentScript" can override the reference and supply an attacker-controlled URL that is later loaded as a script.
Impact
An attacker could execute arbitrary JavaScript on a page that has a strict-dynamic CSP, but only if the page loads the Facebook SDK and its legitimate code calls FB.login().
What the attacker needed
Firefox with Strict Enhanced Tracking Protection; Page loads Facebook SDK shim; Page calls FB.login(); Attacker can inject HTML
How it was found
- Open the test page with Strict Enhanced Tracking Protection enabled; the page has a strict CSP with strict-dynamic and a nonce.
- Click the 'Login with Facebook' button to call FB.login(); an alert dialog pops up.
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 Content Security Policy (CSP) is a rule that tells the browser which scripts are allowed to run. Firefox sometimes replaces tracking scripts with its own simplified versions (shims) to protect privacy. This report shows that an attacker who can inject HTML into a page can trick one shim into loading the attacker's script, defeating the CSP.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.