CSRF — Cross-Site Request Forgery
- CSRF = Cross-Site Request Forgery.
It means: an attacker tricks a logged-in user’s browser into sending an unwanted request to a trusted site.
How it works (simple steps)
- Victim is logged into bank.com (cookie session is active).
- Victim visits attacker site evil.com.
- evil.com auto-submits a hidden form to bank.com/transfer.
- Browser automatically includes victim’s bank.com cookies.
- bank.com thinks request is legitimate and executes it.
When CSRF is possible
- App uses cookie-based auth.
- Sensitive action endpoint has no CSRF protection.
- Endpoint accepts cross-site form/simple requests.
Tricky points
- Attacker usually cannot read the response; they only need the action to execute.
- CORS is not a full CSRF defense.
- GET must never change state (no delete/transfer/update on GET).
- SameSite=Lax helps, but not enough alone for all flows.
- If you have XSS, CSRF defenses can often be bypassed.
Proper defenses (use together)
- CSRF token on every state-changing form/API call.
- Validate Origin (and fallback Referer) for sensitive endpoints.
- Set cookies: Secure, HttpOnly, SameSite (Lax or Strict where possible).
- Keep state-changing actions on POST/PUT/PATCH/DELETE, not GET.
- Require re-auth or step-up confirmation for critical actions (password/email/payout changes).
CSRF vs SSRF (quick difference)
- CSRF: attacker abuses victim’s browser to send requests.
- SSRF: attacker abuses your server to send requests.