What is cross-site request forgery (CSRF)?

Cross-site request forgery (CSRF) is a vulnerability where another website makes a logged-in user's browser send a state-changing request to your app, and the app accepts it because the browser attached the session cookie. The user never meant to send it: the cookie proves who they are, not that they intended the action.

Part of the roadmap: Stage 5, Deeper web and APIs

Why it matters

MITRE files CSRF as CWE-352, and the OWASP Top 10:2025 maps that weakness to A01, broken access control. It matters for any app that authenticates with cookies, because the browser adds those cookies to a request for your domain whichever site started it. A hidden form on a page the attacker controls can then change a user's email, password or settings with their own session.

Browsers now help: a cookie without a SameSite attribute is treated as SameSite=Lax by Chromium, which keeps it off most cross-site POST requests. That is a default, not a fix. Older browsers, cookies set with SameSite=None, GET requests that change data and requests from a sibling subdomain all fall outside it.

Example: a form with no token, on CyberGym and on your app

CyberGym, the legal practice target (OWASP Juice Shop), has a username form on its profile page with no hidden anti-CSRF field. Replaying its POST with the session cookie but an Origin header from a different site was accepted: the server answered 302 and the username changed. Without the cookie the same request was refused, so the cookie did all the authenticating.

To check your own app (https://your-app.example), list every request that changes state, replay each one from a foreign Origin with only the cookie, and see whether it succeeds. The server-side fix checks something a foreign page cannot supply:

// Reject state-changing requests that did not come from your own pages
app.post("/settings", (req, res, next) => {
  if (req.get("Origin") !== "https://your-app.example") return res.sendStatus(403);
  if (req.body.csrfToken !== req.session.csrfToken) return res.sendStatus(403);
  next();
});

Set the session cookie with SameSite=Lax or Strict as well. A token, an Origin check and a SameSite cookie each close part of the gap, and together they cover the cases one alone misses.

Common mistakes

  • Changing data on a GET request. An image tag can send a GET. Keep every state change on POST, PUT, PATCH or DELETE.
  • Relying on SameSite alone. It is a browser default with exceptions. Keep a token or an Origin check on the server.
  • Thinking a JSON API is immune. It is only safe if it rejects form content types and checks Origin; if it reads a cookie, test it.
  • Putting the token in a cookie and checking only that cookie. The browser sends that cookie too. Compare it with a value the page sends in the body or a header.

Related terms

Go deeper

Sources

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. Practise on CyberGym.