CORS “must not be the wildcard '*' when the request's credentials mode is 'include'” – the safe fix

By Abheesht Tripathi · Published

Access to fetch at 'https://api.example.com/me' from origin 'https://app.example.com' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

What it means

Your frontend calls the API with credentials: 'include' (fetch) or withCredentials: true (axios), so the browser attaches cookies. For credentialed requests the CORS rules are stricter: the response must name the exact origin in Access-Control-Allow-Origin and send Access-Control-Allow-Credentials: true. A * is rejected. Firefox reports the same problem as “Credential is not supported if the CORS header 'Access-Control-Allow-Origin' is '*'”. A sibling message in Chrome is “The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'”.

The server usually did handle the request; the browser just refuses to hand the response to your JavaScript.

The tempting fix that creates a hole

Reflecting whatever Origin the request has, together with Allow-Credentials: true, makes the error go away and lets any website read your users' data with their cookies: a page on evil.example calls your API from the victim's browser and reads the response. The same goes for allowing the origin null, which sandboxed iframes can send. Use an allowlist.

Fix: an exact allowlist (fetch-style handlers, Next.js, Hono, Edge Functions)

const ALLOWED_ORIGINS = new Set(['https://app.example.com', 'http://localhost:3000']);

function corsHeaders(origin: string | null): Record<string, string> {
  const headers: Record<string, string> = { Vary: 'Origin' };
  if (origin && ALLOWED_ORIGINS.has(origin)) {
    headers['Access-Control-Allow-Origin'] = origin;
    headers['Access-Control-Allow-Credentials'] = 'true';
    headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS';
    headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization';
  }
  return headers;
}

// preflight
export async function OPTIONS(req: Request) {
  return new Response(null, { status: 204, headers: corsHeaders(req.headers.get('origin')) });
}

export async function GET(req: Request) {
  const data = await loadCurrentUser(req);
  return Response.json(data, { headers: corsHeaders(req.headers.get('origin')) });
}

Vary: Origin stops a CDN from caching one origin's headers and serving them to another. With credentials, * in Allow-Headers or Allow-Methods is treated literally, not as a wildcard, so list them.

Fix: Express

import cors from 'cors';

const ALLOWED_ORIGINS = new Set(['https://app.example.com', 'http://localhost:3000']);

app.use(cors({
  origin(origin, callback) {
    // no Origin header = not a browser cross-origin request; CORS does not apply
    callback(null, !origin || ALLOWED_ORIGINS.has(origin));
  },
  credentials: true,
}));

Cookies have their own rules

Even with correct CORS headers, the browser only sends a cookie if the cookie allows it. A frontend on app.example.com and an API on api.example.com are the same site, so SameSite=Lax cookies work. A frontend on myapp.vercel.app calling api.example.com is cross-site: the cookie needs SameSite=None; Secure, and browsers that block third-party cookies may still drop it. Putting the API on a subdomain of your app's domain avoids that.

Or don't use cookies for the API

If the API authenticates with an Authorization: Bearer token (as Supabase clients do), the request doesn't need credentials: 'include' at all. Remove it, and the credentials rules no longer apply.

How to verify

curl -si -X OPTIONS https://api.example.com/me \
  -H 'Origin: https://app.example.com' \
  -H 'Access-Control-Request-Method: GET' | grep -i '^access-control\|^vary'

curl -si https://api.example.com/me -H 'Origin: https://evil.example' | grep -i '^access-control'

The first shows your exact origin and Access-Control-Allow-Credentials: true; the second shows no Access-Control-Allow-* headers. In the browser, the request succeeds from your app and the Network tab shows the cookie sent.

FAQ

Can I use Access-Control-Allow-Origin: * with cookies?

No. Browsers reject a wildcard origin for requests sent with credentials. Return the exact, allowlisted origin and Access-Control-Allow-Credentials: true, or stop sending cookies and use a bearer token instead.

Related guides

More in Edge Functions, webhooks and APIs · all guides