Cross-Origin Resource Sharing (CORS) controls whether browser JavaScript may read certain responses from a different origin. It is a browser protocol layered on top of the same-origin policy. CORS does not authenticate a caller, authorize a user, or prevent every cross-site request from reaching a server. A secure API still needs authentication, authorization, input validation, CSRF defenses where applicable, and appropriate rate limits.
This distinction explains a common diagnostic surprise: a request can succeed in curl or Postman while a browser reports a CORS error. Those tools do not enforce browser CORS rules. The server may have received the request and performed an action even though the browser prevented the page from reading the response. Treat a CORS failure as a browser access-control symptom, not proof that the endpoint rejected the request.
An origin is the scheme, host, and port tuple. https://app.example.com and http://app.example.com are different origins, as are https://app.example.com and https://admin.example.com. A CORS allowlist should compare the parsed Origin value against exact configured origins. Avoid substring checks such as “ends with example.com” unless the implementation correctly handles label boundaries and the full origin tuple.
Do not reflect any Origin request header into Access-Control-Allow-Origin. That effectively tells the browser to allow every requesting site. For a small product, configure a short list of known frontend origins in environment-specific settings. Reject or omit CORS permission for every other origin. If a server returns a specific origin dynamically, include Vary: Origin so caches do not reuse a response authorized for a different origin.
The wildcard Access-Control-Allow-Origin: * cannot be combined with credentialed browser access. When cookies or browser-managed credentials are required, the server must return the exact permitted origin and Access-Control-Allow-Credentials: true. A broad credentialed policy is dangerous because it may expose user-specific responses to an untrusted web page. Restrict credentialed CORS to origins that actually need it and keep authentication and authorization checks on the server.
For some cross-origin requests, the browser sends an OPTIONS preflight before the actual method. The preflight asks whether the origin, method, and requested headers are allowed. The server should answer only the required methods and headers for an allowed origin. Do not globally allow every method and header to avoid debugging one client. OPTIONS should not create business side effects.
A simple GET may be sent without a preflight, so a preflight-only test does not cover all cross-origin behavior. Conversely, a browser may block access to a response after sending a request. Test the actual route's side-effect behavior as well as the CORS headers. CORS is not a substitute for a CSRF token or origin validation on state-changing cookie-authenticated requests.
Use a known allowed origin, a hostile unrelated origin, a lookalike subdomain, and a missing Origin header. In a controlled test environment, inspect response headers for both normal requests and preflights:
curl -i -H 'Origin: https://app.example.com' https://api.example.com/v1/profile
curl -i -H 'Origin: https://evil.example' https://api.example.com/v1/profile
curl -i -X OPTIONS \
-H 'Origin: https://app.example.com' \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: content-type,authorization' \
https://api.example.com/v1/profile
These requests help inspect server behavior; curl does not enforce CORS. Follow them with a browser test from pages served at allowed and disallowed origins. Confirm that an allowed origin can read only the intended response and a disallowed origin does not receive permissive CORS headers. For state-changing routes, separately assert that unauthenticated or forged requests do not alter data.
When credentials are used, check cookies' SameSite, Secure, and HttpOnly properties and test CSRF protections. Cross-origin and cross-site are related but not identical concepts, especially with subdomains and browser cookie rules. Do not weaken cookie policy just to make CORS work. Prefer same-origin deployment or a backend-for-frontend when that reduces cross-origin complexity.
example.com.attacker.invalid and an unrelated lookalike host.* together with credential permission.Vary: Origin where the response varies by origin.Record the endpoint, response headers, requesting origin, browser version, and whether the request itself reached the server. Redact cookies and tokens from traces. A proxy can alter headers, so compare the origin service response with the edge response if behavior differs.
Vary: Origin when the allowed response changes by request origin.The Fetch Standard defines the CORS protocol used by browsers. OWASP's CSRF guidance explains why browser cross-origin behavior and request-forgery protections need separate treatment. For debugging, compare browser network traces with direct HTTP requests, but make authorization decisions only on the server.
Editorial status: Owner authorized publication without an individual reviewer. Examples target controlled or owned APIs.