SameSite=None cookies and CSRF

SameSite=None cookie

Description

With SameSite=None, the browser can send an authentication cookie on cross-site requests if its other cookie policies allow it. Without server-side CSRF checks, those requests may perform actions using the signed-in user’s permissions.

Potential impact

Unwanted actions can include password or profile changes, payments, and transfers. Depending on account permissions, requests may also send messages, change settings, or create, modify, or delete data.

Remediation

  • Prefer SameSite=Strict for sensitive cookies. Choose Lax if a legitimate flow requires it. In Express, use res.cookie(name, value, { sameSite: 'strict', secure: true, httpOnly: true }).
  • If a third-party embed or SSO flow needs a cross-site cookie, pair SameSite=None with Secure and add server-side CSRF protection. Validate a session-bound CSRF token or use a signed double-submit cookie.
  • Use methods such as POST, PUT, or DELETE for state changes and keep GET read-only. When verifying a request’s origin on the server, require an exact match between a trusted origin and the origin from Origin or Referer.

Examples

Before

javascript
const crypto = require('crypto');
const express = require('express');
const app = express();

// Before: SameSite=None on the authentication cookie, without CSRF defenses.
app.post('/login', (req, res) => {
  const sessionId = crypto.randomBytes(32).toString('hex');
  res.cookie('auth', sessionId, {
    httpOnly: true,
    secure: true,
    sameSite: 'none' // Permits cookies on cross-site requests.
  });
  res.send('logged in');
});

app.listen(3000);

After

javascript
const express = require('express');
const cookieParser = require('cookie-parser');
const crypto = require('crypto');

const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

// Issue a supplementary token; use with the exact Origin/Referer check below.
app.get('/form', (req, res) => {
  const token = crypto.randomBytes(16).toString('hex');
  const sessionId = crypto.randomBytes(32).toString('hex');
  res.cookie('csrfToken', token, { httpOnly: true, secure: true, sameSite: 'strict' });
  res.cookie('session', sessionId, { httpOnly: true, secure: true, sameSite: 'strict' });
  res.send(`<form method="POST" action="/transfer">
              <input type="hidden" name="csrf" value="${token}">
              <button type="submit">send</button>
            </form>`);
});

function verifyOrigin(req) {
  const value = req.headers.origin || req.headers.referer;
  if (typeof value !== 'string') return false;

  try {
    return new URL(value).origin === 'https://example.com';
  } catch {
    return false;
  }
}

// After: SameSite=Strict, exact origin validation, and token comparison.
app.post('/transfer', (req, res) => {
  if (!verifyOrigin(req)) return res.status(403).send('forbidden');
  const cookieToken = req.cookies.csrfToken;
  const formToken = req.body?.csrf;
  if (typeof cookieToken !== 'string' || !/^[0-9a-f]{32}$/.test(cookieToken) ||
      typeof formToken !== 'string' || formToken !== cookieToken) {
    return res.status(403).send('bad token');
  }
  res.send('ok');
});

app.listen(3000);

Explanation:

  • Before: The authentication cookie permits cross-site use. Without token or origin checks, requests from a malicious page may cause unauthorized state changes.
  • After: Use unpredictable session values and SameSite=Strict. Parse Origin/Referer and compare the exact origin, then require matching tokens in the expected nonempty format. Prefixes such as https://example.com.evil are rejected. Do not use this example's simple cookie comparison as the sole defense. Actual login and authorization checks are omitted.

References