Skip to content
cloudgrip

$ man cross-site-request-forgery

//Attacks and vulnerabilities

What is cross-site request forgery (CSRF)?

Cross-site request forgery (CSRF) is an attack where a malicious website causes a logged-in user's browser to send an unwanted, state-changing request to another site. The target site accepts it because the browser attaches the user's session cookie automatically.

  • Attacks and vulnerabilities
  • 3 min read
  • updated Nov 11, 2026

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:

  1. The victim is logged in to the target application, and the session lives in a cookie.
  2. The application has an action worth forging, such as changing an email address or sending money.
  3. 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:

output
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=00000000

The 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.

  • Strict withholds the cookie from every request that starts on another site.
  • Lax sends the cookie only when the user navigates to the site with a safe method such as GET. Form posts from other sites arrive without it.
  • None sends 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

  1. List every request that changes something: settings, payments, invitations, deletions.
  2. For each, check whether the request carries an unpredictable token tied to the session.
  3. Remove the token, then send one issued to another session. Either one succeeding is a finding.
  4. Change the POST to a GET with the same parameters and see if the action still runs.
  5. Inspect the session cookie's SameSite attribute.
  6. Replay the request with an Origin header 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:

output
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 SameSite attribute to Lax or Strict on session cookies, along with Secure and HttpOnly.
  • Check the source. Reject state-changing requests whose Origin or Sec-Fetch-Site header shows another site.
  • Keep GET requests 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.

See what an attacker would find.

Give us a URL and two test logins. If there’s something to find, your first exploited findings land within 24 hours.

You talk to the people who build Hydra and Cerberus. No sales script.

or write to founders@cloudgrip.ai