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-09-01. Synthesized from 74 of 74 curated resources. Browse all 74 CSRF resources →

Understanding Cross-Site Request Forgery (CSRF)

Cross-Site Request Forgery (CSRF), also known as XSRF or session riding, is a class of attacks that exploits the trust a web application places in an authenticated user's browser [1][2][3][4][5]. Unlike Cross-Site Scripting (XSS), which targets the trust a user has in a website, CSRF targets the trust a website has in a user's browser [2][6]. The fundamental mechanism involves tricking a user's browser into sending an unintended, state-changing request to a web application on which the user is currently authenticated [7][1][8][9][10][11][4][5].

When a user logs into a web application, their browser typically stores a session identifier (usually a cookie) that is automatically sent with every subsequent request to that application [7][1][2][4]. This allows the application to maintain the user's authenticated state without requiring re-authentication for every action. A CSRF attack leverages this automatic cookie transmission. An attacker crafts a malicious request, often embedded in a seemingly innocuous link or form, and tricks the victim into triggering it.

When the victim's browser executes the forged request, it automatically includes the valid session cookie, making the request appear legitimate to the web application. The application, unaware that the request originated from an attacker-controlled source, processes it as if it were initiated by the authenticated user. This can lead to various unauthorized actions, such as changing account settings, performing financial transactions, or even escalating privileges, depending on the user's permissions and the application's functionality [7][1][10][11][5].

The impact of a successful CSRF attack can range from minor inconveniences, like changing a user's profile preferences, to severe consequences, such as financial loss, account takeover, or complete compromise of the application if an administrator account is targeted [7][1][10][4][5]. Even seemingly harmless actions, when performed on behalf of an administrator, can have significant downstream effects [12][13][14][15][16].

Core Mechanics of a CSRF Attack

For a CSRF attack to be successful, three primary conditions generally need to be met:

The attacker's goal is to identify a reproducible request that performs a state-changing action and then construct a malicious payload to trigger it. This payload is typically delivered via a link, an embedded image tag, a hidden form that auto-submits, or JavaScript [2][19][11][20][6].

Notable CSRF Attack Vectors and Scenarios

CSRF attacks can be broadly categorized based on the HTTP method used and the delivery mechanism:

GET-Based CSRF

GET requests are often targeted because they can be easily triggered by simply embedding a URL in an image tag, a link, or a meta-refresh tag [2][11][20][6][21]. If a state-changing action is improperly handled via GET requests without adequate protection, an attacker can force a victim's browser to execute it.

For example, a request to change a user's email address might look like:

GET /user/settings?email=attacker@example.com HTTP/1.1

Host: vulnerable-website.com Cookie: sessionid=...

An attacker could embed this in an image tag:

<img src="https://vulnerable-website.com/user/settings?email=attacker@example.com">

When the victim visits the attacker's page, their browser attempts to load the "image," sending the request with the associated session cookie, thus changing the email address [11][20][6][21][5].

POST-Based CSRF

POST requests are more commonly used for state-changing operations. To exploit these, an attacker typically crafts an HTML form with hidden input fields that mimic the legitimate form, and uses JavaScript to auto-submit it upon page load [1][22][11][20][6][21][5].

Consider a password change request:

POST /account/password/change HTTP/1.1

Host: vulnerable-website.com Content-Type: application/x-www-form-urlencoded Cookie: sessionid=... old_password=...&new_password=newpassword

The attacker's payload might look like:

<html>

<body onload="document.forms[0].submit()"> <form action="https://vulnerable-website.com/account/password/change" method="POST"> <input type="hidden" name="old_password" value="anything" /> <input type="hidden" name="new_password" value="attacker_password" /> </form> </body> </html>

When the victim visits this page, the JavaScript automatically submits the form, sending the forged request with the user's credentials [22][11][6][21][5].

JSON-Based CSRF

Modern applications often use JSON for API communication. Exploiting JSON endpoints via CSRF can be more challenging due to browser security policies (like CORS preflight requests for non-simple requests) [23][20][24]. However, attackers can still succeed by:

An example using text/plain and an HTML form:

<form action="https://api.example.com/update" method="POST" enctype="text/plain">

<input type="hidden" name='{"user_settings":{"email":"attacker@example.com"}}' value="" /> </form> <script>document.forms[0].submit();</script>

Stored CSRF

