Security Playbook
07 · Web vulnerabilitiesOWASP A01:20256 min read

CSRF: requests your users never meant to send

Browsers attach cookies to every request to your site, even when another site triggered it. Cross-site request forgery abuses that to act as a logged-in user. Why SameSite cookies changed the game, and when you still need tokens.

In one sentence

Cookies prove which browser sent a request, not which site asked for it. SameSite cookies and CSRF tokens tell your server the request really came from your own pages.

The idea

You're logged in to your bank in one tab. In another tab you open a page with a hidden form that posts to bank.test/transfer. The browser sends that request with your bank cookie, and the bank sees a perfectly authenticated transfer.

CSRF: another site makes your browser act for you
Your browser🍪 session=anabank.testfree-prizes.example01 You log in to bank.test: session cookie saved02 You open free-prizes.example in another tab03 Hidden form auto-submits POST bank.test/transfer04 bank.test receives the request
The browser attaches cookies automatically, so the bank can't tell who started the request. SameSite cookies and CSRF tokens give it that answer.
Turn on SameSite or a CSRF token and replay the attack.

Defenses

DefenseHow it helpsNotes
SameSite=Lax cookiesCookie isn't sent on cross-site POSTs, iframes or fetchesDefault in modern browsers if you don't set it, but set it explicitly
SameSite=StrictCookie never sent cross-site, even on link clicksUsers arriving from email links look logged out
CSRF tokenRandom value in the form that a foreign site can't readNeeded if you use SameSite=None or old browsers matter
Check Origin / Sec-Fetch-SiteReject state-changing requests from other originsCheap extra layer for APIs
No state changes on GETLinks and images can't trigger actionsGET /delete?id=4 is CSRF waiting to happen

Key takeaways

  • 01Browsers attach cookies to cross-site requests unless SameSite says otherwise.
  • 02Set session cookies to SameSite=Lax (or Strict) explicitly.
  • 03Use CSRF tokens or Origin checks for anything that changes state.
  • 04Never change state on GET requests.

Check your understanding

1 of 3

Why does CSRF work?

Related concepts