Cross-site request forgery (CSRF)

Introduction

You are logged in to your bank in one tab. In another tab you open a page that promises free exam notes. Hidden in that page is a form that sends "transfer 9000 taka" to your bank, and it submits itself. Your browser attaches your bank's login cookie to the request automatically, as it does for every request to that site. To the bank, the request looks exactly like one you made.

That is cross-site request forgery (CSRF): another site making your browser send a request you did not intend, using the fact that your browser carries your login with it.

How it works

CSRF needs three things to line up:

  1. The site identifies you by a cookie that the browser sends automatically.
  2. The action can be triggered by a request whose contents the attacker can predict (a form post with known fields).
  3. The site has no check that the request came from its own page.

The attacker never sees the response and never learns your cookie; they only get your browser to send something. That is why CSRF targets actions that change things (transfers, password changes, posts), not pages that show data.

There are two standard defences, and good sites use both:

  • CSRF tokens. The server puts a random, secret value into each of its own forms (and into the session). When a form comes back, the server checks the value. Another site cannot read the bank's pages, so it cannot know the token, and its forged request fails.
  • SameSite cookies. A cookie marked SameSite=Lax (what Chrome and other Chromium-based browsers assume when no value is set) or SameSite=Strict is not sent on cross-site form posts, so the forged request arrives without the login cookie. SameSite=None turns this protection off, and must be paired with Secure.

Checking the request's Origin header against your own site is a further layer.

Example

The bank's transfer handler checks the session cookie and the CSRF token. The real form carries the token; the attacker's page cannot.

// A CSRF token: a secret value the real form carries, which another website cannot read or guess.
const sessions = { "session-abc": { user: "rahim", csrf: "k9Fq2xT7" } };

function transfer(request) {
  const s = sessions[request.cookie];
  if (!s) return "rejected: not logged in";
  if (request.csrf !== s.csrf) return "rejected: missing or wrong CSRF token";
  return "sent " + request.amount + " taka for " + s.user;
}

// The bank's own form includes the hidden token.
console.log(transfer({ cookie: "session-abc", csrf: "k9Fq2xT7", amount: 500 }));
// A trick page on another site makes the browser send the same request; the cookie goes along, the token cannot.
console.log(transfer({ cookie: "session-abc", csrf: undefined, amount: 9000 }));
console.log(transfer({ cookie: "session-abc", csrf: "guess123", amount: 9000 }));
Open in compiler

Line by line

  1. sessions maps a cookie to a logged-in user and that session's CSRF token, k9Fq2xT7.
  2. transfer first looks up the session from the cookie. A missing session means "not logged in".
  3. Then it compares the token sent with the form to the session's token. Any difference rejects the request.
  4. The bank's own form sends the right token: the transfer goes through.
  5. The attacker's page makes the browser send the cookie, but it has no token, or only a guess, so both forged transfers are rejected.

Expected result

sent 500 taka for rahim
rejected: missing or wrong CSRF token
rejected: missing or wrong CSRF token

In the real world

  • Web frameworks such as Django, Rails, Laravel and ASP.NET include CSRF tokens in their forms by default; the danger comes from turning the check off "to make an API call work".
  • Chromium-based browsers treat cookies without a SameSite setting as Lax, which removed many simple CSRF attacks there, but other browsers behave differently, so sites should set it explicitly and keep tokens.
  • APIs that read a token from a custom header (not a cookie) are not open to classic CSRF, because a plain cross-site form cannot set that header.
  • Changing state with a GET request (a link that deletes something) is risky, because even an image tag on another site can trigger a GET.

Common mistakes

  • Using GET for actions that change things. Links and images can fire GET requests from anywhere.
  • Disabling the framework's CSRF check for convenience.
  • Setting SameSite=None on the session cookie without a real need.
  • Thinking HTTPS prevents CSRF. The forged request is a perfectly valid HTTPS request.

Best practices

  • Use POST (or PUT and DELETE) for every change of state.
  • Keep the framework's CSRF tokens on, for every form and state-changing request.
  • Set session cookies to SameSite=Lax or Strict, plus Secure and HttpOnly.
  • For very sensitive actions, such as changing a password, ask for the current password or a second factor again.

Summary

Cross-site request forgery makes your browser send a request to a site where you are logged in, from another site, relying on the browser adding your cookie automatically. The attacker cannot read the response, so CSRF targets actions that change state. A secret CSRF token in the site's own forms, which other sites cannot read, and SameSite cookies, which are not sent on cross-site posts, are the standard defences, with Origin checks and re-authentication as extra layers.

Quick check

1. In a CSRF attack, what does the attacker's site get?
2. Why can't the attacker's page include the right CSRF token?
3. Which change makes a site more exposed to CSRF?
4. A site deletes a post when you visit /delete?id=5. Why is that dangerous?

Key takeaways

  • CSRF uses cookies the browser sends automatically; the attacker cannot read the reply.
  • CSRF tokens and SameSite cookies are the standard defences; use both.
  • Never change state with GET requests.
  • HTTPS does not stop CSRF; the forged request is valid HTTPS.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.