appsec.fyi

CSRF — A Practical Guide

A curated AppSec resource library covering XSS, SQLi, SSRF, IDOR, RCE, XXE, OSINT, and more.

CSRF: A Practical Guide

Curated and synthesized by . Last updated 2026-08-16. Synthesized from 73 of 73 curated resources. Browse all 73 CSRF resources →

Problem Framing: The Pervasive Threat of CSRF

Cross-Site Request Forgery (CSRF), also known as XSRF or "sea surfing," is a class of attacks that weaponizes the inherent trust web applications place in authenticated user sessions [1][2][3]. At its core, CSRF exploits the browser's default behavior of automatically attaching session cookies to requests made to a given domain. An attacker crafts a malicious request, often embedded in a seemingly innocuous webpage or email, which a logged-in user's browser then submits to a trusted application. Because the request is accompanied by the user's valid session cookie, the server treats it as legitimate, enabling unauthorized actions on behalf of the victim [4][5][6].

The consequences of a successful CSRF attack can range from minor annoyances, such as changing user preferences, to severe impacts like unauthorized financial transactions, account takeovers, data exfiltration, or even complete system compromise if an administrator is the victim [7][8][5]. Applications that perform state-changing operations without robust validation of user intent are particularly susceptible [1][2]. The threat is persistent, as even modern web architectures can inadvertently introduce vulnerabilities if not carefully secured [9][10].

Core Mechanics: How CSRF Exploits Trust

The fundamental mechanism of a CSRF attack relies on three key conditions being met:

When these conditions align, an attacker can initiate an attack by creating a malicious webpage. This page typically contains an HTML form, an image tag, or a JavaScript snippet designed to trigger a state-changing request to the target application. When a logged-in victim visits this page, their browser automatically includes their session cookie with the forged request. The server, seeing the valid session cookie, processes the request, executing the attacker's desired action without the victim's knowledge [4][2][6][16].

Notable Techniques and Attack Vectors

CSRF attacks can manifest in various ways, often exploiting specific implementation details or lax security configurations.

GET-Based CSRF

One of the simpler forms of CSRF involves endpoints that perform state-changing actions via HTTP GET requests. Since GET requests can be easily triggered by simple HTML elements like tags or links, attackers can embed these in malicious pages. When the victim's browser attempts to load the or clicks the , the GET request is sent, carrying the session cookie and performing the unwanted action [4][2][6][16][17]. RFC 2616 explicitly discourages using GET for state changes, but implementations sometimes deviate from this, creating vulnerabilities [1].

POST-Based CSRF

POST requests, typically used for state-changing operations, require a slightly more involved attack. The attacker crafts an HTML form with hidden input fields containing the attacker-controlled parameters. This form is then automatically submitted via JavaScript when the victim visits the malicious page [4][2][6][16]. The enctype="text/plain" attribute can be particularly useful here, allowing JSON payloads to be sent within a form submission, potentially bypassing some stricter content-type checks [9][14].

JSON Endpoint Exploitation

Modern applications frequently use JSON for data transfer, often via AJAX or fetch requests. Exploiting JSON endpoints for CSRF typically requires either manipulating the Content-Type header or leveraging misconfigured Cross-Origin Resource Sharing (CORS) policies. An application might accept text/plain or application/x-www-form-urlencoded for JSON payloads, allowing a standard HTML form to submit data that the backend parses as JSON [9][14]. Overly permissive CORS policies, particularly those that dynamically set Access-Control-Allow-Origin and Access-Control-Allow-Credentials: true, can also enable cross-origin application/json requests [9].

Bypassing CSRF Token Validation

When applications implement CSRF tokens, attackers must find ways to bypass these protections. Common bypass techniques include:

SameSite Cookie Restrictions Bypass

Browser-level defenses like SameSite cookies aim to mitigate CSRF. However, bypasses exist:

Referrer/Origin Header Validation Bypass

Some applications rely on validating the Referer or Origin headers to prevent CSRF. However, these headers can be absent (e.g., tags without crossorigin attribute) or manipulated in certain scenarios. Attackers can also bypass simple regex checks on these headers by using lookalike subdomains or carefully crafted URLs [3][14][26].

Chaining Vulnerabilities

