What CORS is and why browsers enforce it
Browsers follow the same-origin policy: a script on https://app.example.com may send requests to another origin, but it may not read the responses unless that origin agrees. An origin is the scheme, host and port together, so https://auth.example.com and https://app.example.com are different origins.
Cross-Origin Resource Sharing (CORS) is how a server agrees. It sends Access-Control-Allow-* headers that name which origins may read its responses, with which methods and headers, and whether cookies may come along. The rules live in the WHATWG Fetch standard.
CORS is enforced by browsers only. It protects users from malicious pages reading data with the user’s cookies; it does not stop servers, scripts or tools outside a browser from calling an API.
Simple requests and preflight requests
Some requests could already be made by an ordinary HTML form, so browsers send them straight away and only check the response. Everything else triggers a preflight: an OPTIONS request that asks the server for permission first.
| Sent without preflight when… | Otherwise a preflight is needed |
|---|---|
Method is GET, HEAD or POST | PUT, PATCH, DELETE or any other method |
Headers are only Accept, Accept-Language, Content-Language, Content-Type or a simple Range | Any other header, such as Authorization or X-Requested-With |
Content-Type is application/x-www-form-urlencoded, multipart/form-data or text/plain | application/json or any other type |
OPTIONS /realms/acme/protocol/openid-connect/userinfo HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Max-Age: 3600
Vary: OriginThe rules the browser applies
The checker applies the same rules, in the same order, as a browser does.
Access-Control-Allow-Origin
It must be exactly the request’s origin, or *. Exactly means character for character: no trailing slash, no path, lowercase scheme and host, and only one value. A list of origins in this header is invalid; the server has to pick the matching one and echo it.
Credentials
When the request includes credentials (cookies or HTTP authentication, as with credentials: 'include' in fetch), the response must send Access-Control-Allow-Credentials: true, and every allow header must name things explicitly. * is not allowed in that case, neither for the origin, the methods nor the headers.
Methods and headers
The preflight response must list the method in Access-Control-Allow-Methods unless it is a simple method, and every non-simple request header in Access-Control-Allow-Headers. Header names compare without regard to case. A * wildcard for headers never covers Authorization; it must always be listed by name.
Status and caching
The preflight must answer with a 2xx status. A preflight that hits authentication and returns 401 fails, because preflights never carry credentials. Access-Control-Max-Age lets the browser reuse a preflight result; Chrome caps it at two hours.
What this checker does and does not do
- It decides whether a request needs a preflight, and says why.
- It checks the response headers you paste against the Fetch standard’s rules for origin, credentials, methods and headers, including the special case for
Authorization. - It notes headers browsers do not let scripts set, the 2xx rule for preflights, preflight caching and the need for
Vary: Origin. - It treats the pasted headers as the preflight response and the actual response together. In real traffic both must pass.
- It sends nothing to the URL you enter. To see what a server really returns, capture the response in your browser’s network panel and paste it.
How to check a request
- Enter the origin of the page whose JavaScript makes the call.
- Enter the URL it calls and choose the method.
- List the headers your code sets, one per line, such as
Authorization: Bearer …orContent-Type: application/json. - Tick credentials if the request sends cookies.
- Paste the server’s response headers, ideally from the failing preflight in the network panel.
- Read the verdict and the step that fails.
CORS with Keycloak
Single-page apps talk to Keycloak from the browser: they exchange the authorization code at the token endpoint, refresh tokens and sometimes call userinfo. Those calls are cross-origin whenever Keycloak and the app have different origins, so CORS has to be right.
Web origins
Keycloak decides which origins receive CORS headers from each client’s Web origins setting. List the app’s origins, such as https://app.example.com, without paths or trailing slashes. + means "the origins of this client’s valid redirect URIs", which is usually what a single-page app needs. * allows every origin and should be avoided.
Allowed origins in tokens
Keycloak also puts the client’s web origins in access tokens as the allowed-origins claim. Some resource servers read it to decide CORS for their own APIs, so the same list governs both.
Admin REST API from a browser
Calling Keycloak’s admin REST API from browser code is cross-origin too, and the client used for it needs web origins set in the same way.
Common CORS errors and fixes
| Error in the console | Cause | Fix |
|---|---|---|
| No Access-Control-Allow-Origin header is present | The server sent no CORS headers for this origin | Add the origin to the allow list, in Keycloak to the client’s web origins |
| The value of Access-Control-Allow-Origin must not be the wildcard * when credentials mode is include | * with cookies | Echo the exact origin and send Allow-Credentials: true |
| Request header field authorization is not allowed | Authorization missing from Allow-Headers | List Authorization by name |
| Response to preflight request does not have HTTP ok status | The OPTIONS request got 401, 404 or a redirect | Let OPTIONS through without authentication and answer 2xx |
| Origin does not match | Trailing slash, wrong scheme or port | Compare the exact origin string |
Security notes
- Never reflect any origin that asks while also allowing credentials. That lets every website read your users’ data with their cookies.
- Avoid
*for authenticated APIs, and avoid pattern matches on origins that an attacker could satisfy, such as "ends with example.com", whichevil-example.comalso does. - Remember that simple requests still reach the server. CORS stops the page from reading the response, not the request from being processed. State-changing endpoints need CSRF protection or non-simple requests.
- Send
Vary: Originwhenever the allowed origin depends on the request, so caches do not serve one origin’s response to another.
Best practices
- Keep an explicit list of allowed origins per environment.
- Prefer bearer tokens in the
Authorizationheader over cookies for cross-origin APIs; then credentials mode is not needed. - Answer preflights quickly and without authentication.
- Set a reasonable
Access-Control-Max-Ageto save round trips. - Where possible, serve the app and its API from the same origin, behind one proxy, and avoid CORS entirely.
Debugging CORS step by step
The browser console names the failing rule, but not always clearly. Working through the request in order finds most problems quickly.
- In the network panel, find the failing request. If there is an OPTIONS request just before it, the preflight is where to look first.
- Check the preflight’s status. Anything other than 2xx fails, including redirects.
- Copy the preflight’s response headers and the request’s Origin, method and headers into this checker.
- If the preflight passes, check the actual response: it needs Access-Control-Allow-Origin too, and Allow-Credentials when cookies are sent.
- Change one thing on the server, reload with the cache disabled, and check again; cached preflights can hide a fix for up to their max age.