Session Management Security and Session Fixation
On this page
Why session IDs matter as much as passwords
Login checks a password once. After that, every request is trusted because it carries a session identifier, usually in a cookie. It works like a coat-check ticket: the cloakroom doesn't ask who you are when you come back, it just hands the coat to whoever holds the ticket. Whoever holds that ID is the user, so stealing, guessing or planting it is as good as knowing the password.
Server-side sessions vs tokens
- Server-side sessions: a random ID in a cookie points to session data stored on the server (memory, Redis, a database). Easy to revoke: delete the record.
- Self-contained tokens (like JWTs, Day 20): the data is in the token, signed. No lookup, but hard to revoke before expiry.
For browser apps, a server-side session in a secure cookie is usually the simplest safe choice. Either way, the identifier must be long and random (at least 128 bits from a cryptographically secure generator).
Secure cookie attributes: HttpOnly, Secure, SameSite
From the test app (express-session with secure settings):
Set-Cookie: sid=<id>; Path=/; Expires=Sun, 11 Oct 2026 17:06:14 GMT; HttpOnly; SameSite=Lax| Attribute | Protects against |
|---|---|
| HttpOnly | JavaScript can't read the cookie, so an XSS bug can't simply steal it (Day 9) |
| Secure | Only sent over HTTPS (on in production; the local test ran over HTTP) |
| SameSite=Lax or Strict | Not sent on most cross-site requests, which blocks most CSRF (Day 10) |
| Short expiry | Limits how long a stolen session is useful |
__Host- name prefix | Browser enforces Secure, Path=/ and no Domain, preventing subdomains from overwriting it |
Session fixation: attack and fix in Express
In session fixation, the attacker doesn't steal the victim's session; they give the victim one:
- The attacker gets a valid, anonymous session ID from the site.
- They get the victim's browser to use it (a crafted link on sites that accept IDs in URLs, a subdomain that sets cookies, or a shared computer).
- The victim logs in. If the server keeps the same session ID, the attacker's copy is now logged in too.
Reproduced against a small Express app. The vulnerable login just marks the existing session as authenticated:
app.post('/login-vulnerable', (req, res) => { req.session.userId = 'ana'; res.json({ ok: true }); });The fix creates a new session ID at login:
app.post('/login', (req, res, next) => {
req.session.regenerate(err => { // new ID; the pre-login ID becomes worthless
if (err) return next(err);
req.session.userId = 'ana';
res.json({ ok: true });
});
});The attacker plants a session, the victim logs in with it, then the attacker tries the planted ID:
/login-vulnerable id changed at login: false attacker sees: {"user":"ana"}
/login id changed at login: true attacker sees: {"user":null}Session lifecycle: regeneration, timeouts and logout
- Regenerate the ID at login, logout, and privilege changes (switching to admin mode, completing MFA).
- Idle timeout (for example 15 to 30 minutes for sensitive apps) and an absolute timeout (for example 8 to 24 hours). The idle timeout ends a session nobody is using, such as a laptop left open in a library. The absolute timeout ends even an active one, so a stolen session can't be kept alive forever by an attacker who keeps making requests.
- Logout destroys the session server-side, not just the cookie.
- Let users see and revoke sessions on other devices, and end all sessions after a password change.
- Never put session IDs in URLs; they leak through logs, history and Referer headers.
Check yourself
Otherwise an attacker who planted or knew the pre-login session ID, for example through a link, shares the session once the victim logs in, which is session fixation. Regenerating the ID at login makes the old one worthless.
It stops JavaScript from reading the cookie, limiting theft via XSS; it doesn't stop XSS from making requests as the user.
Clearing only the browser's cookie leaves the session valid on the server, so anyone who already copied the ID, from a shared computer or a stolen cookie, can keep using it. Server-side deletion ends it for everyone.
An absolute timeout, which ends every session after a fixed time, such as 12 hours, regardless of activity. An idle timeout alone never fires, because the attacker keeps the session active.