Skip to content
Module 2 of 8Foundations
Estimated time: 20 min

Cross-Origin Resource Sharing (CORS)

Master simple requests, preflight OPTIONS handshakes, and credential hazards.

Core Concept & Mental Model

CORS is a W3C standard protocol that relaxes the Same-Origin Policy in a controlled way, allowing servers to declare which cross-origin applications are permitted to read their private API responses.

Threat Vector & Impact

A common misconfiguration is reflecting arbitrary `Origin` headers into `Access-Control-Allow-Origin` while setting `Access-Control-Allow-Credentials: true`, completely bypassing SOP for authenticated sessions.

Defensive Architecture & Mitigation

Strictly validate the request `Origin` header against an allowlist of trusted origins before echoing it. Never combine `Access-Control-Allow-Origin: *` with credentials.

Technical Deep Dive

Simple vs Preflighted Requests

Requests using GET, POST, or HEAD with standard headers (Accept, Content-Type: application/x-www-form-urlencoded, multipart/form-data, text/plain) are Simple Requests. If a request uses PUT, DELETE, custom headers (like Authorization), or Content-Type: application/json, the browser MUST first dispatch an OPTIONS preflight request.

Critical CORS Response Headers

Click a real-world scenario to see how the 5 core headers are configured and evaluated by the browser.

Access-Control-Allow-OriginSpecifies which origin(s) can read the response body.
https://app.example.com
Access-Control-Allow-CredentialsMust be "true" to expose response when cookies or auth tokens are sent.
true
Access-Control-Allow-MethodsPermitted HTTP verbs for cross-origin requests.
GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-HeadersPermitted custom request headers sent by client.
Content-Type, Authorization
Access-Control-Max-AgeSeconds the browser caches preflight OPTIONS answer.
86400 (24h cache)
Valid & SecureOnly https://app.example.com can read responses with cookies/auth. Preflight is cached for 24h to avoid extra roundtrips.

Interactive Mental Model Simulation

Live Visualizer
VISUALIZATION: CORS PREFLIGHT
Server Config:
Browser (Origin A)
Server (Origin B)
Browser is about to send a complex request (e.g., DELETE or Custom Headers).

Code Implementation & Patching

Vulnerable Code (Exploitable)
// Express.js Vulnerability: Blind Origin Reflection with Credentials
app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*');
  res.setHeader('Access-Control-Allow-Credentials', 'true'); // Critical flaw: any malicious site can steal user data!
  next();
});
Secure Patched Code (Hardened)
// Secure CORS Configuration with Strict Allowlist
const ALLOWED_ORIGINS = new Set(['https://app.example.com', 'https://admin.example.com']);

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (origin && ALLOWED_ORIGINS.has(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.setHeader('Access-Control-Max-Age', '86400');
  }
  next();
});
Security Analysis & Core Architectural Rule

Always maintain an immutable Set of authorized origins. Never use regex without anchoring (^ and $) or reflect arbitrary Origin headers when credentials are enabled.

Test Your Knowledge on Cross-Origin Resource Sharing (CORS)

Challenge yourself with realistic scenario questions in the Web Security Arena.

Launch Practice Arena