01 / 21

Secure Session
Management

Learn how web applications track user identity across requests, why sessions are prime targets for attackers, and how to defend them with industry best practices.

Cyber Security

What Is a Session?

HTTP is a stateless protocol: every request is independent with no built-in memory of previous interactions. A session is a server-side mechanism that links a sequence of requests to the same authenticated user, creating the illusion of a continuous conversation.

How Sessions Work

User Browser Server Session Store Login Credentials Set-Cookie: SID=abc Store 1 2 3 4

Why Session Security Matters

If an attacker obtains a valid session token, they become the user without needing the password. Session attacks bypass authentication entirely, making them one of the most dangerous classes of web vulnerabilities.

✕ Session Hijacking
Attacker steals a valid session ID via network sniffing or XSS
✕ Session Fixation
Attacker forces a known session ID onto the victim before login
✕ CSRF
Attacker tricks a logged-in user's browser into making requests

The Session Token: Core Principle

A secure session token must satisfy three properties simultaneously. If any one fails, the entire session mechanism is compromised.

1
Unpredictable: Generated using a cryptographically secure pseudo-random number generator (CSPRNG) with at least 128 bits of entropy
2
Unique: No two active sessions share the same token. Collisions must be statistically impossible
3
Tamper-resistant: The token should reveal nothing about the user, their role, or session metadata

Token Entropy and Strength

The number of possible session IDs determines how hard brute-force guessing is. With \( n \) bits of entropy, an attacker faces \( 2^n \) possible values. Industry standards require at least 128 bits.

Cookie Security Flags

Session cookies must be configured with security attributes that restrict how they can be accessed and transmitted.

Secure
Cookie sent only over HTTPS, never plain HTTP
HttpOnly
Cookie inaccessible to JavaScript, blocking XSS theft
SameSite
Controls cross-site sending: Strict, Lax, or None

Hijacking vs Fixation

Session Hijacking Session Fixation
Attack timing After victim logs in Before victim logs in
Attacker's goal Steal existing token Plant known token
Attack vector XSS, sniffing, malware URL injection, meta tag
Primary defense HttpOnly + Secure flags Regenerate ID on login
OWASP category Broken Authentication Broken Authentication

Session Lifecycle

CREATION Login + new ID VALIDATION Each request check RENEWAL Rotate ID on priv TERMINATION Logout / timeout CSPRNG token Check expiry + IP New ID, same data Invalidate server-side Every stage must enforce security: create securely, validate constantly, rotate often, destroy completely.

Securing Sessions: Step by Step

1
Use HTTPS everywhere: Enforce TLS for the entire application, not just the login page. Set the Secure flag on all session cookies.
2
Generate strong tokens: Use the framework's built-in session manager (which calls a CSPRNG). Never build your own.
3
Set all cookie flags: Apply HttpOnly, Secure, SameSite=Strict, and an appropriate Max-Age or Expires.
4
Regenerate on privilege change: Issue a new session ID after login, role elevation, or password change to prevent fixation.
5
Implement proper logout: Invalidate the session server-side AND clear the cookie on the client. Do both.
6
Set idle and absolute timeouts: Sessions should expire after inactivity (e.g., 15 min) and absolutely (e.g., 8 hours) regardless of activity.

Example 1: Hijacking via XSS

Victim Web App Attacker 1. Injects XSS payload 2. Visits page 3. Malicious JS executes 4. document.cookie sent to attacker

Example 2: Session Fixation

Attacker Victim Server 1. Sends link: ?SID=KNOWN 2. Login 3. SID=KNOWN now valid 4. Attacker uses SID=KNOWN as victim Defense: Regenerate session ID immediately after successful login

Example 3: SameSite vs CSRF

In a CSRF attack, a malicious site tricks the victim's browser into sending a request to a trusted site. Because the browser automatically attaches cookies, the request carries the victim's session.

evil.com Browser bank.com Hidden form + Cookie SameSite=Strict Cookie NOT sent ✕ SameSite=Strict blocks ALL cross-origin cookie sending, defeating CSRF.

Session Timeouts

Two types of timeouts work together to limit the window of opportunity for an attacker who obtains a session token.

Idle Timeout
Expires the session after a period of no activity. Typically 15 to 30 minutes. Protects unattended devices.
Absolute Timeout
Expires the session after a fixed duration regardless of activity. Typically 8 to 24 hours. Forces re-authentication.

Common Mistakes

✕ Token in URL
Putting session IDs in query strings (e.g., ?SID=abc) exposes them in browser history, logs, and Referer headers.
✕ No Regeneration
Keeping the same session ID after login enables session fixation. Always regenerate on authentication.
✕ Client-Only Logout
Deleting the cookie without invalidating the session on the server leaves the token valid if intercepted.

Defense Summary

Threat Primary Defense Key Mechanism
Token guessing CSPRNG with > 128-bit entropy Unpredictability
XSS cookie theft HttpOnly flag JS access blocked
Network sniffing Secure flag + HTTPS Encrypted transport
Session fixation Regenerate ID on login Old token invalidated
CSRF SameSite=Strict Cross-origin blocked
Stolen token reuse Idle + absolute timeout Limited window

Question 1 of 5

Which cookie attribute prevents JavaScript from accessing the session cookie?

Question 2 of 5

In a session fixation attack, when does the attacker set the session ID?

Question 3 of 5

Why should session IDs be generated with a cryptographically secure random number generator (CSPRNG)?

Question 4 of 5

A web application deletes the session cookie on logout but does not invalidate the session on the server. What risk does this create?

Question 5 of 5

Which of the following is the best combination of defenses against session hijacking?