Understanding Cross-Origin Resource Sharing (CORS)
Web browsers enforce the Same-Origin Policy (SOP) to prevent malicious websites from accessing resources on other domains. Cross-Origin Resource Sharing (CORS) is a security mechanism that relaxes this restriction in a controlled way, using HTTP headers to allow servers to explicitly define which external domains may access their resources.
How CORS Works
CORS operates through a handshake between the browser and server:
- A web application requests a resource from a different origin
- The browser checks the server's CORS headers to determine if the request is permitted
- If headers allow the origin, the browser proceeds; otherwise, it blocks the request
Key Principle: CORS shifts security responsibility from the browser to the server, allowing developers to define granular access policies.
Core HTTP Headers
| Header | Purpose | Example Value |
|---|---|---|
Access-Control-Allow-Origin | Specifies which origins can access the resource | https://example.com or * |
Access-Control-Allow-Methods | Lists allowed HTTP methods | GET, POST, OPTIONS |
Access-Control-Allow-Headers | Defines which request headers are permitted | Content-Type, Authorization |
Access-Control-Max-Age | Caches preflight responses (in seconds) | 86400 (24 hours) |
Access-Control-Allow-Credentials | Allows cookies or authentication headers | true |
Types of CORS Requests
Simple Requests
Browsers send simple requests without preflight checks if they meet all criteria:
- Use
GET,HEAD, orPOSTmethods - Only include CORS-safelisted headers (e.g.,
Accept,Content-Type) Content-Typeis one of:application/x-www-form-urlencoded,multipart/form-data, ortext/plain
Example:
GET /api/data HTTP/1.1
Host: api.example.com
Origin: https://client.example.com
Preflight Requests
For "non-simple" requests (e.g., PUT, DELETE, or custom headers), browsers first send an OPTIONS request to verify CORS compliance.
Example Preflight Flow:
- Browser sends:
OPTIONS /api/data HTTP/1.1
Origin: https://client.example.com
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: X-Custom-Header
- Server responds:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://client.example.com
Access-Control-Allow-Methods: PUT, POST
Access-Control-Allow-Headers: X-Custom-Header
Common Use Cases
- APIs: Securely expose endpoints to third-party clients
- CDNs: Serve static assets (images, fonts, stylesheets) from different domains
- Single Sign-On (SSO): Share authentication tokens across subdomains
- Microservices: Enable communication between services hosted on separate domains
Security Risks and Misconfigurations
Critical Pitfalls to Avoid
Wildcard Overuse
- Using
Access-Control-Allow-Origin: *withAccess-Control-Allow-Credentials: trueexposes sensitive data - Fix: Always specify exact origins when credentials are involved
Null Origin Abuse
- Accepting
Origin: null(from local files or sandboxed iframes) can lead to attacks - Fix: Explicitly block
nullorigins or restrict them to trusted contexts
Regex Vulnerabilities
- Poorly crafted regex for origin validation (e.g.,
.*\.example\.com) can be bypassed - Fix: Use exact string matching or a whitelist of allowed domains
Dynamic Origin Reflection
- Echoing the
Originheader without validation allows any domain to access resources - Fix: Validate origins against a predefined list
Best Practices
- Restrict Origins: Use specific domains instead of wildcards for sensitive resources
- Limit Methods/Headers: Only allow necessary HTTP methods and headers
- Reasonable Cache Durations: Set
Access-Control-Max-Ageto balance performance and security (e.g., 5-10 minutes) - Test Configurations: Use tools like CORS Tester to validate headers
- Monitor Logs: Audit CORS-related errors in server logs to detect misconfigurations
Debugging CORS Issues
Common Symptoms
- Browser console errors:
No 'Access-Control-Allow-Origin' header Preflight response did not succeed- Requests blocked despite correct server headers
Troubleshooting Steps
- Verify the
Originheader matches the allowed domains - Check for typos in header names (e.g.,
Access-Control-Allow-Originvs.Access-Control-AllowOrigins) - Ensure the server responds with
200 OKor204 No Contentfor preflight requests - Confirm the server supports
OPTIONSrequests - Use browser DevTools Network tab to inspect request/response headers
Learn More
- MDN Web Docs: CORS – Comprehensive reference with examples
- OWASP CORS Cheat Sheet – Security-focused guidelines
- Google's CORS Guide – Practical implementation tips