Security

What is CSRF?

Cross-Site Request Forgery — an attack that tricks users into performing unwanted actions.

Definition

CSRF (Cross-Site Request Forgery) is an attack where a malicious website tricks a user's browser into making an unintended request to another site where the user is authenticated. Since browsers automatically include cookies, the forged request appears legitimate. Defenses include CSRF tokens (unique per-session or per-request), SameSite cookie attribute, checking Origin/Referer headers, and requiring re-authentication for sensitive actions.

A forged request, step by step

A user is signed in to bank.example, which keeps the session in a cookie. They open a page on evil.example that contains:

<form action="https://bank.example/transfer" method="POST" id="f">
  <input type="hidden" name="to" value="attacker">
  <input type="hidden" name="amount" value="500">
</form>
<script>document.getElementById('f').submit()</script>

If the browser attaches the bank's session cookie to that cross-site POST, the bank receives an authenticated transfer request it cannot tell apart from a real one. The attacker never sees the response and does not need to: the side effect is the attack.

Defenses and what each one misses

DefenseStopsMisses
Synchronizer token: a secret per session or per form, checked on every state-changing requestForged forms and fetches, because the attacker cannot read the tokenAny endpoint you forget to check, and everything once the site has an XSS bug
SameSite=Lax or Strict on the session cookieThe cookie riding along on cross-site POSTsLax still sends the cookie on top-level GET navigations; sibling subdomains count as same-site
Checking Origin or Sec-Fetch-Site on unsafe methodsMost cross-site requests, with no token plumbingClients that send neither header; decide explicitly whether to reject them
Requiring a custom header such as X-Requested-With on a JSON APIPlain HTML form posts, which cannot set custom headersHolds only while CORS refuses untrusted origins

Chrome and Edge treat cookies without a SameSite attribute as Lax, but not every browser does, so set the attribute explicitly and keep a token or an Origin check as the primary defense.

CSRF and XSS are different problems

CSRFXSS
Attacker's code runs onTheir own siteYour site
Can read your responsesNoYes
Main defensesTokens, SameSite cookies, Origin checksOutput encoding, Content-Security-Policy
Beats the other's defensesNoYes: script on your page can read any CSRF token

When to Use CSRF

CSRF matters for any endpoint that changes state and authenticates with something the browser attaches automatically: cookies, HTTP Basic auth, or client certificates. APIs that take a bearer token from JavaScript in the Authorization header are not exposed to classic CSRF, because a forged form cannot add that header. Keep every state change on POST, PUT, PATCH or DELETE, never GET.

CSRF in Practice

  • Set the session cookie with an explicit SameSite attribute: Set-Cookie: session=abc123; Secure; HttpOnly; SameSite=Lax; Path=/
  • Django: put {% csrf_token %} inside every POST form; CsrfViewMiddleware rejects requests without a valid token.
  • Reject cross-site unsafe requests with Fetch Metadata (Express): if (req.method !== 'GET' && req.get('sec-fetch-site') === 'cross-site') { return res.status(403).end(); }

Common Mistakes

  • Changing state with GET, such as /logout or /delete?id=1: SameSite=Lax still sends cookies on top-level GET navigations.
  • Putting the CSRF token in the URL, where it leaks through server logs, browser history and Referer headers.
  • Assuming a JSON API is safe because forms cannot send JSON: a form can post a text/plain body that some servers parse as JSON anyway.
  • Relying on SameSite alone when other people control sibling subdomains; those count as the same site.

Keep the references coming

One email a week: new and trending developer tools, fresh comparisons, and what shipped. Unsubscribe in one click.