Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Firefox blocks a cross-origin request even though the CORS headers are set

ErrorCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

Short answer

Firefox puts a Reason in the console message, and that text is the diagnosis. With headers present, the usual causes are two Access-Control-Allow-Origin values, an origin that does not match exactly, a preflight that lacks a header the request sends, a cross-origin redirect, or a request that never completed.

  • Applies to Firefox current, nginx 1.26, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the Reason in the Firefox console and send the same request with curl while pretending to be your site. The headers in the output show which section below applies.

    curl -i -H "Origin: https://app.example.com" https://api.example.com/resource
  2. 2Match what you see, then jump to its fix

    1. 1Reason: Multiple CORS header 'Access-Control-Allow-Origin' not allowedTwo Access-Control-Allow-Origin headers are sentFix
    2. 2Reason: CORS header 'Access-Control-Allow-Origin' does not matchThe allowed origin does not match the page originFix
    3. 3Reason: missing token 'authorization' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channelThe preflight answer does not list a header the request sendsFix
    4. 4Reason: CORS request external redirect not allowedThe URL redirects to another originFix
    5. 5Reason: CORS request did not succeedThe request never completed, so no headers arrivedFix

What does the error look like?

  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource
  • Reason: Multiple CORS header 'Access-Control-Allow-Origin' not allowed
  • Reason: CORS header 'Access-Control-Allow-Origin' does not match
  • Reason: missing token in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel
  • Reason: CORS request did not succeed

Cause 1 of 5

Two Access-Control-Allow-Origin headers are sent

1Check

The app and a proxy, load balancer or CDN each add the header, or a CORS middleware is registered twice. The curl output shows the header twice.

curl -sI -H "Origin: https://app.example.com" https://api.example.com/resource | grep -i access-control-allow-origin

2Fix

Let exactly one layer set it. In nginx, drop the backend's copy before adding your own.

location /api/ {
    proxy_hide_header Access-Control-Allow-Origin;
    add_header Access-Control-Allow-Origin "https://app.example.com" always;
    proxy_pass http://backend;
}

3Verify

Run the curl command again and confirm one line.

curl -sI -H "Origin: https://app.example.com" https://api.example.com/resource | grep -ic access-control-allow-origin

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 5

The allowed origin does not match the page origin

1Check

An origin is scheme, host and port. A trailing slash, http versus https, or a different port makes the value wrong, and so does a list of several origins in one header.

# compare the page's origin with the header value
console.log(location.origin)

2Fix

Echo back the request's Origin when it is on your allow list, and add Vary: Origin.

const allowed = ["https://app.example.com", "http://localhost:3000"];
app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (allowed.includes(origin)) {
    res.setHeader("Access-Control-Allow-Origin", origin);
    res.setHeader("Vary", "Origin");
  }
  next();
});

3Verify

Repeat the curl command and compare the header with the Origin you sent.

curl -sI -H "Origin: https://app.example.com" https://api.example.com/resource | grep -i access-control-allow-origin

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 5

The preflight answer does not list a header the request sends

1Check

A request with Authorization, a custom header or a JSON content type triggers an OPTIONS preflight. Every header the request sends must be in the preflight's Access-Control-Allow-Headers, and the OPTIONS call needs a successful response.

curl -i -X OPTIONS -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: authorization,content-type" https://api.example.com/resource

2Fix

Add the missing header names to the allowed list, and make sure OPTIONS is answered before any auth middleware.

app.use(cors({
  origin: "https://app.example.com",
  allowedHeaders: ["Content-Type", "Authorization"],
}));

3Verify

Repeat the OPTIONS call and look for the header names in the response.

curl -si -X OPTIONS -H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: authorization" https://api.example.com/resource | grep -i allow-headers

Confirmed by developer.mozilla.org

Did this fix it?

Cause 4 of 5

The URL redirects to another origin

1Check

A CORS request that receives a 301, 307 or 308 redirect to a different origin is blocked.

curl -sI https://service.example.com/data | grep -i -E "^HTTP|^location"

2Fix

Call the final URL the redirect points to, so the request no longer redirects.

const res = await fetch("https://other.example.net/data");

3Verify

The request returns without a redirect and the data is readable.

curl -sI https://other.example.net/data | head -n 1

Confirmed by developer.mozilla.org

Did this fix it?

Cause 5 of 5

The request never completed, so no headers arrived

1Check

This is a network-level failure, not a header problem. Ad blockers and privacy tools, DNS or connection errors, a bad certificate, mixed content, or a server that answers the preflight but not the real request all cause it.

# Firefox DevTools > Network: look at the failed request's status and security tab

2Fix

Try a private window with extensions off, fix any certificate or mixed-content problem, and check the server answers the real request.

# open a private window, then retry the request

3Verify

The request now shows a status code in the Network panel.

# reload and check the Network panel

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Set CORS headers in one place only, and write the allowed origins as a list in code.
  • Test with curl and an Origin header before testing in a browser.
  • Answer OPTIONS requests before authentication runs.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The cors-anywhere.herokuapp.com demo proxy stopped working

Missing required request header. Must specify one of: origin,x-requested-with

The public cors-anywhere.herokuapp.com server is only a demo, is rate limited, and since February 2021 requires an opt-in.

2 causes, each with a fix

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Web apps

CORS fails when you open an HTML file straight from disk

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///

A page opened from disk has an opaque origin, so browsers treat every request it makes as cross-origin, and CORS only works over http or https.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.