CSP 'sandbox' directive prevents content scripts from matching, due to unique origin, breaking also browser features [Screenshots]
Categories
(WebExtensions :: General, defect, P2)
Tracking
(firefox128 fixed)
Tracking | Status | |
---|---|---|
firefox128 | --- | fixed |
People
(Reporter: arantius, Assigned: robwu)
References
(Blocks 3 open bugs)
Details
(Keywords: dev-doc-complete, Whiteboard: [addons-jira])
Attachments
(6 files)
Reporter | ||
Comment 1•7 years ago
|
||
Reporter | ||
Comment 2•7 years ago
|
||
Reporter | ||
Comment 3•7 years ago
|
||
Comment 4•7 years ago
|
||
Reporter | ||
Comment 5•7 years ago
|
||
Updated•7 years ago
|
Updated•7 years ago
|
Updated•7 years ago
|
Updated•7 years ago
|
Comment 8•7 years ago
|
||
Comment 9•7 years ago
|
||
Comment 10•7 years ago
|
||
Comment 11•7 years ago
|
||
Comment 12•7 years ago
|
||
Comment 13•7 years ago
|
||
Comment 14•7 years ago
|
||
Reporter | ||
Comment 15•7 years ago
|
||
Updated•7 years ago
|
Updated•7 years ago
|
Comment 17•7 years ago
|
||
Comment 18•7 years ago
|
||
Comment 19•7 years ago
|
||
Updated•6 years ago
|
Comment 20•6 years ago
|
||
Comment hidden (advocacy) |
Comment hidden (advocacy) |
Comment 23•6 years ago
|
||
Comment 24•6 years ago
|
||
Comment 25•6 years ago
|
||
Comment 26•6 years ago
|
||
Comment 27•6 years ago
|
||
Comment 28•6 years ago
|
||
Comment 29•6 years ago
|
||
Assignee | ||
Comment 30•6 years ago
|
||
Updated•5 years ago
|
Comment 33•5 years ago
|
||
(In reply to Giorgio Maone [:mao] from comment #29)
(In reply to Jet from comment #28)
(In reply to kernp25 from comment #27)
Firefox screenshots also does not work!
It works fine here with 60.x and 61.x and 62.0b5 all (64-bit) on Windows 10
1709,Are you sure you're talking in the context of this bug?
Anything that relies on content scripts, including Mozilla's internal
add-ons which more and more Firefox features are based on like Screenshots,
are broken by this:
- Open https://www.dropbox.com/help/security/enable-two-step-verification
- Try to take a screenshot from the Page Actions menu (the drop-down from
the navigation bar)- Watch the notification "Whoa! Firefox Screenshots went haywire."
Reproducible on both Firefox 61, 62.0b5 and Nightly 63.0a1 (2018-07-05).
I've just tried this, going through exactly the steps you have provided. On Firefox 67.0.4 (64-bit), Windows 10, taking a screenshot of that page works for me (clicking on the three dots inside the address bar to get the option "Take a screenshot"). Has anything been fixed in Firefox? Or has the Dropbox page dropped its CSP or anything?
Updated•5 years ago
|
Updated•5 years ago
|
Comment 34•5 years ago
|
||
Here's another repro:
- Install https://addons.mozilla.org/en-US/firefox/addon/styl-us/
- Install https://raw.githubusercontent.com/StylishThemes/GitHub-Dark/master/github-dark.user.css
- Open https://raw.githubusercontent.com/StylishThemes/GitHub-Dark/master/package.json
- Notice page is unstyled, should be white text on dark background
My current workaround is to remove the CSP "sandbox" directive via https://addons.mozilla.org/en-US/firefox/addon/header-editor/.
Comment 35•5 years ago
|
||
Test file illustrating inability to inject into sandboxed page
Updated•5 years ago
|
Updated•5 years ago
|
Assignee | ||
Updated•5 years ago
|
Comment 38•5 years ago
|
||
I looked into this a little bit. The reason why we fail to apply the content-script to pages with a sandbox CSP directive is simple. Such pages get a null principal. In MozDocumentMatcher::Matches
we allow about:blank to match with a null-principal. (And also seemingly for data: URIs, which is not documented!)
But because this isn't the case here we are rejecting the principal in WebExtensionPolicy::IsRestrictedDoc
.
I found this test page for testing CSP sandboxing.
Assignee | ||
Comment 41•4 years ago
|
||
Detailed manual test case to explore more conditions (source code of page hosted at https://robwu.nl/s/content-script-sandbox/ + extension to test with):
- same-origin iframe
- cross-origin iframe
- srcdoc iframe
- data:-URL iframe.
With the following combinations:
sandbox=""
(=unique origin).sandbox="allow-scripts"
(just to confirm that injection didn't fail due to scripts being blocked)
(for blob:-URLs I also addedsandbox="allow-same-origin"
variants to )- No sandbox (=origin of the page).
Firefox 78's current behavior is to only run content scripts in:
- http(s), but only if not sandboxed.
- srcdoc frames, but only if not sandboxed.
- same-origin blob:-URLs.
- (no content scripts in data:-URLs - bug 1475831)
- (no content scripts in sandboxed pages - this bug (bug 1475831))
Chromium 86.0.4215.0's current behavior is:
- http(s), including sandboxed pages.
- srcdoc frames, but only if not sandboxed.
- no content scripts in blob: or filesystem: URLs.
- no content scripts in data:-URLs yet, but will be: https://bugs.chromium.org/p/chromium/issues/detail?id=55084#c45
Comment 42•4 years ago
|
||
Also, I would like to highlight the issue reported in Bug 1517017.
For iframes with sandbox="" in a moz-extension:// page with sandbox="", hosting cross-origin http/https URLs:
- Firefox 78 - Content scripts do NOT run. BUT changing sandbox to "allow-same-origin" allows content scripts to run.
- Chrome 86 - Content scripts do run.
Assignee | ||
Comment 43•4 years ago
|
||
Following discussion with Chrome (in crbug 55084, linked from bug 1475831#c16), the future injection behavior in Chrome is as follows:
- http(s) including sandboxed pages.
- about: (blank/srcdoc), blob: (and filesystem: - only supported in Chrome), data: if
match_origin_as_fallback
is true - see https://bugzilla.mozilla.org/show_bug.cgi?id=1475831#c16
In Firefox, we will follow the same behavior. This means that bug 1475831 should be fixed by implementing match_origin_as_fallback
, and this bug should be fixed by considering the URL of the document when the origin is opaque (i.e. use document's http(s) URL when document principal is null principal). In case the document's URL does not represent the origin (e.g. about:, data:, ... opposed to http(s)), we would have to somehow determine the non-null origin that was responsible for creating the document. That will probably be handled in bug 1475831 (and if not, we should file a follow-up bug to support content scripts in sandboxed frames where the origin cannot be derived from the document's URL).
Assignee | ||
Comment 45•3 years ago
|
||
(In reply to Rob Wu [:robwu] from comment #43)
In case the document's URL does not represent the origin (e.g. about:, data:, ... opposed to http(s)), we would have to somehow determine the non-null origin that was responsible for creating the document. That will probably be handled in bug 1475831 (and if not, we should file a follow-up bug to support content scripts in sandboxed frames where the origin cannot be derived from the document's URL).
bug 1715167 introduces principal.precursorPrincipal
that offers this information - https://searchfox.org/mozilla-central/rev/c12a59323ee46b29b90c9917a3a7a70ea714ffec/caps/nsIPrincipal.idl#550-563
Comment 46•3 years ago
|
||
see also https://github.com/uBlockOrigin/uBlock-issues/issues/2072
any suggestions?
Comment 47•2 years ago
|
||
Something must have changed recently, because raw.githubusercontent.com
pages are now themed properly.
Comment 48•2 years ago
|
||
Still an issue for extensions. Problematic for many NoScript features.
Comment hidden (advocacy) |
Updated•2 years ago
|
Comment hidden (offtopic) |
Comment hidden (offtopic) |
Assignee | ||
Comment 52•2 years ago
|
||
This is not just an issue with tabs.executeScripts
and content_scripts
/ contentScripts.register
/ userScripts.register
,
but also an issue with the scripting.executeScript
(and scripting.registerContentScripts
) API.
Updated•2 years ago
|
Comment 53•2 years ago
|
||
I looked at this and finding something that works around this issue isn't hard, but the hard part is deciding when to do the unwrapping so that everything stays reasonable secure.
One approach would be handling this in DocInfo::Principal
like this:
@@ -1035,7 +1046,14 @@ nsIPrincipal* DocInfo::Principal() const
return aLoadInfo->TriggeringPrincipal();
}
};
- mPrincipal.emplace(mObj.match(Matcher(*this)));
+
+ nsCOMPtr<nsIPrincipal> principal = mObj.match(Matcher(*this));
+ if (principal && !URL().InheritsPrincipal() && principal->GetIsNullPrincipal()) {
+ if (nsCOMPtr<nsIPrincipal> precursor = principal->GetPrecursorPrincipal()) {
+ principal = precursor;
+ }
+ }
+ mPrincipal.emplace(principal);
}
return
The function doesn't even have that many callers, with WebExtensionPolicy::IsRestrictedDoc
being the important one that currently just denies any non content principals. For a more targeted fix we could also use the precursor principal there. Notably as well is that DocInfo::PrincipalURL
would fallback to the document URL for non content principals in that scenario.
Comment 54•10 months ago
|
||
Not a solution, but at least a workaround: Enable toolkit.legacyUserProfileCustomizations.stylesheets
in About:Config, and create the file "Chrome\userContent.css" to the FF profile's root dir, with the following contents:
@-moz-document domain("gist.githubusercontent.com") {
body {
color-scheme: dark !important;
}
}
While users can implement this workaround manually, maybe it would also be possible to create a new user pref that enforces that styling natively, so users don't have to enable legacy stylesheets if they don't otherwise want to.
Comment 55•7 months ago
|
||
A user just reported this issue for one of my extensions (it injects a script that adds a UI to draw the target WebP image on a canvas and save to a different format):
- https://github.com/jscher2000/Save-webP-as-extension/issues/30
- https://addons.mozilla.org/firefox/addon/save-webp-as-png-or-jpeg/
What surprised me was that sandbox
blocked scripts in the stand-alone image viewer (example: https://www.jeffersonscher.com/res/img-with-csp.php?csp=sandbox - you can test with right-clicking a background area > Take Screenshot).
This is inconsistent with the result for
Content-Security-Policy: script-src: 'none';
which does not prevent content scripts from running on the same pages (example: https://www.jeffersonscher.com/res/img-with-csp.php?csp=noscript - Take Screenshot works)
In the context of the stand-alone image viewer, there is no obvious security benefit to disallowing extension content scripts. It appears only to frustrate user wishes to use extensions to work with the image. But it might not be efficient to handle as a separate bug, so I am commenting here.
Assignee | ||
Comment 56•7 months ago
|
||
ExtensionPolicyService::InjectContentScripts has two code paths, one
with preloading and one without. The most important case, without
preloading (i.e. executing immediately) is covered extensively by unit
tests. The preloading case had zero test coverage before.
Due to existing implementation limitations (*), the actual behavior of
preloading differs logically from the non-preloading case. Before making
any changes to the area, this patch adds documentation (mainly to
DocInfo::PrincipalURL and DocInfo::Principal) and extensive test
coverage to explicitly confirm the correctness of preloading behavior.
(*) in short: with DOMWindow (non-preloading), the principal is always
known and available, whereas with LoadInfo (preloading), the principal
has to be reconstructed, but without enough information available to
tell the difference for null principals.
Assignee | ||
Comment 57•7 months ago
|
||
This patch adds support for matching http(s) (and file:) documents even
if they are sandboxed. Previously, there was no way for extensions to
run content scripts there.
Comment 58•6 months ago
|
||
Comment 59•6 months ago
|
||
bugherder |
https://hg.mozilla.org/mozilla-central/rev/3f63c4b7f235
https://hg.mozilla.org/mozilla-central/rev/6b3f0c4c2c17
Assignee | ||
Updated•6 months ago
|
Comment 60•5 months ago
|
||
Added a release note in Support for content scripts on sandboxed http(s) and file: URLs #34460. Couldn't see any need for changes on the pattern matching, CSP, or script API pages.
Description
•