đĄď¸ Cross-Site Request Forgery (CSRF)
What It Is, Why Itâs Bad, and How to Fix It (Without Going Cross-eyed)
Imagine this:
Youâre logged into your bank account. You open a new tab, read a blog, click a cat video⌠and without knowing it, you just transferred $1,000 to someone else. đł
Thatâs Cross-Site Request Forgery, or CSRF.
A fancy name for a sneaky problem.
Letâs break it down like youâre five.
đŁ What Is CSRF?
Itâs when a bad website tricks your browser into doing something on a site youâre already logged into.
Itâs like someone handing you a form and saying,
âCan you just sign this real quick?â
And it turns out, you just gave away your house.
đ§ How Does It Work?
Hereâs the basic recipe:
Youâre logged into your bank or favorite app in one tab.
You visit a shady site in another tab.
That shady site secretly makes a request (like "Transfer money") as you, because your browser still holds your session cookies.
The bank sees the request, checks your cookies, thinks you sent it â and goes ahead.
Oops.
đ¤ Why Is This Possible?
Because browsers automatically attach your cookies (including login sessions) with every request â even if the request is from another website.
CSRF takes advantage of that loyalty.
The browser doesnât ask:
âDid the user really mean to do this?â
đ So⌠How Do We Fix It?
Letâs flip the table.
â
1. CSRF Tokens (a secret handshake)
Every time your app shows a form (like âUpdate Profileâ or âMake Paymentâ), it includes a hidden secret token â unique to your session.
When the form is submitted, the server checks:
âIs this a valid session?â
âIs the token included?â
âDoes it match?â
If someone tries to trick the user into submitting the form, they wonât have the token.
Boom â request blocked.
â
2. SameSite Cookies
Modern browsers let you flag your cookies with:
Set-Cookie: sessionId=abc123; SameSite=Strict
This tells the browser:
âOnly send this cookie if the request came directly from my site.â
So even if a bad site tries to forge a request, the browser leaves the cookies behind.
No cookie, no access.
â
3. Use POST, Not GET for Actions
Donât let users delete accounts or transfer money via a simple URL (e.g., /delete?id=123
).
Use POST or PUT, and validate the request body.
đ§Ş Example Time
Without CSRF protection:
<form action="https://yourbank.com/transfer" method="POST">
<input type="hidden" name="amount" value="1000">
<input type="hidden" name="toAccount" value="attacker123">
<input type="submit" value="Click me for a prize!"> </form>
The user thinks theyâre clicking a prize â but they just sent money.
With CSRF token:
<input type="hidden" name="csrf_token" value="random123xyz">
The attacker doesnât have the token.
The bank rejects the request.
đ§ Final Thought
CSRF isnât about stealing data â itâs about abusing your authority.
Your browser trusts you.
CSRF tricks it into doing things you didnât mean to do.
The fix isnât complicated:
Use CSRF tokens for state-changing requests.
Set your cookies with
SameSite=Strict
orLax
.Donât let sensitive actions happen through GET URLs.