CSRF (Cross-Site Request Forgery) — Security

CSRF (Cross-Site Request Forgery) abuses the browser attaching cookies to every request automatically. The attacker's page evil.example makes the victim's…

Budi only opened one link from a promo email. He didn't fill anything in or press a transfer button. Yet his online bank balance dropped by ten thousand dollars, and the bank's server logged that transfer as coming from Budi's own login session.

The hole used here is called CSRF (Cross-Site Request Forgery): the attacker's site makes the victim's browser send a request to a site where the victim is logged in, without the victim noticing. This works because the browser automatically attaches a site's cookies to every request to that site, whoever asked for it. Picture a regular courier who always sticks your member card on every parcel to your favourite shop, no matter who gave the courier the parcel. A stranger only has to give the courier a parcel saying "transfer ten thousand dollars". The shop sees your member card on the parcel, so it thinks the parcel comes from you. The courier is the browser, the member card is the cookie, the parcel is the request, the shop is the bank's server, and the stranger is the attacker's site.

How the attack happens

In the Lab, Budi is logged in to bank.example with a balance of $15,000. The server gave him the cookie session=7f3a9c. As long as that cookie is there, every request to bank.example is treated as coming from Budi.

Then Budi opens evil.example from a link in an email. That page makes Budi's browser send a form POST to https://bank.example/transfer, for example with a hidden form that JavaScript sends automatically. Budi doesn't press anything. A form may be sent to any site, so the browser does what the page says.

This is the request that reaches the server, if the cookie is allowed to go along:

POST /transfer HTTP/1.1
Host: bank.example
Origin: https://evil.example
Cookie: session=7f3a9c

to=hacker-account&amount=10000

The server sees session=7f3a9c, recognises Budi, and moves $10,000. The balance drops to $5,000. The server can't tell this request apart from Budi's own click, because this server checks only the cookie. The attacker never sees the cookie either: the browser attaches it.

CSRF needs three things:

SameSite: when the cookie goes along

The SameSite flag on a cookie (an attribute covered in the Cookie Security lesson) decides whether the cookie goes along on requests that come from another site (cross-site). The server sets it when it sends the cookie:

Set-Cookie: session=7f3a9c; Path=/; Secure; HttpOnly; SameSite=Lax

Budi's cookie uses SameSite=Lax. Before you look at the table, guess: when evil.example sends a form POST to bank.example/transfer, does that cookie go along or not?

Request to bank.example None Lax Strict
Form POST from evil.example sent not sent not sent
Link click from evil.example (GET, a page change) sent sent not sent
<img> or fetch from evil.example sent not sent not sent
Request from bank.example itself sent sent sent

So with Lax, the form POST from evil.example is sent without the cookie. The server doesn't recognise Budi, and no transfer happens. That's the answer to the guess.

CSRF token: a secret the attacker can't guess

SameSite alone isn't enough, because its rules differ between browsers. The main defence is a CSRF token: a random string the server makes for each session. The server puts that token in every form, then checks it against the session's token when the form is sent. The evil.example page can't read the contents of a bank.example page, because of the browser's same-origin rule (a page from one site may not read the contents of another site's page). So evil.example doesn't know the token.

In Laravel, you just write @csrf in the Blade form:

<form method="POST" action="/transfer">
  @csrf
  <input name="to">
  <input name="amount">
  <button>Transfer</button>
</form>

The fake form on evil.example has no correct _token, so its request is refused with 419, even when the cookie goes along.

Checking the token on the server

This is the heart of the check, written as a plain function. sessionToken is the token the server keeps for that session, and requestToken is the token sent with the form:

function validateCSRFToken(sessionToken, requestToken) {
  if (!sessionToken || !requestToken) return false; // empty, null, undefined: refuse
  return sessionToken === requestToken; // must be exactly the same
}

console.log(validateCSRFToken('abc123', 'abc123')); // true
console.log(validateCSRFToken('abc123', 'xyz789')); // false
console.log(validateCSRFToken('abc123', '')); // false

In the exercise, your function is tested with these five calls:

Call Result Why
validateCSRFToken('abc123', 'abc123') true exactly the same
validateCSRFToken('abc123', 'xyz789') false different
validateCSRFToken('abc123', '') false the request token is empty
validateCSRFToken('abc123', null) false there is no request token
validateCSRFToken('', '') false both are empty

Laravel itself compares tokens with hash_equals. === can stop at the first character that differs, so the time it takes can reveal how many leading characters are right. The time hash_equals takes is always the same, whether the token differs at the first character or at the last. So the time the check takes gives nothing away.

💡 The Origin header and APIs with a Bearer token

The browser also sends an Origin header (the address of the site the request comes from) on POST requests. In the attack request above, it holds https://evil.example. The server can refuse a request whose Origin isn't https://bank.example, as an extra layer next to the token.

An API where login uses an Authorization: Bearer ... header (the token travels in a header, not a cookie) is naturally safe from CSRF. The browser never attaches that header by itself, so evil.example can't use it. But as soon as your API uses a cookie for login, treat it like a form: add a token.

⚠️ CSRF holes we open ourselves

TL;DR: CSRF (Cross-Site Request Forgery) abuses the browser attaching cookies to every request automatically. The attacker's page evil.example makes the victim's browser send POST /transfer to bank.example, and the bank's server thinks the request comes from the victim. The cookie flag SameSite=Lax holds the cookie back on form POSTs and <img> from other sites, but still sends it when a link is clicked. Strict holds it back every time; None always sends it. The main defence is a CSRF token, a random string per session that is put in the form and can't be read by other sites. In Laravel, @csrf puts _token in the form, and a wrong token is refused with status 419. The check: refuse an empty, null or undefined token first, then compare with ===. Never change data through GET, and a POST form without a CSRF token is vulnerable.

⚡ The browser attaches the cookie itself

Write:

POST /transfer HTTP/1.1
Host: bank.example
Origin: https://evil.example
Cookie: session=7f3a9c

Meaning: CSRF (Cross-Site Request Forgery): the attacker's page evil.example makes the victim's browser send a request to a site where the victim is logged in, here bank.example. The browser attaches the login cookie session=7f3a9c automatically, so the server thinks the request comes from the victim.

⚡ SameSite holds the cookie back

Guess first: a cookie with SameSite=Lax, and evil.example sends a form POST to bank.example/transfer. Does the cookie go along?

It doesn't. The server doesn't recognise the victim, so no transfer happens. From another site, Lax only sends the cookie when you change page through a link (GET).

Meaning: SameSite is a cookie flag that decides when the cookie goes along on requests from another site. Strict holds the cookie back on every such request, None always sends it. The rules differ between browsers, so SameSite alone isn't enough.

⚡ A CSRF token

Write:

<form method="POST" action="/transfer">
  @csrf

Meaning: A CSRF token is a random string per session, put in every form. In Laravel, @csrf puts it in as _token. evil.example can't read the bank.example page, so it doesn't know the token. A POST form without a CSRF token is vulnerable, and Laravel refuses a wrong token with status 419.

⚡ Refuse empty values first

Write:

if (!sessionToken || !requestToken) return false; // empty, null, undefined: refuse
return sessionToken === requestToken; // must be exactly the same

Meaning: sessionToken is the token the server keeps, and requestToken is the token the form sent. The first line refuses an empty, null or undefined token. Without that line, two empty tokens would pass, because '' === '' is true. One more rule: never change data through GET, because the CSRF token isn't checked on GET.

What you will learn