SameSite=None Cookie Compatibility Utility

2.0.5 · active · verified Sun Apr 19

This package provides a utility function (`isSameSiteNoneCompatible`) and an Express.js middleware (`shouldSendSameSiteNone`) to address specific browser compatibility issues with the `SameSite=None; Secure` cookie attribute. Introduced around Chrome 80 in February 2020, the `SameSite=None; Secure` setting is required for cross-site cookies, but some older browsers (notably Chrome 51-66, certain Safari versions, and UC Browser) handle this attribute incorrectly, potentially leading to cookies being rejected or mismanaged. This library detects these incompatible user agents based on a known list from Chromium, allowing developers to dynamically adjust cookie settings to ensure functionality across a broader range of clients. The current stable version is 2.0.5, with recent updates focused on bug fixes and improved TypeScript declarations. It offers a crucial compatibility layer for web applications relying on cross-site cookie functionality, saving developers from maintaining an exhaustive list of incompatible clients themselves.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up the Express middleware to automatically manage `SameSite=None` cookie attributes for incompatible user agents, ensuring cross-site cookie functionality while adhering to modern browser security policies.

import express from 'express';
import { shouldSendSameSiteNone } from 'should-send-same-site-none';

const app = express();
const PORT = process.env.PORT ?? '3000';

// Apply the middleware globally before defining routes.
// This middleware will automatically remove SameSite=None from 'Set-Cookie' headers
// if the requesting client is known to be incompatible.
app.use(shouldSendSameSiteNone);

app.get('/', (req, res) => {
  // When setting cross-site cookies, always set SameSite='None' and Secure=true.
  // The middleware will handle exceptions for incompatible browsers.
  res.cookie('session_id', 'somevalue123', { sameSite: 'none', secure: true, httpOnly: true });
  res.send('Hello World! Cookie set with SameSite=None; Secure (if compatible).');
});

app.listen(Number(PORT), () => {
  console.log(`Server listening on port ${PORT}`);
  console.log('Try visiting with an older Chrome browser (e.g., Chrome 51-66) to see the SameSite=None attribute removed.');
});

view raw JSON →