-
Notifications
You must be signed in to change notification settings - Fork 435
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
Bypassing CSP on Github? #881
Comments
@Owyn I just tried installing HTTPS Everywhere and it didn't change anything. |
Perhaps Firefox with HTTPS Everywhere behaves like Chrome without it cuz Chrome already has its functionality or something 🤔 |
@SonOfDiablo TM BETA 4.10.6112 adds a new experimental API called GM_addScript, which can be used this way: // @grant GM_addScript
[...]
let script_tag = GM_addScript('alert("works!");'); @Owyn |
We could already do Plus won't that bring many troubles for the script including being unable to execute GM_ functions from the scope of the page? but anyway... since scripts don't run at all due to CSP - neither will From testing - CSP issue isn't there after you just install TamperMonkey for the first time without restarting the browser and appears only after you restart the firefox, maybe investigating this could help fix it? |
Though, I'm now running into the exact same CSP issue when trying to inject an iFrame. Is there already functionality for this in Tampermonkey or would that need to be added as well? (If you are interested in having that functionality of course) |
Works fine for me, even after browser restart. |
@SonOfDiablo I'm on Firefox, you're on Chromium |
Maybe |
some more info: quickly disabling and enabling TM on Firefox's extension page makes it possible to run TM & scripts on github and other CSP sites (until browser restart as always), so no need to reinstall anything to reproduce this short-lived "fix" |
Would be great to get the Safari Mac App Store version updated with this feature. Thanks @derjanb |
Usage: GM_addElement('script', { textContent: 'window.foo = "bar";' }); or GM_addElement(shadowDOM, 'style', { textContent: 'div { color: black; };' }); |
v6114 seems to have fixed CSP things quite a bit but... issue with |
Where is I cloned the codebase to try to find it in code, but no branch has been updated in over 3 years!!. tampermonkey/src/environment.js Lines 1053 to 1056 in 13ab3f2
@derjanb How am I supposed to discover capabilities or know what API's are available and how to use them? |
@DerekZiemba It was in fact only documented here: #881 (comment) 😉 I've added it to Tampermonkey's documenation. GM_addElement(tag_name, attributes), GM_addElement(parent_node, tag_name, attributes)Creates an HTML element specified by 'tag_name' and applies all given 'attributes' and returns the injected HTML element. If a 'parent_node' is given, then it is attached to it or to document head or body otherwise. For suitable 'attributes', please consult the appropriate documentation. For example: GM_addElement('script', {
textContent: 'window.foo = "bar";'
});
GM_addElement('script', {
src: 'https://example.com/script.js',
type: 'text/javascript'
});
GM_addElement(document.getElementsByTagName('div')[0], 'img', {
src: 'https://example.com/image.png'
});
GM_addElement(shadowDOM, 'style', {
textContent: 'div { color: black; };'
}); |
Hello, The Thanks |
For JavaScript modules, usage of // blocked by CSP
GM_addElement('script', {
type: 'module',
src: 'https://127.0.0.1:3000/my-script.js'
});
// not blocked, but throws syntax error since my-script.js is a module
GM_addElement('script', {
src: 'https://127.0.0.1:3000/my-script.js'
});
// works
GM_addElement('script', {
type: 'module',
textContent: 'console.log("hello")'
});
// blocked again
GM_addElement('script', {
type: 'module',
textContent: 'import "https://127.0.0.1:3000/my-script.js"'
}); The reason I need modules is that I'm using Vite's dev server, in which I can benefit from the hot reload feature, but it only emits modules instead of vanilla JavaScript. |
@guansss Please try |
@derjanb Thanks for the reply! However this does not seem to work, I've tried setting The userscript I used for test: // ==UserScript==
// @name New Userscript
// @version 0.1
// @match http://localhost:8080/*
// @grant GM_addElement
// @sandbox JavaScript
// ==/UserScript==
GM_addElement('script', {
type: 'module',
src: 'https://cdn.jsdelivr.net/npm/mitt@3.0.0/+esm'
}); The HTML source: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
http-equiv="Content-Security-Policy"
content="default-src 'self'; script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self';"
/>
</head>
</html> The error:
Chrome: 114.0.5735.134 |
You need to remove the CSP header/meta tag. See here https://github.com/lisonge/vite-plugin-monkey#csp the ways to do it |
@7nik Fully disabling CSP may be a security risk, and the risk also applies to everyone that is contributing to the userscript project and being required to do the same thing. And speaking of plugins, I'm as well developing a webpack plugin that actually bypasses CSP without any configuration needed. It's still WIP, but you can check it out here: webpack-monkey. |
From TM's point of view, there is nothing wrong: the element (script) is created, inserted in the DOM and even executed. The error happens only at fetching the code.
Dude, the whole Internet existed for two decades without CSP, and even more, most sites preferred HTTP until browsers started complaining about it. If you want to keep the CSP, you can set up a local proxy to alter the CSP header/meta tag to whatever you want. You can probably even edit hosts to open the site under the original domain. No, thanks. I have a project that used webpack at begging, but when I replaced it with rollup, it started to run and compile 5-10 times faster with smaller output (less dead code). And, if I remember right, webpack doesn't allow top-level-await, which I wanted. |
I'm trying to write a simple script that will replace elements on Github pages.
Expected Behavior
The element I target would be replaced and the JavaScript I'm trying to inject would be executed.
Actual Behavior
Getting a
Refused to load the script...because it violates the following Content Security Policy directive
error message in the console.Full error message
Specifications
Script
Tested in this repo: https://github.com/mingrammer/awesome-finder
The text was updated successfully, but these errors were encountered: