Closed
Bug 1295660
Opened 8 years ago
Closed 7 years ago
Content Script XHR and fetch do not set Origin and Referer to the current content page
Categories
(WebExtensions :: Request Handling, defect, P3)
WebExtensions
Request Handling
Tracking
(Not tracked)
RESOLVED
FIXED
mozilla58
People
(Reporter: bugzilla.mozilla.org, Assigned: rpl)
References
Details
(Keywords: dev-doc-complete, Whiteboard: [design-decision-needed]triaged)
Attachments
(1 file)
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
(deleted),
text/x-review-board-request
|
mixedpuppy
:
review+
|
Details |
Some sites only allow XHR/fetch to retrieve content if correct Referer and Origin headers are set. I'm not referring to CORS restrictions but to server-side deep-linking restrictions.
The page's own XHR is able to issue valid requests, but content scripts associated with the same page are not because they use a different XHR object which sets no referer and a null origin.
This makes the content script XHR less versatile than the untrusted page's XHR.
Comment 1•8 years ago
|
||
I'm not sure that setting the content page as the referrer is the correct behavior here. My gut instinct would be to set the extension script as the origin and referrer by default, but perhaps to allow the extension to override them with the values for the content page, if they so choose.
Component: WebExtensions → WebExtensions: Request Handling
Summary: Content Script XHR and fetch do not set correct Referer and Origin headers → Content Script XHR and fetch do not set Origin and Referer to the current content page
Whiteboard: [design-decision-needed]
I don't think extension-script origin makes much sense for content scripts. It's more reasonable choice for the background page.
Page scripts often want to "intrude" on external APIs and disguise themselves as something allowed to access them, which means need to satisfy server-side checks (user authentication, xsrf protection, deep-linking protection etc.) as if they came from whatever page they would normally come from.
Anyway, an override would be good, but the current default "Origin: null" is quite bad, even no origin header would be preferable.
Updated•8 years ago
|
Priority: -- → P3
Whiteboard: [design-decision-needed] → [design-decision-needed]triaged
Comment 3•8 years ago
|
||
This has been added to the agenda for the May 30 WebExtensions Triage meeting at 10:30am PT.
Call in info: https://wiki.mozilla.org/Add-ons/Contribute/Triage#Details_.26_How_to_Join
Agenda: https://docs.google.com/document/d/1hKKRpGFIaAaI3G_HfPX2Nk8pCchyhUIKJB9y5sIrVV4/edit
Also note that the patching of the xrayed window object[0] makes it difficult to safely obtain an xrayed version of the page's XHR instance. I.e. one can't use that as workaround to send the correct referrer and origin.
[0] http://searchfox.org/mozilla-central/source/toolkit/components/extensions/ExtensionContent.jsm#418-422
Updated•8 years ago
|
Flags: needinfo?(lgreco)
Comment hidden (mozreview-request) |
Assignee | ||
Comment 6•7 years ago
|
||
Comment on attachment 8874891 [details]
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
I've investigated this issue a bit and, as anticipated in Comment 4, in Bug 1284020 we have started to overwrite the window.XMLHttpRequest and window.fetch properties accessible from a content script with the related privileged globals (which are coming from the content script "expanded principal" sandbox and so they can successfully retrieve data from a server that doesn't allow the website in its CORS headers), and by doing so we have prevented the content script to easily makes requests that looks exactly like the ones that the webpage itself can create.
In my opinion it would be more than reasonable that a content script can creates such requests but it would be probably more reasonable to give it access to the original webpage window.XMLHttpRequest and window.fetch properties than creating a totally new feature like allowing to explicitly override the request headers through some kind of new API methods and/or options.
I've attached a patch that does exactly this ("keep a copy of the webpage window.XMLHttpRequest and window.fetch as pageXMLHttpRequest and pageFetch and ensures that the content script can use it and creates requests with the expected headers"), but I'd like to get an initial feedback on the proposed approach from Shane, before moving it to an actual "review" stage.
Flags: needinfo?(lgreco)
Attachment #8874891 -
Flags: feedback?(mixedpuppy)
Comment hidden (mozreview-request) |
Assignee | ||
Updated•7 years ago
|
Attachment #8874891 -
Flags: feedback?(mixedpuppy)
Comment 8•7 years ago
|
||
mozreview-review |
Comment on attachment 8874891 [details]
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
https://reviewboard.mozilla.org/r/146266/#review150382
::: toolkit/components/extensions/ExtensionContent.jsm:433
(Diff revision 1)
> + window.pageXMLHttpRequest = window.XMLHttpRequest;
> + window.pageFetch = window.fetch;
> window.JSON = JSON;
> window.XMLHttpRequest = XMLHttpRequest;
> window.fetch = fetch;
> `, this.sandbox);
The patch is fine, but I really don't like this.
IIUC, before bug 1284020 in content scripts we had:
Fetch & XMLHttpRequest -> sandbox variant presumably allowing cross-domain (closer to a system xhr?)
window.fetch & window.XMLHttpRequest -> the web pages variant these
bug 1284020 changed both to be the sandbox variant, resulting in no access to the webpage xhr from the content script. This was done because jquery does this (the reporter likely didn't understand the difference between window.XMLHttpRequest and XMLHttpRequest):
jQuery.ajaxSettings.xhr = function() {
try {
return new window.XMLHttpRequest();
} catch ( e ) {}
};
Now we want to store the original window.XMLHttpRequest in a non-standard way.
I'm beginning to feel like bug 1284020 was not the right fix, that jquery was the problem there. I'm not certain we should be trying to make jquery work this way. I'm more inclined to backout 1284020 than add another xhr/fetch variable into the mix.
Comment 9•7 years ago
|
||
Kris, I'm curious what you think about backing out bug 1284020 per my comments above.
Flags: needinfo?(kmaglione+bmo)
Updated•7 years ago
|
Attachment #8874891 -
Flags: feedback?(mixedpuppy)
Comment 10•7 years ago
|
||
(In reply to Shane Caraveo (:mixedpuppy) from comment #9)
> Kris, I'm curious what you think about backing out bug 1284020 per my
> comments above.
I'd prefer not to modify window.XMLHttpRequest, but I don't think we can revert that change without causing a lot of breakage. And after bug 1208775 is fixed, there won't be a distinction between `window.XMLHttpRequest` and the global XMLHttpRequest, so it would only be a short term fix at best.
Flags: needinfo?(kmaglione+bmo)
Comment 11•7 years ago
|
||
(In reply to Kris Maglione [:kmag] (busy; behind on reviews) from comment #10)
> (In reply to Shane Caraveo (:mixedpuppy) from comment #9)
> > Kris, I'm curious what you think about backing out bug 1284020 per my
> > comments above.
>
> I'd prefer not to modify window.XMLHttpRequest,
We wouldn't be modifying it, we'd be removing the modification of it that was made.
> but I don't think we can
> revert that change without causing a lot of breakage. And after bug 1208775
> is fixed, there won't be a distinction between `window.XMLHttpRequest` and
> the global XMLHttpRequest, so it would only be a short term fix at best.
What breakage other than 3rd party libraries?
In any case though, given bug 1208775 a different solution would be necessary, 15 months though since that's been touched. Should that be a we+ bug?
Straw man. Anything we overwrite (ie. JSON, XHR, Fetch) could be added to a content object.
Cu.evalInSandbox(`
content = {
XMLHttpRequest: window.XMLHttpRequest,
fetch: window.fetch,
JSON: window.JSON,
}
window.JSON = JSON;
window.XMLHttpRequest = XMLHttpRequest;
window.fetch = fetch;
`);
Would that survive changes from bug 1208775?
Flags: needinfo?(kmaglione+bmo)
Comment hidden (mozreview-request) |
Comment 13•7 years ago
|
||
mozreview-review |
Comment on attachment 8874891 [details]
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
https://reviewboard.mozilla.org/r/146266/#review193294
::: toolkit/components/extensions/test/mochitest/file_page_xhr.html:28
(Diff revision 3)
> + });
> +
> + postMessage({
> + type: "testPageGlobals",
> + hasPageXMLHttpRequest: !!window.pageXMLHttpRequest,
> + hasPageFetch: !!window.pageFetch,
these seem usused
Comment hidden (mozreview-request) |
Assignee | ||
Comment 15•7 years ago
|
||
mozreview-review-reply |
Comment on attachment 8874891 [details]
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
https://reviewboard.mozilla.org/r/146266/#review193294
> these seem usused
yep, it is not needed anymore, thanks for catching it (I should have run a grep on the rest of the patch after removing the not needed assertions in the test).
I've removed it in the updated patch (and I've also applied another minor tweak to the test, to fix the eslint errors related to accessing the `content` object from the content script)
Comment 16•7 years ago
|
||
mozreview-review |
Comment on attachment 8874891 [details]
Bug 1295660 - Content Scripts should be able to make requests that looks as coming from the webpage.
https://reviewboard.mozilla.org/r/146266/#review195168
Attachment #8874891 -
Flags: review+
Updated•7 years ago
|
Flags: needinfo?(kmaglione+bmo)
Assignee | ||
Updated•7 years ago
|
Assignee: nobody → lgreco
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Comment 17•7 years ago
|
||
Pushed by luca.greco@alcacoop.it:
https://hg.mozilla.org/integration/autoland/rev/7656b78bbc44
Content Scripts should be able to make requests that looks as coming from the webpage. r=mixedpuppy
Comment 18•7 years ago
|
||
bugherder |
Status: ASSIGNED → RESOLVED
Closed: 7 years ago
status-firefox58:
--- → fixed
Resolution: --- → FIXED
Target Milestone: --- → mozilla58
Assignee | ||
Updated•7 years ago
|
Keywords: dev-doc-needed
Comment 19•7 years ago
|
||
Is manual testing required on this bug? If Yes, please provide some STR and the proper webextension(if required), if No set the “qe-verify-“ flag.
Flags: needinfo?(lgreco)
Assignee | ||
Updated•7 years ago
|
Flags: needinfo?(lgreco) → qe-verify-
Comment 20•7 years ago
|
||
I wasn't sure where to put this, but the content scripts guide seemed like the best place: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#XHR_and_Fetch.
Please let me know if we need anything else.
Flags: needinfo?(lgreco)
Assignee | ||
Comment 21•7 years ago
|
||
Thanks will, the new section linked above looks correctly describes the behavior and how to choose when extension content scripts may want (or need) to use content.XMLHttpRequest or content.fetch instead of window.XMLHttpRequest or window.fetch.
Flags: needinfo?(lgreco)
Updated•6 years ago
|
Product: Toolkit → WebExtensions
Updated•6 years ago
|
status-firefox58:
fixed → ---
Keywords: dev-doc-needed → dev-doc-complete
Updated•5 years ago
|
You need to log in
before you can comment on or make changes to this bug.
Description
•