Full answer
The console message is the same for many different faults, so work through them in order.
- Open the Network tab and find the failing request. Look at both the OPTIONS preflight and the real request. A failing preflight means the real request was not sent. A 500 or a 404 on the real request means that the server code failed, and the CORS message is hiding the real error.
- Compare headers. Does the response have
Access-Control-Allow-Originthat matches the page origin exactly? An origin has no trailing slash and no path, andhttpandhttpsdiffer. - Check every response type. An error handler, a 404 route, a proxy or a gateway timeout page often does not pass through the middleware that adds the CORS headers. The page then reports a CORS error for what is really a server error. Add the headers before routing, so that they are present on all responses.
- Check redirects. A preflight must not be redirected. A 301 from
httptohttpsor from a missing slash breaks it. Call the final URL. - Check credentials. With
fetch(url, { credentials: 'include' })the server must sendAccess-Control-Allow-Credentials: trueand a specific origin. The wildcard*is rejected. - Check caches. If the answer differs by origin, a missing
Vary: Originlets a cache return the headers of another origin. - Rule out the front end. Using
mode: 'no-cors'does not fix anything, because the response becomes opaque.
Reproduce with curl -i -H 'Origin: https://app.example.com' URL and read the headers. This works because the check is only a header comparison, though curl will not enforce the result.
The test below runs the fixed server and two broken variants, and a page that calls /boom, which returns a 500. With the headers set before the routing, the page can read the 500 status and the JSON error body. When the headers are added only on success, the same call fails with a network error. When the wildcard is used with credentials, it also fails.
import http from 'node:http';
const allowed = new Set((process.env.ALLOWED ?? 'https://app.example.com').split(','));
function cors(req, res) {
const origin = req.headers.origin;
res.setHeader('Vary', 'Origin');
if (origin && allowed.has(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
}
}
http.createServer((req, res) => {
cors(req, res); // before routing, so errors get the headers too
res.setHeader('Content-Type', 'application/json');
if (req.url === '/boom') {
res.statusCode = 500;
return res.end(JSON.stringify({ error: 'database down' }));
}
res.end(JSON.stringify({ ok: true }));
}).listen(process.env.PORT, () => console.log('listening'));Run in headless Chrome 156 through Playwright. Two full runs gave the same results. Three server versions were run against a page on another localhost port: the shown code returned the 500 status and JSON body to the page, a version that adds headers only on success was blocked, and a version using * with credentials was blocked.
Key points
- The CORS message hides the real status. Read the Network tab first.
- Add CORS headers to all responses, including errors, before routing.
- Preflights must not be redirected or require authentication.
- With credentials, use a specific origin and Allow-Credentials true.
- Use Vary: Origin when the header depends on the request.
Common mistakes
- Debugging only in the console and never opening the Network tab.
- Adding the headers in the success handler only.
- Using * together with credentials.
- Turning on no-cors and thinking the issue is solved.
Follow-ups
- How do you test a CORS rule without a browser?
- What does a reverse proxy change about where to set the headers?
- Why must the allowed origin be compared exactly and not with endsWith?
Tips
- Give the order of checks, and say which tool shows each one.
- Mention the error response path, which is the cause that most candidates miss.