Advertisement
Intermediate ASP.NET Core, CORS (Cross-Origin Resource Sharing)

What is the difference between CORS and CSRF protection, and why doesn't a strict CORS policy alone fully protect a cookie-authenticated endpoint?

Short answer

CSRF exploits the browser's automatic inclusion of credentials (cookies) on requests to any site, regardless of CORS -- a properly restrictive CORS policy prevents a malicious site's JavaScript from reading the response of a cross-origin request, but a simple form-based CSRF…

Save ASP.NET Core mock interview
Advertisement
AModel answer
CSRF exploits the browser's automatic inclusion of credentials (cookies) on requests to any site, regardless of CORS -- a properly restrictive CORS policy prevents a malicious site's JavaScript from reading the response of a cross-origin request, but a simple form-based CSRF attack (not requiring the attacker to read the response, just trigger a state-changing action) can still succeed even with strict CORS, meaning dedicated anti-forgery tokens remain necessary for full protection of cookie-authenticated endpoints.
<!-- A malicious site can still trigger this request (CSRF), even with strict CORS,
     because a simple form POST doesn't require reading the response -->
<form action="https://bank.com/transfer" method="POST">
  <input type="hidden" name="amount" value="10000">
</form>
<!-- CORS prevents the attacker's JS from READING the response, but the transfer may still happen -->
Real-world example
A security review clarifies to the team that their strict CORS policy protects their API from cross-origin JavaScript reading sensitive responses, but doesn't fully protect a cookie-authenticated form-based endpoint from CSRF, which still requires anti-forgery tokens.
Likely follow-ups
  • How do anti-forgery tokens provide CSRF protection that CORS can't?
  • Why is SameSite cookie configuration also relevant here?
Related topics