Maaz Zindani

Loading the index…

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

Browser blocks a Firebase Storage request at the CORS preflight

ErrorResponse to preflight request doesn't pass access control check: It does not have HTTP ok status

Short answer

A browser sends an OPTIONS preflight before some cross-origin requests, and Cloud Storage answers it only from the CORS configuration on the bucket. If the bucket has none, or it does not list your origin, method or headers, the browser blocks the call. Write a cors.json and apply it to the bucket.

  • Applies to Cloud Storage, Firebase Storage web SDK, gcloud CLI, Chrome
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the CORS configuration that is on the bucket now. An empty result means the bucket has none.

    gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"
  2. 2Match what you see, then jump to its fix

    1. 1The describe command above shows no cors_configThe bucket has no CORS configurationFix
    2. 2A configuration is listed, yet the browser still blocks the requestThe file exists, but origin, method or headers do not match the requestFix
    3. 3The configuration is correct but the URL host is storage.cloud.google.com, or you rely on the JSON APIThe request goes to an endpoint that does not read the bucket configurationFix

What does the error look like?

  • Response to preflight request doesn't pass access control check: It does not have HTTP ok status

Cause 1 of 3

The bucket has no CORS configuration

1Check

Without a matching configuration the XML API returns no CORS headers, and the browser fails the request.

gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"

2Fix

Create cors.json, then apply it. For plain downloads from the web the Firebase docs use a GET-only file; list your real origin instead of * if you can.

# cors.json
[
  {
    "origin": ["https://example.com"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

gcloud storage buckets update gs://BUCKET_NAME --cors-file=cors.json
# the Firebase docs use the older gsutil form:
gsutil cors set cors.json gs://BUCKET_NAME

3Verify

Describe the bucket again and see your origin in the output, then reload the page and repeat the request.

gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 2 of 3

The file exists, but origin, method or headers do not match the request

1Check

Compare the page's exact origin (scheme, host and port) and the request method with the file. A preflight is sent for methods other than GET, HEAD or POST, for unusual POST content types and for custom headers. If any of them is not allowed, the browser never sends the main request.

gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"

2Fix

Add the missing origin and method, and put every custom request header in responseHeader, then apply the file again.

[
  {
    "origin": ["https://example.com", "http://localhost:5173"],
    "method": ["GET", "PUT", "POST"],
    "responseHeader": ["Content-Type"],
    "maxAgeSeconds": 3600
  }
]

3Verify

Apply the file and watch the OPTIONS request in the browser's network tab: it now returns the allow headers.

gcloud storage buckets update gs://BUCKET_NAME --cors-file=cors.json

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 3 of 3

The request goes to an endpoint that does not read the bucket configuration

1Check

Only the XML API endpoints (storage.googleapis.com/BUCKET_NAME or BUCKET_NAME.storage.googleapis.com) apply the bucket CORS configuration. storage.cloud.google.com does not allow CORS requests at all.

# read the host in the failing request URL in the network tab

2Fix

Request the object through an XML API URL, as in the example below.

https://storage.googleapis.com/BUCKET_NAME/path/to/file.png

3Verify

The request now returns an Access-Control-Allow-Origin header for your origin.

curl -s -I -H "Origin: https://example.com" https://storage.googleapis.com/BUCKET_NAME/path/to/file.png

Confirmed by docs.cloud.google.com

Did this fix it?

How do I stop it coming back?

  • Keep cors.json in the repository and apply it from a documented step, so a new bucket does not go live without it.
  • List real origins, including your local dev address, instead of * where you can.
  • Re-read the bucket configuration after every change to the domain or port.

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

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Web apps

A Chrome extension content script cannot read another site's response

Access to fetch at 'https://api.example.com/price' from origin 'https://shop.example.net' has been blocked by CORS policy

Since Chrome 85 a content script's request is treated as a request from the web page it runs in, so the page's CORS rules apply and the extension's host permissions do not help.

3 causes, each with a fix

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

Next step

Want this handled for good?

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