CCyberwiki

Cross-site request forgery

Cross-site request forgery
ClassAccess control
Also known asCSRF, XSRF, session riding
CWECWE-352: Cross-Site Request Forgery
OWASP Top 10A01:2025 Broken Access Control
Typical severityMedium to High, by what the forged action changes
AffectsApps that identify a signed-in user by a cookie alone and change data on a form post or a GET request

In cross-site request forgery (CSRF, also written XSRF and called session riding), a page on another site makes a visitor's browser send a request to a web application where the visitor is signed in. The browser attaches the visitor's cookies, and the application carries out the request as if the visitor had made it.1

Description

A browser adds the cookies it holds for a site, the session cookie among them, to every request sent to that site, whichever page started the request.2 An application that identifies the user by that cookie alone learns that a request came from the user's browser, and nothing about whether the user meant to send it.34 The attack works when a request changes data, cookies alone identify the sender, and the attacker can predict every parameter.1

CSRF is separate from cross-site scripting: the attacker's code stays on the attacker's own page, so a site free of XSS still needs a CSRF defence.3

Mechanism

An app at app.example.com changes the account email through a form posting to /account/email. An attacker's page on another domain holds a filled-in copy of the form and sends it on load:

<form action="https://app.example.com/account/email" method="POST">
  <input type="hidden" name="email" value="attacker@example.net">
</form>
<script>document.forms[0].submit();</script>

A signed-in user who opens the page sends the form with their session cookie, and the app changes the address. A form post is a "simple request", which browsers send across sites without asking the target first.1 An action that runs on GET needs no form: an <img> tag pointing at it makes the browser call it.1

Impact

The forged request can do what the victim's account can do in the app: change a password, make a purchase, move money or raise an account's privileges.2 An administrator's session can hand over the whole application.4 The OWASP Top 10:2025 maps CSRF to Broken Access Control,5 since the app performs an action the attacker may not perform, with the victim's authority.

In login CSRF, a forged sign-in puts the victim in the attacker's account; card details the victim then saves there become usable by the attacker.2

Notable incidents

On 29 September 2008, Bill Zeller and Ed Felten of Princeton University published four CSRF flaws in large websites.3 At ING Direct, forged requests could open new accounts in a user's name and transfer money out of the user's accounts, which the researchers believed was the first CSRF flaw to move funds out of a financial institution.3 ING's use of SSL did not stop it.6 On YouTube, almost every user action could be forged. On MetaFilter, one forged request set the account email to the attacker's address and a second started the password reset, which mailed the password there. The New York Times, told in September 2007 that its "Email This" feature could mail a visitor's address to an attacker, fixed it on 1 October 2008. All four flaws were fixed.3

Detection

For each route that changes data, find what proves the request came from the app's own pages: a token, an Origin or Sec-Fetch-Site check, or a custom header. A route that changes data on GET is a finding on its own, as is code that switches off a framework's CSRF check. Check the session cookie's Set-Cookie header for SameSite, and whether the app's CORS rules allow credentials for other origins.

Prevention

Framework protection

The first defence is the one the framework already ships.2 In Django, CsrfViewMiddleware is on by default and each POST form to the app's own URLs carries the token tag; a form posting to an external site must not, or the token leaks.7

<form method="post">{% csrf_token %}
  ...
</form>

Next.js Server Actions accept only POST and compare the Origin header with the Host header, rejecting the request when they differ; apps behind a proxy list extra hosts under experimental.serverActions.allowedOrigins.8 Server Actions use no tokens, and a custom Route Handler gets no automatic check, so its protection is written by hand.9

Tokens

In the synchronizer token pattern, the server places an unpredictable value in each page and rejects any data-changing request that does not send it back.1 The token never goes in a URL, where logs and history record it.2 An app with no server-side session can use a signed double-submit cookie, an HMAC-SHA-256 of a session-bound value made with a server secret. The unsigned version fails against anyone who can set cookies on the domain, for example from a taken-over subdomain.2

Fetch metadata and Origin checks

Browsers add a Sec-Fetch-Site header that says whether a request came from the same origin, the same site, another site, or the user directly.1 This Express route, adapted from MDN, accepts only the first two:

app.post("/transfer", (req, res) => {
  const site = req.headers["sec-fetch-site"];
  if (site !== "same-origin" && site !== "same-site") {
    return res.status(403).end();
  }
  // change data
});

All major browsers have sent these headers since March 2023. Keep a fallback that compares Origin, then Referer, with the full origin, so that example.org.attacker.com fails. A missing header or Origin: null does not count as same-origin.2

Requests sent from JavaScript

A fetch() call with Content-Type: application/json or a custom header is not a simple request, and browsers block it across origins by default.1 The browser first sends a preflight request.2 A CORS policy that names the attacker's origin and allows credentials removes this protection.1

SameSite cookies

The SameSite attribute backs up the defences above. Strict keeps the cookie off every cross-site request; Lax sends it on top-level navigation with a safe method such as GET.1 Chromium treats a cookie without the attribute as Lax and Firefox and Safari do not, so the attribute is set explicitly.2 "Same site" covers every subdomain of the domain.1

Set-Cookie: session=<value>; Secure; HttpOnly; SameSite=Lax; Path=/

Other measures

No action changes data on a GET request.4 A password change can ask for the password again; a CAPTCHA does not stop CSRF. An XSS flaw defeats every defence here, because injected script sends requests from the app's own page.2

Prompt for a coding agent

Find every route, form and server action in this project that changes data. Check that each one proves the request came from this app with a CSRF token or an Origin or Sec-Fetch-Site check. Turn on the built-in CSRF protection and remove code that switches it off. Move any action that runs on GET to POST. Set SameSite=Lax on the session cookie. List each route you changed and any you were unsure about.

In SecFlash

SecFlash reports code that switches off a framework's cross-site request check as SF-SEC-739 on the Security page. During the ZAP scan, a form without an anti-CSRF token is reported as SF-SEC-611 and a cookie without SameSite as SF-SEC-630; SF-SEC-501 reports missing cookie flags, SameSite among them.

See also

References

  1. a b c d e f g h i j Mozilla (29 September 2026). "Cross-site request forgery (CSRF)". MDN Web Docs.
  2. a b c d e f g h i j "Cross-Site Request Forgery Prevention Cheat Sheet". OWASP Cheat Sheet Series.
  3. a b c d e Zeller, Bill (29 September 2008, updated 15 October 2008). "Popular Websites Vulnerable to Cross-Site Request Forgery Attacks". CITP Blog. Princeton University.
  4. a b c "CWE-352: Cross-Site Request Forgery". Common Weakness Enumeration. MITRE.
  5. back "A01:2025 Broken Access Control". OWASP Top 10:2025. OWASP Foundation.
  6. back Goodin, Dan (30 September 2008). "Nasty web bug descends on world's most popular sites". The Register.
  7. back "How to use Django's CSRF protection". Django documentation (5.2).
  8. back "How to think about data security in Next.js". Next.js Docs. Vercel.
  9. back "How to Think About Security in Next.js". Next.js Blog. Vercel.