# Is Access-Control-Allow-Origin: * a security risk in my API?

Canonical: https://caelo.sh/guides/cors-wildcard-ai-generated-api
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** Not by itself. A wildcard lets scripts on any website read your API's responses, but browsers refuse it on requests that carry cookies, and CORS never stops curl or a server from calling you. The real risks are reflecting any Origin with credentials allowed, and treating CORS as access control. Protect data with auth checks.

## 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

```js
// 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

```js
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

```ts
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

1. Find every place that sets CORS: `grep -rnE "Access-Control-Allow-Origin|cors\(" --exclude-dir=node_modules .`
2. Sort your endpoints into public (fine with `*`) and private (needs a signed-in user).
3. Make sure every private endpoint checks auth on the server. That is the protection; CORS is not.
4. Replace `origin: true`, or code that copies the request's Origin into the response, with an allowlist.
5. Test from a terminal: `curl -s -I -H "Origin: https://evil.example" https://your-api.example/endpoint` should not return `Access-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](/compare)

## Check your own app

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. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## 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.

## Sources

- [MDN: Access-Control-Allow-Origin.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Origin)
- [MDN: Cross-Origin Resource Sharing (CORS).](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS)
- [MDN: CORS error, credential is not supported if the header is *.](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials)
- [expressjs/cors README (default configuration).](https://github.com/expressjs/cors)
- [PortSwigger Web Security Academy: CORS.](https://portswigger.net/web-security/cors)

## Related guides

- [Security checklist for apps built with Lovable, Bolt, Cursor, Replit or v0](https://caelo.sh/guides/vibe-coded-app-security-checklist)
- [Is my Cursor app secure? What Cursor protects and what it does not](https://caelo.sh/guides/is-my-cursor-app-secure)
- [Can AI write secure code? What peer-reviewed studies found](https://caelo.sh/guides/can-ai-write-secure-code)
- [How do you secure a Supabase Edge Function?](https://caelo.sh/guides/supabase-edge-functions-security)
