[meta] Page CSP should not apply to content inserted by content scripts (V2 issue)
Categories
(WebExtensions :: General, defect, P3)
Tracking
(Not tracked)
People
(Reporter: kmag, Unassigned)
References
(Depends on 3 open bugs)
Details
(Keywords: meta, Whiteboard: triaged)
| Reporter | ||
Comment 2•9 years ago
|
||
Comment 3•9 years ago
|
||
| Reporter | ||
Comment 4•9 years ago
|
||
| Reporter | ||
Comment 7•9 years ago
|
||
| Reporter | ||
Comment 9•9 years ago
|
||
Updated•9 years ago
|
Updated•9 years ago
|
Updated•9 years ago
|
Comment 10•8 years ago
|
||
Updated•8 years ago
|
Comment 13•8 years ago
|
||
Comment 14•8 years ago
|
||
| Reporter | ||
Comment 15•8 years ago
|
||
Comment 16•8 years ago
|
||
Comment 17•8 years ago
|
||
Comment 18•8 years ago
|
||
Comment 20•8 years ago
|
||
Comment 21•8 years ago
|
||
Comment 22•8 years ago
|
||
Comment 24•8 years ago
|
||
Comment 25•8 years ago
|
||
Comment 26•8 years ago
|
||
Comment 27•8 years ago
|
||
Comment 28•8 years ago
|
||
| Reporter | ||
Comment 29•8 years ago
|
||
Comment 30•8 years ago
|
||
Updated•8 years ago
|
Comment 31•8 years ago
|
||
Comment 32•8 years ago
|
||
| Reporter | ||
Comment 33•8 years ago
|
||
Comment 34•8 years ago
|
||
| Reporter | ||
Comment 35•8 years ago
|
||
Comment 36•8 years ago
|
||
| Reporter | ||
Updated•7 years ago
|
Comment 38•7 years ago
|
||
Updated•7 years ago
|
Comment 39•7 years ago
|
||
Comment 40•7 years ago
|
||
Comment 41•7 years ago
|
||
Comment 42•7 years ago
|
||
Comment 43•7 years ago
|
||
Comment 44•7 years ago
|
||
Comment 45•7 years ago
|
||
Comment 46•7 years ago
|
||
Updated•7 years ago
|
Comment 47•6 years ago
|
||
For reference, the WebHint devtools extensions has to add a CSP-quirks detection to work around the silent failing that content script CSP issues lead to: https://github.com/webhintio/hint/issues/2708
Comment 48•6 years ago
|
||
Confirming that React and Redux Devtools are still broken.
Comment 49•6 years ago
|
||
I am going to look into some of those issues. I really want to avoid extensions having to modify the CSP header, recently I heard that we are even removing extensions from AMO for doing that.
I am aware of at least one issue, with <script> and innerText while src works.
Comment 50•6 years ago
|
||
The collateral damage list for this not being fixed is growing. CSP is a reasonable thing for sites to implement but without the ability to override it, end user's are starting to be the loosers.
In my case Firenvim doesn't work on Stack Overflow. I can't really blame Stack Exchange for wanting to lock down their pages as best they can, but the reason I use Firefox is that I expect it to be on my side and do what I want. That essential freedom is what's at stake here, I can't even make my computer do something locally that I want it to do.
Comment 51•6 years ago
|
||
@caleb After looking at you repo, it seems that you're claiming that WebSockets are subjected to the page's CSP. If this is the case, then that's a bug that we'd like to fix. Please file a new bug report with a test case to reproduce the problem (if you cc me on it, I will link other relevant bugs to it).
Side note: A work-around is to create the WebSocket connection from a background page (or if you really want to tie it to the page's lifetime, through an iframe with an extension page in the current document).
Comment 52•6 years ago
|
||
or if you really want to tie it to the page's lifetime, through an iframe with an extension page in the current document
That's already what firenvim does. However, Content Security Policy: Upgrading insecure request ‘ws://127.0.0.1:38779/4030261693’ to use ‘wss’ is still logged in the console. Is there something I can do to work around that?
Comment 53•6 years ago
|
||
(In reply to glacambre from comment #52)
However,
Content Security Policy: Upgrading insecure request ‘ws://127.0.0.1:38779/4030261693’ to use ‘wss’is still logged in the console. Is there something I can do to work around that?
That's a different limitation. In the past, for some personal userscript talking to a little Python program I wrote to run on my PC, I 'simply' made it run wss instead of ws.
I have no experience with firenvim though. So I can't give recommendations on that.
Comment 54•6 years ago
|
||
That's a different limitation
Are you saying that this CSP warning, that only happens on pages that use Content-Security-Policy: upgrade-insecure-requests and that (IMO) has no reason to apply to an iframe the document of which is located in webextension storage, is not covered by this bug? If yes, is there another bugzilla ticket that covers this bug, or should I open one?
Comment 55•6 years ago
|
||
(In reply to glacambre from comment #54)
That's a different limitation
Are you saying that this CSP warning, that only happens on pages that use
Content-Security-Policy: upgrade-insecure-requestsand that (IMO) has no reason to apply to an iframe the document of which is located in webextension storage, is not covered by this bug? If yes, is there another bugzilla ticket that covers this bug, or should I open one?
This bug is a metabug. If you have specific test cases, please file a new bug and mark it as a blocker of this bug if you can.
Updated•6 years ago
|
Comment 56•5 years ago
|
||
Use case: Extensions that can run on any page and inject UI inside a ShadowRoot with a <style> tag in there. I’m using a <style> tag because the user can customize the appearance of the UI. I currently ship a somewhat slow and sucky home-made CSS engine as a workaround on pages with strict CSP (using regex, Element.matches() and Element.style.setProperty()): https://github.com/lydell/LinkHints/blob/6f708e19456fc3e5f0d1a388a2adcf1ffaff1c97/src/renderer/css.js
Comment 57•5 years ago
|
||
here's the simplest test case I could come up with, should take like 30 seconds to set up. at least for me, this is usually resulting in a silent failure e.g. on csp sandbox pages.
manifest.json:
{
"manifest_version": 2,
"name": "Dark Plaintext",
"version": "1.0",
"description": "",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"injector.js"
],
"run_at": "document_end",
"all_frames": true,
"match_about_blank": true
}
],
"permissions": [
"tabs",
"<all_urls>"
]
}
injector.js can be anything but this is what i was personally using to test it, along with web_accessible_resources: style.css in the manifest. if it weren't for the csp thing, i'm pretty sure this tiny snippet plus css rules for body, ::-moz-selection, would be sufficient to implement "dark mode" on any plaintext page.
(function () {
var sheetArr = [];
console.log('running injector');
for (let i = 0; i < document.styleSheets.length; i++) {
sheetArr.push(document.styleSheets[i]);
console.log('added sheet to array');
};
function checkPlain(sheet) {
console.log('checking sheet');
return /plaintext\.css$/i.test(sheet.href) && sheet.disabled === false;
};
if (sheetArr.some(checkPlain)) {
console.log('at least one plain sheet found');
var sheetLink = document.createElement("link");
sheetLink.href = chrome.extension.getURL("style.css");
sheetLink.type = "text/css";
sheetLink.rel = "stylesheet";
(document.head || document.documentElement).appendChild(sheetLink);
};
})();
this one's kinda aggravating me because i don't know of any other way to do this. the only things i can think of are to disable CSP altogether, or (maybe) to try implementing pseudo-content scripts from some kind of globally scoped userchrome.js loader and an autoconfig setup to load that. which seems really outdated and sloppy, using event listeners to see when a page has loaded instead of webextensions APIs, and registering the css as an agent sheet to bypass the CSP. i tried something in between, like injecting the CSS rules into javascript objects instead of making a link DOM object, but the CSP blocks that too. maybe i'm missing something super obvious so hopefully someone can let me know if there really is a way to do this without turning off CSP altogether. it seems crazy to me that there aren't some more granular prefs for overriding CSP. like at least being able to turn off the sandbox restriction would be nice, but it would be ideal if you could keep the all the restrictions intact but allow extensions to bypass any of them. even better, allow extensions to bypass restrictions arbitrarily, provided they request a certain new permission. call it override_csp or something
Comment 58•5 years ago
|
||
(In reply to shmediaproductions from comment #57)
"js": [ "injector.js"
Why don't you instead use "css": [ "style.css" ] in your manifest?
Comment 59•5 years ago
|
||
(In reply to rhill@raymondhill.net from comment #58)
(In reply to shmediaproductions from comment #57)
"js": [ "injector.js"Why don't you instead use
"css": [ "style.css" ]in your manifest?
same difference, script or stylesheet, the src is blocked. the only way around it seems to be disabling CSP. I spent a couple weeks working on an extension that disables CSP for pages based on the stylesheets they have loaded, with that webRequest.onHeadersReceived. didn't run smoothly though, can't seem to verify plaintext.css's presence in the doc before the headers have gone through. so it wouldn't load stylesheet until half a second after the page loaded, which is ugly. I deleted it, but i do remember the methods I used. it was similar to https://github.com/PhilGrayson/chrome-csp-disable/blob/master/background.js and https://github.com/Rufflewind/chrome_cspmod/blob/master/src/background.js
Comment 60•5 years ago
|
||
also just thought I'd mention, it blocks everything the user could conceivably do to modify the style except for console commands made after the page load from within the document scope. so rules in a userContent.css file are ignored. I also tried using the nsI stylesheet service to load + register the stylesheet as an agent sheet and again as an author sheet, but this loads the rules as inline style and the CSP naturally blocks inline styles. same goes for createProcessingInstruction('xml-stylesheet'...) unfortunately. I tried to use constructable stylesheets a while ago, and might have done something wrong, but at the time it seemed like I did it correctly but it never showed up in the list. not unless I did it with the console, but a browser extension can't have that level of access.
in the document scope it just gets blocked, and using the webRequest API it can't seem to access the important info (the lack of any stylesheet requests, or the presence of plaintext.css after response headers finish) before the page loads. I might have messed up my script though, I did essentially base it on chrome extensions and I don't know the extent to which they're really different. where necessary, I substituted 'browser' for 'chrome' and storage.local for storage.sync, but still couldn't seem to get it to recognize that these plaintext docs were not requesting any stylesheets until after the page was idle. I did also run up against the application cache service worker problem where headers are not sent in the first place bc the page is cached, but I copied a solution for that from phil grayson's extension and it worked. so I don't think that's the issue messing up the sequencing.
another weird thing I ran into was an error handler that says "'urls' member of MozRequestFilter does not implement interface MatchPatternSet." this in response to onHeadersReceived trying to evaluate urls. the thing is, I didn't even need to use a match pattern. it gave me that response even when the urls field was blank. maybe this is supposed to request another API I'm not aware of. I went through the webRequest files in the source code to see if I was missing something but I don't think so.
Comment 61•5 years ago
|
||
the webRequest stuff has to run as a background script apparently, but the only way I know of to immediately check the stylesheets that actually worked is running in the scope of the document, like in a content script. so maybe I was supposed to try to set up a messaging interface between a background script checking headers and a content script checking stylesheets. but I couldn't find any way to set manifest.json that would get the content script to load on plaintext files. dark reader and other similar extensions get blocked too, though they don't know it. when you load a local page or chrome UI page, those extensions will say "blocked on this domain" or something, not for CSP reasons but bc of webextensions permissions, but injecting content scripts or stylesheets in a plain text file on raw.githubusercontent.com will be blocked for CSP reasons, but the extension won't show any message about it. and if CSP is disabled, the extension will work as normal.
so for me it seems like any method I could use ultimately runs up against the brick wall that extensions can't get into that document scope without loading a content script, but they can't load a content script while the CSP rules are set to defaults. so any script injected into the document context would have to be done dynamically, from a background script, but I don't know how to do that with webextensions. I tried doing it with a userscript and alice0775's autoconfig loader a long time ago but I either couldn't figure out how to traverse into the browser document, or couldn't figure out how to set up reliable event listeners to check every page load, tab change etc.
Comment 62•3 years ago
|
||
Hi, what's the situation with this? This thing causes issues e.g. for a popular browser addon called uBlock Origin. It can't in some cases override website's policies.
Some related issues:
https://github.com/DandelionSprout/adfilt/discussions/642
https://github.com/uBlockOrigin/uBlock-issues/issues/2079
Comment 63•3 years ago
|
||
Here uBO dev explains the issue https://github.com/uBlockOrigin/uBlock-issues/issues/2079#issuecomment-1088678416 (linking more directly into a comment in the second link).
Updated•3 years ago
|
| Comment hidden (advocacy) |
| Comment hidden (advocacy) |
Comment 67•2 years ago
|
||
I feel like this is a privacy issue:
if the CSP report varies enough¹ from an extension to another, Cloudflare (that handles most of CSP reports) can produce a list of (ip, user-agent, extention), which is concerning from a privacy point of view. It's a bigger privacy concern knowing that Privacy Badger is currently one of those extensions.
[1]: I've seen the report vary from an extension to another, but I did not studied if it's enough to recognize them unambiguously
I've curled a few .fr domains from the AFNIC opendata and 73 out of 80 domains that have CSP reporting uses Cloudflare.
For the curious, I used:
cut -d';' -f1 202302_OPENDATA_A-NomsDeDomaineEnPointFr.csv | while read -r line; do HAS_REPORT="$(curl -m 3 -I "$line" | grep -i 'reporting-endpoints\|report-to\|report-uri')"; if [[ -n "$HAS_REPORT" ]]; then printf "%s\n%s\n\n" "$line" "$HAS_REPORT"; fi; done | tee reports
Comment 68•2 years ago
|
||
(In reply to Julien Palard from comment #67)
I feel like this is a privacy issue
Bug 1588957 covers the privacy issue of extension CSP reports. The reports do not include extension ID since Bug 1705523. uBlock Origin blocks all CSP reports and is superior to Privacy Badger in every way (dynamic blocking creates more privacy issues).
Comment 69•2 years ago
|
||
I've seen mention upthread of this causing an issue in the React Dev Tools, but it looks like nobody's linked it so adding that to the thread. It causes them to fail to load in many instances that work for Chrome.
| Comment hidden (advocacy) |
Comment 71•2 years ago
|
||
(In reply to j-d-salinger from comment #70)
Just add the setting back!!! It's literally all over the web as the solution, it's still in the ABOUT:CONFIG SETTINGS, BUT IT DOESN'T FUNCTION!!!
Did the setting get removed?
If that's the case, I think you should make a new, separate bug about that.
Updated•2 years ago
|
| Comment hidden (advocacy) |
| Comment hidden (advocacy) |
Comment 76•2 years ago
|
||
@Nick Carr: Either please read and follow https://bugzilla.mozilla.org/page.cgi?id=etiquette.html , or spend your time somewhere else. Thanks.
Comment 77•1 year ago
|
||
Is it to possible to get this prioritized by someone on the Extension team? If not, could someone from the team possibly point out specific files/directories in the source code that might be involved so someone from the community might be tempted to tackle it?
Comment 78•1 year ago
|
||
This is a metabug covering many issues. It is not clear what you are referring to.
If you are looking for a way that inline <script> elements can execute in the page's context independently of the web page's CSP, then that is covered by bug 1446231. That bug is tracked but not actively worked on. Note that in Chrome, MV3 content scripts with inline script sources are blocked too.
For use cases where extensions want to run some known scripts in the page's context, extensions can already specify the world option set to "MAIN", when they want to run content scripts, whether through static content_scripts in manifest.json, dynamic scripts through scripting.executeScript, or declarative content scripts through scripting.registerContentScripts.
Comment 79•1 year ago
|
||
(In reply to Rob Wu [:robwu] from comment #78)
This is a metabug covering many issues. It is not clear what you are referring to.
If you are looking for a way that inline
<script>elements can execute in the page's context independently of the web page's CSP, then that is covered by bug 1446231. That bug is tracked but not actively worked on. Note that in Chrome, MV3 content scripts with inline script sources are blocked too.For use cases where extensions want to run some known scripts in the page's context, extensions can already specify the
worldoption set to "MAIN", when they want to run content scripts, whether through static content_scripts in manifest.json, dynamic scripts throughscripting.executeScript, or declarative content scripts throughscripting.registerContentScripts.
Thanks. bug 1446231 is slightly more narrowed; though my Spidey Sense is telling me fixing that one correctly fixes this "meta" bug correctly. I'll comment there.
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
| Comment hidden (off-topic) |
Comment 87•9 months ago
|
||
Can we please not have these discussions in a meta bug with over 100 subscribers? Thank you!
Comment 88•9 months ago
|
||
bug 1446231 is about inline <script> not bypassing CSP.
Description
•