OWASP Session Management

HTTP forgets every visitor after each request. Session management gives the application a memory. After a user logs in, the server hands out a session identifier, and the browser returns it with every request. Whoever holds that identifier acts as the user, so protecting it matters as much as protecting the password.

The Concert Wristband Idea

A concert venue checks your ticket once at the gate and then gives you a wristband. Guards inside the venue glance at the wristband and let you pass. A thief who copies the wristband walks in as you. A session identifier acts as that wristband for a website.

Session Life Cycle

 1. Login           User sends username and password
        |
        ↓
 2. Creation        Server creates a random session ID
        |
        ↓
 3. Delivery        Server sends ID in a secure cookie
        |
        ↓
 4. Use             Browser returns the cookie on each request
        |
        ↓
 5. Expiry          Idle timeout or logout destroys the session

Cookie Flags That Protect Sessions

Set-Cookie: sessionid=9f3a...c71; Secure; HttpOnly; SameSite=Lax; Path=/; Max-Age=1800
FlagProtection
SecureSends the cookie only over HTTPS
HttpOnlyHides the cookie from page scripts, which limits theft through script injection
SameSiteLimits cookie sending on requests that start from other sites
Max-Age or ExpiresLimits how long the browser keeps the cookie

Common Session Attacks

Session Hijacking

An attacker steals a valid identifier through network snooping, script injection, or a leaked log file. The server cannot tell the thief from the real user.

Session Fixation

An attacker plants a known identifier in the victim's browser before login. If the server keeps the same identifier after login, the attacker shares the authenticated session.

Predictable Identifiers

Identifiers built from counters, timestamps, or user names allow guessing. Strong identifiers come from a cryptographically secure random generator and carry at least 128 bits of randomness.

Identifiers in URLs

URLs appear in browser history, server logs, and shared links. A session identifier inside a URL leaks easily.

Design Rules for Strong Sessions

  1. Generate identifiers with the framework's built-in secure generator.
  2. Issue a brand new identifier immediately after login and after any privilege change.
  3. Store the session data on the server. Keep only the identifier in the cookie.
  4. Apply an idle timeout, such as 15 to 30 minutes, and an absolute timeout for the whole session.
  5. Destroy the session on the server when the user logs out. Clearing the browser cookie alone leaves the old session alive.
  6. Ask for the password again before sensitive actions, such as changing an email address.
  7. Allow users to view active sessions and sign out of other devices.

Tokens in Browser Storage

Single-page applications sometimes keep tokens in local storage. Any script on the page can read local storage, so one injected script exposes every token. An HttpOnly cookie removes that exposure.

Testing Your Own Sessions

Log in to a test account and inspect the cookie in the browser developer tools. Confirm that the three protective flags appear. Log out, then try to reuse the old cookie value in a fresh request. The server must reject it. Repeat the check after a password change, because active sessions should end when credentials change.

Key Takeaways

  • A session identifier works like a wristband, so thieves value it highly.
  • Secure, HttpOnly, and SameSite flags close common theft routes.
  • New identifiers at login defeat fixation.
  • Timeouts and real server-side logout limit damage from stolen sessions.