Issue
HTTP does not maintain user state between requests by itself. Web applications commonly use a session identifier—a token sent with each request—to associate a browser with server-side session data, such as the user’s authenticated state and permissions. If an attacker obtains or controls a valid identifier, the application may treat the attacker’s requests as belonging to that user. OWASP describes this as session hijacking. OWASP Session Management Cheat Sheet
Session management vulnerabilities occur when an application creates, transports, validates, renews, or expires session identifiers insecurely. Common causes include:
- Weak or predictable identifiers. A session token should be generated using a cryptographically secure random number generator. Its length alone does not prove it is unpredictable; fixed or patterned portions reduce its effective randomness. OWASP recommends at least 64 bits of entropy, and advises developers creating their own identifiers to use at least 128 bits. OWASP Session Management Cheat Sheet
- Session fixation. This occurs when an application keeps the same session identifier after a user authenticates, allowing an attacker who can cause the victim to use a known identifier to share the authenticated session. Regenerating the identifier after login is a primary defense. OWASP WSTG: Session Fixation
- Insecure token transport or storage. Session identifiers in URLs may be copied into browser history, logs, or referrer data. For cookie-based sessions, missing Secure or HttpOnly attributes can increase exposure: Secure restricts transmission to secure channels, while HttpOnly prevents access through script-facing cookie APIs. Neither attribute fixes every session risk. OWASP Session Management Cheat Sheet RFC 6265
- Incomplete expiration or invalidation. Removing a cookie from the browser does not, by itself, invalidate a copy of that token held elsewhere. The server must reject the session after logout or expiration. Sessions should have an inactivity timeout and an overall lifetime appropriate to the application’s risk. OWASP Session Management Cheat Sheet NIST SP 800-63B-4, §5.1
Examples
Example: session cookie configuration
Set-Cookie: __Host-session=<random-value>; Path=/; Secure; HttpOnly; SameSite=Lax
This example sets a host-only cookie using the __Host- prefix requirements. SameSite=Lax can reduce some cross-site cookie sending, but it is not a universal replacement for application-level CSRF defenses. Choose SameSite based on the application’s cross-site flows and test those flows in supported browsers. OWASP Session Management Cheat Sheet
Example: testing whether logout invalidates a session
First, save the test session cookie from an authorized test login. After logging out, replay it against a protected test endpoint:
GET /account HTTP/1.1Host: app.example.comCookie: __Host-session=<saved-test-value>
The response may require authentication and return no protected account data. A redirect to a login page is common, but the key evidence is that the server does not honor the old token.
Example: possible fixation indicator
Suppose the browser has identifier A before login and still has A afterward. This is an indicator worth investigating. To confirm a fixation flaw, a controlled test must establish that a tester can choose or cause the use of A, that the application accepts A before authentication, and that the same token then grants access to the authenticated test session. An unchanged cookie alone does not establish all of those conditions.
Impact
If an attacker obtains a still-valid session identifier, the attacker may be able to act as the associated user until the session is invalidated or expires. The actions available depend on that user’s permissions and the application’s controls. A session-management weakness does not, by itself, prove that an attacker accessed data, bypassed authentication, or took over an account.
The practical risk depends on whether an attacker can obtain or set a token, whether the application accepts it, how long it remains valid, and what access the session grants. For example, a missing cookie attribute is a configuration weakness to investigate, but it does not alone demonstrate that a session was stolen. Likewise, an unchanged token across login is an indicator of possible session fixation; confirmation requires showing that an attacker-controlled or known pre-login token remains accepted for the authenticated session.
Testing
1. Check session invalidation and privilege changes
Sign in to a test account, capture its session, then sign out or change its password or privileges. Replay the previous session against a protected endpoint and verify access.
Example: A logout page alone does not prove the old session was revoked. Check the server’s response to the old session.
GET /api/profile HTTP/1.1
Host: app.example.test
Cookie: session=OLD_TEST_SESSION
2. Check session rotation and fixation
Compare the session identifier before and after login and any privilege change. Attempt to reuse a pre-login test session after authentication.
Example: A session value that survives login can be a lead. Verify whether the server actually accepts it as authenticated.
GET /api/profile HTTP/1.1
Host: app.example.test
Cookie: session=PRE_LOGIN_TEST_SESSION
3. Review cookie scope and session lifetime
Inspect Secure, HttpOnly, SameSite, Domain, Path, expiry, and idle or absolute timeouts in the supported browser flow. Test whether old sessions remain usable beyond the intended policy.
Example: A missing flag is a configuration issue in context. Its practical impact depends on the cookie’s purpose and browser behavior.
Mitigation
Primary fixes
- Use the session mechanism provided by a maintained framework, configured according to its security guidance. If generating identifiers yourself, use a cryptographically secure random number generator and sufficient unpredictable entropy. Do not encode usernames, roles, or other sensitive state in the identifier. Keep session meaning and authorization data on the server or protect client-held state with a carefully designed, authenticated mechanism. OWASP Session Management Cheat Sheet
- Regenerate the session identifier after login and other privilege changes, and invalidate the previous identifier. This prevents a pre-authentication token from continuing as the authenticated session.
- For cookie-based sessions, set Secure and HttpOnly; choose an explicit SameSite policy; and keep cookie scope as narrow as the application permits. Serve the application over HTTPS and use HSTS where appropriate. These controls address different exposures and do not replace server-side session validation.
- Enforce inactivity and overall timeouts on the server. On logout or expiration, invalidate the server-side session so that replaying the old identifier fails. Where single sign-on is used, define and test how application sessions and identity-provider sessions terminate; they may be separate. OWASP WSTG: Logout Functionality NIST SP 800-63B-4, §5.1
Additional defensive measures
- Avoid placing session identifiers in URLs. Redact tokens from application, proxy, and analytics logs.
- Require reauthentication for sensitive actions where the application’s risk warrants it.
- Protect against cross-site scripting (XSS) and cross-site request forgery (CSRF). HttpOnly limits script access to a cookie, but it does not stop malicious scripts from making requests through the victim’s browser. SameSite helps with some cross-site request scenarios but should be evaluated alongside the application’s CSRF protections.
- Limit access to server-side session stores and protect backups. Consider monitoring for unusual session use, while accounting for shared networks and changing client addresses; binding a session rigidly to an IP address can disrupt legitimate users.
Retest by confirming that identifiers rotate at authentication boundaries, old identifiers fail after logout and expiration, cookie attributes match the intended policy in supported browsers, and protected endpoints reject requests without a valid session.