Security
What is CSRF?
Definition
CSRF is an attack that makes a logged-in user's browser send an unintended state-changing request to a site that trusts it, using the user's own credentials.
Also known as: Cross-Site Request Forgery, XSRF, Session riding
CSRF in more detail
The vulnerability exists because browsers attach cookies to requests automatically, regardless of what caused the request. If you are signed in to your bank and then load an attacker's page, a hidden auto-submitting form posting to the bank arrives with your session cookie attached, and the bank cannot tell it apart from a click you made. The attacker never sees the response — the same-origin policy still blocks that — but the transfer has already happened.
This is the part that separates CSRF from XSS, and the two are routinely mixed up. XSS runs the attacker's script inside your site's origin, so it can read anything the page can read; CSRF runs no script on your site at all and is blind, exploiting only the browser's willingness to send credentials. XSS is the more powerful bug, and note that a site with an XSS flaw cannot defend itself with CSRF tokens, because injected script can simply read the token first.
Defences layer. Set session cookies to SameSite=Lax, already the default in Chromium browsers, so they are not sent on cross-site form posts; use Strict for the most sensitive sessions. Add a synchroniser token — a per-session unpredictable value in a hidden field or custom header that the server checks — or the double-submit cookie variant. Verify the Origin or Referer header on state-changing requests. And never make GET requests change state, because a plain image tag is enough to trigger one.
CSRF example
The attack — a page on evil.example
<form action="https://bank.example/transfer" method="POST">
<input type="hidden" name="to" value="attacker">
<input type="hidden" name="amount" value="5000">
</form>
<script>document.forms[0].submit()</script>
The victim's browser attaches the bank's session cookie.
The attacker cannot read the response — and does not need to.
The defences
Set-Cookie: session=...; SameSite=Lax; HttpOnly; Secure
<input type="hidden" name="_csrf" value="8f2c19ab...">
server: reject if _csrf does not match the session's token
reject when Origin is not https://bank.exampleCSRF — common questions
- What is the difference between CSRF and XSS?
- XSS injects and runs the attacker's script inside your site's origin, so it can read the page, cookies accessible to script and any data the user can see. CSRF injects no script — it just causes the browser to send a request with credentials attached, and cannot read the response.
- Does SameSite=Lax make CSRF tokens unnecessary?
- It removes most of the risk but not all. Lax still permits cross-site top-level GET navigations, not every browser applies it as the default, and the attribute does nothing for authentication schemes not based on cookies. Defence in depth is still the recommendation.
- Are APIs using bearer tokens vulnerable to CSRF?
- Not in the classic way, because a token in an Authorization header is attached by your code, not automatically by the browser. Storing that token in a cookie that is sent automatically brings the vulnerability straight back.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary