Configuring CORS: who can read an API response and where authorization belongs
A CORS error often appears when a frontend moves to another domain or an API gains a new proxy. Before changing the policy, decide which pages should be able to read responses containing personal data. This guide covers browser rules, server-side checks and a small regression plan built around a fictional class scheduling service.
Record the origins you intend to allow
An origin combines a scheme, host and port. The complete combination matters: the HTTPS version of a site and its HTTP version have different origins. Maintain separate lists for production and testing. Add an address when it serves a defined purpose and has an accountable owner; a temporary preview domain should not remain indefinitely.
CORS tells a browser whether it can expose a response to JavaScript on another page. Compare the complete normalized origin, and return one allowed origin for the particular request. Automatically reflecting the incoming Origin header or looking for a familiar domain somewhere in its value does not provide that check.
Separate public routes from personal data
This example is entirely fictional: a class scheduling team publishes an open timetable and stores students’ personal bookings. Its frontend and API use different origins. For a genuinely public timetable requested without credentials, allowing * can be a deliberate choice. Personal bookings need a separate policy and authorization checks.
Each private API route verifies authentication, the organization or record ownership, and permission to perform the action. Non-browser clients do not have to enforce CORS, so omitting an access header does not turn a public endpoint into a private one. Authorization must work independently of the origin allowed for the frontend.
Configure credentialed access separately
If a browser request needs to send credentials across origins, align the client and server settings. Access-Control-Allow-Origin must identify a specific allowed origin; * is unsuitable for that response. Set Access-Control-Allow-Credentials to true only when credentialed access is needed. Omit an unnecessary header rather than setting it to false.
Account for SameSite and other cookie settings, as well as browser restrictions on third-party cookies: CORS headers do not override them. Do not add null to the trusted origins by default.
Operations that change data through a cookie-based session need appropriate CSRF protection. Browsers send some requests without a preflight, so a failure to read the response does not prove that the action never happened.
Inspect both the preflight and the actual response
For some requests, the browser first sends OPTIONS, a preflight that checks the origin, method and headers. The actual request may follow. A successful OPTIONS response alone says nothing about a user’s permission to access a personal booking.
In your own test environment, open the Network panel and compare both stages with the intended behavior. Check separately whether the main request reached the server and which response it produced. Include application errors: useful diagnostics for the allowed frontend must not expose data to additional origins.
Check caching and the final hop to the browser
When a response varies by Origin, preserve Vary: Origin alongside any existing Vary values. This lets a cache distinguish response variants for different origins. Vary does not define confidentiality rules, however: personal responses require their own caching policy suited to the data they contain.
Examine the headers the browser actually receives after the reverse proxy and CDN. An application and a proxy that independently add Access-Control-Allow-Origin can produce conflicting headers. Decide where the policy is managed, and check consistent behavior for successful responses and errors.
Build a small regression matrix
The fictional team prepares two test pages that it controls and records containing synthetic data. One page belongs to an allowed origin; the other is deliberately outside the list. Write down expected outcomes before comparing them with browser behavior and server-side processing. Run every check in your own test environment.
Evaluate the browser’s ability to read a response separately from the server’s permission to perform an action. If a domain change breaks a test, identify the mismatch against this matrix first. Expanding the origin list should correspond to a new product requirement.
- The allowed page reads the intended response; browser JavaScript on the unlisted page cannot read it.
- The private route rejects a request without a session, even when CORS is configured correctly.
- A test user receives only the synthetic records they have permission to access.
- Public responses, personal responses, errors and cached responses preserve the expected rules.
Keep the verified policy with the release
The result is an origin list with owners, rules for public and private routes, and a matrix of expected responses. Run the regression checks when the frontend, API, proxy or sign-in method changes. Assess the disappearance of a console error alongside those outcomes.
You can start a broader review of your own product with a free website or web application audit in CheckMyApp. The link to start the check is at the end of this page.