Cookies, sessions and why they leak
Stage 1 · Lesson 5 · 15 minutes · Requires Lessons 1–4
The one-line answer
HTTP has no memory, so a small piece of text carries your identity on every request — and stealing that text is as good as stealing your password.
Why you should care
Session handling is where authentication actually lives. Passwords are checked once; sessions are checked thousands of times afterwards.
That makes sessions the highest-value target in most web applications. An attacker who obtains a valid session bypasses the password entirely, along with any multi-factor authentication that happened at login. There is nothing left to defeat.
Broken authentication and session management sits permanently in the OWASP Top 10 because it is common, easy to get wrong, and severe when it fails. This lesson is the foundation for a large portion of Stage 3.
How it actually works
Why sessions exist
Stage 0 Lesson 3 established that HTTP is stateless. Each request arrives independently, and the server has no inherent knowledge that the request in front of it belongs to the person who logged in a moment ago.
So on successful login, the server issues an identifier. The browser stores it and sends it back with every subsequent request. The server looks it up and knows who you are.
1. POST /login username + password
2. 200 OK Set-Cookie: session=a8f3d9e2b1
3. GET /dashboard Cookie: session=a8f3d9e2b1
4. 200 OK "Welcome back, Priya"Step 3 is the important one. From that moment, a8f3d9e2b1 is your identity. The password is no longer involved.
Anatomy of a cookie
Set-Cookie: session=a8f3d9e2b1; Domain=example.com; Path=/; Expires=Fri, 21 Feb 2026 09:12:00 GMT; Secure; HttpOnly; SameSite=Lax| Attribute | Purpose |
|---|---|
name=value | The data itself |
Domain | Which host receives it |
Path | Which paths receive it |
Expires / Max-Age | When it dies. Absent means session cookie — gone when the browser closes |
Secure | Only send over HTTPS |
HttpOnly | JavaScript cannot read it |
SameSite | Whether it is sent on cross-site requests |
The three flags that matter
HttpOnly
Blocks JavaScript from reading the cookie via document.cookie.
Without it, any XSS vulnerability becomes complete session theft — the injected script simply reads the cookie and sends it to the attacker. With it, XSS is still serious, but the session itself is not directly readable.
You checked this in Stage 0 Lesson 7: cookies visible in Application → Cookies but absent from document.cookie are the ones protected.
Secure
Prevents transmission over plain HTTP.
Without it, a single accidental HTTP request — a mistyped link, an image, an old bookmark — sends the session in cleartext across the network. Anyone positioned in between reads it. This is exactly the Wireshark demonstration from Lesson 3.
SameSite
Controls whether the cookie accompanies requests originating from other sites.
| Value | Behaviour |
|---|---|
Strict | Never sent cross-site. Strongest, but breaks inbound links to logged-in pages |
Lax | Sent on top-level navigation only. The modern browser default |
None | Always sent. Requires Secure. Necessary for legitimate cross-site flows |
SameSite is the primary structural defence against CSRF, and its adoption as a default significantly reduced that attack class across the web.
Domain and path scope
Domain=example.com → sent to example.com and all subdomains
Domain=app.example.com → sent only to that hostSetting a cookie on the parent domain is a decision with consequences. If example.com issues a session cookie scoped to the whole domain, then blog.example.com, staging.example.com and any other subdomain receive it too. Compromise the weakest subdomain and you have the session for all of them.
This connects directly to subdomain takeover from Stage 0 Lesson 4 — that is precisely why taking over a forgotten subdomain of a trusted brand can be high severity rather than cosmetic.
Two models for holding state
Server-side sessions. The cookie holds a meaningless random identifier. All real data lives in the server's store — memory, Redis, a database.
Advantage: revocation is trivial. Delete the record and the session is dead instantly. Cost: the server must store and look up state on every request.
Client-side tokens (JWT). The cookie or header carries the data itself, signed so it cannot be altered undetected.
Advantage: no server-side storage, scales easily across services. Cost: revocation is genuinely hard. A signed token remains valid until it expires, whether or not the user logged out.
You met JWT structure in Stage 0 Lesson 6 — header, payload and signature, with the payload Base64-encoded and therefore readable by anyone. Stage 4 attacks these properly.
Where tokens get stored, and the trade-off
| Location | XSS risk | CSRF risk |
|---|---|---|
HttpOnly cookie | Protected | Needs SameSite |
localStorage | Fully exposed | Not applicable |
sessionStorage | Fully exposed | Not applicable |
| JavaScript variable | Exposed while running | Not applicable |
Developers frequently choose localStorage for convenience. It is readable by any script on the page, which means one XSS flaw yields the token immediately.
The generally sound answer is an HttpOnly, Secure, SameSite cookie. It trades an XSS risk you cannot easily mitigate for a CSRF risk you can address structurally.
Session lifecycle
Four moments determine whether session handling is sound.
Creation. The identifier must be long and generated by a cryptographically secure random source. Predictable identifiers — sequential, timestamp-derived, or a hash of the username — can be guessed or calculated.
Rotation on privilege change. A new identifier must be issued at login. Failing to do this permits session fixation: an attacker sets a known session value in the victim's browser beforehand, the victim logs in, the identifier remains unchanged, and the attacker's copy is now authenticated.
Expiry. Sessions need both an idle timeout and an absolute maximum lifetime. Indefinite sessions are indefinite exposure.
Destruction. Logout must invalidate the session on the server. Clearing the cookie in the browser alone leaves the identifier valid — anyone holding a copy continues to use it.
That last point is worth stating plainly: a logout that only deletes the client-side cookie is not a logout. It is a genuine and frequently found bug.
See it yourself
Inspect real cookies.
Log into any application. Devtools → Application → Cookies. For each one, check HttpOnly, Secure and SameSite. Compare a bank with a small site — the difference in care is usually visible immediately.
Confirm HttpOnly works.
In the Console:
document.cookieCookies present in the Application tab but missing here are protected. That absence is the flag functioning.
Watch a session being issued.
Open Network, log in, find the login response, and read Set-Cookie in the response headers. Then examine the next request and confirm the cookie is being sent back.
Check rotation.
Note the session value before login and after login. If it did not change, the application is vulnerable to session fixation. This is a real test performed in real assessments, and it takes fifteen seconds.
Check logout properly.
Copy your session value. Log out. Then replay a request using the old value:
curl -H "Cookie: session=<old-value>" https://target/dashboardIf it still returns your data, the logout did not invalidate server-side.
Boundary: perform these tests on your own accounts, on our labs, or on a local instance of something like OWASP Juice Shop. Testing session handling on someone else's application is testing, and Stage 0 Lesson 9 applies fully.
Read the storage.
localStorage
sessionStorageCheck applications you use daily. Tokens sitting in localStorage are common, and now you know what that implies.
How attackers abuse this
Session hijacking via XSS. Injected script reads document.cookie and exfiltrates it. Prevented structurally by HttpOnly.
Session hijacking via network capture. Missing Secure, or plain HTTP anywhere in the flow, exposes the identifier to anyone on the path. Historically automated by tools that made this trivial on shared Wi-Fi.
Session fixation. Plant a known identifier before login, wait for the victim to authenticate, then use the same value. Defeated entirely by rotating on login.
Predictable identifiers. Sequential or derivable values allow enumeration of other users' sessions. Rare in modern frameworks, still found in custom implementations.
CSRF. A malicious page triggers a state-changing request to a site where the victim is authenticated. The browser attaches the cookie automatically. Mitigated by SameSite and anti-CSRF tokens.
Cookie scope abuse. A vulnerability on any subdomain reaching a session scoped to the parent domain.
Tokens that outlive logout. JWTs remain valid until expiry regardless of logout, unless the application maintains a revocation list. An attacker who captured a token retains access.
Reading the payload for recon. Even without breaking a signature, decoding a JWT payload reveals user identifiers, roles, expiry and internal field names — a free map of the authorisation model, as noted in Stage 0 Lesson 6.
Common mistakes
"The session cookie is encrypted." It is usually a random identifier, which needs no encryption because it is meaningless without the server's store. What matters is that it is unpredictable and transmitted safely. A JWT, meanwhile, is signed rather than encrypted — its payload is readable.
"Logging out logs the attacker out." Only if invalidation happens server-side. With stateless tokens and no revocation list, it often does not.
"HTTPS makes session theft impossible." It prevents interception in transit. It does nothing about XSS, malware on the endpoint, or a session left valid indefinitely.
"localStorage is safer because cookies are sent automatically." It removes CSRF exposure and replaces it with total XSS exposure. CSRF has a clean structural fix in SameSite; XSS does not have an equivalently reliable one.
"Long random tokens are authorisation." Stage 0 Lesson 5 covered this. Unguessable is not the same as authorised. The server must still verify that this session may perform this action on this resource.
Check yourself
- An application issues a session cookie with
SecureandSameSite=Laxbut notHttpOnly. What specific attack does this enable, and what is the impact? - You note the session identifier before login and it is identical after login. What is the vulnerability, and how would an attacker use it?
- Why is revoking a JWT harder than revoking a server-side session, and what do applications do about it?
Answers
- Without
HttpOnly, JavaScript can read the cookie viadocument.cookie. Any XSS vulnerability on the site therefore escalates to full session theft — the attacker's script reads the session and transmits it, giving complete account access without a password and bypassing any MFA performed at login.
- Session fixation. The identifier was not rotated on authentication, so an attacker who can set a session value in the victim's browser beforehand — through a link containing a session parameter, or an XSS on a subdomain — holds a copy that becomes authenticated the moment the victim logs in. The fix is issuing a new identifier at every privilege change.
- A server-side session is revoked by deleting one record, and the next request fails immediately. A JWT is self-contained and validated by signature alone, so the server has nothing to delete — the token stays valid until it expires. Applications mitigate this with short expiry times plus refresh tokens, or by maintaining a denylist of revoked tokens, which reintroduces the server-side state that JWTs were adopted to avoid.
Your lab
Challenge 08 — Decode a session cookie
An application stores more in its session cookie than it should. Read it, understand it, and find what the developer assumed nobody would look at.
→ Open Stage 1 labs
Summary in your language
தமிழ் சுருக்கம் (Tamil summary)
Cookies மற்றும் Sessions — அடையாளத்தை சுமக்கும் சிறு உரை
HTTP-க்கு நினைவாற்றல் இல்லை. ஒவ்வொரு request-ம் தனித்தனியாக வருகிறது. எனவே login செய்த பிறகு, server ஒரு அடையாள எண்ணை (session ID) கொடுக்கிறது.
1. POST /login username + password
2. 200 OK Set-Cookie: session=a8f3d9e2b1
3. GET /dashboard Cookie: session=a8f3d9e2b1
4. 200 OK "வணக்கம், Priya"முக்கியமான புரிதல்: அந்த தருணத்தில் இருந்து, a8f3d9e2b1 தான் உங்கள் அடையாளம். Password இனி தேவையில்லை.
எனவே session-ஐ திருடுவது password-ஐ திருடுவதற்கு சமம் — MFA இருந்தாலும் பயனில்லை, ஏனெனில் அது login-ல் ஏற்கனவே முடிந்துவிட்டது.
மூன்று முக்கியமான flags:
1. HttpOnly — JavaScript இந்த cookie-ஐ படிக்க முடியாது.
இது இல்லாவிட்டால், ஒரு XSS bug இருந்தாலே போதும் — attacker-ன் script cookie-ஐ படித்து அனுப்பிவிடும். முழு கணக்கும் கைப்பற்றப்படும்.
2. Secure — HTTPS-ல் மட்டுமே அனுப்பப்படும்.
இது இல்லாவிட்டால், ஒரு தவறான HTTP request போதும் — session வெளிப்படையாக network-ல் செல்லும். Lesson 3-ல் Wireshark-ல் பார்த்தது இதுதான்.
3. SameSite — வேறு website-ல் இருந்து வரும் request-களுக்கு அனுப்பப்படுமா?
இதுதான் CSRF தாக்குதலுக்கு எதிரான முக்கிய தடுப்பு.
இரண்டு வகையான session:
- Server-side session — cookie-ல் வெறும் random எண். தரவு server-ல். logout உடனடியாக வேலை செய்யும்.
- JWT (client-side token) — தரவே token-ல் இருக்கும். logout கடினம் — token காலாவதி ஆகும் வரை செல்லுபடியாகும்.
localStorage-ல் token வைப்பது ஏன் ஆபத்து?
எந்த JavaScript-ம் அதைப் படிக்க முடியும். ஒரு XSS bug போதும், token போய்விடும்.
சிறந்த வழி: HttpOnly + Secure + SameSite cookie.
நீங்களே சோதிக்கக்கூடிய இரண்டு விஷயங்கள்:
- Session rotation — login-க்கு முன்பும் பின்பும் session ID ஒன்றாகவே இருக்கிறதா? இருந்தால் அது session fixation பாதிப்பு.
- Logout சரியாக வேலை செய்கிறதா? பழைய session ID-ஐ வைத்து மீண்டும் முயற்சி செய்யுங்கள். வேலை செய்தால், logout server-ல் நடக்கவில்லை — இது உண்மையான bug.
கவனிக்க: உங்கள் சொந்த கணக்குகளில் மட்டும் இதை சோதியுங்கள். Stage 0 Lesson 9 பொருந்தும்.
हिंदी सारांश (Hindi summary)
Cookies और Sessions — पहचान ढोने वाला छोटा सा text
HTTP की कोई याददाश्त नहीं होती। हर request अलग से आती है। इसलिए login के बाद server एक पहचान संख्या (session ID) देता है।
1. POST /login username + password
2. 200 OK Set-Cookie: session=a8f3d9e2b1
3. GET /dashboard Cookie: session=a8f3d9e2b1
4. 200 OK "नमस्ते, Priya"ज़रूरी समझ: उस पल से, a8f3d9e2b1 ही आपकी पहचान है। Password की अब ज़रूरत नहीं।
इसलिए session चुराना password चुराने के बराबर है — MFA भी बेकार, क्योंकि वह login पर पहले ही हो चुका।
तीन ज़रूरी flags:
1. HttpOnly — JavaScript इस cookie को पढ़ नहीं सकता।
अगर यह न हो, तो एक XSS bug ही काफ़ी है — attacker का script cookie पढ़कर भेज देगा। पूरा account चला जाएगा।
2. Secure — सिर्फ़ HTTPS पर भेजा जाएगा।
अगर यह न हो, तो एक ग़लत HTTP request काफ़ी है — session खुले में network पर चला जाएगा। Lesson 3 में Wireshark में यही देखा था।
3. SameSite — दूसरी website से आने वाली requests के साथ भेजा जाए या नहीं?
यही CSRF हमले के ख़िलाफ़ मुख्य बचाव है।
दो तरह के session:
- Server-side session — cookie में सिर्फ़ random संख्या। data server पर। logout तुरंत काम करता है।
- JWT (client-side token) — data ख़ुद token में। logout मुश्किल — token expire होने तक चलता रहता है।
localStorage में token रखना ख़तरनाक क्यों है?
कोई भी JavaScript उसे पढ़ सकता है। एक XSS bug काफ़ी है, token चला गया।
बेहतर तरीक़ा: HttpOnly + Secure + SameSite cookie।
दो चीज़ें जो आप ख़ुद जाँच सकते हैं:
- Session rotation — login से पहले और बाद में session ID एक ही है? तो यह session fixation कमज़ोरी है।
- Logout सही काम कर रहा है? पुरानी session ID से दोबारा कोशिश करें। अगर चल गई, तो logout server पर हुआ ही नहीं — यह असली bug है।
ध्यान दें: यह सब सिर्फ़ अपने account पर जाँचें। Stage 0 Lesson 9 लागू होता है।
Want this summary in Malayalam, Telugu, Kannada or another language? Tell us — the most requested languages get added next.
What's next
You can now read and test how a system and its sessions behave. Next: stop typing the same commands by hand. A few lines of Bash turn any check you do twice into a script that does it for you.
→ Lesson 6: Bash scripting: make the terminal work for you