#Why this matters
When you log in to a web application, your browser stores a session cookie and sends it automatically on every subsequent request to that site. This convenience creates ambient authority: if a malicious page at evil.test makes your browser submit a hidden form to lab.local, your browser attaches your lab.local session cookie unless the site explicitly prevents it.
The Same-Origin Policy stops evil.test from reading the HTTP response from lab.local, but it does not stop the browser from sending the request. If the request changes a password, transfers funds, or updates an email address, the damage happens before any response is ever read.
#How cross-site requests carry credentials
A classic CSRF flaw needs three conditions:
- A relevant action. The endpoint changes state or privileges (changing email, adding an admin, triggering a transfer).
- Cookie-only authentication. The server identifies the user solely through a cookie or HTTP Basic auth, with no unpredictable secret in the request body or custom header.
- Predictable parameters. All required fields can be guessed or hardcoded by an external page.
Notice that HTML forms using application/x-www-form-urlencoded, multipart/form-data, or text/plain qualify as CORS simple requests. The browser sends them immediately without asking lab.local via an OPTIONS preflight check. By contrast, sending Content-Type: application/json with a custom header from fetch() triggers a CORS preflight, which blocks unapproved cross-origin writes.
#Testing for CSRF safely
In a lab, start by capturing a state-changing request in Burp Suite and checking what authenticates it:
POST /profile/email HTTP/1.1
Host: lab.local
Cookie: session=eyJhbGciOi...; SameSite=None
Content-Type: application/x-www-form-urlencoded
email=attacker@example.test&csrf_token=8a4f2c91Test the defences systematically:
- Remove the token. Delete
csrf_tokenor send an empty value; if the server still returns200 OK, validation is missing or skipped when absent. - Swap the token. Use a token from a second test account (
bob) onalice's session; if accepted, tokens are not bound to the user session. - Change the method. Try
GET /profile/email?email=attacker@example.test; some frameworks accept query parameters for routes intended only forPOST.
#SameSite cookies and their limits
The SameSite attribute on Set-Cookie tells the browser when to withhold cookies on cross-site requests.
| Attribute | Cross-site top-level GET | Cross-site POST form | Typical use |
|---|---|---|---|
Strict |
Cookie withheld | Cookie withheld | High-security admin sessions |
Lax |
Cookie sent | Cookie withheld | Default for normal user sessions |
None; Secure |
Cookie sent | Cookie sent | Cross-site embeds over HTTPS only |
Modern browsers treat cookies without an explicit SameSite attribute as Lax by default, though Chromium applies a legacy 120-second exception allowing newly created default-Lax cookies on top-level cross-site POST requests. Setting SameSite=Lax explicitly removes that 120-second window.
Even explicit SameSite=Lax has two important limits:
- State-changing GET requests. Because
Laxallows cookies on top-level navigations (<a href="...">orwindow.location), any state change triggered byGETremains exploitable. - Same-site subdomains.
SameSitechecks the registrable domain (scheme + eTLD+1), not the full origin. Ifblog.lab.localhas an XSS flaw or untrusted tenant content, requests fromblog.lab.localtoapp.lab.localcount as same-site and carrySameSite=Strictcookies.
#Defending properly
Treat SameSite as defence in depth, not a complete replacement for explicit CSRF protection:
- Use the synchronizer token pattern. Generate a cryptographically random token per session, embed it in forms or meta tags, and verify it on every
POST,PUT,PATCH, andDELETErequest using constant-time comparison. - Use
GETonly for safe, read-only actions. Never mutate server state onGETorHEAD. - Require custom headers for JSON APIs. Enforce
Content-Type: application/jsonstrictly on the server (rejectingtext/plainbodies) and keep CORSAccess-Control-Allow-Originrestricted to trusted origins without wildcards alongsideAllow-Credentials: true. - Set
SameSite=LaxorStrictwithHttpOnlyandSecureflags on all session cookies.
#Common mistakes
- Checking
csrf_tokenonly if the parameter exists in the request body. - Validating the
RefererorOriginheader with a loose substring check such asstr_contains($origin, "lab.local"), which matcheslab.local.evil.test. - Relying on
SameSite=Laxwhile leaving state-changingGETroutes or subdomain XSS issues unpatched. - Assuming CORS blocks form submissions: CORS restricts reading responses and sending non-simple requests, not classic HTML forms.
#Summary
- CSRF exploits ambient cookie authority so a third-party site triggers unintended actions as a logged-in victim.
- Standard HTML forms bypass CORS preflight because they count as simple requests.
SameSite=Laxblocks cross-sitePOSTforms, yet still permits top-levelGETnavigations and same-site subdomain requests.- Reliable defence combines non-mutating
GETsemantics, session-bound anti-CSRF tokens, strictContent-Typechecks on APIs, andSameSitecookies.