CSRF &
Mitigations
Understand Cross-Site Request Forgery, how attackers exploit browser trust, and master the defenses: CSRF tokens, SameSite cookies, and more.
What is CSRF?
Cross-Site Request Forgery (CSRF) is a web security vulnerability where an attacker tricks a victim's browser into sending an unwanted, authenticated request to a web application the victim is already logged into. The application cannot distinguish this forged request from a legitimate one.
The Browser Trust Problem
Anatomy of a CSRF Attack
Why CSRF Works
CSRF vs XSS: Key Distinction
| CSRF | XSS | |
|---|---|---|
| Exploits trust of | Server trusts the user's browser | User trusts the website |
| Attacker can | Perform actions as the victim | Read/modify page content and steal data |
| Requires | Victim to be logged in | Vulnerable input handling on the site |
| Attack vector | External malicious site | Injected script on the target site |
| Reads response? | No: fire-and-forget | Yes: full DOM access |
Mitigation 1: CSRF Tokens
A CSRF token (also called a synchronizer token) is a secret, unpredictable value generated by the server and embedded in each form. The server validates this token on every state-changing request. Because the attacker's malicious site cannot read the token, it cannot forge a valid request.
How Tokens Work Step-by-Step
Mitigation 2: SameSite Cookies
The SameSite cookie attribute tells the browser when to attach cookies to cross-site requests. It directly addresses the root cause of CSRF: automatic cookie attachment.
SameSite Comparison
| Scenario | None | Lax | Strict |
|---|---|---|---|
| Top-level link click | ✓ Sent | ✓ Sent | ✕ Not sent |
| Cross-site form POST | ✓ Sent | ✕ Not sent | ✕ Not sent |
| Cross-site iframe | ✓ Sent | ✕ Not sent | ✕ Not sent |
| Cross-site AJAX | ✓ Sent | ✕ Not sent | ✕ Not sent |
| CSRF protection | None | Partial | Strong |
Additional Defenses
How to Apply: Defense-in-Depth
Example 1: Vulnerable Bank Form
A bank has a money transfer form that only checks the session cookie. No CSRF token, no SameSite attribute. An attacker creates a malicious page:
Example 2: Token-Protected Form
Now the bank adds a CSRF token. The legitimate form includes a hidden field with a server-generated token:
Example 3: SameSite Cookie Defense
The bank sets its session cookie with the SameSite=Lax attribute:
Common Mistakes
Mitigation Summary
| Defense | Strength | Limitation |
|---|---|---|
| CSRF Token | Strongest; validates intent | Requires server-side logic and form modification |
| SameSite=Lax | Blocks cross-site POST/AJAX/iframe | Does not block GET-based CSRF; browser support varies |
| SameSite=Strict | Blocks all cross-site requests | Breaks legitimate link navigation from external sites |
| Origin/Referer Check | Simple server-side check | Headers can be stripped by proxies or privacy settings |
| Custom Headers | Works well for AJAX APIs | Only applies to JavaScript-initiated requests, not forms |
Question 1 of 5
What does a CSRF attack primarily exploit?
Question 2 of 5
Why can't an attacker's malicious website read a CSRF token embedded in a form on bank.com?
Question 3 of 5
A website sets its session cookie with SameSite=Lax. Which of the following cross-site requests would still include the session cookie?
Question 4 of 5
A developer implements a "delete account" feature using a GET request to /delete-account. Why is this especially dangerous from a CSRF perspective?
Question 5 of 5
Which combination provides the strongest defense-in-depth against CSRF?