Understand CORS Without Treating It as Authentication
Overview
CORS controls which browser origins may read responses; it does not authenticate callers or prevent direct HTTP requests.
Know what the browser enforces
The same-origin policy limits how scripts from one origin can read resources from another. CORS lets a server declare which origins may access selected responses through browser APIs. It is not a firewall: command-line clients, servers, and malicious scripts outside the browser are not constrained by the browser's CORS checks.
Allow only required origins
For a public API, a specific allowlist is easier to reason about than reflecting any supplied Origin. If credentials are enabled, do not use Access-Control-Allow-Origin: *; return an explicitly approved origin and Access-Control-Allow-Credentials: true. Add Vary: Origin when the response changes according to the request origin.
Handle preflight requests
Requests using certain methods or non-simple headers trigger an OPTIONS preflight. Respond with the allowed methods and headers, and keep the list narrow. Cache preflight results with a deliberate Access-Control-Max-Age value while recognizing that browser behavior may impose limits.
Keep authorization independent
Validate access tokens, sessions, permissions, and resource ownership on the server for every protected request. A successful CORS response does not mean the user is authorized. Test allowed and disallowed origins, credentialed requests, preflight behavior, and direct API calls.
Implementation checklist
- Define the expected behavior and supported environments.
- Keep security, accessibility, and failure states in the design.
- Measure the result with representative user journeys.
- Document operational assumptions and review them after changes.