---
title: "The browser rejected * with cookies? Don't echo the Origin back"
description: "With a request that carries cookies, the browser won't accept Access-Control-Allow-Origin set to *, so many people echo back any Origin. Use an allowlist instead, plus Vary: Origin."
url: https://ihsanalapsi.dev/articles/cors-credentials-wildcard
lang: en
date: 2026-11-20
updated: 2026-11-20
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# The browser rejected * with cookies? Don't echo the Origin back

You have a front end on `app.myshop.com` that fetches data from `api.myshop.com`, and the user is logged in with a cookie. You set `Access-Control-Allow-Origin: *` and the browser refused. You searched online and found a fix that "works": send back the Origin value as it came in. Here you'll see why that fix exposes your users' data to any site, and what the right setup is.

## What is CORS?

CORS is a header-based way for a server to tell the browser which other sites may read its responses.

According to [MDN's CORS guide](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS), the server names those sites in Access-Control-Allow-Origin, and the browser is the one that enforces the decision.

## Why did the browser reject `*` with cookies?

The browser rejects `*` on requests that carry cookies or credentials, because then every site on the internet could read responses holding the user's data. On such a request, the server has to name one site explicitly.

The same guide says that if a request carries a cookie and the response has `Access-Control-Allow-Origin: *`, the browser blocks access to the response and reports a CORS error in the console. The response also needs `Access-Control-Allow-Credentials: true`, or the browser ignores it.

## What's wrong with "echo the Origin back"?

The bug is that you send back whatever the browser sent in Origin, with `Allow-Credentials: true`, for any site. You've built `*` in a way the browser can't catch.

Take an attacker's site, `evil-site.com`. Your user has it open while logged in to your app. The page sends a request to `api.myshop.com` with the user's cookie, and the server answers `Access-Control-Allow-Origin: https://evil-site.com` because that's how the request came in. The browser sees an explicit site and allowed credentials, so it hands the response to the attacker's page: orders, address, email, everything.

## How do you write the right setup?

You write a list of allowed sites, send the Origin back only if it's on the list, and add `Vary: Origin`. Any site outside the list gets no header at all.

```javascript
const allowed = new Set(['https://app.myshop.com']);

app.use((req, res, next) => {
  const origin = req.get('Origin');
  if (origin && allowed.has(origin)) {
    res.set('Access-Control-Allow-Origin', origin);
    res.set('Access-Control-Allow-Credentials', 'true');
  }
  res.append('Vary', 'Origin');
  next();
});
```

1. Compare the full value, scheme included, not with `endsWith` or `includes`, because `myshop.com.evil-site.com` passes that kind of check.
2. MDN's guide says that when the server sets a single origin that changes based on an allowlist, it should include `Origin` in `Vary`, so clients know the response differs by site.
3. Test it: send a request with `Origin: https://evil-site.com` using `curl`, and no `Access-Control-Allow-Origin` should come back.

## Where do you start now?

The lesson for us as developers: when the browser rejects a setting, it's usually protecting you from something, so working around it isn't the fix. Open your CORS setup today, and look for any line that echoes the Origin without a list.

If your API relies on a cookie, also read [the CSRF token and SameSite article](/articles/csrf-token-samesite), because the cookie that rides along with the request is the same problem from another side. All app security articles are in [the app security topic](/articles?tag=app-security), and if you want someone to test your whole API, there's [Muhassan](/muhassan). How many sites are on your list now, and how many do you still need?

## FAQ

**Does CORS protect my server from requests?**

No. CORS is a way for the server to tell the browser which other sites may read its responses, and the browser enforces it. Permissions and checks on the server stay your job.

**If my API is public and uses no cookies, is * fine?**

Usually yes. The problem with * is on requests that carry cookies or credentials, and there the browser rejects it anyway.

**Why do I need Vary: Origin?**

Because the allowed site in the response changes with the site that asked for it. Vary: Origin tells clients that the server's responses differ based on the Origin request header.
