If a browser throws CORS errors only when the request goes through Cloudflare, one of three things is happening: the edge cache stored a response with the wrong (or no) Access-Control-Allow-Origin header and is replaying it to every visitor, a security feature is blocking or challenging the OPTIONS preflight, or Cloudflare generated an error page of its own — which never carries CORS headers — and the browser reports that as a CORS failure instead of the real error. The fix is almost never “more CORS headers.” It is bypassing cache on the API path, letting preflights through, or reading past the console message to the actual status code.
What’s happening
CORS headers are just response headers, and Cloudflare caches responses with their headers. If your origin varies Access-Control-Allow-Origin by request Origin — the standard pattern for an allowlist — the first requester’s header gets frozen into the cached copy. Cloudflare does not use Vary: Origin to split the cache; apart from Accept-Encoding (and image format variants on some plans), the Vary header is not part of the cache key. So app-a.example.com warms the cache, app-b.example.com receives Access-Control-Allow-Origin: https://app-a.example.com, and the browser correctly refuses it. Direct to the origin, every request gets a fresh, correct header — which is exactly why the bug “only happens behind Cloudflare.”
Preflights fail differently. An OPTIONS preflight is sent by the browser with no cookies and no ability to execute JavaScript. If a WAF custom rule, rate limit, or bot feature answers it with a challenge or a 403, the preflight dies, and the browser reports the actual request as a CORS error. Cloudflare’s own error pages (522, 523, 1xxx, challenge interstitials) carry no Access-Control-Allow-Origin either, so any edge-generated error on a cross-origin call surfaces in the console as CORS, masking the real problem.
What to check first
- Does the same request succeed when sent directly to the origin, bypassing the proxy? If yes, keep reading. If it fails both ways, this is a plain origin misconfiguration.
- What does
cf-cache-statussay on the failing response?HITpoints at the stale-header problem. - Is the failing response even a 200? A 403 with a
cf-mitigated: challengeheader, or a 52x, is not a header problem at all. - Check Security » Events in the dashboard for blocked
OPTIONSrequests on the API hostname.
How to verify
Simulate a preflight and a real request with two different origins, and read the headers Cloudflare returns:
# Preflight as the browser would send it
curl -s -o /dev/null -D - -X OPTIONS "https://api.example.com/v1/data" \
-H "Origin: https://app-b.example.com" \
-H "Access-Control-Request-Method: POST" \
-H "Access-Control-Request-Headers: content-type"
# Same GET from two different origins - compare access-control-allow-origin
curl -sI "https://api.example.com/v1/data" -H "Origin: https://app-a.example.com" \
| grep -i "access-control\|cf-cache-status\|cf-ray\|vary"
curl -sI "https://api.example.com/v1/data" -H "Origin: https://app-b.example.com" \
| grep -i "access-control\|cf-cache-status\|cf-ray\|vary"
# Rule out the origin by talking to it directly
curl -sI --resolve api.example.com:443:<ORIGIN_IP> "https://api.example.com/v1/data" \
-H "Origin: https://app-b.example.com"
# Did the preflight even reach the origin?
tail -f /var/log/nginx/access.log | grep OPTIONS
If the second curl returns cf-cache-status: HIT with the first origin’s Access-Control-Allow-Origin value, you have the cached-header problem. If the OPTIONS call returns 403 or an HTML challenge page, a security rule is eating preflights. If the direct --resolve test misbehaves too, stop blaming Cloudflare.
The fix
- Bypass cache on the API paths. Create a Cache Rule (Rules » Cache Rules) matching
api.example.comor the/api/path prefix with Bypass cache. API responses that vary byOrigin,Authorization, or user should not be edge-cached in the first place. Purge the zone cache after saving so poisoned copies die immediately. - Fix the origin’s headers for cacheable endpoints. If some CORS’d content genuinely should be cached (fonts, public JSON), have the origin send a single stable
Access-Control-Allow-Originvalue — either*for public, credential-less resources, or one fixed origin. A per-request reflected value plus caching is the poison combination. Keep sendingVary: Originfor downstream caches, but do not expect Cloudflare to honour it; splitting the cache key by header is an Enterprise custom-cache-key feature, so on Free, Pro, and Business the honest answer is bypass or a fixed value. - Or set the headers at the edge with a Transform Rule. A Response Header Modification rule can set
Access-Control-Allow-Originstatically for one known frontend, or dynamically reflect the request origin when the rule’s filter already restricts which origins match. See the Transform Rules reference for the expression syntax, and the Rules docs for current per-plan rule quotas (Free zones get 10 rules per phase). - Let preflights through security features. Add a WAF skip rule for
OPTIONSrequests to the API path, or exempt the path from the rule that fires. Note the plan reality: Bot Fight Mode on Free has no exception mechanism — if it interferes, your options are turning it off or moving the API to a hostname it does not cover. - Make error responses carry CORS headers at the origin. Frameworks often attach CORS headers only on success, so a 500 shows up in the console as “CORS error.” You cannot do this for Cloudflare’s own 52x pages — which is one more reason to alert on status codes, not console text.
When this fix is wrong
If the failure reproduces against the origin directly, none of the above applies — fix the application’s CORS config. If only one user sees it, suspect a browser extension or corporate proxy. And if the response carries cf-mitigated: challenge, you have a WAF or bot-management tuning problem wearing a CORS costume; tune the security rule instead of touching headers.
Common mistakes
- Testing with curl and declaring CORS “fine.” curl does not enforce CORS; only browsers do. curl shows you the headers — the browser decides.
- Sending
Access-Control-Allow-Origin: *on credentialed requests. Browsers reject the wildcard whencredentials: includeis set. You need an explicit origin plusAccess-Control-Allow-Credentials: true. - Setting CORS headers at both origin and edge. Duplicate
Access-Control-Allow-Originheaders are an automatic browser rejection. Pick one owner. - Purging the cache without fixing the cause. The next visitor from the “wrong” origin re-poisons it within minutes.
Prevention
Keep API hostnames on Bypass cache as standing policy, decide once whether the origin or the edge owns CORS headers, and add a synthetic check that sends a real preflight from your least-common frontend origin. Review cache rules and cacheability whenever a new API path ships. If “is it Cloudflare or is it us” keeps eating your evenings, that triage is literally the job of 24/7 Monitoring & Response — book a monitoring and response review.