CORS Request and Response Headers
You'll learn the specific HTTP headers that browsers and servers use to communicate cross-origin access permissions.
What Are CORS Headers?
CORS (Cross-Origin Resource Sharing) headers are special HTTP headers that tell browsers whether a cross-origin request should be allowed. Think of them as a security handshake between your browser and a server: the browser asks "May I access this resource from a different origin?" and the server responds with headers that say "Yes, but only under these conditions."
Key Request Headers
When making a cross-origin request, the browser automatically adds:
Origin– Identifies where the request came from (e.g.,https://example.com)Access-Control-Request-Method– Used in preflight requests to ask "Can I use this HTTP method?" (e.g.,PUT)Access-Control-Request-Headers– Used in preflight requests to ask "Can I send these custom headers?"
Key Response Headers
The server controls access through response headers:
Access-Control-Allow-Origin– Specifies which origins can access the resource (*for all, or specific origins likehttps://trusted.com)Access-Control-Allow-Credentials– Set totrueto allow cookies and authentication headers (requires specific origin, not*)Access-Control-Allow-Methods– Lists permitted HTTP methods (GET, POST, PUT)Access-Control-Allow-Headers– Lists permitted custom headersAccess-Control-Max-Age– How long (in seconds) preflight results can be cachedAccess-Control-Expose-Headers– Which response headers JavaScript can read
Security Implications
Misconfigured CORS headers can expose your API to unauthorized origins, leak sensitive data, or enable CSRF-like attacks. Always whitelist specific origins rather than using wildcards with credentials.
Key Takeaway: CORS headers are the gatekeeper between browsers and cross-origin servers—understanding Access-Control-Allow-Origin, credentials, methods, and headers is essential for both enabling legitimate access and preventing unauthorized cross-origin attacks.