How to enable CORS on Firefox
Asked Answered
H

6

46

How can I allow CORS on Firefox?

I easily managed it on Chrome and Internet Explorer, but I am totally failing at it with Firefox. I edited the following about:config entry

security.fileuri.strict_origin_policy = false

This attempt has been posted several times here and is told on other sites too, but it doesn't have any effect. I read the Mozilla guide to same-origin policies:

Cross-Origin Resource Sharing (CORS)

but it just explains CORS and the related topics. A workaround to enable it on Firefox is not listed.

Is there a definitive solution?

PS: FORCECORS does not work either somehow...

Heavierthanair answered 26/8, 2014 at 11:33 Comment(1)
There is no simple answer when it comes to CORS. This website has all the information you need on both Server and Client side enable-cors.orgHighroad
S
32

Do nothing to the browser. CORS is supported by default on all modern browsers (and since Firefox 3.5).

The server being accessed by JavaScript has to give the site hosting the HTML document in which the JS is running permission via CORS HTTP response headers.


security.fileuri.strict_origin_policy is used to give JS in local HTML documents access to your entire hard disk. Don't set it to false as it makes you vulnerable to attacks from downloaded HTML documents (including email attachments).

Sensitive answered 26/8, 2014 at 13:42 Comment(3)
Thanks, I am trying this approach now, but I still can not access the web service on my tomcat. I tried the advanced example cited on the following link: tomcat.apache.org/tomcat-7.0-doc/config/filter.html#CORS_Filter/… Can you tell me what I still have to manage?Heavierthanair
Since you haven't provided any code that you are using to try to access the web server, nor have you quoted the error messages you get in the JavaScript console and nor have you quoted the HTTP requests and responses that JavaScript is making and receiving (and which are visible in the Net tab of your browser's developer tools) — no.Sensitive
The request is aborted so no response headers at all. The problem is only in firefox. not in chrome.Survey
P
10

It's only possible when the server sends this header: Access-Control-Allow-Origin: *

If this is your code then you can set up it like this (PHP):

header('Access-Control-Allow-Origin: *');
Pascasia answered 15/10, 2014 at 21:39 Comment(3)
just a warning note, adding Access-Control-Allow-Origin: * everywhere enables CORS for anyone and everyone. While you should have security measures in place whatever the case, if the API is only used by specific resources then you should limit which domains are allowed via a comma-separated-list instead of supplying *Nightshirt
This is insecure. Do not do this unless you are sure it is what you want.Stipule
I've done this with a simple python server but my browser still blocks it.Healthy
T
4

This Firefox add-on may work for you:

https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

It can toggle CORS on and off for development purposes.

Torose answered 27/7, 2018 at 18:38 Comment(1)
Another extension to do the trick: addons.mozilla.org/en-US/firefox/addon/…Tricotine
P
2

I was stucked with this problem for a long time (CORS does not work in FF, but works in Chrome and others). No advice could help. Finally, i found that my local dev subdomain (like sub.example.dev) was not explicitly mentioned in /etc/hosts, thus FF just is not able to find it and shows confusing error message 'Aborted...' in dev tools panel.

Putting the exact subdomain into my local /etc/hosts fixed the problem. /etc/hosts is just a plain-text file in unix systems, so you can open it under the root user and put your subdomain in front of '127.0.0.1' ip address.

Pettifogger answered 16/11, 2016 at 20:23 Comment(1)
probably describing how one "puts" a subdomain into /etc/hosts would be helpful. Best regardsCastorina
C
0

Very often you have no option to set up the sending server, so I changed the XMLHttpRequest.open call in my JavaScript code to a local get-file.php file where I have the following code in it:

<?php
  $file = file($_GET['url']);
  echo implode('', $file);
?>

The JavaScript code is doing this:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    // File content is now in the this.responseText
  }
};
xhttp.open("GET", "get-file.php?url=http://site/file", true);
xhttp.send();

In my case this solved the restriction/situation just perfectly. There isn't any need to hack Firefox or servers. Just load your JavaScript/HTML file with that small PHP file into the server and you're done.

Crooks answered 10/5, 2018 at 9:57 Comment(0)
C
0

CORS issue only on firefox

If you have a CORS issue on Firefox but not on other browsers you might need to enable Enterprise Roots certificates

  • go to the firefox page: about:config
  • then search for enable security.enterprise_roots.enabled
  • click on it a set it to true
Chalkstone answered 1/2 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.