CSRF

On this page 7

CSRF, or cross-site request forgery, is a web vulnerability in which a malicious site causes a victim’s browser to send an authenticated request to another application where the victim is already logged in, so the application performs an action the user never intended. In application security, CSRF is a classic example of the confused deputy problem: the server trusts the request because the browser automatically attaches the user’s credentials.

CSRF does not let the attacker read the response. Its power lies in state-changing actions, such as changing an email address, transferring funds, adding an administrator or altering settings, which the attacker triggers blindly but effectively.

How a CSRF attack works

A CSRF attack works because browsers automatically include cookies with requests to a site, regardless of which page initiated the request. If an application relies only on a session cookie to identify the user, it cannot tell whether a request came from its own form or from a hostile page.

The attack follows a short sequence. The victim logs in to the target application and keeps the session open. The victim then visits an attacker-controlled page, which contains an auto-submitting form, an image tag or a script pointing at a sensitive endpoint on the target. The browser sends the request with the session cookie, and the server processes it as legitimate.

Three conditions must hold for CSRF to succeed, and removing any one of them prevents the attack.

ConditionWhy it mattersHow to remove it
A relevant action existsSomething valuable can be changedRequire re-authentication for critical actions
Cookie-based session handlingCredentials are attached automaticallyUse SameSite cookies or header-based tokens
No unpredictable parametersAttacker can build the full requestAdd a secret anti-CSRF token
Preconditions for a successful CSRF attack

Defenses against CSRF

The standard defense is a synchronizer token: a random, unpredictable value tied to the user session and embedded in every state-changing form or request header. The server rejects any request without a valid token, and the attacker cannot read the token because of the same-origin policy.

The table compares the main defense mechanisms and their limitations.

DefenseMechanismLimitation
Synchronizer tokenServer-stored secret checked per requestRequires server-side state
Double-submit cookieToken in cookie must match token in requestWeak if subdomains can set cookies
SameSite cookie attributeBrowser withholds cookie on cross-site requestsLax still allows top-level GET navigation
Origin and Referer checksServer verifies the request source headerHeaders may be missing in some cases
Custom request headerAPI requires a header forms cannot setRelies on correct CORS configuration
User re-authenticationPassword or MFA for critical actionsAdds friction for users
CSRF defense mechanisms

Most modern browsers treat cookies without a SameSite attribute as SameSite=Lax by default, which blocks many classic CSRF attacks. This is a valuable defense in depth, not a replacement for tokens, because behavior differs between browsers and Lax still permits some cross-site requests.

CSRF compared with XSS and other web flaws

CSRF exploits the trust a site places in the user’s browser, while XSS exploits the trust a user places in the site. The distinction matters because the defenses are different and one flaw can defeat the protections against the other.

IssueAttacker capabilityPrimary defense
CSRFTriggers actions blindly as the userAnti-CSRF tokens, SameSite cookies
XSSRuns script inside the trusted siteOutput encoding, CSP
SQL injectionManipulates database queriesParameterized queries
ClickjackingTricks the user into clicking hidden UIFrame restrictions in CSP
CSRF and neighboring application security issues

A single XSS flaw defeats CSRF tokens, because injected script runs on the trusted origin and can read the token from the page. For this reason, broken access control and injection flaws listed in the OWASP Top 10 are usually remediated together with CSRF rather than in isolation.

Where CSRF protection is needed

CSRF protection is needed wherever the browser sends credentials automatically. The risk depends far more on the authentication model than on the programming language or framework.

Application typeCSRF riskReason
Server-rendered app with session cookiesHighCookies sent on every request
Single-page app using cookie sessionsHighSame automatic cookie behavior
API with bearer tokens in headersLowBrowser does not attach the token automatically
Login and logout formsModerateLogin CSRF can bind the victim to an attacker account
Intranet apps and device admin pagesHighOften lack any token protection
CSRF exposure by application type

Most mature web frameworks include built-in CSRF middleware that generates and validates tokens. The safest practice is to keep it enabled globally and to exempt endpoints only with a documented reason.

Common CSRF implementation mistakes

Many CSRF vulnerabilities appear in applications that have a defense but apply it inconsistently. Security testing frequently finds the following gaps.

  • Changing state with GET requests, which bypasses form tokens and SameSite Lax protection.
  • Validating the token only when present, so an attacker simply omits it.
  • Using a token that is not bound to the session, which lets an attacker reuse their own valid token.
  • Accepting any content type on JSON endpoints, allowing simple cross-site form posts to reach them.
  • Overly permissive CORS with credentials, which can let other origins read responses and tokens.

The simple explanation

Imagine you are signed in to your bank in one browser tab and then open a harmless-looking page in another tab. That page can secretly tell your browser to send a request to the bank, and because you are still signed in, the bank thinks the request came from you.

CSRF is the name for this trick. Websites stop it by adding a secret code to every real form, one that outside pages cannot see or guess. A request without the right code is simply refused.

How it is used in practice

During a security review of an internal ticketing system, a tester notices that the endpoint for changing a user’s notification email accepts a simple form POST with only the session cookie for identification. The tester builds a page with a hidden auto-submitting form and confirms that a logged-in administrator who visits it has their email silently changed, which would allow an attacker to take over the account through password reset.

The development team enables the framework’s CSRF token middleware for all state-changing routes, sets session cookies to SameSite=Lax with the Secure flag, and requires the current password before email changes. A retest confirms that cross-site requests are now rejected with an error and that no state-changing endpoint accepts requests without a valid token.