Cross-site request forgery is easiest to understand as a story about a helpful browser that does one thing too eagerly.

Priya logs into her company’s internal expense portal in the morning. The portal sets a session cookie, and her browser now holds it. At lunch she opens a link from a forum in another tab. That page, controlled by an attacker, quietly contains a hidden form that submits a request to the expense portal’s “change bank account for reimbursements” endpoint. Her browser sends the request, and because it is going to the expense portal’s domain, the browser automatically attaches the portal’s session cookie. The portal sees a valid session from Priya and processes the change. Priya never clicked anything on the portal. Her browser did it on her behalf.

That is CSRF. The attacker never sees Priya’s cookie or the portal’s response. They do not need to. They only need the action to happen.

The conditions that make it possible

CSRF needs three things to line up:

  1. A state-changing action worth triggering: transferring money, changing an email address or bank account, adding a user, approving something.
  2. Ambient authentication: the application relies only on something the browser sends automatically, usually a cookie, sometimes HTTP basic auth or client certificates.
  3. Predictable request parameters: the attacker can construct the full request in advance, with no secret value they cannot know.

Remove any one of these and the attack fails. Every CSRF defence works by breaking one of them.

How it differs from cross-site scripting

People mix up CSRF and XSS because both involve “cross-site” and the browser. The difference is where the attacker’s code runs. In XSS, attacker code runs inside the vulnerable site, so it can read responses and do anything the user can. In CSRF, the attacker’s page stays outside and can only fire requests blindly at the target. XSS is therefore more powerful: an XSS flaw defeats most CSRF protections, because code running inside the site can read the anti-forgery token itself.

Defence 1: anti-CSRF tokens

The classic fix breaks condition 3. The server generates an unpredictable token tied to the user’s session and embeds it in each form or makes it available to the page’s scripts. Every state-changing request must include that token, typically in a hidden field or a custom header. The attacker’s page cannot read pages from the target site (the browser’s same-origin policy stops that), so it cannot learn the token and cannot build a valid request.

Most web frameworks, including Django, Rails, Spring Security, ASP.NET and Laravel, include CSRF token protection, often enabled by default. Problems arise when developers disable it for an endpoint “because the mobile app was failing”, or when the token is checked only if present, which an attacker satisfies by simply leaving it out.

Defence 2: SameSite cookies

The SameSite cookie attribute breaks condition 2 by telling the browser not to attach the cookie to requests that originate from other sites. It has three values:

  • Strict: the cookie is sent only when the request comes from the same site. Even following a link from an email to the site arrives without the cookie, so users appear logged out on that first click. Safe, sometimes inconvenient.
  • Lax: the cookie is sent on top-level navigations using safe methods, such as clicking a link, but not on cross-site form posts, background requests or embedded resources. This blocks the typical CSRF form submission while keeping normal links working.
  • None: the cookie is sent with all cross-site requests, which is needed for some legitimate embedded or third-party scenarios. Browsers require such cookies to also be marked Secure.

Chrome and other Chromium-based browsers began treating cookies without a SameSite attribute as Lax by default in 2020, which reduced CSRF exposure across much of the web. Not every browser applies the same default, though, so relying on browser defaults is risky. Set the attribute explicitly.

SameSite has a subtle limit: “site” means the registrable domain, not the exact origin. A request from one subdomain of your company to another counts as same-site. If any subdomain hosts user content or is poorly secured, it may be able to send requests that carry the cookie. That is one reason tokens are still recommended alongside SameSite.

Defence 3: checking where requests come from

Browsers send headers such as Origin and, in modern browsers, the Sec-Fetch-Site header, indicating whether a request is same-origin, same-site or cross-site. Servers can reject state-changing requests whose origin does not match. This is a useful additional check, especially for APIs, but older clients and some proxies may strip or omit headers, so it is usually combined with other measures.

Design choices that help

Never change state on GET requests. A GET request can be triggered by an image tag or a link prefetch. If visiting a URL deletes something, CSRF becomes trivial and even search engine crawlers can cause damage.

Use explicit authentication headers for APIs. APIs that require a bearer token in an Authorization header, rather than relying on cookies, are not vulnerable to classic CSRF, because a browser does not attach that header automatically. The trade-off is that the token must be stored somewhere the page’s scripts can reach, which raises the stakes of any XSS flaw.

Re-confirm sensitive actions. Requiring the current password or an OTP to change a bank account, a registered mobile number or a payout destination adds a step the attacker’s page cannot complete. Indian banks and payment apps already do this for most high-value changes, and internal enterprise tools should too.

Watch CORS settings. Cross-Origin Resource Sharing headers control which other sites may read your responses. A permissive configuration that reflects any requesting origin and allows credentials effectively lets other sites read your pages, including tokens, which undoes CSRF protection.

CSRF is less common than it was a decade ago, thanks to framework defaults and browser changes. It survives in older applications, internal tools built quickly, and any place where someone switched protection off to fix an integration. When reviewing a feature that changes something important, the test is simple: could another website cause this request to be sent with the user’s cookie, and would the server accept it?

Written by the RP Strativa Team. The expense portal scenario is illustrative.

Leave a Reply

Trending