Diagnose a Browser CORS Error From the Preflight and Response Headers

Help me diagnose a browser CORS error using evidence, not guesswork. Work only from what I provide, and tell me exactly what to collect next if something is missing.

I will provide:
- The exact console error text.
- From the browser's network panel: the failing request's method, URL, request headers, and response status and headers. If there was an OPTIONS preflight request, include that request and its response too.
- The page's origin (scheme, host, and port) and the API's origin.
- Whether the request sends cookies or credentials (for example `credentials: 'include'` or `withCredentials`).
- The server or proxy CORS configuration, if I have it.

Work through these steps:
1. Decide whether this request needed a preflight, and explain why: a non-simple method, a custom header such as Authorization, or a Content-Type other than the three allowed for simple requests.
2. If a preflight ran, check its response: a 2xx status, no redirect, `Access-Control-Allow-Origin` matching the page origin, `Access-Control-Allow-Methods` including the method, and `Access-Control-Allow-Headers` including every requested header.
3. Check the actual response: is `Access-Control-Allow-Origin` present and correct? With credentials, it must name the exact origin (not `*`), and `Access-Control-Allow-Credentials: true` must be present.
4. Check whether the "CORS error" is really a server error in disguise: a 4xx or 5xx response from a code path, auth layer, or proxy that doesn't add CORS headers. The browser then reports CORS and hides the real status.
5. If the server reflects the Origin header back, check for `Vary: Origin`, and whether a cache or CDN could serve one origin's response to another.
6. Name the single most likely cause, the evidence for it, the specific config change to fix it, and how to confirm the fix in the network panel.

Never suggest disabling browser security, using a public CORS proxy, or setting `Access-Control-Allow-Origin: *` together with credentials.

---

When to use it: when a frontend call fails with a CORS message and the team is about to change server settings at random. CORS failures are deterministic, so the headers almost always show the cause.

How to adapt it: if an API gateway, ingress, or CDN sits in front of the app, ask the model to check which layer sets each header, because duplicated or conflicting `Access-Control-Allow-Origin` headers are a common cause. For local development, include the dev server's proxy config.

Example input: a console error saying no Access-Control-Allow-Origin header is present on a POST with `Authorization` and `Content-Type: application/json`. The preflight returns 204 with correct headers, and the POST returns 401 with no CORS headers.

What good output looks like: it explains that the preflight was required and passed, identifies the 401 from the auth middleware (which runs before the CORS middleware and returns without CORS headers) as the real problem, recommends adding CORS headers to error responses or reordering the middleware, and says to confirm by seeing the 401 status in the network panel and checking the token.

Sign in to upvote this prompt.