+++ This bug was initially created as a clone of Bug #1207394 +++
This is a follow-up to bug 1254194 comment 29.
In Chrome, web page CSP is not supposed to apply to content inserted by a content script. So, the following should all work, in theory:
* Injecting an inline <script> node should execute the script in a page with "script-src 'none'"
* Injecting an image from "http://example.com" into a page with "default-src 'self'" should load the image.
The following do not work in Chrome, but they seem to prefer that they would:
* Injecting a node with an "onclicked" listener into a page with "script-src 'none'" does not execute the listener, because the script is not executed during the call from the content script that created it.
*** Bug 1267291 has been marked as a duplicate of this bug. ***
Dan, do you have any thoughts on how much of this behavior we want to copy? I'm actually not inclined to implement it at all, except to the extent that add-ons should be allowed to inject resources packaged with the extension.
We favor the desires of the user above the page author. We usually assume that the add-on is acting on the user's behalf (the user chose to install it) so it'd be nice if add-ons could bypass CSP. Although sometimes they do slimy stuff for themselves so it's also nice when what they're doing is explicit so reviewers can check it out.
We also are fighting an architecture that is hard to change. If something sticks in an <img> tag, that results in an image load generated by that page. At the networking/nsIContentPolicy level where CSP is enforced we can't tell the difference between script in the page creating that <img> tag and an add-on or chrome creating it.
This is not special to web extensions, and we have essentially this feature request for regular add-ons on file somewhere; I'd call this a dupe of that one. But you're here and seem to have the energy to fight this battle so here are two possible ways to "fix" it.
a) What I've suggested before is to add a method to the internal CSP that lets us expand the policy, rather than the current mechanism that only lets additional content policies tighten the policy. So a Flickr mashup could call this API and add "img-src *.flickr.com" or maybe "all-src *.flickr.com" (I just made up all-src -- we'd need something to override all policies, not a default to be used if other policies weren't set). For WebExtension and Add-on SDK addons this override could be in the add-on metadata (e.g. manifest.json) and easy to review for sanity.
b) I don't think we know in all the places, but it's possible that for DOM-based insertion (as opposed to parser-based like setting innerHTML) we still know the script principal by time we're calling AsyncOpen2 and can use that rather than the page principal. I get 167 non-test hits for AsyncOpen2 so this would be a painful and error-prone approach.
OK, I'll try to figure out how doable this is, then.
I think we should still respect some restrictions, though, and at least forbid loading non-secure resources into secure pages with policies that forbid it.
I'm facing the same problem right now with my extension. I would opt for bypass CSP. As Daniel Veditz mention, the user chose to install the add-on with all pros (new features...) on cons (permissions request...). So CSP is just another entry on that list, depending on your point of view part of pro or con ;)
What would be a good workaround around this to bypass the CSP? I was thinking about using a background script, but that sounds like a lot of overhead just to make my extension working in Firefox.
Any word on when this might get fixed? Several of my addon's beta testers are running into this bug, and a lot more of the regular users will probably be experiencing it next month when Firefox 48 releases.
This won't be fixed any time in the next few months.
A possible solution: Provide an API to convert blobs to moz-extension: URIs. AIUI those URIs bypass CSP, unlike blob: URIs. So images could be downloaded as blob via XHR, converted to an extension URI and then injected as <img src="moz-extension:...">
It requires extra work by addon authors, but that way the browser won't have to track which parts of the DOM would have to be treated as privileged somehow. The concept of DOM ownership is murky anyway. It's a shared resource between addons and the page.
An alternative would be an API that allows addons to whitelist patterns of content they intend to load into a page. The downside is that it is more coarse-grained than the blob approach and thus increases the potential exploit surface - but only by the whitelist - for malicious scripts that makes it into the site.
(In reply to The 8472 from comment #8)
> A possible solution: Provide an API to convert blobs to moz-extension: URIs.
> AIUI those URIs bypass CSP, unlike blob: URIs. So images could be downloaded
> as blob via XHR, converted to an extension URI and then injected as <img
It would be much easier to just treat blobs with moz-extension: origins as CSP-exempt, like we do for moz-extension: URLs themselves. Feel free to file a separate bug for that, and I'll look into implementing it.
> It requires extra work by addon authors, but that way the browser won't have
> to track which parts of the DOM would have to be treated as privileged
> somehow. The concept of DOM ownership is murky anyway. It's a shared
> resource between addons and the page.
This is something that we're going to have to do regardless.
Possible dupe mentioned in comment 3 is bug 615708.
*** Bug 1363139 has been marked as a duplicate of this bug. ***
*** Bug 1366467 has been marked as a duplicate of this bug. ***
On the GM issue tracker the idea was brought up to inject per-userscript nonces into each -src rule of CSPs for each page load which could then be exposed to content scripts via an API. I think this is cleaner than "DOM ownership" because ownership could easily lead to transitive privilege leaks if that also applies to <script> or <iframe> elements.
It looks like the Augury team has run into this issue where they can't inject their own scripts via a content script.
(In reply to Bryan Clark (DevTools PM) [:clarkbw] from comment #14)
> It looks like the Augury team has run into this issue where they can't
> inject their own scripts via a content script.
Content from extensions should be immune to CSP. They just can't inject web content which would violate a page's CSP.
Duplicate of bug 615708?
Also related to bug 1389874.
This is breaking crucial devtools extensions on CSP secured pages.
Filed in https://github.com/gaearon/redux-devtools/issues/380#issuecomment-329085853
Script is injected here: https://github.com/zalmoxisus/redux-devtools-extension/blob/ec3c33d7e2db17df13d3dfe06d02e12da3feee6c/src/browser/extension/inject/pageScriptWrap.js#L10
:clarkbw mentioned in Comment 14 with code linked similar to the inject pattern from Redux.
More addons that are not converted to extensions yet will run into this.
ni? :mconca for input on prioritizing this from a web extension product perspective.
*** Bug 1400924 has been marked as a duplicate of this bug. ***
The CSP spec is explicit about whether CSP should affect extensions:
> Policy enforced on a resource SHOULD NOT interfere with the operation of user-agent features like addons, extensions, or bookmarklets. These kinds of features generally advance the user’s priority over page authors, as espoused in [HTML-DESIGN].
> Moreover, applying CSP to these kinds of features produces a substantial amount of noise in violation reports, significantly reducing their value to developers.
> Chrome, for example, excludes the chrome-extension: scheme from CSP checks, and does some work to ensure that extension-driven injections are allowed, regardless of a page’s policy.
Thanks to everyone for the constructive comments. We are currently targeting to have this feature land in 58.
At least for the specific case of <script> tags any bypass should be opt-in or require extension-specific APIs to work.
I also have concerns about other tags such as <img src="...">, specifically that it does not obscure the included URL. If blob URI + CSP nonce were the norm there would be less information leakage by default.
This is probably not a top concern for most extensions, but since we have no overlays that are invisible to content at least some content-blocking or privacy-focused extensions probably want to reveal as little information as possible to the web content which could report those modifications back to the server.
*** Bug 615708 has been marked as a duplicate of this bug. ***
Why are addon script inserted as page scripts in the first place? Addons are supposed to be application-wide.
This bug refers to content scripts which are injected into page context so that the addon can interact with the DOM and page scripts. Not addons themselves.
Why are they injected into the page? How come an addon can't just interact with a page without modifying it?
It can. But if you want to inject content into the page, then you're modifying the page. Now what happens if you inject content that loads resources from locations the page's CSP doesn't allow?
I was pointed here by someone in IRC. I'm finding that both content scripts registered in `manifest.json` and calls to `tabs.executeScript()` fail for pages with a strict CSP.
For the manifest case:
We register a script which `"matches": ["*://*/*.user.js"],` but navigate to a matching URL with a strict CSP, like https://raw.githubusercontent.com/reek/anti-adblock-killer/master/anti-adblock-killer.user.js , and the script does not run.
In the case of `tabs.executeScript()` we get an error that says "Missing host permission for the tab" despite having the "<all_urls>" permission.
So I guess I'm implicitly blocked from injecting content (which I'm not really even trying to do) because I can't even run the script that would be doing such injecting.
(In reply to Anthony Lieuallen from comment #28)
> I was pointed here by someone in IRC. I'm finding that both content scripts
> registered in `manifest.json` and calls to `tabs.executeScript()` fail for
> pages with a strict CSP.
> For the manifest case:
> We register a script which `"matches": ["*://*/*.user.js"],` but navigate to
> a matching URL with a strict CSP, like
> adblock-killer.user.js , and the script does not run.
> In the case of `tabs.executeScript()` we get an error that says "Missing
> host permission for the tab" despite having the "<all_urls>" permission.
CSP doesn't affect content script execution at all. In any case, I can't
reproduce this issue with a simple test extension. Can you provide a complete
(In reply to Kris Maglione [:kmag] (long backlog; ping on IRC if you're blocked) from comment #29)
> Can you provide a complete testcase?
See bug #1411641 for more detailed steps including reduced reproduction case.
Where can this be tested? Is it implemented somewhere?
Are the tests from bug #1411641 valid? I am asking because of https://bugzilla.mozilla.org/show_bug.cgi?id=1411641#c4.
So far I have https://bugzilla.mozilla.org/show_bug.cgi?id=1400924 and https://bugzilla.mozilla.org/show_bug.cgi?id=615708, are these scenarios valid?
Do you have any other scenarios and web extension examples I can use?
This is also breaking the live preview of the Asciidoctor extension.
Code can be found here: https://github.com/asciidoctor/asciidoctor-chrome-extension/
The following URL should be rendered but nothing happens: https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/README.adoc
Since the CSP on GitHub is really strict, I believe that the content scripts are not executed.
Content-Security-Policy default-src 'none'; style-src 'unsafe-inline'; sandbox
(In reply to ggrossetie from comment #32)
> This is also breaking the live preview of the Asciidoctor extension.
> Code can be found here:
> The following URL should be rendered but nothing happens:
This is a mostly unrelated issue. See bug 1411641.
(In reply to Kris Maglione [:kmag] (long backlog; ping on IRC if you're blocked) from comment #33)
> This is a mostly unrelated issue. See bug 1411641.
Indeed thanks for the pointer.
I've also noticed that my extension is not able to load Google Fonts:
Content Security Policy: The page’s settings blocked the loading of a resource at https://fonts.googleapis.com/css?family=Varela+Round|Open+Sans:400italic,700italic,400,700 (“style-src https://gitlab.com 'unsafe-inline' https://gl-static.global.ssl.fastly.net”).
The CSP on this page (gitlab.com) is:
Content-Security-Policy: object-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://gl-static.global.ssl.fastly.net https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; style-src 'self' 'unsafe-inline' https://gl-static.global.ssl.fastly.net; img-src * data: blob:; frame-src 'self' https://www.google.com/recaptcha/; frame-ancestors 'self'; connect-src 'self' https://gl-static.global.ssl.fastly.net wss://gitlab.com https://sentry.gitlap.com https://customers.gitlab.com; report-uri https://sentry-infra.gitlap.com/api/3/csp-report/?sentry_key=a664fdde83424b43a991f25fa7c78987
Given the error message, we can see that the CSP of the page is applied to the content inserted by my content scripts.
(In reply to ggrossetie from comment #34)
> (In reply to Kris Maglione [:kmag] (long backlog; ping on IRC if you're
> blocked) from comment #33)
> > This is a mostly unrelated issue. See bug 1411641.
> Indeed thanks for the pointer.
> I've also noticed that my extension is not able to load Google Fonts:
> Content Security Policy: The page’s settings blocked the loading of a
> resource at
> 700italic,400,700 (“style-src https://gitlab.com 'unsafe-inline'
This should work if you bundle the CSS in your extension, after bug 1415352 is fixed. The exemption that allows extensions to load blocked stylesheets doesn't extend to content loaded by those stylesheets.
Ok, so in other words, I can't use @import in my stylesheets but I can use url with a "local" path (ie. the font is bundled in my extension), correct ?
*** Bug 1419678 has been marked as a duplicate of this bug. ***
Per :ckerschb, Bug 965637 should eliminate these kind of problems.
Any progress on this? Specifically I find overkill to block unsafe_inline on style-src at all.
Any updates? I want to run userscripts on a CSP-protected domain with restrictive script-src policies. And I cannot ; it feels like Firefox endorses the site strict policy instead of empowering the user.
(In reply to Jules RANDOLPH from comment #40)
> Any updates? I want to run userscripts on a CSP-protected domain with
> restrictive script-src policies. And I cannot ; it feels like Firefox
> endorses the site strict policy instead of empowering the user.
Can’t you use the the `contentScripts` API¹?
(In reply to ExE Boss from comment #41)
> (In reply to Jules RANDOLPH from comment #40)
> > Any updates? I want to run userscripts on a CSP-protected domain with
> > restrictive script-src policies. And I cannot ; it feels like Firefox
> > endorses the site strict policy instead of empowering the user.
> Can’t you use the the `contentScripts` API¹?
Thanks, I will ping Tampermonkey authors.
This is still breaking react and redux devtools.
> Can’t you use the the `contentScripts` API¹?
My understanding is that contentScripts don't have the same access to objects like ``unsafeWindow``. I'm still looking into the exact details for everything, so it could be that there's a secondary privilege or alternate approach that I'm missing.
Assuming that there isn't, currently this is blocking an extension I use, ViolentMonkey, from allowing scripts to run on sites like Github and Twitter.
I understand and support the security side of things, but it feels like (especially in the case of Twitter) their use of CSP is probably not just about security, but also somewhat about them making it harder for any of their users to significantly alter the page. I would support some kind of security mechanism that didn't allow website operators to decide to hamper a user's ability to build webextensions for that domain that leverage more powerful extension APIs.
(In reply to danshumway from comment #44)
> > Can’t you use the the `contentScripts` API¹?
> My understanding is that contentScripts don't have the same access to
> objects like ``unsafeWindow``.
(In reply to Rob Wu [:robwu] from comment #45)
Thanks, will take a look and do some additional research.