Deutsch
Security wikiWeb security

What is CORS (cross-origin resource sharing)?

Also known as: CORS, Access-Control-Allow-Origin, preflight request, cross-origin requests

CORS is the browser rule set that decides which other websites may read your API’s responses. What it protects, what it doesn’t, and the settings to avoid.

By Tim GeithnerPublished 2 min read

Short answer

Cross-origin resource sharing (CORS) is a set of HTTP headers that lets a server tell browsers which other origins may read its responses. It relaxes the browser’s same-origin policy; it is not an access control for your API, because tools outside the browser ignore it. The dangerous setting is reflecting any origin together with Access-Control-Allow-Credentials: true on an API that uses cookies.

Ask an AI about this page:ChatGPTClaude

On this page

Cross-origin resource sharing (CORS) is a mechanism based on HTTP headers that lets a server tell browsers which other origins (a combination of scheme, domain and port) may read its responses. By default, the browser’s same-origin policy stops a script on site-a.example from reading a response from api.site-b.example. CORS headers such as Access-Control-Allow-Origin relax that rule for the origins the server names.

CORS in an AI-built app

You meet CORS the first time your React app calls a Supabase edge function and the browser console shows a CORS error. Before such a call, the browser sends an OPTIONS “preflight” request and expects the function to answer with the allowed origins, methods and headers. The Supabase docs show a function that answers the preflight and allows headers like authorization, apikey and content-type, with Access-Control-Allow-Origin: *.

That wildcard is fine for an API that identifies users by a token the app puts into a header. The risk starts when an AI builder “fixes” a CORS error on a cookie-based endpoint by reflecting whatever origin the request came from and adding Access-Control-Allow-Credentials: true. Now any website can make a signed-in user’s browser call that endpoint with their cookies and read the answer. OWASP lists CORS misconfiguration under A01 Broken Access Control, and MDN notes that the same setup also opens state-changing endpoints to CSRF.

Two facts to keep in mind:

  • CORS is not access control. It only governs browsers. A script, curl or another server can call your API whatever the headers say.
  • The wildcard and credentials don’t mix. MDN explains that browsers block a credentialed response whose Access-Control-Allow-Origin is *. That is why the risky setups echo the origin instead.

How to check your app

  • Search your edge functions and API routes for Access-Control-Allow-Origin and Access-Control-Allow-Credentials.
  • Flag any code that copies the request’s Origin header into the response, or accepts every origin that merely contains your domain name.
  • For each endpoint that allows credentials, write down the exact origins that need it; there are usually one or two.

How to fix or prevent it

Allow credentials only for a fixed list of your own origins, compare the Origin header exactly against that list, and keep the wildcard for endpoints that use bearer tokens. Most of all, check authentication and authorization in the function itself, because CORS will never do it for you. The full walk-through is in CORS explained for vibe coders, and the fix for the dangerous combination in CORS wildcard with credentials.

Frequently asked questions

Does CORS protect my API from attackers?

No. CORS is enforced by browsers and only controls whether a script on another site may read the response. Someone calling your API with curl or their own server is not affected. Authentication and authorization have to be checked on the server.

Is Access-Control-Allow-Origin: * dangerous?

Not by itself on an API that uses bearer tokens in a header; Supabase’s own Edge Function example uses it. It becomes a problem on endpoints that rely on cookies, and browsers refuse a wildcard origin for credentialed requests anyway.

Why do I get a CORS error when calling my edge function?

Usually because the function does not answer the browser’s OPTIONS preflight with the CORS headers, or does not allow headers the Supabase client sends, such as authorization and apikey. The Supabase docs show how to handle the preflight.

Sources

  1. 1.Cross-Origin Resource Sharing (CORS) — MDN Web Docs
  2. 2.CORS support for invoking from the browser — Supabase Docs
  3. 3.A01:2025 Broken Access Control — OWASP Top 10
  4. 4.Cross-site request forgery (CSRF) — MDN Web Docs

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