What is CORS?
Cross-Origin Resource Sharing (CORS) is the browser’s HTTP mechanism for deciding whether scripts may access responses from another origin. Servers opt into permitted cross-origin access through response headers.
How CORS works
An origin combines a URL’s scheme, host, and port. Before some cross-origin requests, the browser sends an OPTIONS preflight describing the intended method and headers. If the server permits it, the browser can send the actual request and then checks whether its response may be exposed to JavaScript. Other requests are sent without preflight. In either case, a response blocked by CORS does not prove the server never received or accepted the upload.
Key facts
- CORS is not authentication or authorization. A command-line client is not restricted by the browser’s same-origin policy.
- Credentialed cross-origin response access requires an explicit allowed origin and appropriate credential settings;
Access-Control-Allow-Origin: *is not a substitute. - JavaScript may need
Access-Control-Expose-Headersto read response headers such as an upload resource’sLocation. Sending a header is different from exposing it to the caller. - Using fetch with
mode: "no-cors"does not grant readable cross-origin access. An opaque response hides the status and body needed to validate an upload.
When CORS matters
Configure CORS on the actual upload or storage endpoint. Check both preflight and upload responses, including failures, and allow only the origins, methods, and headers that the application needs.