01 / 20

CSRF &
Mitigations

Understand Cross-Site Request Forgery, how attackers exploit browser trust, and master the defenses: CSRF tokens, SameSite cookies, and more.

Cyber Security

What is CSRF?

Cross-Site Request Forgery (CSRF) is a web security vulnerability where an attacker tricks a victim's browser into sending an unwanted, authenticated request to a web application the victim is already logged into. The application cannot distinguish this forged request from a legitimate one.

The Browser Trust Problem

✓ Legitimate Request
User clicks "Transfer $100" on their bank's website. Browser sends the request with the session cookie attached.
✕ Forged Request
User visits a malicious page that silently submits "Transfer $100" to the bank. Browser still attaches the session cookie automatically.

Anatomy of a CSRF Attack

Victim Browser Bank Server Evil Site 1. Logs in 2. Session cookie 3. Visits evil link 4. Hidden form 5. Request + cookie!

Why CSRF Works

1
Cookies are automatic. Browsers attach cookies to every request for a domain, no matter which site initiated the request.
2
Same-origin policy doesn't block outgoing requests. It only restricts reading the response. The forged request still arrives at the server.
3
No user interaction required. A hidden form with auto-submit JavaScript fires the moment the victim loads the malicious page.
4
Server trusts the cookie alone. Without additional verification, the server assumes any request with a valid session cookie is legitimate.

CSRF vs XSS: Key Distinction

CSRF XSS
Exploits trust of Server trusts the user's browser User trusts the website
Attacker can Perform actions as the victim Read/modify page content and steal data
Requires Victim to be logged in Vulnerable input handling on the site
Attack vector External malicious site Injected script on the target site
Reads response? No: fire-and-forget Yes: full DOM access

Mitigation 1: CSRF Tokens

A CSRF token (also called a synchronizer token) is a secret, unpredictable value generated by the server and embedded in each form. The server validates this token on every state-changing request. Because the attacker's malicious site cannot read the token, it cannot forge a valid request.

Server HTML Form token=a3f9c7... POST /transfer token=a3f9c7... 1. Embed token 2. Submit 3. Validate ✓ Evil Site No token! ✕

How Tokens Work Step-by-Step

1
Server generates a unique token per session (or per request) and stores it server-side.
2
Token is embedded in the HTML form as a hidden input field: <input type="hidden" name="csrf_token" value="a3f9c7...">.
3
User submits the form. The token travels as a form parameter alongside the request.
4
Server compares the submitted token with the stored token. If they match, the request is processed. If not, it is rejected.

Mitigation 2: SameSite Cookies

The SameSite cookie attribute tells the browser when to attach cookies to cross-site requests. It directly addresses the root cause of CSRF: automatic cookie attachment.

SameSite=None
Cookie sent with all requests, including cross-site. Provides no CSRF protection. Requires Secure flag.
SameSite=Lax
Cookie sent on top-level navigations (clicking a link) but not on cross-site POST, iframe, or AJAX. Default in modern browsers.
SameSite=Strict
Cookie is never sent on cross-site requests. Maximum protection but can break legitimate cross-site navigation flows.

SameSite Comparison

Scenario None Lax Strict
Top-level link click ✓ Sent ✓ Sent ✕ Not sent
Cross-site form POST ✓ Sent ✕ Not sent ✕ Not sent
Cross-site iframe ✓ Sent ✕ Not sent ✕ Not sent
Cross-site AJAX ✓ Sent ✕ Not sent ✕ Not sent
CSRF protection None Partial Strong

Additional Defenses

Custom Headers
Require a custom HTTP header (e.g., X-Requested-With) on state-changing requests. Simple forms cannot set custom headers, so cross-site requests are blocked.
Origin/Referer Check
Server checks the Origin or Referer header to verify the request came from the same site. Provides defense-in-depth but headers can be missing or stripped.
Double Submit Cookie
A token is stored in both a cookie and a request parameter. Server checks they match. Works without server-side token storage.

How to Apply: Defense-in-Depth

1
Set SameSite=Lax on all session cookies. This is the first line of defense and blocks most CSRF vectors with zero code changes.
2
Implement CSRF tokens for all state-changing endpoints. Use your framework's built-in CSRF middleware (Django, Rails, Express-csurf, etc.).
3
Verify the Origin header server-side. Reject requests where the Origin does not match your domain.
4
Never use GET for state-changing operations. Transfers, deletions, and settings changes must use POST, PUT, or DELETE.
5
Require re-authentication for sensitive actions. Ask for password or 2FA before fund transfers, email changes, etc.

Example 1: Vulnerable Bank Form

A bank has a money transfer form that only checks the session cookie. No CSRF token, no SameSite attribute. An attacker creates a malicious page:

Example 2: Token-Protected Form

Now the bank adds a CSRF token. The legitimate form includes a hidden field with a server-generated token:

Example 3: SameSite Cookie Defense

The bank sets its session cookie with the SameSite=Lax attribute:

evil.com Browser bank.com POST No cookie! ✕ SameSite=Lax blocks it

Common Mistakes

✕ Token in URL
Putting CSRF tokens in GET parameters exposes them in browser history, server logs, and Referer headers. Always use hidden form fields or custom headers.
✕ Shared Token
Using the same CSRF token for all users or never rotating it. Tokens must be unique per session and cryptographically random.
✕ GET for Actions
Using GET requests for state changes (e.g., /delete?id=5). GET requests bypass SameSite=Lax and can be triggered by images, links, or iframes.

Mitigation Summary

Defense Strength Limitation
CSRF Token Strongest; validates intent Requires server-side logic and form modification
SameSite=Lax Blocks cross-site POST/AJAX/iframe Does not block GET-based CSRF; browser support varies
SameSite=Strict Blocks all cross-site requests Breaks legitimate link navigation from external sites
Origin/Referer Check Simple server-side check Headers can be stripped by proxies or privacy settings
Custom Headers Works well for AJAX APIs Only applies to JavaScript-initiated requests, not forms

Question 1 of 5

What does a CSRF attack primarily exploit?

Question 2 of 5

Why can't an attacker's malicious website read a CSRF token embedded in a form on bank.com?

Question 3 of 5

A website sets its session cookie with SameSite=Lax. Which of the following cross-site requests would still include the session cookie?

Question 4 of 5

A developer implements a "delete account" feature using a GET request to /delete-account. Why is this especially dangerous from a CSRF perspective?

Question 5 of 5

Which combination provides the strongest defense-in-depth against CSRF?