Your frontend calls your API and the browser console fills with a red CORS error. You paste it into your AI coding tool, and the fix comes back in seconds: allow every origin. The error goes away. A few days before launch, you start to wonder if that was safe.
The short answer: allowing all origins with Access-Control-Allow-Origin: * is safe for public data that needs no login. It is the wrong setting for anything tied to a user account. The bigger risk is the second "fix" that often follows it, where the server copies back whatever origin asked and allows cookies too. This post explains the difference, shows the safe setup, and ends with a two minute test for your live URL.
Is it safe to allow all origins in CORS?
Allowing all origins in CORS is safe only when every response from that server is public: the same data for every visitor, with no cookies and no login. It is not safe for an API that returns account data. It is never safe to pair an allow-everyone rule with credentials.
This table covers the cases most small apps run into.
| What the endpoint serves | Wildcard OK? | Why |
|---|---|---|
| Fonts, public images, an open data feed | Yes | There is nothing private to read |
| A public, read-only API with no login | Yes | Every caller gets the same response anyway |
| An API that takes a bearer token in a header | Low risk, but use a list | Other sites do not have the token. A list keeps you covered if you move to cookies later |
| An API that uses cookies for login | No | Browsers refuse the wildcard with cookies, and the usual workaround is the real hole |
| A tool on localhost or an office network | No | A public site the user visits could read it through their browser |
If your app has user accounts, treat the wildcard as a placeholder from development and replace it before launch.
What does Access-Control-Allow-Origin: * do?
The header Access-Control-Allow-Origin: * tells browsers that a page from any website may read this response. It loosens the same-origin policy, the browser rule that stops one site's scripts from reading another site's data. It changes nothing about who can send requests to your server.
An origin is three things together: the scheme, the host and the port. That is why http://localhost:5173 (your frontend) and http://localhost:3000 (your API) count as different origins, and why the error shows up on day one of most projects.
By default, a script on one origin can send a request to another origin, but the browser will not let the script read the answer. MDN's CORS guide describes the headers a server sends to opt out of that rule. The key point is who does the enforcing. Your server only sends a header. The visitor's browser decides what to do with it.
So the CORS error is the browser protecting your user from other websites. When you loosen it, you decide which other sites may read your API's answers inside that browser.
Why does the wildcard stop working when you add cookies?
The wildcard stops working with cookies because browsers refuse to share a logged-in response with every site. If a request carries cookies and the response says Access-Control-Allow-Origin: *, the browser blocks the page from reading it. The server has to name one exact origin instead.
This is a built-in safety limit. MDN states that for requests with credentials the server must not use the wildcard, and that the browser will block access to the response if it does. Firefox reports it with this error:
Reason: Credential is not supported if the CORS header
'Access-Control-Allow-Origin' is '*'
You usually meet it the day you add login and the frontend starts sending credentials: 'include'. This is the moment things go wrong. The quick way to clear the error is to make the server repeat back whatever origin called it. In an Express app, it tends to look like one of these:
// Pattern 1: the cors package told to echo the caller
app.use(cors({ origin: true, credentials: true }))
// Pattern 2: the same thing by hand
res.setHeader('Access-Control-Allow-Origin', req.headers.origin)
res.setHeader('Access-Control-Allow-Credentials', 'true')
The cors package README documents both halves. Calling cors() with no options sets the wildcard, and origin: true reflects the request origin. The error clears and the app works. Nothing in the console says the second version is far more open than the first.
Why is reflecting the Origin header worse than the wildcard?
Reflecting the Origin header is worse than the wildcard because it removes the one safety limit the wildcard has. The server tells every website, by name, that it may read responses and send the user's cookies. A page your logged-in user visits can then read their account data from your API.
PortSwigger's CORS guide describes the result plainly: when a server echoes the origin and allows credentials, any domain can read resources from it. The attack needs no stolen password. It goes like this:
- Your user is logged in to your app in one tab.
- They open a link to a page the attacker controls.
- That page runs
fetch('https://api.yourapp.com/me', { credentials: 'include' }). - The browser attaches the user's cookie. Your server answers with their data and a header naming the attacker's site as allowed.
- The browser hands the response to the attacker's script, which sends it home.
This is a common mistake. A 2018 USENIX Security study of the Alexa top 50,000 sites found insecure CORS settings on 27.5% of the subdomains that used CORS at all. In PortSwigger's 2016 research, a Bitcoin exchange had an endpoint that reflected any origin with credentials, which let another site take users' API keys. The Go package rs/cors had a flaw, tracked as CVE-2018-20744, that turned a wildcard policy into origin reflection in versions before 1.5.0.
One thing limits the damage, and it is worth being exact about. The attack only works if the browser sends the cookie on that cross-site request. MDN's Set-Cookie reference says a SameSite=Lax cookie is not sent on a cross-site fetch(), and that some browsers treat cookies as Lax by default. But many apps with a frontend on one domain and an API on another set SameSite=None so that login works at all, and those cookies are sent. Subdomains of your own domain also count as the same site, so even a Lax cookie is sent on requests from any of them, including a forgotten subdomain that someone else now controls. Do not lean on cookie settings to cover for an open CORS rule.
The wildcard tells every website it can read your public data. Reflecting the origin with credentials tells every website it can read your users' private data. The second one is the setting to find before launch.
Does CORS stop anyone from calling your API directly?
No. CORS does not stop anyone from calling your API directly. Only browsers enforce it, on behalf of the user. curl, Postman, a script or another server never check it. Every endpoint still needs its own authentication, authorization and rate limits, whatever your CORS settings say.
The cors package README says it plainly: CORS is not access control. A strict origin list does nothing against a person with a terminal. If an endpoint returns private data to a request with no valid session or token, that is the bug to fix, and no header will hide it. The same goes for cost: an open AI endpoint can be called in a loop from any server, which is how a denial of wallet bill starts.
CORS is also not a defense against forged requests. PortSwigger states that CORS is not a protection against cross-site request forgery (CSRF). The reason is in how browsers treat simple requests. MDN lists them: a GET, HEAD or POST with a plain form content type goes out with no preflight check. The server receives it and acts on it, even though the calling page cannot read the answer. The OWASP CORS cheat sheet makes the same point: a cross-site form POST is still processed by the server.
So keep two questions apart. CORS answers "which websites may read my responses in a browser?" Your auth code answers "who is this, and what may they do?" If you sign users in with tokens, the checks that matter are covered in common JWT authentication mistakes.
When is a wildcard origin the right choice?
A wildcard origin is the right choice when the response is meant for everyone: fonts, public images, open data, a public read-only API or an embeddable widget. If a stranger with curl would get the same data, the wildcard gives nothing away. The OWASP testing guide only calls it insecure when the response holds sensitive information.
Two cases sit in a grey area.
APIs that use a bearer token. If your frontend keeps a token and sends it in an Authorization header, another website cannot make the browser attach it the way it attaches a cookie. A wildcard here is low risk. An explicit list is still the better habit, so a later switch to cookie sessions does not quietly open a hole.
Local and internal tools. PortSwigger warns against wildcards on internal networks. A service that trusts anyone who can reach it (a dev server, an admin panel on an office network) has no login to protect it. If it also allows all origins, a public web page can use a visitor's browser to read it. Chrome added a guard here: since Chrome 142, released in October 2025, a public site needs the user's permission before it can send requests to the local network. Treat that as a backstop.
How do you replace the wildcard with an allowlist?
To replace the wildcard with an allowlist, keep a short list of the exact origins you own. Compare each request's Origin header against it with an exact string match, and send that origin back only when it matches. Add Vary: Origin so caches do not serve one site's answer to another.
With the Express cors package, pass an array:
const allowed = ['https://app.example.com']
if (process.env.NODE_ENV !== 'production') {
allowed.push('http://localhost:5173')
}
app.use(cors({ origin: allowed, credentials: true }))
In a Next.js route handler, or any place you set headers by hand, the logic is the same:
const allowed = new Set(['https://app.example.com'])
export async function GET(req) {
const origin = req.headers.get('origin')
const headers = { Vary: 'Origin' }
if (allowed.has(origin)) {
headers['Access-Control-Allow-Origin'] = origin
headers['Access-Control-Allow-Credentials'] = 'true'
}
return Response.json(await loadData(req), { headers })
}
MDN notes that the header takes a single origin, so a server with several allowed sites has to pick the matching one per request. That is why Vary: Origin matters. In FastAPI, pass the same kind of list to allow_origins. The FastAPI docs say a wildcard there cannot be combined with allow_credentials=True.
Most broken allowlists fail in one of these ways:
- Loose matching. A check such as
origin.endsWith('example.com')also acceptsevil-example.com. A "starts with" check acceptsexample.com.evil.net. PortSwigger shows both. Use exact matches. - Allowing
null. It looks harmless and helps local files work. MDN warns against it, because any site can produce anullorigin with a sandboxed iframe. - Whole hosting domains. A pattern that allows every subdomain of a shared host, such as every
vercel.appornetlify.appaddress, allows every other customer of that host. List your own preview URLs or leave previews out. - Localhost in production. Keep dev origins behind an environment check, as in the first example.
How do you test your CORS settings before launch?
To test your CORS settings, send your live API a request with a made-up Origin header and read the response headers. If the made-up origin comes back in Access-Control-Allow-Origin, your server trusts every website. If it comes back next to Access-Control-Allow-Credentials: true, any website can read your logged-in users' data. A server with an allowlist sends no Access-Control-Allow-Origin header back for an origin it does not know.
It may still send Access-Control-Allow-Credentials: true or Vary: Origin. That is harmless on its own, because the browser will not share a response that does not name the calling origin.
Swap in a real endpoint from your app:
curl -s -D - -o /dev/null https://api.example.com/api/me \
-H "Origin: https://evil.example"
Then run through this list:
- Send a foreign origin. Use the command above. You want no
Access-Control-Allow-Originheader in the reply, or one that names only your own site. - Send
Origin: null. The reply should not containAccess-Control-Allow-Origin: null. - Send a lookalike. Try your domain with a prefix and a suffix, such as
https://evil-example.comandhttps://example.com.evil.net. - Send your real origin. It should come back exactly, with
Vary: Originin the response. - Search the code. Look for
origin: true,req.headers.origin,allow_origins=["*"]and barecors()calls. AI tools add these to clear an error, a habit covered in the hidden security cost of vibe coding. - Check the platform too. Hosts and API gateways can add CORS headers of their own. The live response is the truth, so test the deployed URL.
CORS is one item on a longer list. The rest is in the pre-launch security checklist for indie developers.
Scanning for this before you ship
For a second look at the code, the free IOnclad browser scanner runs a 167-rule subset in your browser, with no signup and no email wall. One of its rules flags JavaScript and TypeScript lines that assign req.headers.origin or request.headers.origin directly to origin with a colon or equals sign, as in origin: req.headers.origin. Your code stays in the tab.
A pattern rule like that is a prompt to go and look. It does not prove anything either way. It does not match origin: true, a setHeader call like Pattern 2 above, a quoted '*', a Python config, or a header added by your host. It can also flag a safe allowlist that reads the origin into a variable first. A clean result tells you nothing about those, so run the curl test either way.
The IOnclad desktop app looks wider, with 26 scanners and 600+ checks across secrets, git history, dependencies, auth and deploy settings, and it returns one Ship-It verdict with a file, line and suggested fix for each finding. No scanner can promise an app is secure. What it can do is point at lines worth a second look before a stranger finds them.
This post is part of our app security guides for indie developers.