Overview
This vulnerability occurs when an application fails to properly invalidate user sessions after a period of inactivity (idle timeout) or after a maximum total duration (absolute timeout). If sessions remain valid indefinitely or for excessively long periods (e.g., months or years), the risk of session hijacking increases significantly. An attacker who manages to steal a session identifier (e.g., via XSS, network sniffing if notSecure, or guessing) has a much larger window of opportunity to reuse it. Furthermore, users accessing the application from shared computers might leave their sessions active inadvertently if there’s no timeout. ⏳➡️🔓
Business Impact
Insufficient session expiration leads to:- Prolonged Session Hijacking Window: Stolen session tokens remain valid for longer, increasing the chance an attacker can successfully use them.
- Increased Risk from Shared Computers: Users might remain logged in on public terminals, allowing subsequent users to access their accounts.
- Resource Consumption: Maintaining a large number of inactive sessions can consume server memory.
- Compliance Issues: Many security standards mandate specific session timeout durations.
Reference Details
CWE ID: CWE-613
OWASP Top 10 (2021): A07:2021 - Identification and Authentication Failures
Severity: Medium
Framework-Specific Analysis and Remediation
Session expiration is typically managed through framework configuration settings that control cookie expiry and server-side session data lifetime. Key Remediation Principles:- Implement Idle Timeout: Automatically log out users after a period of inactivity (e.g., 15-30 minutes). This involves tracking the last activity time on the server-side session.
- Implement Absolute Timeout: Automatically log out users after a fixed maximum duration, regardless of activity (e.g., 8-24 hours). This limits the maximum lifetime of any session identifier.
- Secure Logout Functionality: Provide a clear logout button that properly invalidates the session on the server-side.
- Cookie Expiry vs. Server Expiry: Ensure both the session cookie’s expiry (
Expires/Max-Ageattributes) and the server-side session data’s lifetime are configured appropriately. Server-side expiration is generally more critical.
- Python
- Java
- .NET(C#)
- PHP
- Node.js
- Ruby
Framework Context
ConfiguringSESSION_COOKIE_AGE and SESSION_SAVE_EVERY_REQUEST in Django settings.py. Flask requires configuring the session interface (e.g., PERMANENT_SESSION_LIFETIME for Flask-Session).Vulnerable Scenario 1: Django Session Never Expires
Vulnerable Scenario 2: Flask Default Session Lifetime (Browser Session)
Flask’s default client-side cookie session typically lasts only for the browser session unlesssession.permanent = True is set along with PERMANENT_SESSION_LIFETIME. If using server-side sessions (like Flask-Session), defaults might be too long or missing.Mitigation and Best Practices
- Django: Set
SESSION_COOKIE_AGEto a reasonable absolute timeout (e.g.,8 * 60 * 60for 8 hours). SetSESSION_SAVE_EVERY_REQUEST = Trueto enable idle timeout behavior (session expiry is reset on each request). Configure the session cleanup mechanism (./manage.py clearsessions). - Flask: Set
PERMANENT_SESSION_LIFETIME(atimedeltaobject or seconds) to a reasonable absolute timeout. Ensuresession.permanent = Trueis set after login if using this lifetime. For idle timeout, custom logic tracking last activity time in the session is usually needed.
Secure Code Example
Testing Strategy
Log into the application.- Idle Timeout: Leave the browser inactive for longer than the configured idle timeout period. Try performing an action (e.g., refresh page, click link). You should be logged out or prompted to log back in.
- Absolute Timeout: Note the login time. Wait longer than the configured absolute timeout period (even if you were active). Try performing an action. You should be logged out.
Inspect the session cookie’s
Expires/Max-Ageattribute in browser developer tools (though server-side expiration is more important).

