Login
ChallengesLearn
Scoreboard
Teams
SPNZ

LearnAccess ControlCSRF
Access Control·Lesson 8 of 12

CSRF

Cross-Site Request Forgery — the browser sends the cookie automatically. An img tag on a forum triggers a password change on a bank the victim left open in another tab.

Intermediate13 min
Access ControlCSRFWeb
Loading lesson…
PreviousJWT AttacksNextRate Limit & Auth Bypass

© 2026 SPNZ.

Terms of ServicePrivacy PolicyCookie Policy

Cross-Site Request Forgery (CSRF) exploits the browser's automatic cookie inclusion. When a user is authenticated to a website, that site's cookies are sent with every request to its origin — even if the request originates from an attacker's page. A single <img> tag can trigger a state-changing action on a site the victim left open in another tab.

What you'll be able to do
  • Understand how the browser's same-origin cookie behaviour enables CSRF.
  • Distinguish GET CSRF from POST CSRF and identify vulnerable patterns.
  • Explain the 2008 uTorrent CSRF vulnerability that allowed remote code execution.
  • Apply SameSite cookies, CSRF tokens, and custom headers as defences.
Key terms
CSRF
Cross-Site Request Forgery — an attack that forces an authenticated user to execute unwanted actions on a web application by using the browser's automatic cookie inclusion (CWE-352).
SameSite cookie
A cookie attribute that restricts when the cookie is sent. SameSite=Lax (default in modern browsers) blocks cookies on cross-site POST requests. SameSite=Strict blocks all cross-site cookie sends.
CSRF token
A unique, unpredictable value generated by the server and embedded in forms or custom headers. The server validates the token on every state-changing request to ensure the request originated from the genuine UI.
What is it?

The browser follows orders

When a user visits a page on an attacker-controlled site, the attacker can embed HTML elements that trigger requests to other origins. The most common vector is a simple image tag: <img src="https://bank.com/transfer?amount=1000&to=attacker" />. The browser makes a GET request to the bank's server — and since the user has an active session cookie for the bank, the cookie is included automatically. If the bank's transfer endpoint accepts GET requests, the transfer executes.

POST-based CSRF uses a hidden form that auto-submits: <form action="https://bank.com/transfer" method="POST">. The attacker pre-fills the form fields and submits it via JavaScriptform.submit(). The browser sends the POST request with the user's cookies, and the server cannot distinguish this from a legitimate form submission.

CSRF attack flow
Mini Map
Press enter or space to select a node. You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Try it

CSRF Demo

The sandbox below shows two browser tabs side by side: a victim logged into a banking app and an attacker-controlled page. Toggle the SameSite cookie protection to see how the bank website can be protected against CSRF attacks.

CSRF Demodev
Ssecurity
Victim bank session
Account balancesession=abc123
$10,000.00
Recent transactions
Salary deposit+$5,000.00
Rent payment-$2,000.00
Attacker pagemalicious
This page contains a hidden CSRF attack.
HTML source
<img
  src="bank.com/transfer?
    amount=1000&
    to=attacker-account-999"
  width="0"
  height="0"
/>
Cookies: session=abc123
Real-world relevance

uTorrent CSRF (2008)

In 2008, a critical CSRF vulnerability was discovered in uTorrent's web UI. The BitTorrent client exposed a web-based management interface on localhost:8080 that accepted commands via GET requests. An attacker could craft a page with image tags pointing to URLs like http://localhost:8080/gui/?action=download&url=magnet:.... Since the browser treats localhost as a valid origin and uTorrent used cookie-based auth, any website could force the victim's uTorrent client to download arbitrary files.

The attack was compounded by the fact that uTorrent had a "settings" endpoint that could modify the application's startup script path, effectively allowing remote code execution. The uTorrent case demonstrates that CSRF is not limited to web applications — any service with an HTTP interface and cookie-based auth is vulnerable.

Mitigation

SameSite cookies, CSRF tokens, custom headers

Modern browsers ship three CSRF defences. Use all of them.

javascriptvulnerable
// VULNERABLE — GET request transfers money
app.get('/transfer', async (req, res) => {
  await db.transfer(req.query.amount, req.query.to);
  res.send('OK');
});

// SAFE — only accept POST with SameSite cookie + CSRF token
app.post('/transfer', parseForm, csrfProtection, async (req, res) => {
  await db.transfer(req.body.amount, req.body.to);
  res.send('OK');
});

// SAFE cookie config
Set-Cookie: session=abc123; SameSite=Lax; HttpOnly; Secure

// SAFE custom header check (reject if missing X-Requested-With)
app.use((req, res, next) => {
  if (req.method !== 'GET' && !req.headers['x-requested-with']) {
    return res.status(403).send('CSRF protection');
  }
  next();
});

Additional layers: never use GET for state-changing operations; require a custom header like X-Requested-With (which cross-origin requests from simple HTML elements cannot set); and use the Double Submit Cookie pattern where a cryptographically random CSRF token is set as a non-HttpOnly cookie and must also be sent in a custom header.

Further reading
  • CWE-352: Cross-Site Request Forgery(MITRE)
  • uTorrent CSRF Remote Code Execution (2008)(Exploit-DB)
  • SameSite cookie explained (web.dev)(Google)
Key takeaways

What to remember

  • CSRF exploits the browser's automatic cookie inclusion — cookies are sent with every request to the cookie's origin, regardless of what page initiated the request.
  • An <img>, <script>, or hidden form can trigger a GET or POST request that the server treats as legitimate user action.
  • SameSite=Lax (default in modern browsers) blocks cookies on cross-site POST requests but allows GET. SameSite=Strict blocks all cross-site requests but can break legitimate redirect-based flows.
  • CSRF tokens and custom headers provide defence in depth. Require a value the attacker cannot guess or derive from cookies alone.
  • The 2008 uTorrent case showed that even desktop applications with HTTP interfaces are vulnerable to CSRF — cookie-based auth on localhost is not safe from cross-origin requests.

Knowledge check

0/3 answered · 0 correct
  1. 1.What makes CSRF possible?

  2. 2.Which SameSite cookie setting blocks all cross-site cookie sends, including GET requests?

  3. 3.Why does a custom header like X-Requested-With provide CSRF protection?