Cross-Origin Resource Sharing (CORS) is a mechanism that allows a server to indicate which origins are permitted to load its resources. Browsers enforce the same-origin policy by default and use CORS headers to relax it.
See also: HTTP — CORS, REST
Key Points
- When allowing credentials, you must specify the exact origin, headers, and methods — wildcards (
*) are disallowed forAccess-Control-Allow-Origin,Access-Control-Allow-Headers, andAccess-Control-Allow-Methods - Respond to preflight requests with
204 No Content - Preflight (
OPTIONS) is triggered by non-simple requests (e.g. custom headers,PUT/DELETEmethods,application/jsoncontent type) Access-Control-Allow-Origin— which origins may access the resourceAccess-Control-Allow-Methods— permitted HTTP methods (disallows*when credentials are enabled)Access-Control-Allow-Headers— permitted request headersAccess-Control-Allow-Credentials: true— allows cookies,Authorizationheaders, or TLS client certificates on cross-origin requestsAccess-Control-Expose-Headers— response headers that browser JavaScript is permitted to read beyond CORS-safelisted defaultsAccess-Control-Max-Age— how long preflight results can be cachedVary: Origin— always set when dynamically reflecting the request’sOriginheader, preventing CDNs and proxy caches from serving a cached response to the wrong origin (cache poisoning)