How to configure CORS safely (Express, FastAPI, Supabase)
Updated 8 November 2026 · 6 min read
CORS errors are one of the most searched problems in web development, and the quickest fix people find is Access-Control-Allow-Origin: *. Sometimes that is fine. Sometimes it opens your API to any website on the internet. This guide explains the difference and gives safe settings for the common stacks.
What CORS actually does
Browsers stop a page on one site from reading responses from another site, unless that other site says it’s allowed. CORS is how a server says it. When app.example.com calls api.example.com, the browser checks the Access-Control-Allow-Origin header in the response and only hands the data to the page if the origin is allowed.
Two things follow from that:
- CORS only protects users in browsers. Servers, scripts and
curlignore it, so it is not access control. Your API still needs authentication. - What CORS protects is your logged-in users. If a malicious site can make requests with your users’ cookies and read the answers, it can act as them.
When “*” is fine
Access-Control-Allow-Origin: * is reasonable for a truly public API that:
- returns only public data, like a list of countries or public product prices
- doesn’t use cookies or other credentials
- doesn’t change anything on the server
Browsers also refuse to send cookies on credentialed requests when the origin is *, which limits the damage. The danger comes when a server reflects whatever origin it receives and also allows credentials. That is the same as * with cookies switched on.
When it isn’t
Avoid * and reflected origins when the API:
- uses cookies or a session for login
- returns personal or account data
- changes data (creating, updating or deleting)
- is an internal or admin endpoint
In those cases, list the exact sites that may call it.
Express
import cors from "cors";
const allowed = ["https://app.example.com", "https://www.example.com"];
app.use(cors({
origin: (origin, cb) => cb(null, !origin || allowed.includes(origin)),
credentials: true, // only if the frontend sends cookies
}));
!origin lets requests with no Origin header through, such as server-to-server calls. Those aren’t browser requests, so CORS doesn’t apply to them anyway; authentication still does.
Avoid origin: true together with credentials: true. It reflects any origin and allows cookies.
FastAPI
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.com"],
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["Authorization", "Content-Type"],
)
Django has the same idea in django-cors-headers: set CORS_ALLOWED_ORIGINS to a list, and keep CORS_ALLOW_ALL_ORIGINS = False.
Supabase Edge Functions
Many Supabase examples start with 'Access-Control-Allow-Origin': '*'. That’s fine for a function only Supabase itself calls, such as a webhook or an auth hook. For a function your frontend calls with a user’s token, limit it:
const ALLOWED = new Set(["https://app.example.com"]);
function corsHeaders(req: Request) {
const origin = req.headers.get("origin") ?? "";
return {
"Access-Control-Allow-Origin": ALLOWED.has(origin) ? origin : "https://app.example.com",
"Access-Control-Allow-Headers": "authorization, content-type, apikey",
"Vary": "Origin",
};
}
Deno.serve(async (req) => {
if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders(req) });
// ... check the user's token, then do the work
return new Response(JSON.stringify(result), { headers: { ...corsHeaders(req), "Content-Type": "application/json" } });
});
Vary: Origin tells caches that the answer depends on the origin, so one site’s response isn’t served to another.
A quick checklist
- List every endpoint that uses cookies or returns account data. None of them should allow
*or reflect any origin. - Keep the allowed origins in one place, ideally from configuration per environment.
- Allow only the methods and headers the frontend uses.
- Remember CORS isn’t authentication. Every private endpoint still checks who is calling.
What Lintsure checks
The API open to requests from any website rule flags origin: "*", origin: true, Access-Control-Allow-Origin: *, CORS_ALLOW_ALL_ORIGINS = True and allow_origins=["*"]. It marks them Partial, because a public read-only API may be fine. You confirm, then fix it or accept it with a reason. It relates to SOC 2 CC6.6 and ISO 27001 A.8.20.
Check your own code
Lintsure runs these checks on your machine and never uploads your code.
npx lintsure scanThis guide is our own reading of public texts, written for software teams. It is not legal advice and it doesn't cover your whole obligation, only the parts code can show. Ask a qualified person before any audit or decision. Disclaimer