guides · 2026-11-14
How to Fix CORS Errors Without Opening Your API to Every Website
Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team
The short answer
The CORS error is the browser protecting your users; '*' and reflecting any Origin both 'fix' it by switching the protection off — the reflected variant even lets any website make credentialed requests as your logged-in users. Replace both with an explicit allow-list of your own origins, and never combine credentials with a wildcard or a reflection.
Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.
What is Cross-Origin Resource Sharing (CORS) misconfiguration?
server headers that tell browsers which other sites may read your API's responses. A wildcard lets any site read public responses; reflecting the request's Origin with credentials lets any site read private, per-user responses — effectively deleting the same-origin policy for your API.
Why your AI keeps suggesting the wildcard
Every developer meets the red CORS console error; every AI model has read ten thousand forum answers whose accepted fix is Access-Control-Allow-Origin: *. It works — the error vanishes — because the protection has been removed. Vibe-Code Audit flags both the literal wildcard and the more dangerous reflected variant (the header set from the request's Origin value) plus framework config like origins='*'. The reflection pattern is the one generated code produces when a slightly better tutorial says 'echo the origin back so credentials work' — which is precisely the configuration that must never exist.
What an attacker actually gains
With a wildcard and no credentials, the exposure is limited to data your API gives unauthenticated callers anyway. With reflection (or a wildcard on a cookie-authenticated API that a framework 'helpfully' upgrades), any website your user visits can fetch their account data, settings and tokens from your API using their live session, and read the responses. That is a confidentiality breach with no foothold needed — the victim's browser does the work. Distinguish the two cases before panicking or relaxing: check whether Access-Control-Allow-Credentials: true coexists with anything but a fixed allow-list.
The allow-list pattern, done once
Enumerate the exact origins your frontend uses — production domain, www variant if separate, staging if it must call production APIs (prefer it does not) — and have the server compare the Origin header against that list, emitting the matched origin only. Everything else gets no CORS headers, which browsers read as 'no'. In Express, cors({ origin: allowlist, credentials: true }); in FastAPI, CORSMiddleware with the explicit list; in raw configs, a map, never a regex that matches substrings of attacker domains (evil-example.com must not pass a check for example.com — anchor the full string).
Local development gets its own entry (http://localhost:3000) in a development-only config, so the wildcard never hitchhikes to production inside an environment default.
Verify from the outside
Test with curl: send Origin: https://evil.example and confirm the response carries no Access-Control-Allow-Origin for it (or none at all); send your real origin and confirm it is echoed with credentials if you need them. A scanner's live and code checks catch the static shapes; the curl test catches framework behaviour — some stacks reflect origins dynamically in ways source patterns miss. Re-run both after any framework upgrade or config refactor.
Preflight requests and the errors developers actually see
Most 'CORS is broken' reports are preflight failures: before a cross-origin request with custom headers or a JSON content type, the browser sends an OPTIONS probe, and if the server does not answer it with the right allow-headers and allow-methods, the real request never happens. Opening the origin to '*' appears to fix preflights too, which is why the wildcard survives code review — it fixes the visible error at every layer simultaneously by removing the policy at every layer. When you move to an allow-list, test the preflight explicitly: send OPTIONS with your real origin and the headers your frontend actually uses, and confirm the response names them. A correct origin list with a forgotten allow-header produces the same red console error as no CORS at all, and teams under time pressure 'fix' that by reverting to the wildcard. Verify the whole handshake once, write it down as a test, and the configuration stops being folklore that only one deploy ever got right.
Worked example: Willow & Rye, a bakery ordering platform
Willow & Rye (fictional), taking about 480 orders a week, had CORS errors during its launch week and shipped the fix a tutorial suggested: reflect any Origin with credentials enabled. A code scan flagged 'CORS reflected origin' as HIGH. A proof-of-concept page on another domain, opened while logged in, read the victim's order history and saved addresses from the API. The platform moved to a three-origin allow-list (storefront, admin, localhost in dev only), verified with curl from allowed and hostile origins, and the re-scan cleared both the reflection and an old wildcard on a static route nobody remembered adding.
Five actions to take today
- Grep your codebase and configs for Access-Control-Allow-Origin set to '*' or to a request-derived value.
- Determine whether credentials are enabled anywhere near those rules — that combination is the critical one.
- Replace wildcard/reflection with an explicit allow-list of full origins your frontend actually uses; anchor matches to complete strings.
- Move localhost development origins into a dev-only config so they never ship in production defaults.
- Verify with curl using a hostile Origin and your real Origin, then re-scan the code to confirm no wildcard survives.
Sources
- MDN Web Docs — Cross-Origin Resource Sharing (CORS), Mozilla's reference for the CORS headers and preflight mechanics: developer.mozilla.org, 2026
- OWASP Top 10, the OWASP Foundation's standard awareness list of the most critical web application security risks: owasp.org, 2021
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command, MITRE's weakness entry for SQL injection and its mitigations: cwe.mitre.org, 2026
Frequently asked questions
Is Access-Control-Allow-Origin: * ever acceptable?
For a genuinely public, unauthenticated, read-only API (public data anyone may fetch), yes. The moment responses depend on who is asking — cookies, tokens, accounts — '*' is the wrong answer.
Does CORS protect my API from non-browser attackers?
No. CORS governs browsers only; curl ignores it. It protects your users' browsers from being used as confused deputies — which is exactly the attack that matters for session-based APIs.
Why did reflecting the origin seem to work so well?
Because it tells every browser 'this origin is trusted' about whichever origin asked. It is an allow-list that allows everyone — indistinguishable from correct behaviour until someone hostile asks.
My mobile app has no Origin. Will an allow-list break it?
Non-browser clients do not send Origin and are unaffected by CORS headers. The allow-list only shapes what browsers may read; your app keeps working.
Related guides
- Debug Mode in Production: What One Stack Trace Hands an Attacker
- Why AI-Generated Login and API Endpoints Ship Without Rate Limits
- GraphQL Introspection and Batching: The Two Settings AI Starters Leave On
- Prototype Pollution in JavaScript: How One JSON Body Poisons Every Object
- Path Traversal: How a Download Endpoint Serves Files It Never Meant to Share
- Command Injection: shell=True, os.system and child_process.exec in Generated Code
- 7 Security Mistakes AI Coders Keep Making
- Supply-Chain Attacks in the AI-Coding Era
- Secret Hygiene: Stop API Key Leaks
- What Code Scanners Can't Find
- Cursor-Generated Code Review Checklist
- Bolt.new Pre-Deploy Security Checklist
Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.
Informational explainer, not a security guarantee. Scan your code free before every deploy.