CYBER SECURITY • APPLICATION AND WEB SECURITY

CSRF & Mitigations — Explain CSRF conceptually and mitigations (tokens/same-site) (conceptual)

Understanding how cross-site request forgery exploits browser trust and how tokens and cookie policies defend against it.

Historical Context & Motivation

Web applications emerged in the mid-1990s as thin clients that relied on the stateless HTTP protocol, which by design attaches no persistent identity to a request. To bridge this gap, cookies were introduced by Netscape in 1994, allowing servers to associate state with a browser session. This convenience, however, created a fundamental assumption: any request arriving with valid cookies must have been intentionally issued by the authenticated user. Attackers quickly realized they could abuse this assumption by tricking a victim's browser into sending requests the victim never intended to make. The class of attacks that exploits this trust became known as Cross-Site Request Forgery, or CSRF (sometimes pronounced "sea-surf"). Understanding CSRF requires recognizing that the browser, not the user, is the entity that attaches credentials to outgoing HTTP requests — and the browser cannot distinguish a legitimate click from a forged one embedded on a malicious page.

1994
Cookies Introduced
Netscape introduces HTTP cookies to maintain session state, establishing the ambient-authority model that CSRF later exploits.
2001
CSRF Named and Formalized
Security researcher Peter Watkins coins the term "Cross-Site Request Forgery" on the Bugtraq mailing list, distinguishing it from XSS and session hijacking.
2008
Major Real-World Exploits
Netflix, Gmail, and several banking applications are found vulnerable to CSRF. The attack against Gmail allowed an attacker to steal a victim's entire contact list through a forged GET request.
2016
SameSite Cookie Attribute
The IETF publishes RFC 6265bis introducing the SameSite cookie attribute, enabling browsers to restrict when cookies are sent on cross-origin requests.
2020
SameSite=Lax by Default
Chrome 80 and other major browsers adopt SameSite=Lax as the default cookie behavior, significantly reducing the attack surface for CSRF without requiring any server-side changes.

The central question CSRF addresses is deceptively simple: how can a server verify that a state-changing request genuinely reflects the user's intent rather than an attacker's manipulation? This question has driven decades of research into token-based defenses, origin-checking heuristics, and browser-level protections that continue to evolve today.

Core Principles & Definitions

