VibeSecCheck
Home/Security Guides/cors-misconfiguration-fix
High RiskHeaders, Auth & APIsCORSNext.jsSupabase EdgeExpress

Fixing Permissive CORS Misconfigurations

Permissive CORS policies — especially reflecting arbitrary Origin headers alongside Access-Control-Allow-Credentials: true — allow malicious third-party websites to make authenticated requests on behalf of your logged-in users.

Why AI Builders Introduce This

The moment a founder pastes a browser CORS error into Cursor or Bolt.new, the AI routinely answers with `Access-Control-Allow-Origin: *` or `origin: true` to make the red console text go away.

Real-World Exploit Impact
  • Malicious sites can read private API responses using a logged-in victim's session cookies
  • Cross-origin state-changing mutations bypass browser same-origin isolation
  • Browsers reject `*` + credentials, prompting AIs to reflect `req.headers.origin` — which is even worse!

Vulnerable Pattern vs. Production Fix

Vulnerable Origin Reflection With Credentialstypescript
// DANGER: Trusts ANY attacker origin with user cookies!
const origin = req.headers.get("origin") || "*";
return new Response(JSON.stringify(privateData), {
  headers: {
    "Access-Control-Allow-Origin": origin,
    "Access-Control-Allow-Credentials": "true",
  },
});
Strict Explicit Origin Allowlist
const ALLOWED_ORIGINS = new Set([
  "https://app.yourdomain.com",
  ...(process.env.NODE_ENV === "development" ? ["http://localhost:3000"] : []),
]);

export function buildCorsHeaders(req: Request): Record<string, string> {
  const origin = req.headers.get("origin") || "";
  if (!ALLOWED_ORIGINS.has(origin)) return {};
  return {
    "Access-Control-Allow-Origin": origin,
    "Access-Control-Allow-Credentials": "true",
    "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type, Authorization",
    Vary: "Origin",
  };
}

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit every API route, Edge Function, and middleware file for CORS headers:
1. Replace any Access-Control-Allow-Origin: * (on authenticated routes) or unchecked req.headers.get('origin') reflection with an explicit allowlist driven by process.env.NEXT_PUBLIC_SITE_URL.
2. Always include Vary: Origin when dynamically matching allowed origins.
3. Restrict Access-Control-Allow-Methods and Access-Control-Allow-Headers to only what the frontend actually uses.

How to Verify Your Fix Worked

  1. 1Send `curl -i -H 'Origin: https://evil.example' https://yourapp.com/api/me` and confirm no `Access-Control-Allow-Origin: https://evil.example` header is returned.
  2. 2Test legitimate frontend requests in staging to confirm preflight OPTIONS succeeds.
  3. 3Re-scan with VibeSecCheck to confirm the Permissive CORS check passes.

Frequently Asked Questions

When is Access-Control-Allow-Origin: * actually safe?

Only on truly public, unauthenticated read-only endpoints (such as public documentation feeds or CDN assets) that never return user-specific data or accept credentials.

Why do I need Vary: Origin when using an allowlist?

Without Vary: Origin, CDNs like Cloudflare or Vercel might cache the CORS header from one origin and serve it incorrectly to another.

Does CORS replace authentication?

No. CORS is purely a browser enforcement mechanism — curl or server scripts ignore CORS completely, so you still need server-side session checks.

Related Security Playbooks