CSRF: requests your users never meant to send

Cross-site request forgery (CSRF, also XSRF) tricks a signed-in user's browser into sending a request to your site that the user did not intend: changing an email address, moving money, deleting data. The browser attaches the user's cookies automatically, so the site sees a valid, authenticated request. The defence is to require proof that the request came from your own pages.

Section Web attacks Updated Weakness CWE-352

Quick facts

Also called
XSRF, session riding, one-click attack
Weakness
CWE-352: cross-site request forgery
Main fix
Anti-forgery tokens and SameSite cookies
Does a WAF stop it?
Little. The forged request is valid and carries the user's own session

Definitions follow OWASP and CWE-352.

How a CSRF attack works

  1. The victim is signed in to your site. Their browser holds a session cookie for it.
  2. The victim visits another page. A malicious site, a forum post or an email the attacker controls.
  3. That page makes the browser send a request to your site. Through a form that submits itself, an image address or a script.
  4. The browser attaches the session cookie. Your site sees an authenticated request and carries it out.

The attacker never sees the response; CSRF is about making the victim act, not about reading. It works only on actions that change something and can be triggered by a request the attacker can predict in full.

WHAT EACH DEFENCE COVERS BLOCKS CROSS-SITE FORM POST BLOCKS SAME-SITE SUBDOMAIN SURVIVES OLD BROWSERS NO PROTECTION SAMESITE COOKIES ANTI-FORGERY TOKEN TOKEN + SAMESITE // a token works on its own; SameSite adds depth
// four setups, and why a token is the core defence

A CSRF example, step by step

Imagine a site where a signed-in user can change the email address on their account by submitting a form with one field. The form has no token, and the session cookie has no SameSite attribute.

An attacker builds a page with an identical hidden form that sets the email to an address they control, and makes it submit itself when the page loads. A user who is signed in and opens that page sends the change without seeing anything. The attacker then asks for a password reset, which goes to the new address, and takes over the account. A token in the real form, unknown to the attacker, would have made the forged submission fail.

CSRF vs XSS: what the difference is

Cross-site scripting puts attacker script inside your site, where it can read pages and act as the user. CSRF works from outside: it can make the user's browser send a request, but it cannot read anything back.

The two interact. A site with an XSS flaw loses most CSRF protection, because script on the site can read the token the same way the page does. Fixing XSS is part of keeping CSRF defences meaningful.

How to prevent CSRF

  1. Use anti-forgery tokens. A secret value tied to the session (a CSRF token), sent in every form and checked on the server; the OWASP CSRF Prevention Cheat Sheet describes the patterns.
  2. Set SameSite on session cookies. With SameSite=Lax or Strict, browsers stop sending the cookie on most cross-site requests; see Set-Cookie on MDN.
  3. Never change state with GET. Actions that change data should require a method a link or image cannot trigger.
  4. Check Origin or Referer on sensitive actions. As an extra layer, not the only one.
  5. Ask again for the most sensitive actions. Re-entering a password or a second factor defeats a forged request.

What a WAF can do about CSRF

Very little on its own. A forged request has the right method, the right parameters and the user's real cookies; nothing in its content looks like an attack. A filter can check headers such as Origin on chosen paths, but the decision about which requests need proof of intent belongs to the application.

That is why CSRF sits in broken access control: the server accepted an action without checking that the user meant it.

Questions

What is CSRF?

CSRF, cross-site request forgery, is an attack that makes a signed-in user's browser send a request to a site the user did not intend, using the user's own cookies to make it look legitimate.

How do you prevent CSRF attacks?

Use anti-forgery tokens checked on the server, set SameSite on session cookies, never change state with GET requests, and ask users to confirm the most sensitive actions.

What is the difference between CSRF and XSS?

XSS runs attacker script inside your site and can read and act. CSRF makes the victim's browser send a request from another site; it can act but cannot read the response.

Is CSRF still a risk with SameSite cookies?

Less of one, but yes. SameSite does not cover requests from same-site subdomains or older browsers, so anti-forgery tokens remain the core defence.