Cross-Site Request Forgery (CSRF) tricks an authenticated user's browser into sending an unwanted request to a site where they're already logged in — without their knowledge. The server receives what looks like a perfectly legitimate, authenticated request, so it acts on it.
When you log into a website, the server sets a session cookie in your browser. Every request your browser makes to that domain — regardless of which page triggered it — automatically includes that cookie. This is normal browser behaviour. CSRF abuses it.
Students often confuse these two. The key difference: XSS injects code that runs in the victim's browser on the target site — the attacker controls what the victim's browser does on the legitimate site. CSRF makes the victim's browser send a request from a different site — the attacker doesn't need any code injection on the target, just a way to make the victim's browser send a forged request.
Attacker injects JS
into the TARGET site.
Victim's browser runs
the attacker's code
on the legit site.
Attacker hosts a page
ELSEWHERE that tricks
the browser into sending
a request to the target
with real cookies.
CSRF can be used to transfer funds, change account email/password, make purchases, delete accounts, or perform any state-changing action a logged-in user can do. The attacker just needs to trick the victim into visiting a page — via a phishing link, a malicious ad, or even a forum post with a hidden image tag.
POST /transfer
Cookie: session=abc
to=attacker&amt=5000
// Any site can forge this
POST /transfer
Cookie: session=abc
csrf_token=x9kP2mQz
// Token unknown to attacker
The standard fix is a CSRF token — a secret, random, per-session value embedded in every form. The server validates it on submission. Since the attacker's page is on a different origin, it cannot read the token from the legitimate site (Same-Origin Policy), so it cannot forge a valid request.
SameSite=Strict or Lax) tells browsers not to send cookies on cross-site requests. Origin/Referer header validation checks where the request came from. Modern frameworks include CSRF protection by default — always keep it enabled.- CSRF abuses automatic cookie attachment, so the server sees a valid session on a forged request.
- A CSRF token must be secret, session-bound and validated by value, never by mere presence.
- SameSite=Lax or Strict cookies block most cross-site requests but should be combined with tokens.
- CSRF needs no code injection on the target site, unlike XSS.
What is CSRF and how does it work?
CSRF forces a logged-in user's browser to send a request to a site they are authenticated on. The browser automatically attaches the session cookie, so the server treats the forged request as legitimate.
How do CSRF tokens prevent attacks?
The server issues a random, session-bound token embedded in each form and checks its value on submit. An attacker on another origin cannot read the token because of the Same-Origin Policy, so they cannot forge a valid request.
Does SameSite cookie protection replace CSRF tokens?
No. SameSite=Lax or Strict blocks most cross-site cookie sending, but it works best as an extra layer alongside validated tokens and Origin header checks.
Why is checking only that a CSRF token exists insecure?
If the server only tests that the field is present, an attacker can submit any value and pass. The token must be compared against the secret value stored in the session.
When Alice logs in, the server gives her a session cookie. Her browser stores it and sends it with every subsequent request to that domain — even requests triggered by a completely different website.
The server sees: valid session cookie = trusted user. It doesn't ask "where did this request actually come from?"
The request body contains to= and amount= — both values the attacker already knows. The only "authentication" is the session cookie, which the browser sends automatically.
This means an attacker can write a page that sends this exact same POST request. The browser will helpfully attach Alice's cookie without asking.
Alice doesn't click anything, doesn't see anything — the page loads, the form submits, and VulnerableBank processes the transfer because the session cookie came along for the ride.
In Stage 2, click "Simulate Alice visiting the evil page" — this is what happens when Alice clicks a phishing link.
if (isset($_POST['csrf_token'])) { // proceed }
Notice anything? It only checks whether the field exists — not whether the value is correct. A token that checks the presence of a field, not its secret value, is completely useless.
Think: what if you send any value as the token? Would the server accept it?
1) Secret — generated server-side, not guessable
2) Tied to the session — different for every user
3) Validated properly — server checks the value, not just presence
4) Single use (ideally) — rotated after each form submission
The token in Stage 3 fails condition 3. In real-world code, always validate the token value against the session-stored value — never just check if the field exists.
VulnerableBank
// logged in as: alice // no csrf protection
free-prizes.evil.com
// attacker-controlled page
<form action="http://bank.local/transfer"
method="POST" id="x">
<input name="to" value="attacker99">
<input name="amount" value="5000">
</form>
<script>x.submit()</script>
You won a free iPhone! Click below to claim your prize!
VulnerableBank v2
// "csrf protected" — or is it?
// BUG: only checks if field exists!
process_transfer();
} else {
die("CSRF check failed");
}