Cross-origin requests: configure CORS and diagnose preflight failures
For browser JavaScript to read an API response from another origin, the server must allow that origin with Cross-Origin Resource Sharing (CORS) response headers. CORS controls whether a browser shares a response; it does not replace API authentication, authorization, or CSRF protection. Keep the allowed origins, methods, and headers narrow, and name the exact origin when credentials are used.
For browser JavaScript to read an API response from another origin, the server must allow that origin with Cross-Origin Resource Sharing (CORS) response headers. CORS controls whether a browser shares a response; it does not replace API authentication, authorization, or CSRF protection. Keep the allowed origins, methods, and headers narrow, and name the exact origin when credentials are used.
Origins and what CORS does
An origin is defined by the scheme, host, and port. For example, https://app.example.test and https://api.example.test are different origins because their hosts differ. The browser’s same-origin policy restricts pages from freely reading cross-origin responses. CORS lets the server explicitly permit a browser to share a particular response.
CORS primarily controls whether JavaScript can read a response. A simple request may still reach and be processed by the server even when the browser blocks the response from the calling script. Do not infer that no request or side effect occurred merely because the browser reports a CORS error. Cookie-authenticated state changes need separate CSRF defenses. Non-browser clients such as curl do not enforce browser CORS checks.
Simple requests and preflight
The browser adds an Origin header to a CORS request. A request within the browser’s simple-request rules can go directly to the server. A request using a method or headers that require a preflight first sends an OPTIONS request, often with:
OPTIONS /v1/orders HTTP/1.1
Origin: https://app.example.test
Access-Control-Request-Method: PATCH
Access-Control-Request-Headers: authorization, content-typeThe server’s preflight response declares the permitted origin, methods, and request headers. This is a format example; set the values to match the API’s policy.
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.test
Access-Control-Allow-Methods: GET, PATCH
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Max-Age: 600
Vary: OriginThe browser then sends the actual request. Its response must also contain a matching Access-Control-Allow-Origin for JavaScript to read it. A successful preflight alone is not enough if the actual response lacks CORS headers. Avoid requiring cookies or tokens on the preflight as though it were the authenticated API operation; design the OPTIONS response policy separately.
Requests with credentials
fetch with credentials: "include" can send cookies and other credentials cross-origin. In this case the server cannot use Access-Control-Allow-Origin: *; it must return the exact requesting origin and Access-Control-Allow-Credentials: true. The value true is case-sensitive. Browser cookie rules such as SameSite and Secure still apply, so CORS permission alone does not guarantee that a cookie is sent.
An Authorization header or JSON content type can trigger preflight. If JavaScript needs to read a response header that is not exposed by default, the server must list it in Access-Control-Expose-Headers. Do not allow origins, headers, methods, or credentials that are not needed.
When the allowed response varies by request origin, send Vary: Origin so caches do not reuse a response prepared for a different origin. Do not reflect an unvalidated Origin value as if it were an allowlist.
Diagnose failures
In browser developer tools, check whether an `OPTIONS` request occurred before the actual request.
Compare `Origin`, `Access-Control-Request-Method`, and `Access-Control-Request-Headers` with the preflight response’s allow values.
Check whether the browser sent the actual request. If it did, inspect that response for the required CORS headers.
For cookies, separately check the fetch credentials setting, exact allowed origin, `Access-Control-Allow-Credentials: true`, and cookie policy.
A successful `curl` request does not show that browser CORS will pass; curl does not enforce the browser’s CORS response check.
Check server logs to learn whether the request was processed. A browser CORS message alone does not show whether the server rejected it or whether it caused a side effect.
Before release
Are only required production origins allowed, with development and preview origins excluded?
Are methods and request/response headers limited to those the feature needs?
Are appropriate CORS headers present on both preflight and actual responses?
For credentialed requests, is the exact origin used instead of a wildcard, with authentication and CSRF controls handled separately?
If the response varies by origin, is `Vary: Origin` set and the allowlist validated?
Diagnose CORS by comparing the server’s allow policy with the request the browser actually sent. Keep CORS as a browser response-sharing rule; handle who may call the API and whether a state-changing request was intended through authentication, authorization, and CSRF controls.
Sources
[WHATWG Fetch Standard: CORS protocol](https://fetch.spec.whatwg.org/#cors-protocol)
[WHATWG Fetch Standard: CORS protocol and credentials](https://fetch.spec.whatwg.org/#cors-protocol-and-credentials)
[OWASP: Cross-Site Request Forgery (CSRF)](https://owasp.org/www-community/attacks/csrf)