Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content-security-policy set by twitter.com causes injected scriptlets to not run #235

Closed
3 of 8 tasks
anvakl opened this issue Sep 21, 2018 · 12 comments
Closed
3 of 8 tasks
Labels
external issue involving an external factor Firefox specific to Firefox fixed issue has been addressed

Comments

@anvakl
Copy link

anvakl commented Sep 21, 2018

Prerequisites

  • I verified that this is not a filter issue
  • This is not a support issue or a question
  • I performed a cursory search of the issue tracker to avoid opening a duplicate issue
    • Your issue may already be reported.
  • I tried to reproduce the issue when...
    • uBlock Origin is the only extension
    • uBlock Origin with default lists/settings
    • using a new, unmodified browser profile
  • I am running the latest version of uBlock Origin
  • I checked the documentation to understand that the issue I report is not a normal behavior

Description

content security policy set by twitter doesnot allow inline scripts without nonce= attribute to run.

csp set by twitter is

“script-src https://connect.facebook.net https://cm.g.doubleclick.net https://ssl.google-analytics.com https://graph.facebook.com https://twitter.com 'unsafe-eval' https://*.twimg.com https://api.twitter.com https://analytics.twitter.com https://publish.twitter.com https://ton.twitter.com https://syndication.twitter.com https://www.google.com https://t.tellapart.com https://platform.twitter.com https://www.google-analytics.com blob: 'nonce-P+pysraZ1bM1TVgufAjzPw==' https://twitter.com”

I got that from beowser console.

A specific URL where the issue occurs

https://twitter.com/gorhill

Steps to Reproduce

  1. Create a rule twitter.com##+js(addEventListener-logger.js)
  2. open https://twitter.com/gorhill
  3. open browser console to see log.

Expected behavior:

Scriptlet logs all event listeners

Actual behavior:

Get an error saying : Content Security Policy: The page’s settings blocked the loading of a resource at self

Palemoon displays full csp.
Firefox says it is because of script-src

Your environment

Firefox 60 ESR with uBlock1.16.21rc1
Palemoon 28.1 with uBlock-legacy1.16.4.4

Windows 8.1 x64

@uBlock-user uBlock-user added the Firefox specific to Firefox label Sep 21, 2018
@uBlock-user
Copy link
Contributor

uBlock-user commented Sep 21, 2018

@uBlock-user uBlock-user added the external issue involving an external factor label Sep 21, 2018
@joey04
Copy link

joey04 commented Sep 21, 2018

Question:
Would a workaround be to use a regular content script, like uBO-extra or uBO's subscriber link finder?

@uBlock-user
Copy link
Contributor

Firefox does have a contentScripts API which could be used if it's possible.

@gwarser
Copy link

gwarser commented Sep 21, 2018

Duplicate of gorhill/uBlock#2823

@gwarser gwarser marked this as a duplicate of gorhill/uBlock#2823 Sep 21, 2018
@gwarser gwarser added the duplicate This issue or pull request already exists label Sep 21, 2018
@uBlock-user uBlock-user removed Firefox specific to Firefox external issue involving an external factor labels Sep 21, 2018
@gorhill
Copy link
Member

gorhill commented Sep 21, 2018

Duplicate of gorhill/uBlock#2823

Different, that other one was about redirect to data: URI causing the failure -- and this was fixed by using web-accessible resources. The one here is about inline script tags injected in the DOM.

@uBlock-user uBlock-user added external issue involving an external factor Firefox specific to Firefox and removed duplicate This issue or pull request already exists labels Sep 21, 2018
@gwarser
Copy link

gwarser commented Sep 21, 2018

Then "see also".

@JustOff
Copy link

JustOff commented Feb 2, 2020

Since Firefox bug #1267027 is still not fixed, and @gorhill is clearly against uBO lowering CSP rules set by sites, I created a separate add-on called Scriptlet Doctor, which provides such a workaround for both Firefox current and legacy-based browsers.

By default, Scriptlet Doctor modifies CSP only for a specific list of domains that can be configured. Currently, this list is pre-filled with domains requested by RU AdList admin @dimisa-RUAdList.

Hope someone finds it useful.

@MasterKia
Copy link
Member

uBlockOrigin/uAssets#14605 (reply in thread):

I need to investigate what can be done with Firefox, there might be a way.

@gorhill Is there a chance to reopen this issue?

@evilpie
Copy link

evilpie commented Dec 9, 2022

There is a relatively simple workaround and I am not sure if you have considered doing that @gorhill. The problem is that uBlock uses an actual inline script for injection, but Firefox only bypasses the CSP for <script> loads triggered via an URL.

The following change makes uBlock immune to the page's CSP:

+++ b/src/js/contentscript.js
@@ -468,8 +468,11 @@ vAPI.injectScriptlet = function(doc, text) {
     let script;
     try {
         script = doc.createElement('script');
-        script.appendChild(doc.createTextNode(text));
+        let blob = new Blob([decodeURIComponent(scriptlets)], {type: 'text/javascript'});
+        let url = document.URL.createObjectURL(blob);
+        script.url = url;
         (doc.head || doc.documentElement || doc).appendChild(script);
+        document.URL.revokeObjectURL(url);
     } catch (ex) {
     }
     if ( script ) {

@uBlock-user
Copy link
Contributor

The following change makes uBlock immune to the page's CSP:

Immune to blob: being blocked by page's CSP ?

@evilpie
Copy link

evilpie commented Dec 11, 2022

Immune to blob: being blocked by page's CSP ?

Sure this works even with Content-Security-Policy: script-src 'none';

Edit: sandbox would probably still be problematic, but that isn't widely used.

@gorhill
Copy link
Member

gorhill commented Dec 11, 2022

@evilpie Thanks for the suggested fix, seems to work fine in both Firefox and Chromium, will publish a new version with fix as suggested.

@gorhill gorhill reopened this Dec 11, 2022
gorhill added a commit to gorhill/uBlock that referenced this issue Dec 11, 2022
Related issue:
- uBlockOrigin/uBlock-issues#235

Fixed as suggested by <https://github.com/evilpie>, to safely
bypass a page's own CSP.
@uBlock-user uBlock-user added the fixed issue has been addressed label Dec 11, 2022
@gwarser gwarser closed this as completed Dec 12, 2022
MasterKia referenced this issue in uBlockOrigin/uAssets Aug 13, 2023
Unfortunately, the flawed conclusions people are reaching because of
this flawed tool are all over the place, including some spamming results
from this pointless tools in uBO's own thread on Wilders Security despite
advises to refrain from using the tool to evaluate content blockers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external issue involving an external factor Firefox specific to Firefox fixed issue has been addressed
Projects
None yet
Development

No branches or pull requests

8 participants