Learn / Authentication
mediumNext.js · Express · Supabase

Three flags that decide whether a session can be stolen

A session cookie is the thing that says "this browser is signed in". Three attributes control who can read it and where it is sent. Miss them and a script on the page, or a plain-HTTP request, can carry the session away.

Scanwright reports a cookie that is missing any of the three. It reads the Set-Cookie header; it never reads the cookie value.

What each flag does

Why AI-built apps miss them

Token-in-local-storage patterns, hand-rolled cookies with res.setHeader, and copying a Supabase example that keeps the session in a readable cookie. The fix is almost always one line in the place the cookie is set.

The fix

Set all three attributes wherever a session cookie is written. For Supabase, use the server-side auth helpers (@supabase/ssr) so the session lives in an HttpOnly cookie managed for you.

  1. Find every place a session cookie is set (framework auth config, res.cookie, Set-Cookie headers).
  2. Add Secure, HttpOnly and SameSite=Lax; set Path=/ and a sensible Max-Age.
  3. If the app reads the token in the browser, move that logic to a server route so the cookie can be HttpOnly.
  4. Re-scan, then use the per-finding re-check to confirm.
// Express
res.cookie('session', token, { httpOnly: true, secure: true, sameSite: 'lax', path: '/', maxAge: 7 * 24 * 3600 * 1000 });

// Next.js route handler
import { cookies } from 'next/headers';
cookies().set('session', token, { httpOnly: true, secure: true, sameSite: 'lax', path: '/', maxAge: 60 * 60 * 24 * 7 });

FAQ

My app keeps the token in localStorage. Is that worse?

Yes: any script on the page can read localStorage, and there is no HttpOnly equivalent. Move the session into an HttpOnly cookie set by the server.

Does SameSite replace a CSRF token?

For most apps SameSite=Lax is enough for cookie-based sessions. Keep a CSRF token if you support older browsers or need cross-site POSTs on purpose.