# Session Management
> Session management flaws let an attacker obtain, plant, forge or keep a valid session. Examples: an ID that's guessable or never rotated, a cookie that's readable or injectable, a stateless token signed with a weak key, or a session that survives logout and password change.
## How it works
After login, the session identifier (or signed token) *is* the user. Whoever presents it is authenticated, and MFA has already been satisfied ([[raw/onsecurity.io/session-management-vulnerabilities-what-developers-get-wrong-4358|OnSecurity]]). Security rests on five properties:
- **Unpredictable**: IDs come from a CSPRNG with at least 64 bits of entropy, carry no meaningful content, and use a generic cookie name ([[raw/cheatsheetseries.owasp.org/owasp-session-management-cheat-sheet-3896|OWASP]]).
- **Rotated at privilege change**: if the pre-login ID survives authentication, an attacker who planted it beforehand (URL token, injected cookie, XSS, `<meta>` Set-Cookie) shares the victim's authenticated session. This is **session fixation**, as opposed to hijacking, which steals an ID after login ([[raw/owasp.org/owasp-session-fixation-attack-3894|OWASP]], [[raw/owasp.org/owasp-wstg-testing-for-session-fixation-3892|WSTG]]).
- **Confined**: `Secure`, `HttpOnly` and `SameSite` limit theft over the network, through XSS and cross-site use ([[raw/onsecurity.io/session-management-vulnerabilities-what-developers-get-wrong-4358|OnSecurity]]). The `__Host-` and `__Secure-` prefixes are meant to stop subdomains overwriting cookies (cookie tossing). Browser/server parsing differentials defeat them: Unicode whitespace that Django or ASP.NET strip from cookie names, and legacy `$Version=1` parsing in Tomcat and Jetty ([[raw/portswigger.net/cookie-chaos-how-to-bypass-host-and-secure-cookie-prefixes-6980|Cookie Chaos]], [[raw/blog.ankursundara.com/cookie-bugs-smuggling-injection-5034|cookie smuggling]]). Tokens kept in `localStorage` are readable by any XSS on any app that shares them ([[raw/medium.com/sign-in-once-get-hacked-everywhere-the-hidden-cost-of-jwts-in-local-st-15444|JWTs in localStorage]]).
- **Unforgeable**: stateless sessions (JWTs, Flask, Django and Laravel signed cookies) move the risk to the signing key and the verification code. Weak or default secrets let attackers re-sign modified sessions ([[raw/ian.sh/introducing-cookiemonster-a-tool-for-breaking-stateless-authentication-7031|CookieMonster]]). Loose comparison of cookie hashes allows magic-hash forgery ([[raw/github.com/vvveb-1-0-9-0-authentication-bypass-in-generatecookie-14754|Vvveb]]).
- **Revocable and expiring**: idle and absolute timeouts, server-side invalidation on logout, and the ability to kill all sessions ([[raw/cheatsheetseries.owasp.org/owasp-session-management-cheat-sheet-3896|OWASP]]). A self-contained JWT stays valid until `exp` whatever the user does. Revocation needs short access tokens plus revocable refresh tokens, versioning or a denylist ([[raw/medium.com/why-your-logout-button-doesn-t-log-out-a-jwt-15144|JWT logout]], [[raw/medium.com/fast-logins-you-can-still-revoke-sessions-jwts-and-refresh-tokens-expl-14757|revocable fast logins]]).
Theft vectors now go beyond the web app itself. Infostealer malware harvests browser cookies at scale ([[raw/pretalx.com/the-cookie-is-a-lie-handing-canarytoken-sessions-to-infostealers-chcon-15537|CHCon talk]]). Desktop apps keep tokens on disk under DPAPI keys that the local user can recover ([[raw/blog.randorisec.fr/stealing-microsoft-teams-access-tokens-in-2025-6978|Teams tokens]]). AiTM proxies capture the post-MFA cookie ([[Adversary-in-the-Middle Phishing]]). Network sniffing, XSS, MITM and malware remain the core hijacking routes ([[raw/seraphicsecurity.com/session-hijacking-in-2025-techniques-attack-examples-and-defenses-4360|Seraphic]]).
## What to check
- [ ] Compare the session ID before and after login, and after privilege changes (role switch, MFA completion). Does it rotate?
- [ ] Will the app accept a session ID you supply (URL parameter, injected cookie) and authenticate it?
- [ ] ID quality: length, randomness (sample many), embedded data, framework-revealing cookie name.
- [ ] Cookie attributes: `Secure`, `HttpOnly`, `SameSite`, `Domain` scope, `Path`, prefixes. Can a sibling subdomain set a cookie that the app trusts?
- [ ] Server cookie parsing: does it normalize names (whitespace, case, legacy `$Version`) differently from browsers?
- [ ] Stateless tokens: identify the format, test for weak, known or default signing keys, and check `alg` handling (see [[JSON Web Tokens]]).
- [ ] Token storage on the client: `localStorage` or `sessionStorage` vs `HttpOnly` cookie. Is a refresh token cookie cross-site usable (`SameSite=None` together with permissive CORS)?
- [ ] Lifecycle: does logout invalidate server-side (replay the old cookie or token)? Is there an idle and absolute timeout? Is "log out everywhere" real? Are sessions revoked on password reset?
- [ ] Do session IDs leak via URLs, logs, error messages, WebSocket or debug responses?
## Notable cases
- **Zammad (CVE-2026-102489)**: a crafted WebSocket request leaked session cookies in error messages. An admin session was then used to overwrite the reset-email template, giving RCE through ERB. Used in the DIVD incident ([[raw/horizon3.ai/cve-2026-102489-deep-dive-zammad-session-leak-to-rce-12220|Horizon3]]).
- **Langflow (CVE-2025-34291, CVSS 9.4)**: a refresh-token cookie with `SameSite=None`, permissive CORS and an unauthenticated `/api/v1/refresh` let attackers steal access tokens, leading to ATO and then RCE via code validation ([[raw/obsidiansecurity.com/cve-2025-34291-critical-account-takeover-and-rce-in-langflow-3513|Obsidian]]).
- **Cookie Chaos**: `__Host-` and `__Secure-` bypasses through server-side name normalization (Django `.strip()`, ASP.NET) and legacy parsing (Tomcat, Jetty) ([[raw/portswigger.net/cookie-chaos-how-to-bypass-host-and-secure-cookie-prefixes-6980|PortSwigger]]).
- **Cookie tossing for forced login**: stored SVG XSS on an S3-backed subdomain was used to toss session cookies onto the main app ([[raw/medium.com/from-stored-xss-to-forced-login-via-cookie-tossing-13545|writeup]]).
- **Vvveb <1.0.9.0**: `generateCookie()` compared MD5 cookie hashes loosely, so "0e" magic-hash cookies forged persistent logins ([[raw/github.com/vvveb-1-0-9-0-authentication-bypass-in-generatecookie-14754|advisory]]).
- **CookieMonster**: a scanner that decodes and unsigns Django, Flask and Laravel sessions and JWTs built with weak secrets, then re-signs them ([[raw/ian.sh/introducing-cookiemonster-a-tool-for-breaking-stateless-authentication-7031|ian.sh]]).
## Defenses
- Use the framework's session manager. Generate IDs with a CSPRNG (≥64 bits) and give the cookie a generic name ([[raw/cheatsheetseries.owasp.org/owasp-session-management-cheat-sheet-3896|OWASP]]).
- Regenerate the ID unconditionally at login and on every privilege change. Reject IDs the server didn't issue ([[raw/onsecurity.io/session-management-vulnerabilities-what-developers-get-wrong-4358|OnSecurity]], [[raw/owasp.org/owasp-wstg-testing-for-session-fixation-3892|WSTG]]).
- Set `Secure; HttpOnly; SameSite=Lax/Strict`, use `__Host-` with host-only scope, and use HSTS. Make sure the server parses cookie names exactly (no trimming or normalization), and keep the server stack patched ([[raw/portswigger.net/cookie-chaos-how-to-bypass-host-and-secure-cookie-prefixes-6980|PortSwigger]]).
- Keep tokens in `HttpOnly` cookies rather than web storage ([[raw/medium.com/sign-in-once-get-hacked-everywhere-the-hidden-cost-of-jwts-in-local-st-15444|writeup]]).
- Use strong, rotated signing keys and constant-time strict comparison ([[raw/ian.sh/introducing-cookiemonster-a-tool-for-breaking-stateless-authentication-7031|CookieMonster]]).
- Apply idle and absolute timeouts, server-side invalidation on logout, and revoke-all on credential change. For JWTs, use short access tokens with revocable refresh tokens or a version or denylist check ([[raw/medium.com/fast-logins-you-can-still-revoke-sessions-jwts-and-refresh-tokens-expl-14757|writeup]]).
- Consider binding sessions to client properties and detecting anomalies such as IP or User-Agent changes ([[raw/cheatsheetseries.owasp.org/owasp-session-management-cheat-sheet-3896|OWASP]]).
- Deploy session-cookie canaries to detect infostealer replay ([[raw/pretalx.com/the-cookie-is-a-lie-handing-canarytoken-sessions-to-infostealers-chcon-15537|CHCon]]).
## Related
- [[Authentication Security]] · [[Account Takeover]] · [[MFA Bypass]] · [[Password Reset Vulnerabilities]] · [[Adversary-in-the-Middle Phishing]]
- [[SameSite Cookies]] · [[Login CSRF]] · [[Cross-Site Scripting]] · [[JSON Web Tokens]] · [[JWT Secret Cracking]] · [[Remote Code Execution]] · [[Security Talks]]
## Sources
- [[raw/cheatsheetseries.owasp.org/owasp-session-management-cheat-sheet-3896|OWASP Session Management Cheat Sheet]]
- [[raw/owasp.org/owasp-wstg-testing-for-session-fixation-3892|OWASP WSTG: Testing for Session Fixation]]
- [[raw/owasp.org/owasp-session-fixation-attack-3894|OWASP: Session fixation attack]]
- [[raw/onsecurity.io/session-management-vulnerabilities-what-developers-get-wrong-4358|Session Management Vulnerabilities: What Developers Get Wrong]]
- [[raw/seraphicsecurity.com/session-hijacking-in-2025-techniques-attack-examples-and-defenses-4360|Session Hijacking in 2025: Techniques, Attack Examples and Defenses]]
- [[raw/portswigger.net/cookie-chaos-how-to-bypass-host-and-secure-cookie-prefixes-6980|Cookie Chaos: How to bypass __Host and __Secure cookie prefixes]]
- [[raw/blog.ankursundara.com/cookie-bugs-smuggling-injection-5034|Cookie Bugs - Smuggling & Injection]]
- [[raw/medium.com/from-stored-xss-to-forced-login-via-cookie-tossing-13545|From Stored XSS to Forced Login via Cookie Tossing]]
- [[raw/ian.sh/introducing-cookiemonster-a-tool-for-breaking-stateless-authentication-7031|Introducing CookieMonster: a tool for breaking stateless authentication]]
- [[raw/github.com/vvveb-1-0-9-0-authentication-bypass-in-generatecookie-14754|Vvveb 1.0.9.0 authentication bypass in generateCookie()]]
- [[raw/medium.com/sign-in-once-get-hacked-everywhere-the-hidden-cost-of-jwts-in-local-st-15444|Sign In Once, Get Hacked Everywhere: The Hidden Cost of JWTs in Local Storage]]
- [[raw/obsidiansecurity.com/cve-2025-34291-critical-account-takeover-and-rce-in-langflow-3513|CVE-2025-34291: Critical Account Takeover and RCE in Langflow]]
- [[raw/horizon3.ai/cve-2026-102489-deep-dive-zammad-session-leak-to-rce-12220|CVE-2026-102489 Deep-Dive: Zammad Session Leak to RCE]]
- [[raw/medium.com/why-your-logout-button-doesn-t-log-out-a-jwt-15144|Why Your Logout Button Doesn’t Log Out a JWT]]
- [[raw/medium.com/fast-logins-you-can-still-revoke-sessions-jwts-and-refresh-tokens-expl-14757|Fast Logins You Can Still Revoke: Sessions, JWTs, and Refresh Tokens Explained]]
- [[raw/pretalx.com/the-cookie-is-a-lie-handing-canarytoken-sessions-to-infostealers-chcon-15537|The cookie is a lie: handing Canarytoken sessions to infostealers (CHCon 2026)]]
- [[raw/blog.randorisec.fr/stealing-microsoft-teams-access-tokens-in-2025-6978|Stealing Microsoft Teams access tokens in 2025]]