Session management
A session is the credential a user carries after login, so a stolen or stale
session is a stolen identity. The dangers are a token that never dies, one
that survives a password change, and one exposed to script or sent in the
clear. Good session handling is about controlling the token's lifetime and
reach.
Method
- Set the cookie flags that lock the token down. Mark session cookies
HttpOnly (script cannot read them), Secure (HTTPS only), and
SameSite=Lax or Strict (limits cross-site sending). Scope with Path
and avoid a wildcard Domain. These four attributes block the cheapest
theft routes.
- Rotate the session ID on every privilege change. Issue a new
identifier at login, at logout, and when a user elevates (assumes admin,
re-authenticates for a sensitive action). Rotation defeats fixation and
shrinks the window a captured pre-elevation token is worth anything.
- Expire on both idle and absolute timeouts. Enforce an inactivity
timeout (for example 30 minutes) and a hard maximum lifetime (8 to 24
hours) after which re-login is required regardless of activity. A token
that lives forever is a permanent key once copied.
- Store sessions server-side so you can revoke them. Keep a server
record (a session store, Redis, a table) keyed by an opaque random ID, so
logout, a password reset, or an admin action can delete it and end the
session at once. If you use stateless JWTs, add a revocation list or keep
lifetimes very short, because a plain JWT cannot be recalled.
- Generate identifiers from a CSPRNG. Use at least 128 bits from a
cryptographically secure source (
secrets.token_urlsafe,
crypto.randomBytes), not a counter, a timestamp, or Math.random. A
guessable session ID is an open door that needs no theft.
- Invalidate sessions on the events that demand it. On logout, delete
the server record, do not just clear the cookie. On password change or
reset, revoke that user's other sessions so a thief already inside is
evicted.
Checks
- Do the response headers show
HttpOnly, Secure, and SameSite on the
session cookie?
- After a password reset, are the user's other active sessions rejected on
their next request?
- Does an idle session stop working after the timeout, verified by waiting
past it?
Boundaries
Session management picks up after authentication succeeds and hands off to
authorization for per-request access decisions. It does not cover the login
flow itself (see authn-design) or token-based API patterns like OAuth bearer
tokens, whose lifecycle differs. Framework session middleware handles most
of this correctly; the risk is in overrides and custom stores.
1---2name: session-management3description: Handle sessions so identifiers rotate on privilege change, expire on inactivity, can be revoked server-side, and ride only on hardened cookies. Use when issuing, storing, or validating session tokens after a user signs in.4---56# Session management78A session is the credential a user carries after login, so a stolen or stale9session is a stolen identity. The dangers are a token that never dies, one10that survives a password change, and one exposed to script or sent in the11clear. Good session handling is about controlling the token's lifetime and12reach.1314## Method15161. **Set the cookie flags that lock the token down.** Mark session cookies17 `HttpOnly` (script cannot read them), `Secure` (HTTPS only), and18 `SameSite=Lax` or `Strict` (limits cross-site sending). Scope with `Path`19 and avoid a wildcard `Domain`. These four attributes block the cheapest20 theft routes.212. **Rotate the session ID on every privilege change.** Issue a new22 identifier at login, at logout, and when a user elevates (assumes admin,23 re-authenticates for a sensitive action). Rotation defeats fixation and24 shrinks the window a captured pre-elevation token is worth anything.253. **Expire on both idle and absolute timeouts.** Enforce an inactivity26 timeout (for example 30 minutes) and a hard maximum lifetime (8 to 2427 hours) after which re-login is required regardless of activity. A token28 that lives forever is a permanent key once copied.294. **Store sessions server-side so you can revoke them.** Keep a server30 record (a session store, Redis, a table) keyed by an opaque random ID, so31 logout, a password reset, or an admin action can delete it and end the32 session at once. If you use stateless JWTs, add a revocation list or keep33 lifetimes very short, because a plain JWT cannot be recalled.345. **Generate identifiers from a CSPRNG.** Use at least 128 bits from a35 cryptographically secure source (`secrets.token_urlsafe`,36 `crypto.randomBytes`), not a counter, a timestamp, or `Math.random`. A37 guessable session ID is an open door that needs no theft.386. **Invalidate sessions on the events that demand it.** On logout, delete39 the server record, do not just clear the cookie. On password change or40 reset, revoke that user's other sessions so a thief already inside is41 evicted.4243## Checks4445- Do the response headers show `HttpOnly`, `Secure`, and `SameSite` on the46 session cookie?47- After a password reset, are the user's other active sessions rejected on48 their next request?49- Does an idle session stop working after the timeout, verified by waiting50 past it?5152## Boundaries5354Session management picks up after authentication succeeds and hands off to55authorization for per-request access decisions. It does not cover the login56flow itself (see authn-design) or token-based API patterns like OAuth bearer57tokens, whose lifecycle differs. Framework session middleware handles most58of this correctly; the risk is in overrides and custom stores.