CSRF: Tricking a User Into Doing Something
Cross-Site Request Forgery is an attack where an attacker tricks you into performing an action on a different website while you're logged in. Your browser automatically includes your authentication cookies, so the server thinks the request is legitimate.
The attack
You're logged into your bank with the tab open. You then visit a malicious site containing:
<img src="https://yourbank.com/transfer?to=attacker&amount=1000">
Your browser loads the image and automatically includes your bank's session cookie. The bank processes the transfer. The attacker stole money using your browser.
Why it works
The browser trusts the user's identity (the cookie), not the request source. It has no way to tell if the request came from the bank's own pages or a malicious site.
GET requests are especially dangerous
State-changing actions should NEVER be GET requests. An attacker can trigger them silently via images, links, or frames.
The fix: CSRF tokens
Include a random, unique token in forms. Validate it on submission. An attacker can't forge a request without knowing the token.
<form method="POST" action="/transfer">
<input type="hidden" name="csrf_token" value="random123">
<input name="amount" type="number">
</form>