Generate CORS policy configurations for various frameworks and servers
Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers that controls how resources on a web page can be requested from another domain outside the domain from which the resource originated.
The Same-Origin Policy (SOP) is a critical security mechanism that restricts how documents or scripts from one origin can interact with resources from another origin. CORS provides a way to relax this restriction in a controlled manner.
Two URLs have the same origin if they have:
https://example.com:443https://example.com/api/datahttps://api.example.com/data (different subdomain)
Specifies which origins can access the resource.
Access-Control-Allow-Origin: https://example.comAccess-Control-Allow-Origin: * (allow all - insecure)
Specifies which HTTP methods are allowed when accessing the resource.
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Specifies which HTTP headers can be used during the actual request.
Access-Control-Allow-Headers: Content-Type, Authorization
Specifies which headers are safe to expose to the client.
Access-Control-Expose-Headers: X-Custom-Header
Specifies how long preflight request results can be cached.
Access-Control-Max-Age: 3600 (1 hour)
Indicates whether the request can include credentials (cookies, HTTP authentication).
Access-Control-Allow-Credentials: true
Don't trigger a preflight. Must meet all these conditions:
Browser sends an OPTIONS request first to check if the actual request is safe to send:
OPTIONS /api/data HTTP/1.1
Origin: https://example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: Content-Type
Access-Control-Allow-Origin: *Access-Control-Allow-Origin: https://trusted-site.comYou cannot use wildcards with credentials:
Access-Control-Allow-Origin: *Access-Control-Allow-Credentials: true
For multiple allowed origins, maintain a whitelist and validate requests:
allowed_origins = ['https://app1.com', 'https://app2.com']
origin = request.headers.get('Origin')
if origin in allowed_origins:
response.headers['Access-Control-Allow-Origin'] = origin
Only allow methods and headers your API actually uses:
* for headersCause: Server didn't send CORS headers
Solution: Configure server to send proper CORS headers
Cause: Using * with credentials
Solution: Specify exact origin or remove credentials
Cause: HTTP method not in allowed list
Solution: Add the method to Access-Control-Allow-Methods
Cause: Custom header not in allowed list
Solution: Add the header to Access-Control-Allow-Headers
Never use in production:
Access-Control-Allow-Origin: *
This allows any website to access your API and potentially steal user data or perform unauthorized actions.
GET, HEAD, POST