The Complete CORS Headers Cheat Sheet: A Developer’s Guide

Introduction to CORS

Cross-Origin Resource Sharing (CORS) is a security mechanism built into browsers that restricts web pages from making requests to a different domain than the one that served the original page. CORS headers enable servers to specify which origins are permitted to access their resources, enhancing security while allowing legitimate cross-origin communication.

Core CORS Concepts

  • Same-Origin Policy: By default, web browsers restrict web pages from making requests to a different domain than the one that served the web page.
  • Cross-Origin Request: Any request made from one origin (domain, protocol, or port) to a different origin.
  • Preflight Request: An automatic OPTIONS request sent by browsers before the actual request to check if the CORS protocol is understood.
  • Simple Requests: Requests that don’t trigger a preflight (specific methods and headers only).

Essential CORS Headers

HeaderPurposeExample Value
Access-Control-Allow-OriginSpecifies which origins can access the resourcehttps://example.com or * (all origins)
Access-Control-Allow-MethodsSpecifies the HTTP methods allowedGET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-HeadersSpecifies which headers can be usedContent-Type, Authorization
Access-Control-Allow-CredentialsControls if credentials can be included in requesttrue (cannot be used with * origin)
Access-Control-Max-AgeDuration to cache preflight results (seconds)3600 (1 hour)
Access-Control-Expose-HeadersHeaders accessible to JavaScriptContent-Length, X-Custom-Header

Implementing CORS: Step-by-Step

  1. Identify requirements:

    • Determine which origins need access
    • Identify required HTTP methods
    • Decide if credentials should be allowed
  2. Set up the server response headers:

    • Configure basic headers for all responses
    • Set up preflight response for OPTIONS requests
  3. Test cross-origin requests:

    • Verify simple requests work
    • Verify preflight requests resolve correctly
    • Test with actual client applications
  4. Monitor and adjust:

    • Check for CORS errors in browser console
    • Refine headers as needed

Common CORS Scenarios

Public API (No Credentials)

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, OPTIONS
Access-Control-Max-Age: 3600

Restricted API with Authentication

Access-Control-Allow-Origin: https://trusted-client.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 3600

Multiple Allowed Origins

// Set dynamically based on the Origin header
if (allowedOrigins.includes(requestOrigin)) {
  response.setHeader('Access-Control-Allow-Origin', requestOrigin);
}

Common CORS Challenges and Solutions

ChallengeSolution
“No ‘Access-Control-Allow-Origin’ header is present”Add the correct origin to server response headers
“Method not allowed by Access-Control-Allow-Methods”Include the required HTTP method in the Allow-Methods header
“Request header field not allowed”Add the custom header to Access-Control-Allow-Headers
“Credentials flag is true, but Access-Control-Allow-Origin is not specific origin”Replace * with specific origin when using credentials
CORS errors in production but not developmentEnsure production environment has matching CORS configuration

CORS Implementation by Framework

FrameworkImplementation
Express.jsUse cors middleware: app.use(cors({origin: 'https://example.com', methods: ['GET', 'POST']}))
DjangoUse django-cors-headers package and add to MIDDLEWARE setting
Spring BootUse @CrossOrigin annotation or configure CorsRegistry
ASP.NETConfigure CORS policy in Startup.cs using services.AddCors()
FlaskUse Flask-CORS extension: CORS(app, resources={r"/api/*": {"origins": "*"}})

Best Practices

  • Be specific with origins: Avoid * whenever possible, especially with credentials
  • Limit exposed methods: Only include HTTP methods your API actually needs
  • Set reasonable max-age: Balance between performance and flexibility to change
  • Test thoroughly: Use browser developer tools to verify CORS is working correctly
  • Secure implementation: Don’t use CORS as your only security measure
  • Use proper error handling: Return meaningful error messages for CORS issues
  • Consider environment differences: Ensure consistency between development and production

Troubleshooting CORS Issues

  1. Check the browser console for specific CORS error messages
  2. Verify network requests using browser developer tools
  3. Confirm server configuration is correctly implementing CORS headers
  4. Use CORS debugging tools like “CORS Unblock” extension during development
  5. Test with curl to isolate browser vs. server issues

Resources for Further Learning

Scroll to Top