-
Notifications
You must be signed in to change notification settings - Fork 480
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
[Firefox] Can't modify page properties on sites which use CSP #997
Comments
Incidentally, I'd prefer to use Ditto |
AFAIK Tampermonkey rewrites the CSP response header to allow its script elements. We could do it as well but it seems quite radical, I'm not even sure Mozilla will allow it in the future, they dislike addons rewriting CSP last time I heard.
// ==UserScript==
// @name test wrappedJSObject
// @match https://www.google.com/*
// @grant GM_foo
// @run-at document-start
// @inject-into content
// ==/UserScript==
const pageWnd = unsafeWindow.wrappedJSObject;
const p = pageWnd.XMLHttpRequest.prototype;
const {open} = p;
const xhrOpen = function (method, url) {
console.warn('intercepted', url);
return open.apply(this, arguments);
}
p.open = exportFunction(xhrOpen, pageWnd); |
Don't forget to use exportFunction or cloneInto with cloneFunctions:true option. |
This ended up being quite fiddly to get working. While I appreciate the fact that a workaround exists, I still think this is a bug and that For anyone else who needs a working example of this in action, see here. In particular, note that getting This doesn't workconst descriptor = cloneInto({ value: json }, unsafeWindow.wrappedJSObject)
Object.defineProperty(xhr, 'responseText', descriptor) This worksconst descriptor = cloneInto({ value: json }, unsafeWindow.wrappedJSObject)
unsafeWindow.wrappedJSObject.Object.defineProperty(xhr, 'responseText', descriptor) |
We couldn't make
Yep. Every global builtin such as |
Legacy scripts expect
That's why I think this bug should remain open until it's either fixed in Firefox, or Violentmonkey for Firefox migrates to the new userscript API. |
The problem was that if such code overrides a standard window property via unsafeWindow it will completely break a lot of pages which actually happened last time we tried. |
Then call it
|
Tampermonkey modifies CSP of the sites which is rather intrusive. I'm not convinced we should do the same as I explained above. Greasemonkey 3 doesn't run anymore in modern Firefox 57+. Greasemonkey 4 doesn't care about compatibility with the older scripts so we can't follow suit. Anyway AFAIK there aren't that many scripts that are affected by this predicament as people don't normally use the |
In this case, there is no need to clone the |
this isn't needed as it's not accessed directly: violentmonkey/violentmonkey#997 (comment)
Thanks! Unfortunately, that doesn't appear to work for me in this case (install), but I can't see an error message (the script just hangs), so it's possible I've overlooked something. Do you have an example of it working without the clone?
Where does it say that?
I recommend that this is provided by the userscript engine as |
Use
This conclusion is drawn from the documentation and my observations. |
Yeah, that doesn't work either (install). It doesn't modify the XHR object in the page (why would it?).
I look forward to seeing a working example to confirm this. |
You must export this function, because it is called by xhr, which is in the page scope.
Something like that?
|
We should take this to my repo rather than further derailing this issue (if you have a particular fix or feature in mind), but, to be clear, that function (and the userscript) works fine without that, and doesn't work with your suggested changes. As for the example you've given, I don't see any difference between that and assigning directly to unsafeWindow.wrappedJSObject.console.log = func - which is what the script already does, e.g.: unsafeWindow.wrappedJSObject.XMLHttpRequest.prototype.send = func |
@chocolateboy How to determine that your script is working properly?
Try execute |
@chocolateboy I apologize for misleading you. It seems that your method is the only working one, although it has a strange implementation. |
This is my abiding issue with Violentmonkey (which I ❤️ - thank you!), but I can't see an open issue for it. There are related closed issues, and this issue may point the way to a fix, but I thought it'd be better to track the bug explicitly rather than inferring it from the documentation[1] and scattered comments.
What is the problem?
It's not possible to modify/mutate direct or nested properties of a page's window object with the following combination:
Sites which use CSP don't run in Violentmonkey for Firefox unless
@inject-into content
is enabled, but@inject-into content
is not compatible with@grant none
, which is needed to modify page objects.Userscript engines this works in:
Userscript engines this doesn't work in:
How to reproduce it?
What is the expected result?
XHR#open should be hooked and the message should be logged on those sites.
What is the actual result?
XHR#open isn't hooked and the message isn't logged.
Related issues
Environment
Footnotes
@inject-into content
] mode."@grant none
."@grant none
isn't supported.unsafeWindow
is but I couldn't get the XHR#open hook to work.The text was updated successfully, but these errors were encountered: