Track 08 · Lesson 8 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
Track 08 · Attacks and defenses
Cross-site request forgery (CSRF)
Lesson 8 of 20· 7 min
Cross-site request forgery tricks a logged-in browser into doing something its user never asked for. Another site sends the request; your browser helpfully attaches your cookie; your server sees a valid session.
The free cruise
You’re logged in to your bank in another tab
bank.example · logged in as Ana
$5,000.00
Available balance
- Open the page to watch the forged request.
🎉 You won a free cruise! (evil.example)
<!-- hidden on evil.example -->
<form action="https://bank.example/transfer" method="POST">
<input type="hidden" name="to" value="mallory">
<input type="hidden" name="amount" value="1000">
</form>
<script>document.forms[0].submit()</script>Defenses, in layers#
SameSite=LaxorStricton session cookies: the browser leaves the cookie off background requests from other sites.- CSRF tokens: a secret value in every form that other sites can’t read, so they can’t copy it.
- Origin checks: refuse state-changing requests whose
Originheader isn’t your own site. - Never change data with GET. Lax still sends cookies when someone follows a link.
CORS doesn’t help here: the forged request is sent whether or not anyone can read the answer.
The reverse: login CSRF#
Sometimes the attacker wants you logged in to their account. They send you their own email-verification or magic link; the page logs in whoever opens it; you then upload files or connect accounts that now belong to them.
Login CSRF through a verify link
Then replay it against the fix
Login CSRF via a verify link
Attacker
Victim
browser
Verify page
/verify-email
Your API
backend
Step 1 of 8 · Attacker → Your API
Sign up attacker@evil.test
The attacker creates their own account.
- Login CSRF:
- The reverse of normal CSRF: the attacker logs the victim into the attacker's account. The victim does not notice and uploads files or connects accounts that now belong to the attacker.
- CSRF (cross-site request forgery):
- Tricking a logged-in victim's browser into performing an action they did not intend, using their own session. In OAuth, the forged action is 'link this account'.
In a code review
MediumFrontendLogin or verification that happens on page load
Fix: Show which account is affected and require a click; never silently replace a different signed-in user.
0 of 2 checked
Check yourself