---
title: "A button on another site changed a user's email? How to stop CSRF"
description: "CSRF tricks a logged-in user's browser into sending a request to your site they never meant to send. The main fix is a server-generated token checked on every state-changing request, with SameSite as an extra layer."
url: https://ihsanalapsi.dev/articles/csrf-token-samesite
lang: en
date: 2026-11-13
updated: 2026-11-13
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# A button on another site changed a user's email? How to stop CSRF

A user of your site opened a page on another site that has nothing to do with you, and clicked a "see the photo" button. A minute later, the email on their account with you belongs to someone else. No password was stolen and no server was breached: their own browser sent the request to your site. This article shows how that happens, and how one token closes it.

## What is CSRF?

CSRF (Cross-Site Request Forgery) is an attack where a foreign site tricks the browser of a user who is logged in to your site into sending a request the user never meant to send. Your site sees a request with a valid session, so it runs it.

The [OWASP cheat sheet on CSRF prevention](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html) describes the attack as happening when a malicious web site, email, blog, instant message, or program tricks the browser. So the victim does not need to install anything; opening a page is enough.

In business-owner terms: someone hands a bank clerk a pre-filled transfer slip and says "sign it, it's yours". The clerk knows the customer, so the clerk signs.

## Why doesn't the session alone protect you?

The session alone does not protect you because it proves who the user is, not that the user chose to send the request. The session cookie travels with the request to your site even when the request started on a foreign page.

The mistake is simple: the developer assumes "the user is logged in" means "the user wants this request". This is what a vulnerable route looks like:

```javascript
app.post('/account/email', requireLogin, async (req, res) => {
  await User.updateOne({ _id: req.user.id }, { email: req.body.email });
  res.redirect('/account');
});
```

`requireLogin` checks that a session exists, and nothing more. Any page on the internet with a form that posts to this route can change the email of any user who has a session with you.

## How does a CSRF token stop the attack?

A CSRF token is a random value the server generates and puts in the page, then expects back with every request that changes something. The foreign site cannot read your page, so it does not know the value, so its request is rejected.

This is called the synchronizer token pattern, and the cheat sheet sets its rules:

1. The server generates the token, once per session or for each request.
2. The token is sent to the browser as part of the response payload, like a hidden form field, not in a cookie.
3. It must not leak in the URL or in the server logs.
4. If a request arrives without the token, or with a wrong value, it is rejected.

In the form:

```html
<form method="POST" action="/account/email">
  <input type="hidden" name="csrf_token" value="{{ csrfToken }}">
  <input type="email" name="email">
  <button>Save</button>
</form>
```

And on the server, a check before any route that changes data:

```javascript
import { randomBytes, timingSafeEqual } from 'node:crypto';

function issueCsrfToken(req) {
  req.session.csrfToken ??= randomBytes(32).toString('hex');
  return req.session.csrfToken;
}

function checkCsrf(req, res, next) {
  const sent = Buffer.from(String(req.body.csrf_token ?? ''));
  const saved = Buffer.from(req.session.csrfToken ?? '');
  if (saved.length === 0 || sent.length !== saved.length || !timingSafeEqual(sent, saved)) {
    return res.sendStatus(403);
  }
  next();
}
```

## Isn't SameSite enough?

SameSite is not enough on its own because it is a defense-in-depth layer, not a replacement for the token. The cheat sheet says it plainly: useful as an extra layer, but it does not replace a proper CSRF defense in most deployments.

There is a practical reason too: the default Lax behavior still sends the cookie when the user navigates to your site through a normal link with a safe method like GET. So if you have a route that changes something on a GET request, Lax does not stop it. That is why the cheat sheet says not to use GET for any state-changing operation.

## Where do you start now?

The lesson for us as developers: the session says who the user is, and the token says the request came from your own page. You need both on every request that changes something.

1. Check whether your framework has built-in CSRF protection, and use it. That is the first advice in the cheat sheet.
2. If it does not, add a token to every request that changes data: POST, PUT, PATCH and DELETE.
3. Look for any GET route that deletes, edits or sends something, and turn it into a POST.
4. Keep SameSite on the session cookie as an extra layer, not as the only protection.

We covered the rest of the cookie settings in [checking session cookie flags](/articles/session-cookie-flags-check). All our app security articles are in one place: [app security articles](/articles?tag=app-security). And if you want someone to try these requests against your platform from a foreign site, the way an attacker would, that is what [Muhassan](/muhassan) does.

How many routes on your site change data without asking for any token?

## FAQ

**Where do I put the CSRF token: in a cookie or in the page?**

In the page. The OWASP cheat sheet says that in the synchronizer pattern the token is not sent in a cookie but as part of the response payload, like a hidden form field, and it must not leak in the URL or the server logs.

**If my cookie has SameSite=Lax, do I still need a token?**

Yes. OWASP treats SameSite as a defense-in-depth layer, not a replacement for a proper CSRF defense in most deployments. Lax still sends the cookie on top-level navigations that use safe methods like GET.

**My framework has built-in CSRF protection. What should I do?**

Use it. That is the first advice in the OWASP cheat sheet. Just make sure it is on for every route that changes data, and that you did not turn it off for one route and forget.
