Skip to content

Did you know?

A CORS error doesn't mean the request was blocked: the server usually ran it, the browser only hides the response.

CORS only controls reading responses; protect state-changing endpoints with auth and CSRF tokens.

Explained

A CORS error makes it look as if the request was blocked, but usually it wasn’t. For simple requests (GET, HEAD and form-style POST), the browser sends the request, the server runs it and answers, and only then does the browser hide the response from your JavaScript because the Access-Control-Allow-Origin header is missing or wrong.

In our test with Express 5.3 and the cors package, a request from a disallowed origin still returned 200 OK with the data; only the allow header was left out:

curl -s -D - -H "Origin: https://evil.com" http://localhost:3000/api/items
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true

[{"id":1}]

So CORS is not access control and not CSRF protection: a state-changing endpoint still runs. Protect it with authentication and CSRF tokens or SameSite cookies, and use CORS only to decide which websites may read responses in the browser.

Learn more

← All tech tips · Suggest a fix

Back to top