Skip to content

Lesson 3 of 14 · 13m

Fixation, Rotation, Expiry

Rotating the session ID at every privilege transition kills fixation; idle and absolute TTLs bound what a stolen ticket is worth.

Fixation: the login that upgrades a planted ticket

Session fixation has a three-step shape:

  1. The attacker obtains a valid but anonymous session ID — some servers mint one on first visit; older PHP apps with session.use_only_cookies=0 would even adopt ?PHPSESSID=abc straight from the URL.
  2. The victim is steered into using that ID — a link, a planted cookie, a subdomain setter.
  3. The victim logs in. The server keeps the same ID and flips its server-side state to authenticated. The attacker, who has held the ID the whole time, refreshes and rides the authenticated session.

Nobody stole anything. The server politely upgraded a ticket the attacker printed. The countermeasure is equally mechanical: rotate the session ID at every privilege transition. The anonymous session's ID dies; a fresh ID maps to the authenticated record. The planted value becomes a dead pointer.

Every framework ships the primitive:

// express-session
req.session.regenerate(err => { /* new SID, old record gone */ });
session_regenerate_id(true); // delete_old_session=true is the part people forget
# Rails
reset_session

regenerate_id without the delete flag, or regenerate-without-destroy equivalents, leaves the old ID valid — half a fix. The transition points that demand rotation: login, MFA step-up (aal1 → aal2), privilege elevation, and — cheap insurance — after password reset. Frameworks that rotate on login by default earned that default the hard way; the fixation bugs that motivated it were endemic in mid-2000s PHP/Java apps where pre-auth session IDs persisted through authentication and even appeared in URLs.

Two details that distinguish a real implementation from a cargo-culted one: rotation must happen after credential verification succeeds (rotating on failed attempts is harmless but pointless churn), and the old ID's server-side record must be deleted or invalidated — a regenerated ID that leaves the old pointer valid just doubles the ticket count.

Expiry is two clocks, and both are yours

Sessions need two independent TTLs:

  • Idle timeout: N minutes since last activity. Sliding — the lookup refreshes the clock. Admin panels deserve something like 15–30 minutes; a consumer app can run longer.
  • Absolute timeout: a hard wall-clock cap — 8 to 24 hours is common — after which the session dies regardless of activity. This bounds the value of a stolen token and forces periodic re-proof.

"Remember me" is not a longer session; it's a second artifact — a long-lived, rotatable token that exists solely to mint fresh sessions. Blending the two means either everyone gets logged out constantly or stolen sessions live for weeks. Cross-tab logout is the related UX edge: BroadcastChannel or a localStorage event lets one tab's logout kill the others — minor, but "I logged out and the other tab still worked" reads as a bug to users, and under theft it is one.

The wrong mental model

"The session ends when the user clicks logout" — only if you delete the server record; a destroyed cookie with a live server record is still a valid ticket if anyone copied it. And "cookie Max-Age is the session lifetime" — Max-Age is enforced by the browser. The TTL in Redis or your sessions.expires_at column is the actual control.

What to actually do

Rotate IDs on every privilege change and destroy the old record. Enforce idle + absolute TTLs server-side; make logout delete the store entry, not just instruct the browser. Keep the remember-me token separate and rotatable, and treat a presented-but-destroyed session ID hitting an authenticated route as a security event worth logging — under fixation, that request is the attacker noticing their ticket died. Cheap to implement, expensive to skip.

Keep reading

Related articles from the SudoSecurity blog.