• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Browser blocks fetch from api.example.com when page is on app.example.com — why does the same code work in curl but not in browser?

Formal Definition

Per Wikipedia: “CORS is a browser security mechanism that blocks cross-origin requests unless server sends Access-Control-Allow-Origin headers.”

Explanation

CORS is the bouncer checking the guest list — same-origin is allowed in, cross-origin needs explicit invite from server.

How It Works

  1. Browser sends Origin header
  2. Server checks allow list
  3. Server replies with ACAO header if allowed
  4. Browser enforces — blocks if missing
  5. Preflight OPTIONS for non-simple requests

Visual Explanation

cors A Browser B CORS Check A->B step 1 C Server B->C step 2

Semantic Network

semantic_cors THIS CORS REL1 Related THIS--REL1 related REL2 Prereq THIS--REL2 builds from

Key Properties

  • Only enforced by browsers, not by curl
  • Simple GET/POST may skip preflight
  • Credentials need explicit Allow-Credentials

Real-World Example

fetch('https://api.example.com/data', {mode: 'cors'})
  .then(r => r.json())

Connections

  • Built from: HTTP API — CORS protects HTTP APIs
  • Related: Client — client triggers CORS
  • Related: Server — server configures CORS
  • Contrasts with: API — API without browser has no CORS

Edge Cases & Gotchas

  • Wildcard * with credentials fails — must list origin
  • Caching preflight without Vary: Origin causes leaks