To understand CSRF deeply, several foundational concepts must be established. The attack and its mitigations revolve around the interplay between the browser's credential-management policies, the same-origin policy, and the distinction between reading data and triggering side effects. CSRF is categorized as a confused deputy attack — the browser (the deputy) is tricked into using its authority (the user's cookies) on behalf of a malicious principal (the attacker's page).

1

Ambient Authority

Browsers automatically attach cookies, HTTP authentication headers, and client-side TLS certificates to every request matching the credential's scope. The server has no built-in way to determine whether the user actively chose to make that request.
2

Same-Origin Policy (SOP)

The SOP restricts scripts on one origin from reading responses from another origin. Critically, it does not prevent cross-origin requests from being sent — only from being read. CSRF exploits the gap between sending and reading.
3

State-Changing Operations

CSRF targets operations that modify server-side state: transferring funds, changing passwords, or deleting resources. Safe (idempotent) methods like GET should never cause side effects, but many real-world applications violate this HTTP semantic.
4

Origin vs. Site

An origin is the tuple (scheme, host, port). A site is a broader concept based on the registrable domain (eTLD+1). The SameSite cookie attribute operates at the site level, while CORS operates at the origin level — a distinction critical for configuring defenses.
KEY TAKEAWAY
Think of CSRF like a sealed, pre-addressed envelope. Your browser is a mailroom clerk who dutifully stamps and sends any envelope bearing your return address (cookies), regardless of who dropped it in the outbox. The clerk cannot tell whether you wrote the letter or a stranger slipped it in. CSRF mitigations are analogous to requiring a secret code inside the envelope that only you could have written — something the stranger cannot forge because they never see your internal documents.

Anatomy of a CSRF Attack

The following diagram illustrates the sequence of events in a typical CSRF attack. The victim is authenticated to a legitimate banking application and, in a separate browser tab, visits a malicious website controlled by the attacker. The malicious page contains a hidden form or image tag that causes the browser to issue a forged request to the bank, complete with the victim's session cookie.

The CSRF attack relies on the browser's automatic attachment of cookies (Step 3). The bank server cannot distinguish this forged request from a legitimate one because both carry the same valid session cookie.

Notice that the attacker never needs to steal the victim's cookie. The attack works precisely because the attacker does not need to read the server's response — the same-origin policy blocks that. CSRF only requires that the browser sends the request with attached credentials, which the browser does automatically. This is the crucial distinction between CSRF and cross-site scripting (XSS): XSS steals data by reading it, while CSRF takes actions by writing to the server without ever seeing the response.

How CSRF Attacks Are Delivered

Attack Vectors

CSRF attacks can be delivered through several mechanisms, each exploiting the browser's willingness to issue cross-origin requests. The simplest vector is an image tag: an attacker embeds <img src="https://bank.com/transfer?to=attacker&amount=5000"> on their page, which causes the browser to issue a GET request to the bank's endpoint. If the bank processes state-changing operations on GET (a violation of HTTP semantics), the transfer executes silently. More sophisticated attacks use hidden forms with JavaScript auto-submission. The attacker crafts a <form> element with method="POST" and action="https://bank.com/transfer", populates hidden fields with the desired parameters, and calls form.submit() via JavaScript the moment the page loads.

Why the Same-Origin Policy Doesn't Help

Students often assume the same-origin policy prevents CSRF, but this is a common misconception. The SOP governs what a script on one origin can read from another origin, not what it can send. Cross-origin form submissions, image loads, script includes, and certain fetch() requests with simple content types are all permitted by browsers without CORS preflight checks. The attacker does not need to read the bank's response — they only need the server to process the request. The SOP thus provides confidentiality (the attacker cannot read the response) but not integrity (the attacker can still trigger side effects).

Formal Characterization of the Vulnerability

A CSRF vulnerability exists when three conditions are simultaneously met. First, the application performs a state-changing action that an attacker wants to trigger (e.g., transfer funds, change email). Second, the application relies solely on ambient credentials (cookies or HTTP authentication) to authorize the request, with no additional unpredictable parameter. Third, the parameters of the request are fully predictable — the attacker can construct the exact request body or query string without knowing any secret value unique to the user's session.

  • Condition 1: A relevant state-changing action exists (e.g., POST /changePassword)
  • Condition 2: Authorization relies only on cookies — no additional secret token is required
  • Condition 3: All request parameters are predictable by the attacker
⚠️ Common Misconception
CSRF does not require the attacker to steal any credentials. The victim's browser voluntarily sends its cookies to the target server. This is by design — cookies are scoped to domains, not origins, and the browser has no mechanism to know whether the human behind the keyboard initiated the request.

CSRF Mitigation Strategies

Mitigating CSRF involves breaking at least one of the three conditions identified in the previous section. The most widely deployed defenses fall into two categories: token-based defenses that make request parameters unpredictable, and browser-level defenses that restrict when cookies are attached to cross-site requests. A robust application typically layers both approaches as defense in depth.

The Synchronizer Token Pattern works because the CSRF token introduces an unpredictable parameter that the attacker cannot forge. The same-origin policy prevents the attacker from reading the bank's HTML to extract the token.

Token-Based Defenses

The Synchronizer Token Pattern is the most established defense. When a user requests a form, the server generates a cryptographically random token, stores it in the user's session, and embeds it as a hidden field. When the form is submitted, the server compares the submitted token against the stored value. Because the attacker cannot read cross-origin responses, they cannot extract the token from the form's HTML. A variation called the Double-Submit Cookie pattern avoids server-side session storage: the server sets a random value in a cookie and also requires the same value in a request header or body parameter. Since an attacker can cause the browser to send cookies but cannot read or set cookies for another domain (absent other vulnerabilities), they cannot replicate the value in the request body.

SameSite Cookie Attribute

The SameSite cookie attribute instructs the browser to restrict when a cookie is sent along with cross-site requests. It accepts three values: Strict, Lax, and None. With Strict, the browser never sends the cookie on any cross-site request, including top-level navigations — meaning that if a user clicks a link on evil.com to bank.com, they arrive unauthenticated. With Lax, cookies are sent on top-level GET navigations (so link clicks work) but withheld from cross-site POST requests and subresource loads (images, iframes). Since most CSRF attacks require POST, Lax provides strong default protection. None disables the restriction entirely and requires the Secure flag, sending the cookie on all requests as in the pre-SameSite era.

Origin and Referer Header Checking

An alternative or supplementary defense is to inspect the Origin or Referer HTTP headers on incoming requests. A legitimate request to bank.com should carry Origin: https://bank.com; a forged request from evil.com carries Origin: https://evil.com. While conceptually simple, this approach has reliability concerns: some browsers, proxies, or privacy extensions may strip these headers, and misconfigured servers that accept requests without an Origin header create exploitable gaps. It is therefore typically used as a defense-in-depth measure rather than a sole mitigation.

Worked Example: Identifying and Mitigating a CSRF Vulnerability

Consider a social media application where authenticated users can change their email address by submitting a POST request to /account/email with the parameter new_email. The application authenticates users via a session cookie named sid and does not include any CSRF token. We will walk through identifying the vulnerability, crafting a proof-of-concept exploit, and implementing a mitigation.

Identifying and Fixing a CSRF Vulnerability
1
Step 1 — Identify the Vulnerable EndpointExamine the request that changes the user's email. The legitimate request looks like: POST /account/email HTTP/1.1 with body new_email=user@example.com and header Cookie: sid=abc123. Check the three CSRF conditions: (1) it is state-changing, (2) authorization relies on the cookie alone, and (3) the parameter new_email is fully predictable by an attacker.
All three conditions met — CSRF vulnerability confirmed.
2
Step 2 — Craft a Proof-of-Concept ExploitThe attacker creates a page on evil.com containing a hidden auto-submitting form: <form method="POST" action="https://social.com/account/email"><input type="hidden" name="new_email" value="attacker@evil.com"></form><script>document.forms[0].submit();</script> When the victim visits this page while logged into social.com, the browser submits the form with the victim's sid cookie, changing their email to the attacker's address — enabling a password reset takeover.
Exploit silently changes victim's email to attacker@evil.com.
3
Step 3 — Apply Synchronizer Token MitigationOn the server side, generate a 128-bit cryptographically random token when the email-change form is requested: csrf_token = secrets.token_hex(16). Store this token in the user's session and embed it in the form as <input type="hidden" name="csrf_token" value="...">. On POST, compare the submitted token to the session-stored token. Reject the request if they do not match.
Token breaks Condition 3 — parameters are no longer predictable.
4
Step 4 — Add SameSite Cookie DefenseAs a second layer, configure the session cookie with the SameSite attribute: Set-Cookie: sid=abc123; SameSite=Lax; Secure; HttpOnly. With SameSite=Lax, the browser will not attach the sid cookie to the cross-site POST from evil.com. Even if the token check had a bug, this browser-level defense independently blocks the attack.
SameSite=Lax breaks Condition 2 — cookies are not attached to cross-site POST.
5
Step 5 — Verify Defense in DepthTest the mitigations: replay the proof-of-concept exploit. The browser should either (a) not send the cookie at all (SameSite defense), or (b) if SameSite is not enforced (older browsers), the server should reject the request due to the missing or incorrect CSRF token. Log rejected CSRF attempts for monitoring. Together, the token and SameSite attribute provide overlapping protection against CSRF.
Both layers active — defense in depth achieved.

Comparing CSRF Mitigation Approaches

No single CSRF mitigation is perfect for every scenario. Token-based approaches require careful implementation on every state-changing endpoint, while browser-level defenses depend on client adoption and can break legitimate cross-site workflows. The table below compares the major approaches across several dimensions that practitioners must weigh when designing a defense strategy.

Comparison of major CSRF mitigation strategies
DefenseStrengthsLimitations
Synchronizer TokenWell-understood, framework support (Django, Rails, Spring), works on all browsers, server has full control.Requires server-side session state, easy to forget on new endpoints, token leakage via Referer header if embedded in URLs.
Double-Submit CookieStateless (no server session needed), simple to implement in SPAs and APIs.Vulnerable if attacker can set cookies on sibling subdomains (subdomain takeover), requires HTTPS to prevent cookie injection.
SameSite=LaxNo server code changes needed, default in modern browsers, blocks cross-site POST automatically.Does not protect GET-based state changes, older browsers ignore the attribute, can break legitimate cross-site SSO flows.
SameSite=StrictStrongest cookie restriction, blocks all cross-site cookie attachment including top-level navigations.Users arriving via external links (email, search) appear unauthenticated, degrading UX significantly.
Origin/Referer CheckZero overhead, no tokens to manage, easy to centralize in middleware.Headers can be stripped by proxies/extensions, Referer may leak sensitive URL paths, requires careful null-origin handling.
KEY TAKEAWAY
In practice, CSRF defense mirrors the security principle of defense in depth. Just as a castle uses both a moat and a portcullis rather than relying on a single barrier, modern web applications should combine a token-based defense (the portcullis that blocks forged requests) with SameSite cookies (the moat that prevents credentials from reaching the attacker's requests in the first place). Neither alone is sufficient, but together they create overlapping protections that are extremely difficult to circumvent.

Connection to Advanced Web Security

CSRF does not exist in isolation — it interacts with and is sometimes amplified by other web vulnerabilities. A Cross-Site Scripting (XSS) vulnerability on the target site completely defeats token-based CSRF defenses because the attacker's injected script runs in the same origin and can read the CSRF token from the DOM. This is why OWASP ranks XSS and CSRF as complementary threats: eliminating CSRF without also preventing XSS leaves the application vulnerable, since XSS provides a bypass for most CSRF mitigations. Advanced defensive architectures move toward capability-based authorization, where each request carries an unforgeable bearer token (such as a JWT in an Authorization header) rather than relying on ambient cookies. This fundamentally eliminates CSRF because the attacker's page cannot programmatically access or attach the bearer token from another origin's JavaScript context.

Traditional cookie-based auth vs. modern bearer-token architectures
ConceptTraditional (Cookie-Based)Modern (Token-Based Auth)
Credential AttachmentAutomatic — browser attaches cookies to every request matching the domainManual — JavaScript explicitly sets Authorization header; not sent automatically
CSRF RiskInherent — ambient authority enables CSRF by designEliminated — attacker cannot set custom headers on cross-origin requests
XSS Impact on CSRFXSS can extract CSRF tokens from the DOM, bypassing token defensesXSS can steal the bearer token from JavaScript memory (mitigated by storing tokens in HttpOnly cookies + CSRF defense)
Example ArchitecturesServer-rendered apps (Django, Rails, PHP sessions)SPAs with API backends (React + REST API with JWT), OAuth 2.0 resource servers

Looking ahead, proposals like the Fetch Metadata request headers (Sec-Fetch-Site, Sec-Fetch-Mode) give servers fine-grained visibility into the context of each request, enabling resource isolation policies that reject cross-site requests at the application edge. Combined with the ongoing evolution of the SameSite attribute and emerging standards like CHIPS (Cookies Having Independent Partitioned State), the web platform is steadily moving toward a model where cross-site credential leakage becomes structurally impossible rather than merely mitigated.

Practice Problems

PROBLEM 1CONCEPTUAL
A student claims that CSRF is impossible because the same-origin policy prevents cross-origin requests. Explain precisely why this claim is incorrect, distinguishing between what the SOP restricts and what it permits.
PROBLEM 2BASIC CALCULATION
A server generates CSRF tokens using a cryptographically secure random number generator producing tokens of 128 bits. If an attacker can submit 1,000 guesses per second and the token is valid for 30 minutes, what is the probability of the attacker guessing the token within the validity window? Express your answer in terms of powers of two and provide a numerical approximation.
PROBLEM 3INTERMEDIATE
A web application uses the double-submit cookie pattern for CSRF protection. An attacker discovers a subdomain takeover vulnerability on images.example.com, which shares the same registrable domain as app.example.com. Explain how the attacker could bypass the CSRF defense and what additional measures would prevent this attack.
PROBLEM 4APPLIED
You are building a single-page application (SPA) that communicates with a REST API on a different subdomain (api.myapp.com). The API uses session cookies for authentication. Design a CSRF defense strategy that accounts for: (a) the cross-origin nature of the requests, (b) the SPA's inability to embed hidden form fields, and (c) the need to support multiple concurrent browser tabs. Justify each design choice.
PROBLEM 5CRITICAL THINKING
Consider a hypothetical future in which all browsers enforce SameSite=Strict on every cookie by default. Would CSRF be entirely eliminated? Analyze potential edge cases, residual attack surfaces, and implications for the web ecosystem. Your answer should consider at least three distinct scenarios where CSRF-like attacks might persist.

Summary & Key Concepts

Cross-Site Request Forgery (CSRF) is a confused deputy attack that exploits the browser's ambient authority model — the automatic attachment of cookies to outgoing requests regardless of who initiated them. A CSRF vulnerability exists when a state-changing endpoint relies solely on cookies for authorization and all request parameters are predictable by the attacker. The same-origin policy does not prevent CSRF because it restricts reading responses, not sending requests.

Effective mitigations include synchronizer tokens (which introduce an unpredictable parameter the attacker cannot forge), the double-submit cookie pattern (stateless variant), and the SameSite cookie attribute (which instructs browsers to withhold cookies from cross-site requests). Defense in depth — layering token-based and browser-level defenses — is the recommended practice, as no single mitigation is universally sufficient. Modern architectures using bearer tokens in Authorization headers eliminate the ambient authority problem entirely, representing a structural shift away from cookie-based CSRF vulnerabilities.

Varsity Tutors • Cyber Security • CSRF & Mitigations