Skip to content

Lesson 1 of 14 · 14m

What a Session Really Is

A session is server-side state keyed by an opaque cookie value — everything else is plumbing.

The mechanism, no ceremony

A user POSTs a username and password to /login. The server checks the hash, generates 128 bits of cryptographic randomness, stores it in a session record — a Redis SETEX sess:9f3c... 3600 '{\"uid\":4517}', a row in a sessions table, an in-memory map — and replies with Set-Cookie: session=9f3c.... On every request after that, the browser sends the cookie back and the server looks the value up. If the lookup hits, the request is authenticated. If it misses, it isn't.

That is the entire mechanism. The cookie contains no identity, no permissions, no signature. It is an opaque pointer into state that lives on your side of the wire, which is exactly what makes server-side sessions the right default: you can kill a session by deleting a row.

The wrong mental model

The mental model people carry is "the cookie is the login" — as if the browser holds a signed certificate of identity. Under that model, stealing a cookie is mysterious and expiring one is fiddly. Under the real model it's boring: the cookie is a bearer ticket. Anyone presenting it is the user, because the server has no other way to distinguish them. Session hijacking isn't cracking anything; it's copying a pointer.

The opposite wrong model is "JWTs made sessions stateless, so this is legacy." A JWT moves claims into the cookie — but revocation, refresh rotation, device lists, and aal state still live server-side, as the token lessons in this course show. You never actually leave session-land; you just relocate which bytes are checked where.

Evidence it mattered

In October 2010, Eric Butler released Firesheep at ToorCon 12: a Firefox extension that sniffed session cookies off open Wi-Fi and handed you a one-click "become this person" sidebar for Facebook, Twitter, GitHub, Amazon. Nothing was cracked. The cookie was the account, and it was crossing the room in plaintext. Firesheep is a big part of why the web went HTTPS-everywhere — Facebook added always-on SSL in January 2011 and default-on HTTPS in 2012, and Google pushed HSTS and HTTPS-everywhere posture across its properties over the same window.

Where the state lives

The store choice is an ops decision with a security budget:

  • In-memory map: fastest, dies on restart, doesn't scale past one process. Fine for dev, a footgun in prod — every deploy logs everyone out.
  • Redis/Valkey: the standard. SETEX gives you TTL for free; expiry is enforced server-side where it belongs.
  • Postgres table: durable, auditable, slower per lookup; what Supabase-flavored stacks end up with for refresh tokens anyway.

Whatever the store, two expiries matter and they're different: the cookie's Max-Age (client-side politeness) and the record's TTL (the actual security control). Attackers don't respect Max-Age; your store's TTL is the only clock that counts.

What the session record holds

The server-side record is where policy lives. A reasonable row: id, user_id, created_at, last_seen_at, expires_at, ip, user_agent, aal (auth-assurance level — aal2 after MFA step-up on Supabase/GoTrue), and a generation counter if you rotate IDs in place. last_seen_at drives the idle timeout; expires_at the absolute cap. Storing ip/user_agent isn't enforcement — UAs change, users roam — it's audit context and a signal worth alerting on: a session record that jumped ASN and device class in one request is worth a re-auth challenge, not a silent pass.

The same table powers the feature users actually want: "active sessions / devices" pages and "log out everywhere else." Both are SELECT ... WHERE user_id = ? and DELETE — free once the store is real. That's the quiet product advantage of server-side sessions over bearer-token-only designs: enumeration and revocation are native operations.

What to actually do

  • Session IDs come from a CSPRNG, at least 128 bits — crypto.randomBytes(32).toString('base64url'), never Math.random() or a counter.
  • Store IDs hashed in the DB if the table getting dumped worries you (it should): sha256(sid) as the key means a leaked dump yields unusable pointers.
  • Enforce expiry in the store, both idle and absolute. The cookie lifetime is UX; the TTL is security.
  • Log the store operations — creates, lookups that miss, deletes — not cookie values.
  • Expose a session/device list and a "sign out other sessions" button; they're reads and deletes on data you already have.

Keep reading

Related articles from the SudoSecurity blog.