Cross-Site Request Forgery (CSRF) Attacks
Learn the interview-ready mental model, practical trade-offs, and production patterns for this web fundamentals topic.
Topic content
Cross-Site Request Forgery (CSRF) exploits a user's active session by tricking their browser into making unwanted requests to a trusted site. Prevention requires tokens, SameSite cookies, and protecting all state-changing endpoints.
The attacker tricks the victim's browser into signing a request (using the victim's valid session cookie) without their knowledge. The bank (server) accepts it because it looks legitimate.
1How CSRF Attacks Work
The attacker creates a malicious page that makes a request to the victim's authenticated site. The browser automatically includes cookies, making the request appear legitimate.
2CSRF Token Protection
Server generates a unique token per session, includes it in forms, and validates it on state-changing requests.
- ✓CSRF exploits authenticated sessions via forged requests
- ✓Always protect POST, PUT, DELETE, and PATCH endpoints
- ✓CSRF tokens are the most reliable defense
- ✓SameSite=Strict/Lax cookies provide strong additional protection
- ✓Use framework CSRF middleware when available
- ✓Never use GET for actions that change state