CSRF is often more impactful when chained with other vulnerabilities. For instance, a Stored XSS vulnerability can be used to inject a CSRF payload directly into a trusted website, significantly increasing the attack's reach and impact [27]. A critical impact is observed when CSRF leads to Remote Code Execution (RCE) via arbitrary file uploads, as seen in some WordPress themes [28].

Detection and Prevention Strategies

Robust defense against CSRF requires a multi-layered approach.

Primary Defenses: Synchronizer Token Pattern (STP)

The gold standard for CSRF protection is the Synchronizer Token Pattern (STP) [7][15][29]. This involves:

If the token is missing, invalid, or doesn't match, the request is rejected [7][13][15]. Tokens should be transmitted securely (e.g., not in URLs or logs) and ideally invalidated after each use [15]. Frameworks like Ruby on Rails and Django have built-in support for STP [8][30][31][32].

Double Submit Cookie Pattern

An alternative, stateless approach is the Double Submit Cookie pattern. Here, the server issues a CSRF token in a cookie. This same token is then included in a hidden form field or custom header. The server validates that the cookie token matches the request token. This is simpler as it doesn't require server-side session state for tokens, but is less secure if the token in the cookie can be tampered with or if XSS is present [7][3][15]. Using signed cookies with HMAC for the token is a recommended enhancement [15].

SameSite Cookie Attribute

The SameSite cookie attribute provides a browser-level defense.

While Lax is a good default, it does not protect against CSRF attacks that leverage GET requests or method overrides [20][21][22][35]. Strict offers stronger protection but with usability trade-offs. Many modern applications must use SameSite=None for cross-origin functionality, necessitating other CSRF defenses [36][37].

Fetch Metadata Headers

For modern single-page applications (SPAs) and API-driven sites, Fetch metadata headers like Sec-Fetch-Site can be used. Servers can inspect these headers to determine if a request is same-origin, same-site, or cross-site, and block cross-site requests if they are not expected [13]. This approach is particularly effective for state-changing requests initiated via fetch or XMLHttpRequest where custom headers are common [13].

Referrer and Origin Header Validation

Validating the Referer or Origin headers can provide a layer of defense, especially when combined with other methods. However, relying solely on these headers is not recommended due to their potential absence or spoofability [3][26].

Other Best Practices

Tooling for Detection and Exploitation

Several tools can assist in identifying and demonstrating CSRF vulnerabilities:

Recent Developments and Evolving Threats

The landscape of CSRF is continuously shaped by browser security features and evolving attack techniques.

SameSite=Lax as Default

The introduction of SameSite=Lax as the default for cookies in modern browsers has significantly reduced many traditional CSRF vectors. However, bypasses exploiting GET requests, method overrides, or SameSite=None configurations remain relevant [34][21][36][35]. Chrome's temporary allowance for POST requests within a short time window after a Lax cookie was set also presented a temporary bypass vector [36].

Fetch Metadata and CORS

Fetch metadata headers offer a promising defense, especially for API-driven applications, by allowing servers to scrutinize the origin of requests. Similarly, properly configured CORS policies are crucial, but misconfigurations can still lead to vulnerabilities [13][42][36].

Exploiting JSON and API Endpoints

As applications increasingly rely on JSON and APIs, attackers are focusing on these areas. Techniques like manipulating content types or exploiting permissive CORS policies are key to performing CSRF against these modern interfaces [9][43][37]. Vulnerabilities like CVE-2026-40925 in WWBN AVideo and CVE-2026-34394 in the same platform highlight the risks when configuration endpoints are not properly protected [24][43].

Chaining with XSS and Other Vulnerabilities

The most severe impacts often arise from chaining CSRF with other vulnerabilities. For example, Stored XSS can be used to inject CSRF payloads directly into trusted pages [27][44]. Similarly, vulnerabilities like arbitrary file upload in WordPress themes, when combined with CSRF, can lead to remote code execution [28].

Where to Go Deeper

For those looking to further their understanding and practical skills in CSRF, the following resources are invaluable:

