Session Management
How a website remembers that a visitor has signed in across otherwise unrelated requests, usually with a random identifier in a cookie, and the attacks that target that identifier.
Session management is how a website remembers who a visitor is between requests. HTTP is stateless: each request arrives on its own, with no built-in memory of the last one. After someone signs in, something must accompany every later request to say "this is still the same person." That something is the session.
The classic design is a session ID: a long random value the server generates at sign-in, stores alongside the user's details, and sends to the browser in a cookie. The browser returns it with every request, and the server looks it up. Everything depends on the value being unguessable, so it should come from a cryptographically secure random source, with at least 128 bits of randomness; see Pseudorandom Number Generator. The cookie should be marked so scripts can't read it, sent only over encrypted connections, and withheld from cross-site requests; see HTTP Cookie.
Sessions can live in two places:
- Server-side. The ID points to a record in a database or a fast Key-Value Store. Logging someone out is instant: delete the record.
- Client-side. The session data itself travels in a signed token, so no lookup is needed; see JSON Web Token. It scales easily but can't be revoked before it expires without rebuilding a server-side list.
Sessions need a lifecycle. An idle timeout ends a session after inactivity, an absolute timeout ends it after a fixed span regardless, and logout must invalidate it on the server, not just delete the browser's cookie. "Log out of all devices" requires the server to know every session a user holds. A remember me option usually issues a separate long-lived token that can create fresh sessions, rather than stretching the session itself.
The main attacks target the identifier:
- Session hijacking. An attacker who obtains a valid ID becomes the user. IDs have been stolen by injected scripts (Cross-Site Scripting) and, before encryption was universal, by eavesdropping on shared networks. A 2010 browser extension called Firesheep made that one click on public Wi-Fi, which helped push major sites to HTTPS everywhere.
- Session fixation. An attacker plants a known ID in the victim's browser before they sign in, then uses it afterward. The defense is to issue a new ID at every sign-in.
- Riding the session. Because browsers attach cookies automatically, another site can trigger actions using the victim's session; see Cross-Site Request Forgery.
Behind a Load Balancer, requests from one user can reach different servers, so sessions either live in a shared store or the balancer pins each user to one server, called sticky sessions.
Passwords, passkeys, and Single Sign-On establish identity once; the session carries it from then on.
See also2
Multi-Factor Authentication
Requiring a second kind of proof besides a password to sign in, such as a code, a phone prompt, or a hardware key, and why only the methods tied to the real website stop modern phishing.
Web Platform11 connections
Referrer Policy
The rules for when a browser tells a site which page a visitor came from, a header misspelled in the 1990s that leaked private addresses for decades until browsers began trimming it by default.
Web Platform12 connections
Linked from11
- Cross-Site Request ForgeryWeb Platform
Tricking a logged-in user's browser into sending a state-changing request to another site, which works because the browser attaches that site's cookies automatically.
- Cross-Site ScriptingWeb Platform
Injecting attacker-controlled script into a page that a victim's browser then executes with the victim's own privileges.
- HTTP CookieWeb Platform
A small piece of state a server asks the browser to store and send back on later requests, governed by attributes that decide exactly where it goes.
- JSON Web TokenWeb Platform
A signed, self-contained token that carries claims a server can verify without a database lookup.
- Key-Value StoreData & Content
The simplest kind of database, which stores opaque values under unique keys and supports little more than get, put and delete, trading query power for speed and easy scaling.
- Load BalancingNetworks & Distribution
A load balancer spreads requests across many backend instances, turning "one server" into "one address."
- MiddlewareWeb Platform
Functions that sit between an incoming web request and the code that answers it, each able to inspect, change, or end the request, chained in an order that quietly determines how the whole application behaves.
- Multi-Factor AuthenticationWeb Platform
Requiring a second kind of proof besides a password to sign in, such as a code, a phone prompt, or a hardware key, and why only the methods tied to the real website stop modern phishing.
- Pseudorandom Number GeneratorComputation & Algorithms
An algorithm that produces a sequence of numbers that looks random but is fully determined by a starting seed, and the separate, stricter kind needed for security.
- Referrer PolicyWeb Platform
The rules for when a browser tells a site which page a visitor came from, a header misspelled in the 1990s that leaked private addresses for decades until browsers began trimming it by default.
- Single Sign-OnWeb Platform
Signing in once with a central identity provider and being trusted by many separate applications, through signed assertions passed by browser redirects, which concentrates both convenience and risk in one place.