Monitory
← All guides

Guides

How to secure CORS on your site or API

The CORS configuration that lets any website read your users' data, how to test it and how to fix it.

What CORS decides

Cross-origin resource sharing headers decide which other websites may read your responses from a visitor's browser. Misconfigured, they let a site you do not control read data that belongs to your logged-in users.

The dangerous combination

A server that echoes back any origin in Access-Control-Allow-Origin while also sending Access-Control-Allow-Credentials: true. With that, any website your users visit can read their data from your site while they are logged in.

Test it yourself

curl -s -I -H "Origin: https://evil.example" https://example.com/api/me | grep -i "access-control"

If the response repeats https://evil.example in Access-Control-Allow-Origin and also sends Access-Control-Allow-Credentials: true, the configuration is dangerous.

Fix it

  • Never reflect the Origin header back as is. Keep a list of the origins you trust and only send those back.
  • In Laravel, list your front-end origins in allowed_origins in config/cors.php instead of *, and only enable supports_credentials for those.
  • Public APIs that need no cookies can use Access-Control-Allow-Origin: * without credentials.

Keep watching

The CORS check sends a request with a foreign Origin once a day and checks whether your server trusts it. It fails when any origin may read the responses with the visitor's credentials, and warns when any origin may read them without credentials, which is fine for truly public data and risky for anything else. It tests your monitored URL and, when the API check has an endpoint, that endpoint too.

Keep reading