ChatGPT解决这个技术问题 Extra ChatGPT

Disable firefox same origin policy

I'm developing a local research tool that requires me to turn off Firefox's same origin policy (in terms of script access, I don't really care about cross domain requests).

More specifically, I want scripts in the host domain to be able to access arbitrary elements in any iframes embedded in the page, regardless of their domain.

I'm aware previous Q&As which mentioned the CORS FF extension, but that is not what I need, since it only allows CORS, but not script access.

If it cannot be done easily, I would also appreciate any insights that point me to specific part of FF src code that I can modify to disable SOP, so that I can recompile FF.

It would be an interesting thing with developers. Since the same origin policy is designed for the security of the users and not the developers, it should be made possible to allow the scripts from the given site to go across the restrictions. But developers are also people, so you could loose your personal information as well.
I believe it's not possible right now, here is related bug report in Firefox Bugzilla: bugzilla.mozilla.org/show_bug.cgi?id=1039678
Only good solution is to inject the headers by plugin based on domains: stackoverflow.com/a/44093160/956397 Everthing else is insecure...

G
German Lashevich

There's a Firefox extension that adds the CORS headers to any HTTP response working on the latest Firefox (build 36.0.1) released March 5, 2015. I tested it and it's working on both Windows 7 and Mavericks. I'll guide you throught the steps to get it working.

1) Getting the extension

You can either download the xpi from here (author builds) or from here (mirror, may not be updated).

Or download the files from GitHub. Now it's also on Firefox Marketplace: Download here. In this case, the addon is installed after you click install and you can skip to step 4.

If you downloaded the xpi you can jump to step 3. If you downloaded the zip from GitHub, go to step 2.

2) Building the xpi

You need to extract the zip, get inside the "cors-everywhere-firefox-addon-master" folder, select all the items and zip them. Then, rename the created zip as *.xpi

Note: If you are using the OS X gui, it may create some hidden files, so you 'd be better using the command line.

3) Installing the xpi

You can just drag and drop the xpi to firefox, or go to: "about:addons", click on the cog on the top right corner and select "install add on from file", then select you .xpi file. Now, restart firefox.

4) Getting it to work

Now, the extension won't be working by default. You need to drag the extension icon to the extension bar, but don't worry. There are pictures!

Click on the Firefox Menu

Click on Customise

https://i.stack.imgur.com/VSspG.png

Drag CorsE to the bar

Now, click on the icon, when it's green the CORS headers will be added to any HTTP response

https://i.stack.imgur.com/ead0z.png

5) Testing if it's working

jQuery

$.get( "http://example.com/", function( data ) {
  console.log (data);
});

JavaScript

xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        console.log(xmlhttp.responseText);
    }
}

xmlhttp.open("GET","http://example.com/");
xmlhttp.send();

6) Final considerations

Note that https to http is not allowed.

There may be a way around it, but it's behind the scope of the question.


You can disable HTTP/HTTPS mixed content protection by setting security.mixed_content.block_active_content to false and security.mixed_content.block_display_content to true. Keep in mind you are disabling some security and this should be a temporary solution.
As the author of this addon, I'm not actually convinced it would solve this particular question. It's nice to get a mention though.
@spenibus - you should get your add on signed - I can't install it :( - support.mozilla.org/en-US/kb/…
@PeterAjtai Mozilla keeps on trying hard to annoy me I see. Awaiting review: addons.mozilla.org/en-US/firefox/addon/cors-everywhere Should hopefully get auto signed.
in about:config set xpinstall.signatures.required to false to install the addon. It worked for me.
N
Niklas
about:config -> security.fileuri.strict_origin_policy -> false

Thanks @Niklas, however, i think this only disables fileuri same origin policy checks - probably used for local web dev testing. It still stops me when I'm trying to access DOM nodes in an iframe with domain B from a JavaScript in domain A.
this doesn't do anything
Confirmed it does work in my firefox (developer) version: 40. Thank you for the tip @Niklas.
This is a setting specifically for debugging, and controls a local files access to other local files (set to true a local file can only access local files in the same folder or sub folders, set to false a local file can access all local files). Source
It does do something, in my case it allows me to access local resources from a document served over file:// protocol. Computer scientists ought to put more weight into the word "anything" -- unless you have tested everything (which you haven't), try to be more conservative with your remarks. Same goes for use of word "useless".
Y
Yuchen Zhou

I realized my older answer is downvoted because I didn't specify how to disable FF's same origin policy specifically. Here I will give a more detailed answer:

Warning: This requires a re-compilation of FF, and the newly compiled version of Firefox will not be able to enable SOP again.

Check out Mozilla's Firefox's source code, find nsScriptSecurityManager.cpp in the src directory. I will use the one listed here as example: http://mxr.mozilla.org/aviarybranch/source/caps/src/nsScriptSecurityManager.cpp

Go to the function implementation nsScriptSecurityManager::CheckSameOriginURI, which is line 568 as of date 03/02/2016.

Make that function always return NS_OK.

This will disable SOP for good.

The browser addon answer by @Giacomo should be useful for most people and I have accepted that answer, however, for my personal research needs (TL;won't explain here) it is not enough and I figure other researchers may need to do what I did here to fully kill SOP.


Line 499 as of today, Git mirror: github.com/mozilla/gecko-dev/blob/…
T
Tan Mai Van

I wrote an add-on to overcome this issue in Firefox (Chrome, Opera version will have soon). It works with the latest Firefox version, with beautiful UI and support JS regex: https://addons.mozilla.org/en-US/firefox/addon/cross-domain-cors

https://i.stack.imgur.com/fU2fa.png


Thanks you. Additionally, there's also CORS-Everywhere Extension which is similar.
It's not working. I tried enabling it and I still get an error saying "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource".
K
Khado Mikhal

As of September 2016 this addon is the best to disable CORS: https://github.com/fredericlb/Force-CORS/releases

In the options panel you can configure which header to inject and specific website to have it enabled automatically.

https://i.stack.imgur.com/kLFrb.png


From wiki.mozilla.org/Add-ons/Extension_Signing: Firefox 48: Release and Beta versions of Firefox for Desktop will not allow unsigned extensions to be installed, with no override. I could not find a signed version of this addon.
@FelixM Here's how to do it: ghacks.net/2016/08/14/…
@FelixM Firefox Developer Edition has an option "xpinstall.signatures.required" boolean in the "about:config" flags. However, version 0.1.1 of this extension is not compatible with Firefox Developer Edition 58.0 (Quantum).
o
ob.yann

The cors-everywhere addon works for me until Firefox 68, after 68 I need to adjust 'privacy.file_unique_origin' -> false (by open 'about:config') to solve 'CORS request not HTTP' for new CORS same-origin rule introduced.

NOTE: 12/2021 updated. Since firefox 95 the 'CORS request not HTTP' can't be disabled by adjusting 'privacy.file_unique_origin'. See above 'CORS request not HTTP' link, it already updated by official rencently. The only way for me is '....Developers who need to perform local testing should now set up a local server.'


A
Amar T

For me worked by setting content.cors.disable to false


d
double-beep

In about:config add content.cors.disable (empty string).


Has this been tested? From what I'm reading, this pref was designed to make all CORS requests fail when set to true, but says nothing about false or other values. "In Firefox, the preference that disables CORS is content.cors.disable. Setting this to true disables CORS, so whenever that's the case, CORS requests will always fail with this error." developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/…
As of Firefox 68.7 this setting is not even available.
Found this setting in FF 84 but it didn't help me overcome my issue of FF wanting valid CORS policy to foreign server.
in FF85 content.cors.disable exists, but its boolean, remove/edit is not possible