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.
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
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.
The Session Token: Core Principle
A secure session token must satisfy three properties simultaneously. If any one fails, the entire session mechanism is compromised.
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.
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
Securing Sessions: Step by Step
Example 1: Hijacking via XSS
Example 2: Session Fixation
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.
Session Timeouts
Two types of timeouts work together to limit the window of opportunity for an attacker who obtains a session token.
Common Mistakes
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?