A browser can refuse to expose an API response even after the server has processed the request. Fixing that console error by reflecting every supplied origin can expose authenticated responses to an unintended website. Define which browser origins may read which responses before changing CORS headers.
Consider a browser application at https://app.example.com calling https://api.example.com. The intended policy allows that application to read authenticated responses while denying an unapproved origin the same access. The header exchange below shows that permission; API authorization and protection against unwanted mutations remain separate responsibilities.
Define the reader and the data it may access
Inventory the browser clients that need cross-origin response access. Record their exact scheme, host, and port, along with the API operations they use. Separate development origins from production origins instead of authorizing a broad family of hosts for convenience.
The Fetch standard's CORS protocol defines the response-sharing mechanism. Use it to distinguish the browser's permission to expose a response from the server's permission to perform the operation. A command-line client is not constrained by the same browser response-access check.
For the synthetic API, decide whether the document response is public or tied to a logged-in session. Public resources may have different sharing requirements from account-specific data. Avoid applying one broad middleware policy to both without examining the consequences.
Keep the ownership of approved origins explicit. A subdomain can change purpose, be delegated, or stop hosting the application. Review origin authorization when deployment ownership changes, not only when the API endpoint changes.
Match an approved origin as a complete value
Validate the received origin against a maintained exact allowlist or another narrowly defined policy with equivalent rigor. A substring check for example.com does not establish that the host is the intended application. Likewise, trusting all sibling subdomains assumes that every sibling has the same security boundary.
For credentialed response sharing, the Fetch standard requires an explicit allowed origin rather than the wildcard origin value. The credential permission also needs the appropriate explicit response setting. Configure these together for the intended client instead of treating each header as an independent troubleshooting switch.
Do not accept an arbitrary origin merely because the request also includes a valid session. That session authenticates the browser user; it does not authorize the website that is asking to read the response. Keep the two decisions distinct in code review and in the access inventory.
Treat a serialized null origin and an absent origin deliberately. Decide which legitimate clients require each case, if any, and apply the API's ordinary authentication and authorization rules regardless. Avoid turning special origin values into universal bypasses.
Understand preflight without making it authorization
Consider a JSON POST from the approved application. The browser's preflight describes the intended request; it does not carry the application's session cookie as proof of access. This is an illustrative HTTP exchange with no session values:
OPTIONS /documents HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
For this approved origin, method, and header set, a corresponding permission response can be:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type
Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers
The actual response also needs the applicable allowed-origin and credential headers; putting them only on OPTIONS is insufficient. The application must explicitly request credentials through its browser client, and cookie delivery still follows cookie and browser policy. The example's request header list is deliberately minimal. If the application uses a CSRF header, that header also needs explicit preflight permission without weakening its server-side validation.
For an unapproved origin such as https://unapproved.example, omit permission to read the protected response and apply the service's defined preflight rejection behavior. Never reflect it simply because it was supplied. Compare a valid origin with a different port, an unrelated domain containing a familiar name, and null; none is the approved value above.
These headers describe response sharing, not whether creating a document is authorized. The actual request must still pass authentication, document permissions, body validation, and the application's CSRF policy before creating anything. An ordinary form-style request can reach a mutation endpoint without this JSON preflight, so server-side checks cannot depend on the browser having performed the exchange.
Some cross-origin requests require the browser to send a preflight request describing the intended method and headers. Review which methods and headers the application actually needs, then authorize only that capability through the CORS policy.
The preflight should not perform the underlying business action. Its job is to participate in the browser's permission check. Ensure that routing and middleware do not accidentally enqueue work or modify session state while handling that request.
Do not assume that a request without a successful preflight is harmless or impossible. Some request forms can reach the server without that preliminary exchange. The CSRF guide addresses verifying mutation intent for cookie-authenticated applications.
Browser credential delivery also depends on cookie attributes and browser policies. Correct CORS response headers do not override every restriction on cookies. Test the actual supported browser and deployment arrangement before weakening unrelated cookie protections to resolve a failed integration.
Keep response caching aligned with origin decisions
If the allowed-origin response value changes according to the requesting origin, ensure the caching layer distinguishes the relevant variants. The Fetch cache guidance describes the relationship between origin-dependent responses and the Vary mechanism.
Inspect the delivered response after the CDN, reverse proxy, and application middleware. A correct local header can be overwritten or cached incorrectly downstream. Include error responses and redirects in the inspection so legitimate clients can handle failures without a misleading CORS symptom.
Keep private response caching policy separate from origin variation. Distinguishing origins does not itself authorize a shared cache to retain account data. The HTTP validator guide explains why response identity and privacy scope need their own decisions.
Review preflight caching as a deployment concern. A browser may retain an earlier permission result for a period. Do not promise immediate policy revocation based only on changing a preflight response; sensitive API operations must continue enforcing authorization at the authoritative request boundary.
Verify allowed and denied browser paths
Use controlled test pages on an allowed origin and an unapproved origin. Verify whether each can read the intended response, and inspect the server-side effect independently. A browser console error alone does not establish that the server rejected a mutation.
Test credentialed and noncredentialed access, an unsupported method, an extra request header, a normal API error, and a response served through the real cache path. Confirm that the public error contains no internal configuration or stack trace.
Repeat the allowed and denied-origin requests through the deployed proxy path in both orders. A cached allowed response must not grant an unrelated origin access, and a cached denied response must not break the legitimate client. If an overly broad policy is discovered, narrow it and assess whether exposed data or capabilities need additional containment. Preserve server-side mutation checks during that recovery; making the browser hide an error body is not evidence that the operation was denied.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit