Deutsch
Fix libraryMediumCORS · APIs

CORS with credentials for any origin: other sites read your users’ data

An API that echoes any Origin back and allows credentials lets every website read your users’ responses with their cookies. Use a fixed allowlist instead.

By Tim GeithnerPublished 3 min read

ShipRely's Security audit checks for this today.

Short answer

Browsers refuse Access-Control-Allow-Origin: * on requests that carry cookies, so a common ‘fix’ for the CORS error is to echo back whatever Origin the request names and add Access-Control-Allow-Credentials: true. That tells the browser every website may read your API’s answers with the visitor’s cookies. If your API authenticates with a Bearer token, keep the wildcard and drop credentials; if it uses cookies, allow only a fixed list of your own origins.

Ask an AI about this page:ChatGPTClaude

On this page

This defect is an API that allows every website to read its responses while the visitor’s cookies are attached. It usually looks like an echo: the server copies the request’s Origin header into Access-Control-Allow-Origin and adds Access-Control-Allow-Credentials: true. Any page your user visits can then call your API in the background, as that user, and read the answer. It is rated medium: the impact is reading or changing whatever the cookie session allows, but browser cookie rules block many real-world cases, and APIs that use Bearer tokens aren’t affected.

Why it matters

CORS is how a server tells the browser which other websites may read its responses. MDN is explicit about requests with credentials such as cookies: the server must name an explicit origin, not the * wildcard, and if it sends * anyway, the browser blocks the response and shows a CORS error.

That error is where the defect starts. Searching for a quick fix, or asking an AI tool, often produces this:

VulnerableReflected origin with credentials
Deno.serve((req) => {
  const origin = req.headers.get("Origin") ?? "*";
  const headers = {
    "Access-Control-Allow-Origin": origin, // echoes any site back
    "Access-Control-Allow-Credentials": "true", // and lets it send the user's cookies
  };
  if (req.method === "OPTIONS") return new Response(null, { headers });
  return Response.json({ ok: true }, { headers });
});

The error disappears, because the browser now sees a specific origin. But the origin is whatever the caller says, so the server effectively allows everyone, with credentials. That’s the “permissive cross-domain policy” CWE-942 describes.

Whether this is exploitable depends on how your API authenticates:

  • Bearer token in the Authorization header (the supabase-js default): the browser never adds that header by itself, so another site has nothing to send. The wildcard is fine here.
  • Cookies (for example server-rendered apps using Supabase’s SSR helpers, or your own session cookie): the browser may attach them to a request started by another site. A reflected origin plus credentials then hands the response to that site.

How to find it in your app

Search for CORS headers set in code:

Search your repository
grep -rn "Access-Control-Allow-Credentials" supabase/functions src app pages 2>/dev/null
grep -rnE "Access-Control-Allow-Origin\"?\s*[:,]\s*(origin|req\.headers|request\.headers)" supabase/functions src app pages 2>/dev/null
grep -rn "credentials: *true\|origin: *true" src app server 2>/dev/null

The last line catches the cors middleware for Express-style servers, where origin: true reflects any origin. Then ask your own API what it allows:

Check your own endpoint
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://your-app.example/api/me

If the response contains both Access-Control-Allow-Origin: https://example.com and Access-Control-Allow-Credentials: true, the API trusts every site with credentials.

How to fix it

If the endpoint uses Bearer tokens, drop credentials. For Supabase Edge Functions, use Supabase’s own CORS headers, which the docs recommend importing from the SDK so they stay in sync with what the client sends:

Safesupabase/functions/me/index.ts
import { corsHeaders } from "npm:@supabase/supabase-js@^2/cors";

Deno.serve((req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });
  return Response.json({ ok: true }, { headers: corsHeaders });
});

No Access-Control-Allow-Credentials, no echo. In the browser, remove credentials: "include" or withCredentials: true from calls that don’t need cookies.

If the endpoint really uses cookies, allow a fixed list of your own origins. Compare the request’s origin with the list, only then set it with credentials, and add Vary: Origin as MDN advises for answers that change per origin:

SafeAllowlist for cookie-based APIs
const ALLOWED_ORIGINS = new Set([
  "https://your-app.example",
  "https://www.your-app.example",
]);

function corsHeaders(req: Request): Record<string, string> {
  const origin = req.headers.get("Origin") ?? "";
  const headers: Record<string, string> = {
    "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type, x-retry-count",
    "Access-Control-Allow-Methods": "POST, OPTIONS",
    Vary: "Origin",
  };
  if (ALLOWED_ORIGINS.has(origin)) {
    headers["Access-Control-Allow-Origin"] = origin;
    headers["Access-Control-Allow-Credentials"] = "true"; // only if you really use cookies
  }
  return headers;
}

Compare exact strings from a set. Checks like origin.endsWith("your-app.example") or a regex without anchors also match look-alike domains. Remember that CORS only controls who may read responses; state-changing cookie requests also need CSRF protection.

Fix permissive CORS with credentials

Find every place that sets CORS headers (edge functions, API routes, middleware, the cors package). Wherever Access-Control-Allow-Origin is copied from the request’s Origin header or the cors middleware uses origin: true together with credentials, fix it: if the endpoint authenticates with an Authorization Bearer token, use a fixed header set with Access-Control-Allow-Origin: * and no Access-Control-Allow-Credentials (for Supabase Edge Functions import corsHeaders from npm:@supabase/supabase-js@^2/cors). If the endpoint needs cookies, compare the Origin against a fixed Set of our production origins, set Allow-Origin and Allow-Credentials only for exact matches, and add Vary: Origin.

Remove credentials: “include” or withCredentials from frontend calls that don’t need cookies. List every endpoint you changed and which option you used.

Check that the fix works

Run the request from above against your own endpoints, once with a foreign origin and once with your own:

Foreign origin, then your own
curl -s -D - -o /dev/null -H "Origin: https://example.com" https://your-app.example/api/me
curl -s -D - -o /dev/null -H "Origin: https://your-app.example" https://your-app.example/api/me
  1. With https://example.com, the answer must not contain Access-Control-Allow-Credentials: true together with that origin.
  2. With your own origin, a cookie-based endpoint returns your origin, Access-Control-Allow-Credentials: true and Vary: Origin.
  3. Use your app normally in the browser. The console must show no CORS errors.

Frequently asked questions

Is Access-Control-Allow-Origin: * on my Supabase Edge Function a problem?

Usually not. Supabase’s own CORS headers use the wildcard, and supabase-js sends the session as an Authorization header, which the browser never adds on its own. Another site can call the function, but not with your user’s token. The risk starts when you add credentials.

Why did the CORS error appear in the first place?

Because the request was sent with credentials (fetch with credentials: ‘include’, or axios withCredentials) and the response said *. MDN says the browser blocks that combination. If you don’t need cookies on that request, remove the credentials option instead of changing the server.

Why only medium?

Modern browsers limit cookies on cross-site requests (SameSite defaults and third-party cookie restrictions), which blocks many attacks in practice. You shouldn’t rely on that: it depends on cookie settings and browser, and the server is explicitly granting access.

Sources

  1. 1.Cross-Origin Resource Sharing (CORS) — MDN Web Docs
  2. 2.CORS support for invoking from the browser — Supabase Docs
  3. 3.CWE-942: Permissive Cross-domain Security Policy with Untrusted Domains — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt