Session Management
Last updated
Last updated
This page contains recommendations for the implementation of session management.
Do not store any sensitive data (tokens, credentials, PII, etc.) in a session ID.
Use the session management built into the framework you are using instead of implementing a homemade one from scratch.
Use up-to-date and well-known frameworks and libraries that implement session management.
Review and change the default configuration of the framework or library you are using to enhance its security.
Consider session IDs as untrusted data, as any other user input.
Implement an idle or inactivity timeout for every session.
Implement a mechanism to allow users to actively close a session (logout) after they have finished using an application.
Invalidate the session at least on the server side while closing a session.
Use different session IDs and token names for pre- and post-authentication flows.
Do not pass a session ID in an URL (path, query or fragment).
Renew or regenerate a session ID after any privilege level change within the associated user session (anonymous -> regular user
, regular user -> admin user
, etc.).
If a framework is used, change the default session ID name to something neutral, for example, sessionid
or id
.
Implement an absolute timeout for every session regardless of session activity.
Provide users with the ability to manage active sessions (view and close active sessions).
Use session IDs of length 16+ bytes.
Do not accept a session ID that has never been generated by an application. In case of receiving one, generate a new one for anonymous access and set it to a user.
Use the exp
claim to implement a session timeout.
Use the following algorithm to implement the logout functionality:
Store the jti
claim (unique token identifier) for each issued token.
If a user logged out from an application, move the jti
to a list of blocked tokens.
Remove a token from the block list when a token expires (check the exp
claim to determine if a token has expired).
Use the base
cookie format to store session IDs, see the page.
Do not cache session IDs if caching application contents is allowed, see the page.
Handle and store session IDs according to the page.
Log successful and unsuccessful events related to a session lifecycle (such as creation, regeneration, revoke) including attempts to access resources with invalid session IDs, see the page.
Use the ultimate
cookie format to store session IDs, see the page.
Generate a session ID using a cryptographically strong generator, see the page.
Use JSON Web Tokens (JWT) to implement stateless session management, see the page.