Is Access-Control-Allow-Origin: * a security risk in my API?
What CORS does, and what it does not
MDN explains that browsers restrict cross-origin HTTP requests initiated from scripts, and that CORS headers are how a server relaxes that. It is a browser rule. PortSwigger's Web Security Academy puts it plainly: CORS "is never a replacement for server-side protection of sensitive data."
On the wildcard, MDN says: "For requests without credentials, the literal value * can be specified as a wildcard. Attempting to use the wildcard with credentials results in an error." So * alone cannot leak a logged-in user's cookie-based data. It is right for genuinely public APIs.
The pattern that is actually dangerous
Vulnerable: reflect any origin and allow cookies
// Express with the cors package
app.use(cors({ origin: true, credentials: true }));
// origin: true echoes back whatever Origin the request sent
Fixed: an explicit allowlist
app.use(cors({
origin: ["https://myapp.com", "https://www.myapp.com"],
credentials: true,
}));
Next.js route handler: set the header only for your own origin
const ALLOWED = new Set(["https://myapp.com", "https://www.myapp.com"]);
export async function GET(req: Request) {
const origin = req.headers.get("origin") ?? "";
const headers = new Headers({ Vary: "Origin" });
if (ALLOWED.has(origin)) headers.set("Access-Control-Allow-Origin", origin);
return Response.json(await loadData(req), { headers });
}
PortSwigger says dynamically reflecting origins from cross-origin requests without validation "is readily exploitable and should be avoided." With cookies allowed, any website the user visits can read their data from your API. MDN also advises sending Vary: Origin when the header depends on the request, and warns that the null origin value should be avoided.
The cors package for Express defaults to origin: "*" when you call cors() with no options. That is fine for a public API and a sign to check further when the API serves private data.
Check your API, step by step
- Find every place that sets CORS:
grep -rnE "Access-Control-Allow-Origin|cors\(" --exclude-dir=node_modules . - Sort your endpoints into public (fine with
*) and private (needs a signed-in user). - Make sure every private endpoint checks auth on the server. That is the protection; CORS is not.
- Replace
origin: true, or code that copies the request's Origin into the response, with an allowlist. - Test from a terminal:
curl -s -I -H "Origin: https://evil.example" https://your-api.example/endpointshould not returnAccess-Control-Allow-Origin: https://evil.example.
Can a scanner find this?
Caelo does not check CORS settings. The grep and curl test above are the quickest check. Caelo does check the keys and database rules those endpoints usually depend on.
- Install Caelo: npx caelo . or the MCP server
- How Caelo compares with gitleaks, TruffleHog, Semgrep and others
1. Run the free local check
npx caelo .
Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and .env files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Act on a finding
Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.
For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.
Questions
Why does the browser say the wildcard is not allowed with credentials?
Because the CORS rules forbid Access-Control-Allow-Origin: * on credentialed requests. The browser blocks the response. Set the specific origin instead, and only for origins you trust.
Does CORS protect me from CSRF?
No. PortSwigger notes that CORS is not a protection against cross-origin attacks such as cross-site request forgery. Use SameSite cookies and CSRF tokens or checks for that.
Supabase's Edge Function example uses Access-Control-Allow-Origin: *. Is that wrong?
Not for functions that authenticate with a token in the Authorization header, because browsers do not attach that token automatically. The function's own auth check is what protects it.