Sources cited in this guide

  1. Cross-site request forgery - Wikipedia — en.wikipedia.org
  2. What is CSRF? Attacks, Mitigation, Prevention - Acunetix — acunetix.com
  3. CSRF: Cross Site Request Forgery Example - Imperva — imperva.com
  4. The Bug Bounty Guide to Exploiting CSRF Vulnerabilities - YesWeHack — yeswehack.com
  5. CSRF Attacks - Rapid7 — rapid7.com
  6. Cross-Site Request Forgery (CSRF) Attack Guide | Hackviser — hackviser.com
  7. How to protect Node.js apps from CSRF attacks — snyk.io
  8. CSRF - OWASP Foundation — owasp.org
  9. CSRF in the Age of JSON — directdefense.com
  10. What Is CSRF? - Palo Alto Networks — paloaltonetworks.com
  11. https://portswigger.net/web-security/csrf — portswigger.net
  12. CSRF: Advanced Exploitation Guide - Intigriti — intigriti.com
  13. Cross-site request forgery (CSRF) - Security - MDN Web Docs — developer.mozilla.org
  14. CSRF (Cross Site Request Forgery) | HackTricks — book.hacktricks.xyz
  15. Cross-Site Request Forgery Prevention Cheat Sheet | OWASP — cheatsheetseries.owasp.org
  16. CSRF & Bypasses | Cobalt — cobalt.io
  17. Avoiding CSRF Attacks with API Design — thedreaming.org
  18. Web Security Academy: CSRF SameSite Lax Bypass via Method Override — medium.com
  19. Lab: SameSite Lax Bypass via Method Override | PortSwigger — portswigger.net
  20. CSRF Attacks: Bypassing SameSite Cookies — blog.cybersamir.com
  21. Bypassing SameSite Cookie Restrictions - CSRF | PortSwigger — portswigger.net
  22. Bypass SameSite Cookies Default to Lax and get CSRF — medium.com
  23. Lab: SameSite Lax Bypass via Cookie Refresh | PortSwigger — portswigger.net
  24. CVE-2026-40925: CSRF in WWBN AVideo Configuration Endpoint — radar.offseq.com
  25. CSRF Protection - Clerk Docs — clerk.com
  26. https://scotthelme.co.uk/csrf-is-dead/ — scotthelme.co.uk
  27. Chaining Stored XSS and CSRF in Typemill CMS: A Deep Dive into Attribute Injection — infosecwriteups.com
  28. CVE-2025-12821: WordPress NewsBlogger CSRF Allowing RCE — sentinelone.com
  29. In Praise of CSRF Tokens – Tim MalcomVetter – Medium — medium.com
  30. WordPress Front End Security: CSRF and Nonces | CSS-Tricks — css-tricks.com
  31. ruby - Sinatra CSRF Authenticity tokens - Stack Overflow — stackoverflow.com
  32. A Deep Dive into CSRF Protection in Rails – Ruby Inside – Medium — medium.com
  33. Preventing CSRF with the SameSite Cookie Attribute — invicti.com
  34. Advanced CSRF: How to Bypass SameSite Cookie Protections — sajjapremsai.github.io
  35. Samesite by Default and What It Means for Bug Bounty Hunters — blog.reconless.com
  36. Samesite by Default and What It Means for Bug Bounty Hunters — blog.reconless.com
  37. https://medium.com/@shub66452/account-takeover-using-csrf-json-based-a0e6efd1bffc — medium.com
  38. web application - Should login and logout action have CSRF protection? - In — security.stackexchange.com
  39. Web Application Security: Anti-CSRF & Cookie SameSite Options — bitsight.com
  40. 0xInfection/XSRFProbe — github.com
  41. 0ang3el/EasyCSRF — github.com
  42. https://mixmax.com/blog/modern-csrf — mixmax.com
  43. CVE-2026-34394: Wwbn Avideo CSRF Vulnerability — sentinelone.com
  44. Self-XSS + CSRF to Stored XSS — medium.com
  45. Cookies: HTTP State Management Mechanism (RFC 6265bis) — httpwg.org
  46. CWE-352: Cross-Site Request Forgery — cwe.mitre.org
  47. CVE-2026-44613: Turning a CSRF into Silent Unauthorized Actions — ox.security
  48. Vulnerabilities in KTM System e-BOK software — cert.pl
  49. Vulnerabilities in PAC4J software — cert.pl
  50. CVE-2025-9611: Microsoft Playwright MCP Server CSRF Flaw — sentinelone.com
  51. CVE-2025-23797: WP Options Editor CSRF Vulnerability — sentinelone.com
📚 This guide is synthesized from the full text of resources curated in the CSRF library, and refreshed as new material is added.