Stored CSRF occurs when the malicious payload is embedded within the application's own content, such as in user-generated comments or profile fields that support HTML injection [10][4]. When other users view this content, the embedded CSRF payload is triggered, increasing the likelihood of exploitation [10][4][20]. An example is injecting an `` tag with a malicious URL into a comment section.

Chaining CSRF with Other Vulnerabilities

CSRF is often more potent when chained with other vulnerabilities. For instance, a Stored XSS vulnerability can be used to inject CSRF payloads that require JavaScript execution or to exfiltrate CSRF tokens [26][27][6][28][29]. Similarly, a CSRF vulnerability could allow an attacker to modify settings that enable other attacks, like disabling security features or updating authentication mechanisms [13][14][15][16].

Mitigation and Prevention Strategies

Several robust mechanisms can effectively defend against CSRF attacks:

Synchronizer Token Pattern (STP)

This is the most widely recommended and effective method [7][20][18][30][28]. It involves generating a unique, unpredictable token for each user session or, ideally, each sensitive request. This token is embedded in forms (as a hidden field) or sent via custom HTTP headers with AJAX requests [7][20][18]. The server then validates the received token against the one stored in the session before processing the request [7][18]. An attacker, unable to obtain or guess this token, cannot forge a valid request [20][18][28].

Key characteristics of CSRF tokens:

Example of a token embedded in a form:

<form action="/transfer" method="POST">

<input type="hidden" name="csrf_token" value="UNIQUE_UNPREDICTABLE_TOKEN"> <!-- other form fields --> <button type="submit">Submit</button> </form>

The server would then use a function like wp_verify_nonce() in WordPress to validate the token [31].

SameSite Cookie Attribute

The SameSite cookie attribute is a powerful browser-level defense that restricts when cookies are sent with cross-site requests [7][9][32][33][34][35][36][37][38].

By default, most modern browsers apply SameSite=Lax to cookies without an explicit attribute, providing a baseline level of CSRF protection [33][37][40]. However, Lax can be bypassed using GET-based CSRF attacks or by exploiting method overrides if the server doesn't enforce the HTTP method strictly [41][33][42][17][35].

Fetch Metadata Headers

The Sec-Fetch-Site header, along with Sec-Fetch-Mode and Sec-Fetch-Dest, can provide valuable context about the origin and nature of a request [20][18]. Servers can inspect Sec-Fetch-Site to differentiate between same-origin, same-site, and cross-site requests. By blocking cross-site requests that are not intended to be cross-site, this header can prevent CSRF attacks, particularly for non-simple requests initiated by JavaScript [20][18]. For example, an Express.js application can check this header to allow only same-origin or same-site requests.

app.post("/transfer", (req, res) => {

const secFetchSite = req.headers["sec-fetch-site"]; if (secFetchSite === "same-origin" || secFetchSite === "same-site") { // Process the request } else { // Block the request res.status(403).send("Forbidden"); } });

Referer and Origin Header Validation

Validating the Referer and Origin headers can offer a layer of defense, especially if implemented correctly [20][18][43][36]. However, these headers can be spoofed or absent in certain scenarios (e.g., ` tags without crossorigin, or privacy settings), making them less reliable as a sole defense mechanism [18][43][36]. Relying on precise regex matches for the Referer` header is also prone to bypasses [17][18].

CSRF Protection for Login/Logout

While often overlooked, login and logout endpoints can also be vulnerable to CSRF [44]. Login CSRF can allow an attacker to log a victim into the attacker's account, potentially enabling various chained attacks [44]. CSRF protection on logout can be part of a multi-stage attack if it forces a logout that prompts the user to re-authenticate, potentially on a spoofed page [44]. It is generally recommended to protect these actions as well.

Detection and Testing

Detecting CSRF vulnerabilities involves identifying sensitive actions that modify state and checking for the presence and effectiveness of anti-CSRF measures.

When testing, observe how the application responds to requests missing or with tampered CSRF tokens, invalid methods, or unexpected content types.

Notable Vulnerabilities and Trends

Recent reports highlight ongoing CSRF vulnerabilities across various applications:

The trend of SameSite=None being used, often for cross-origin iframe embedding, can inadvertently increase the attack surface for CSRF if not properly protected with tokens [14][16][43]. Similarly, lax enforcement of HTTP methods or content types can facilitate bypasses [17][25].

Where to Go Deeper

For a comprehensive understanding and advanced techniques, consult the following resources:

Sources cited in this guide

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