Cross-Site Request Forgery (CSRF)
Learn how CSRF attacks forge authenticated requests and how CSRF tokens and SameSite cookies defend against them.
Cross-Site Request Forgery (CSRF) is a free Cyber Security Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cyber Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is CSRF?
Cross-Site Request Forgery (CSRF) tricks an authenticated user's browser into making unauthorized requests to a web application. The browser automatically sends cookies with requests, so the server cannot distinguish legitimate from forged requests without extra measures.
How CSRF Works
Scenario:
- Victim is logged into bank.com (session cookie in browser)
- Victim visits attacker's page with:
<img src="https://bank.com/transfer?to=attacker&amount=1000"> - Browser sends the GET request with the bank.com cookie attached
- Bank processes the transfer
CSRF with POST Requests
POST CSRF requires a form:
<form action="https://bank.com/transfer" method="POST" id="f">
<input name="to" value="attacker">
<input name="amount" value="1000">
</form>
<script>document.getElementById("f").submit()</script>CSRF Tokens
The primary defense is a CSRF token: a random, secret, per-session (or per-request) value embedded in forms. The server validates the token on every state-changing request. An attacker on another domain cannot read the token (same-origin policy).
SameSite Cookie Attribute
SameSite=Strict: cookie not sent with cross-site requests at all. SameSite=Lax: cookie sent with safe top-level navigations (links) but not with POST from other sites. Modern browsers default to Lax, significantly reducing CSRF risk.
Double Submit Cookie Pattern
An alternative to server-side token storage: set a random CSRF cookie and require it to also be submitted as a request parameter. An attacker cannot read the cookie (same-origin), so they cannot match it in the form data.
Custom Request Headers
For AJAX requests, requiring a custom header (e.g., X-Requested-With: XMLHttpRequest) provides CSRF protection because browsers block cross-origin scripts from setting arbitrary headers (CORS enforces this).
When CSRF Tokens Are Not Enough
CSRF tokens fail if:
- XSS exists — attacker can read the token via JavaScript
- Token is leaked in URL (Referer header)
- Token is predictable or reused
- CORS is misconfigured to allow the attacker's origin
Testing for CSRF
Testing steps:
- Identify state-changing requests (POST, PUT, DELETE)
- Remove or modify the CSRF token and resubmit
- Craft a cross-origin form submission and check if it succeeds
- Verify SameSite attribute on session cookies
CSRF in APIs
REST APIs using JSON are often exempt from CSRF if they:
- Require
Content-Type: application/json(HTML forms can't set this) - Use token-based auth (Authorization header, not cookies)
But APIs that accept cookies must still implement CSRF protection.
Modern CSRF Landscape
With SameSite=Lax as default in Chrome, Firefox, and Safari, many traditional CSRF attacks are blocked. However, subdomain attacks and certain navigation patterns can still bypass Lax. Combine SameSite with CSRF tokens for robust defense.
Quick Check: CSRF
What is the primary defense against CSRF attacks in web applications?
Lesson Recap
CSRF exploits browser auto-attachment of cookies to forge authenticated requests from malicious sites. Primary defense: CSRF tokens validated server-side. Secondary: SameSite=Strict/Lax cookie attribute. APIs using token auth in headers (not cookies) are naturally CSRF-resistant. Combine defenses; XSS can bypass CSRF tokens if present.
Frequently asked questions
Is the “Cross-Site Request Forgery (CSRF)” lesson free?
Yes — the full text of “Cross-Site Request Forgery (CSRF)” is free to read here on the web, and the Cyber Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cyber Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cross-Site Request Forgery (CSRF)”?
Learn how CSRF attacks forge authenticated requests and how CSRF tokens and SameSite cookies defend against them. You practise Cyber Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cyber Security Academy?
No prior experience is required. Cyber Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cross-Site Request Forgery (CSRF)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cyber Security Academy lesson?
Yes. Every Cyber Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- SQL Injection: How and Why It Works
- Cross-Site Scripting (XSS)
- Cross-Site Request Forgery (CSRF)
- Security Misconfiguration and Exposed Services