← All lessons Stage 3 · Lesson 7
20 min

CSRF: making the browser act for you

Stage 3 · Lesson 7 · 20 minutes · Requires Stage 1 Lesson 5


The one-line answer

Cross-site request forgery (CSRF) is when a malicious page makes a logged-in victim's browser send a request to another site — and that site accepts it, because the browser attached the victim's cookies automatically.

The boundary: CSRF acts on a real person's account by definition. Practise only on PortSwigger labs, which provide a simulated victim.


Why you should care

With CSRF, the attacker never needs your password or your session cookie. They just need you to visit a page while you're logged in somewhere else. That's enough to change your email, transfer money, or change settings — whatever the vulnerable site allows with one request.

CSRF is less common than it used to be because browsers added defaults that block many cases. But it still appears, especially in older apps and in places developers forgot, and understanding it explains why cookies have the SameSite flag you met in Stage 1 Lesson 5.


How it actually works

The ingredients

A CSRF attack needs three things:

  1. A useful action — e.g. "change email" on bank.example.
  2. Cookie-based sessions — the site identifies you only by the session cookie.
  3. No unpredictable values in the request — everything the request needs, the attacker can know in advance.

Why the browser helps the attacker

Recall Stage 1 Lesson 5: browsers attach a site's cookies to every request to that site. Historically that included requests started by other sites. The server sees a valid session cookie and assumes the request came from the user's own action.

The attack

You're logged in to shop.example. Changing your email sends:

POST /account/email HTTP/2
Host: shop.example
Cookie: session=<your session>
Content-Type: application/x-www-form-urlencoded

[email protected]

An attacker hosts a page on evil.example with a hidden form that sends the same request — with their email — and submits itself automatically when the page loads:

<form action="https://shop.example/account/email" method="POST">
  <input type="hidden" name="email" value="[email protected]">
</form>
<script>document.forms[0].submit()</script>

You visit evil.example (a link in a message, an ad). Your browser sends the POST to shop.example with your cookie. The shop changes your email. The attacker resets your password via the new email. Account taken.

Notice the attacker never saw your cookie — the browser just used it. That's the difference from XSS (Lesson 3): XSS runs code inside the target site; CSRF only sends a request to it from outside.

Where SameSite comes in

The SameSite cookie attribute tells the browser when to attach a cookie to requests started by other sites:

ValueBehaviour
StrictNever sent on cross-site requests
LaxSent on top-level navigation (clicking a link) but not on cross-site POST forms or background requests
NoneAlways sent (must also be Secure)

Modern browsers treat cookies without a SameSite attribute as Lax by default in many cases, which blocks the classic POST attack above. That's why CSRF became rarer — but not extinct:

  • sites that set SameSite=None for their own reasons,
  • state-changing actions done with GET requests (which Lax allows on navigation),
  • older browsers and edge cases,
  • sites that rely on SameSite instead of proper tokens.

Impact

Whatever one request can do: change email or password, change delivery address, transfer funds, add an admin user, delete data. Severity depends entirely on the action.


See it yourself

Only on PortSwigger labs (they host the "attacker" page and simulate the victim):

  1. Read the CSRF topic introduction (link below).
  2. Do the apprentice lab CSRF vulnerability with no defences. Capture the change-email request in Burp first. Notice: no token, nothing the attacker can't predict.
  3. Build the attack page in the lab's exploit server, following the lab's instructions, and deliver it to the simulated victim.
  4. Read the lab's explanation of SameSite. Write down which of the three ingredients the lab was missing a defence for.

How attackers abuse this

Attackers find any state-changing request that relies only on cookies, then plant the auto-submitting page wherever victims will load it — forums, ads, messages, compromised sites. It's often combined with social engineering ("check out this deal").

The defensive fix:

  • Anti-CSRF tokens. Put a random, secret, per-session (or per-request) token in every state-changing form or request, and check it on the server. The attacker's page can't read it, so its forged request fails. Most frameworks provide this built in — turn it on and don't disable it.
  • SameSite cookies. Set session cookies to SameSite=Lax or Strict explicitly. Strong second layer; not a replacement for tokens.
  • Never change state with GET. GET should only read. Changes use POST/PUT/DELETE.
  • Re-authentication for sensitive actions — ask for the password or an OTP before changing email, password or payment details.
  • Check the Origin / Referer header as an extra signal (useful, but not on its own).

Common mistakes

  • Assuming SameSite defaults solve everything. Defaults vary and have exceptions. Use tokens.
  • Token checked only if present. If removing the token makes the request succeed, the check is useless.
  • Token not tied to the session. A token from the attacker's own account shouldn't work for the victim.
  • State changes via GET (/delete?id=5).
  • Confusing CSRF with XSS. CSRF sends a request blindly; XSS runs code inside the site. An XSS bug usually defeats CSRF tokens, since the script can read them.

Check yourself

  1. What three conditions does a CSRF attack need?
  2. Why doesn't the attacker need to steal the victim's session cookie?
  3. What is the primary fix for CSRF, and why does it work?
Answers
  1. A useful action, cookie-only session handling, and no unpredictable parameters in the request.
  2. The victim's browser attaches the cookie automatically to the forged request; the attacker only needs the browser to send it.
  3. Anti-CSRF tokens: a secret random value the server requires in every state-changing request. The attacker's page can't read it, so it can't include the correct value.

Your lab

  • PortSwigger CSRF: https://portswigger.net/web-security/csrf

Writeup: the vulnerable action, which ingredient made it possible, the proof from the lab, and the fix (token + SameSite + no-GET changes).


Summary in your language

தமிழில் சுருக்கம்

CSRF என்பது ஒரு தீங்கிழைக்கும் page, login செய்துள்ள பயனரின் browser-ஐ வேறொரு தளத்துக்கு request அனுப்ப வைப்பது — browser தானாக cookie-களைச் சேர்ப்பதால் அந்தத் தளம் அதை ஏற்றுக்கொள்கிறது. Attacker-க்கு உங்கள் கடவுச்சொல்லோ cookie-யோ தேவையில்லை. SameSite cookie அமைப்பு பல தாக்குதல்களைத் தடுக்கிறது, ஆனால் எல்லாவற்றையும் அல்ல. தீர்வு: anti-CSRF tokens, SameSite=Lax/Strict, GET மூலம் மாற்றங்கள் செய்யாமை, முக்கியச் செயல்களுக்கு மீண்டும் உறுதிப்படுத்தல்.

हिंदी में सारांश

CSRF में एक नुकसानदेह page, login किए हुए user के browser से किसी दूसरी site पर request भिजवाता है — और वह site उसे मान लेती है क्योंकि browser अपने-आप cookies जोड़ देता है। Attacker को आपका password या cookie नहीं चाहिए। SameSite cookie setting कई attacks रोकती है, पर सब नहीं। इलाज: anti-CSRF tokens, SameSite=Lax/Strict, GET से कोई बदलाव नहीं, और ज़रूरी कामों के लिए दोबारा पुष्टि।

Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.


What's next

CSRF makes the browser send requests for you — next, make the server do it: SSRF: making the server fetch for you