Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 1 · Lesson 4 · 4 min read

Session Management Security and Session Fixation

On this page
  1. Why session IDs matter as much as passwords
  2. Server-side sessions vs tokens
  3. Secure cookie attributes: HttpOnly, Secure, SameSite
  4. Session fixation: attack and fix in Express
  5. Session lifecycle: regeneration, timeouts and logout
  6. Check yourself

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).

From the test app (express-session with secure settings):

Code
Set-Cookie: sid=<id>; Path=/; Expires=Sun, 11 Oct 2026 17:06:14 GMT; HttpOnly; SameSite=Lax
AttributeProtects against
HttpOnlyJavaScript can't read the cookie, so an XSS bug can't simply steal it (Day 9)
SecureOnly sent over HTTPS (on in production; the local test ran over HTTP)
SameSite=Lax or StrictNot sent on most cross-site requests, which blocks most CSRF (Day 10)
Short expiryLimits how long a stolen session is useful
__Host- name prefixBrowser enforces Secure, Path=/ and no Domain, preventing subdomains from overwriting it
The server issues a new random session ID at login and on privilege changes, delivered in an HttpOnly, Secure, SameSite cookie, and destroys it on logout.
The server issues a new random session ID at login and on privilege changes, delivered in an HttpOnly, Secure, SameSite cookie, and destroys it on logout.

Session fixation: attack and fix in Express

In session fixation, the attacker doesn't steal the victim's session; they give the victim one:

  1. The attacker gets a valid, anonymous session ID from the site.
  2. 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).
  3. 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:

JavaScript
app.post('/login-vulnerable', (req, res) => { req.session.userId = 'ana'; res.json({ ok: true }); });

The fix creates a new session ID at login:

JavaScript
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:

Code
/login-vulnerable  id changed at login: false  attacker sees: {"user":"ana"}
/login             id changed at login: true  attacker sees: {"user":null}
  • 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.