01 / 21

Session
Management

Understand how web applications track users across requests using cookies, tokens, and session IDs: the invisible glue of the modern web.

Cyber Security

What Is a Session?

HTTP is a stateless protocol: each request the browser sends is independent, with no built-in memory of previous interactions. A session is the mechanism that lets a server remember who you are across multiple requests, from login to logout.

Why Sessions Matter

Authentication
Keep users logged in so they don't re-enter credentials on every page load.
Personalization
Store preferences, shopping carts, and UI settings between requests.
Security
Control access to protected resources and enforce timeouts against misuse.

The Session Lifecycle

1. Login User authenticates 2. Create Server issues ID 3. Maintain ID sent per request 4. Destroy Logout / timeout Browser (Client) Web Server Session ID

What Are Cookies?

A cookie is a small piece of data (up to ~4 KB) that a server sends to a browser via the Set-Cookie HTTP header. The browser stores it and automatically attaches it to every subsequent request to that domain.

Server Set-Cookie: sid=abc123; HttpOnly; Secure Browser Cookie: sid=abc123 (sent automatically) Response Request

Cookie Security Flags

HttpOnly
Prevents JavaScript from reading the cookie, blocking XSS-based theft.
Secure
Cookie is only sent over HTTPS, preventing interception on plain HTTP.
SameSite
Controls cross-origin sending: Strict, Lax, or None, mitigating CSRF attacks.

What Are Tokens?

A token is a self-contained credential, often a JSON Web Token (JWT), that encodes user identity and claims. Unlike cookies, tokens are typically stored in application memory or localStorage and sent via the Authorization header.

JSON Web Token (JWT) Structure Header Algorithm + Type . Payload User Claims + Expiry . Signature Integrity Verification

Cookies vs Tokens

Cookies Tokens (JWT)
Storage Browser cookie jar localStorage / memory
Sent via Automatic (Cookie header) Manual (Authorization header)
State Server-side (session store) Stateless (self-contained)
Scalability Requires shared store Easily scaled horizontally
CSRF Risk Higher (auto-sent) Lower (manual attach)
XSS Risk Lower (HttpOnly flag) Higher (if in localStorage)

Session ID Deep Dive

A session ID is a long, random, unpredictable string generated by the server. It acts as a lookup key into a server-side session store (database, Redis, or memory) that holds the actual user data.

1
High entropy: at least 128 bits of randomness to prevent brute-force guessing.
2
Opaque: the ID itself reveals nothing about the user or session contents.
3
Regenerated: a new ID is issued after login to prevent session fixation.
4
Short-lived: sessions expire after idle timeout (e.g., 15-30 minutes).

How to Secure a Session

1
Use HTTPS everywhere: encrypt all traffic so session IDs cannot be intercepted in transit.
2
Set HttpOnly + Secure + SameSite: apply all three cookie flags to block XSS and CSRF vectors.
3
Regenerate IDs on login: issue a fresh session ID after authentication to prevent fixation attacks.
4
Enforce timeouts: destroy sessions after inactivity (idle timeout) and after a maximum lifetime (absolute timeout).
5
Invalidate on logout: delete the server-side session record and clear the cookie on the client.

Example: Session Fixation

Attacker Victim Server 1. Attacker obtains a valid session ID: sid=EVIL 2. Attacker tricks victim into using sid=EVIL 3. Victim logs in with sid=EVIL 4. Attacker uses sid=EVIL to hijack authenticated session Fix: Regenerate session ID after login

Example: Session Hijacking

Victim Server Cookie: sid=abc123 Attacker (sniffer) intercepts replays sid Fix: HTTPS + Secure flag + short timeouts

Example: CSRF via Sessions

Victim Browser Malicious Site Bank Server visits forged request (cookie auto-attached) Fix: SameSite cookies + CSRF tokens

Token Best Practices

1
Short expiry: access tokens should expire in minutes (e.g., 15 min), not hours or days.
2
Refresh tokens: use a separate long-lived token stored securely to issue new access tokens.
3
Avoid localStorage: store tokens in memory or HttpOnly cookies to reduce XSS exposure.
4
Validate signatures: always verify the JWT signature server-side before trusting any claims.

Common Mistakes

✕ Predictable IDs
Using sequential integers or timestamps as session IDs makes them trivially guessable.
✕ No Regeneration
Keeping the same session ID before and after login enables session fixation attacks.
✕ Missing Flags
Omitting HttpOnly or Secure flags leaves cookies vulnerable to XSS and network sniffing.
✕ No Timeout
Sessions that never expire give attackers unlimited time to exploit stolen credentials.
✕ JWT in localStorage
Storing tokens in localStorage exposes them to any XSS vulnerability on the page.
✕ Trusting Client Data
Never trust a JWT payload without verifying the cryptographic signature server-side.

Session Management Summary

Concept Purpose Key Defence
Session ID Identifies user across requests High entropy + regeneration
Cookies Transport session ID automatically HttpOnly, Secure, SameSite
Tokens (JWT) Stateless authentication Short expiry + signature check
Session Fixation Attack: force known ID Regenerate ID on login
Session Hijacking Attack: steal valid ID HTTPS + Secure flag
CSRF Attack: forge requests SameSite + CSRF tokens

Question 1 of 5

HTTP is described as a "stateless" protocol. What does this mean for session management?

Question 2 of 5

Which cookie flag prevents JavaScript from accessing the cookie, thereby mitigating XSS-based session theft?

Question 3 of 5

An attacker sends a victim a link containing a pre-set session ID. After the victim logs in, the attacker uses that same ID to access the victim's account. Which attack is this?

Question 4 of 5

A JWT (JSON Web Token) consists of three parts. Which part ensures the token has not been tampered with?

Question 5 of 5

CSRF attacks exploit cookies because cookies are sent automatically with every request. Which combination of defences best mitigates CSRF?