How it works
CSRF abuses the way browsers handle cookies. When a browser sends a request to a site, it includes that site's cookies, even if the request was started by a page on a completely different site. If the session cookie is the only thing the server checks, it cannot tell a request the user meant to send from one an attacker's page sent on their behalf.
Three conditions must hold:
- The victim is logged in to the target application, and the session lives in a cookie.
- The application has an action worth forging, such as changing an email address or sending money.
- The request contains nothing the attacker cannot predict: no secret token and no check on where the request came from.
The attacker never sees the response, so CSRF can change data and cannot read it.
An API that only accepts a token in the Authorization header is not exposed, because another site cannot attach that header. The risk returns if the API also accepts a session cookie.
What it looks like
The victim is signed in at app.acme.test and then visits a page the attacker controls. That page contains a hidden form pointed at the target application and a line of script that submits it at once. The victim's browser sends this:
POST /account/payout-details HTTP/1.1
Host: app.acme.test
Origin: https://attacker-site.example
Cookie: session=<the victim's session cookie>
account_holder=Mallory&account_number=00000000The session cookie is valid, so a vulnerable server updates the payout details and replies 200 OK. The Origin header shows that the request started on another site. The server did not look at it.
Do SameSite cookies solve it?
SameSite is a cookie attribute that tells the browser when to include a cookie on requests that start on other sites. It removes many CSRF attacks, and it leaves gaps.
Strictwithholds the cookie from every request that starts on another site.Laxsends the cookie only when the user navigates to the site with a safe method such asGET. Form posts from other sites arrive without it.Nonesends the cookie everywhere and offers no protection.
The gaps: an action that changes state on a GET request is still exposed under Lax. "Same site" includes sibling subdomains, so a flaw on blog.acme.test can attack app.acme.test. Browsers also differ in what they do when no attribute is set. Treat SameSite as one layer and keep a token.
How to test for it
- List every request that changes something: settings, payments, invitations, deletions.
- For each, check whether the request carries an unpredictable token tied to the session.
- Remove the token, then send one issued to another session. Either one succeeding is a finding.
- Change the
POSTto aGETwith the same parameters and see if the action still runs. - Inspect the session cookie's
SameSiteattribute. - Replay the request with an
Originheader from another site, using a test account, and compare the outcome.
How to fix it
Use the protection built into your framework where one exists. A protected exchange looks like this:
POST /account/payout-details HTTP/1.1
Host: app.acme.test
Origin: https://app.acme.test
Cookie: session=<session cookie>
X-CSRF-Token: <random value the server tied to this session>The attacker's page cannot read or guess the token, so a forged request arrives without it and is refused. The defenses are:
- Use anti-CSRF tokens. Require a per-session random value on every state-changing request, and reject requests where it is missing or wrong.
- Set the
SameSiteattribute toLaxorStricton session cookies, along withSecureandHttpOnly. - Check the source. Reject state-changing requests whose
OriginorSec-Fetch-Siteheader shows another site. - Keep
GETrequests read-only.
CSRF vs XSS
In CSRF, the attacker's request comes from a different site and rides on the victim's cookies. In cross-site scripting, the attacker's script runs inside the trusted site and can read pages as well as send requests. XSS defeats CSRF tokens, because the script can read the token from the page.
The similar-sounding server-side request forgery is unrelated: there the server is tricked into making requests, not the browser.