Skip to main content
Security guideReviewed

Session management vulnerabilities: how a session should start, change and end

After someone signs in, a single random value stands in for their password on every request.

This guide explains in plain English how that value should be issued, when it must change, when it must stop working and who should be able to end it, with the standard behind each rule. It comes with a session lifecycle state diagram and six local checks you can run with Node.js alone.

Session lifecycle state diagram and local session checks

An illustration on an invented account. Part 1 is a session lifecycle state diagram in plain text, and Part 2 lists every transition with what must happen to the identifier and the requirement behind it.

How to use it

Parts 3 to 6 are a Node.js lab with no installs: a session service in a flawed and a fixed version with a clock the checks can move, six checks that print every request and result, a regression test that fails on the flawed version, and steps to point the checks at your own application. Run it on your own machine, and use the checks only on applications you own or are authorised in writing to test.

session-management.txt

Introduction14 lines

SESSION LIFECYCLE STATE DIAGRAM AND LOCAL SESSION CHECKS An illustration. The account, its password, the one-time code and the reset code are invented, and the lab runs only on your own machine against its own local server. Use the checks on applications you own or are authorised in writing to test, with test accounts. Written and run on: Node.js 26.0.0, the only version it was run on. It uses node:http, node:crypto, node:test and node:assert, which ship with Node.js, and the fetch() function built into Node.js, so there is nothing to install. Every output below was copied from that run. Timings will differ on your machine, and the ports are picked by the system and never printed. Source names used below: ASVS OWASP Application Security Verification Standard 5.0.0, chapter V7 Session Management, and V8 Authorization for 8.3.2. The level follows each requirement number. OWASP SM OWASP Session Management Cheat Sheet OWASP FP OWASP Forgot Password Cheat Sheet OWASP AUTH OWASP Authentication Cheat Sheet WSTG-SESS OWASP Web Security Testing Guide v4.2, session management tests (SESS-01 to SESS-09) CWE MITRE CWE version 5.0 NIST NIST SP 800-63B-4, section 5 Session Management, with sections 2.1.3, 2.2.3 and 2.3.3 for the timeouts at each assurance level

What a session is, and why whoever holds the identifier is the user

OWASP's Session Management Cheat Sheet starts from the fact that HTTP is stateless: each request and response pair is independent of the others.

So once someone signs in, the application needs a way to recognise their next request as theirs without asking for the password again. That is a session, which the cheat sheet defines as a sequence of HTTP request and response transactions associated with the same user.

The application tracks it with a session identifier: a value the browser sends back with every request, carried in a cookie in the design this page assumes.

The identifier is the whole proof. The cheat sheet says that once an authenticated session exists, the session ID is temporarily equivalent to the strongest authentication method the application uses, such as a password or a one-time password. Holding the identifier is worth as much as passing that sign-in.

NIST SP 800-63B-4 puts it in its own terms: the continuity of an authenticated session SHALL be based on possession of a session secret issued at the time of authentication. Whoever presents the identifier is treated as the user.

The cheat sheet lists how someone else comes to hold it: disclosure, capture, prediction, brute force or fixation of the session ID, each of which leads to session hijacking, where an attacker can fully impersonate the victim.

Session management is therefore a set of decisions about one value: how it is issued, when it must change, when it must die, and who can end it. A mistake in any of them is a session management vulnerability, and MITRE's weakness list now names them separately.

In CWE version 5.0, CWE-1462 Improper Session Management is the parent of CWE-384 Session Fixation, CWE-1463 Insufficient Session Timeout Value, CWE-1464 Improper Session Invalidation and CWE-1467 Use of Singular Authentication Artifact for Multiple User Sessions, and CWE-1464 has two children of its own: CWE-1465, for sessions not invalidated at logout, and CWE-1466, for sessions not invalidated after a credential change.

The older CWE-613, Insufficient Session Expiration, is marked deprecated in the same version and points to CWE-1463 and CWE-1464 instead.

The OWASP Top 10:2025 still lists CWE-613 among the weaknesses mapped to A07 Authentication Failures, and says sessions should be handled by a server-side, built-in session manager, with identifiers invalidated after logout and after idle and absolute timeouts.

This guide covers the session's life after sign-in. Five neighbouring guides take the rest: cookie security covers the cookie attributes themselves, JWT security covers token formats and signature checks, authentication failures covers the sign-up, sign-in and password reset flows, OAuth and OIDC security covers signing in through another provider and the tokens it issues, and sensitive data exposure covers what your responses reveal.

How a session identifier should be issued

The identifier has to be impossible to guess, and the sources set the floor in different terms.

The OWASP cheat sheet says session identifiers must have at least 64 bits of entropy and must come from a cryptographically secure pseudorandom number generator. It works an example: with 64 bits, an attacker trying 10,000 guesses per second against an application with 100,000 valid sessions at once would expect to spend about 585 years finding one.

For an identifier you create yourself rather than take from your framework, it asks for a generator size of at least 128 bits. ASVS 5.0 requirement 7.2.3, at Level 1, asks that reference tokens are unique, come from a cryptographically secure generator and have at least 128 bits of entropy.

NIST SP 800-63B-4 requires session secrets that come from an approved random bit generator, are at least 64 bits long and are generated by the session host in direct response to an authentication event.

The value itself should mean nothing. The cheat sheet says the session ID must be meaningless and must never include sensitive or personally identifiable information, and that what it stands for, such as the user, their role and their timeouts, belongs on the server.

NIST says a session cookie SHOULD contain only an opaque string and SHALL NOT contain cleartext personal information. WSTG-SESS-01, OWASP's test of the session management schema, calls putting specific data in the token instead of a generic value a common mistake.

The server should accept only identifiers it issued. The cheat sheet calls this strict session management, as opposed to permissive, which accepts any value the user sets and creates a session for it, and it notes that PHP defaults to permissive. Its rule is that an application should never accept a session ID it never generated; it should issue a new one instead and treat the attempt as suspicious.

ASVS 5.0 requirement 7.2.1, at Level 1, adds that all session token verification happens in a trusted backend service.

Use your framework's session handling rather than writing your own, as the cheat sheet recommends, keep it on the latest version and review its defaults. If the session store could leak, through a backup for example, the cheat sheet suggests storing a one-way verifier rather than the raw token, because high entropy protects against guessing and not against someone who can read the store.

The lab on this page keeps only a SHA-256 hash of each token for that reason, and gives each session a separate public handle so the account page can list sessions without showing their tokens.

Keep the identifier out of URLs. The cheat sheet says an ID in a URL can be disclosed in links and logs, browser history and bookmarks, the Referer header and search engines, and that a web application should use cookies to exchange session IDs.

WSTG-SESS-04, the test for exposed session variables, adds that in general the session ID should never be sent over unencrypted transport and should never be cached. For session cookie security, NIST says a session cookie SHALL be tagged to be accessible only on HTTPS sessions and SHALL be accessible to the minimum practical hostnames and paths; the cookie security guide covers those attributes and the rest in detail.

When the identifier must change: sign-in and privilege changes

The OWASP cheat sheet's rule is that the session ID must be renewed after any privilege level change, and the most common case, where renewal is mandatory, is sign-in, when the user moves from anonymous to authenticated.

It names password changes, permission changes and switching from a regular user role to an administrator role as other cases to consider, and says the old ID must be destroyed. ASVS 5.0 requirement 7.2.4, at Level 1, asks for a new session token on authentication, including re-authentication, and for the current token to be terminated.

Skipping that step is what makes session fixation possible. An application can start a session before sign-in, for a shopping cart or a language choice; WSTG-SESS-03 uses the cart as its example. That test says fixation is enabled by keeping the same session cookie value before and after authentication: if the value is not refreshed at login, whoever knew it before the user signed in now holds a signed-in session.

The cheat sheet explains how it differs from most other session attacks: the attacker sets the ID in the victim's browser rather than collecting the victim's ID.

CWE-384 describes the weakness as authenticating a user, or otherwise establishing a new session, without invalidating any existing session identifier, and its first mitigation is to invalidate existing identifiers before authorising a new session.

Frameworks that manage sessions do this on their own sign-in path. Django's documentation says its login() function calls cycle_key(), which creates a new session key while keeping the session data, to mitigate session fixation. The express-session README's login example calls req.session.regenerate() before storing the user, with a comment that this helps guard against forms of session fixation.

The exposure sits in any sign-in path that goes around those functions: a custom login route, a callback from another identity provider, a magic link, or an API login that writes the session itself. Each one needs the same rotation, and check 1 in the lab is the test for it.

Reauthentication should produce a new identifier too. ASVS 5.0 requirement 7.5.1, at Level 2, asks for full re-authentication before changes to sensitive account attributes which may affect authentication, such as the email address, phone number, multi-factor settings or other information used in account recovery, and requirement 7.5.3, at Level 3, asks for further authentication with at least one factor, or secondary verification, before highly sensitive transactions or operations.

Requirement 7.2.4 asks for a new session token on re-authentication, so a re-authentication done for either reason should issue one.

Privileges can also go down rather than up, for example when an administrator removes someone's role. ASVS requirement 8.3.2, at Level 3, asks that changes to the values behind authorisation decisions are applied immediately, and where they cannot be, such as when relying on data in self-contained tokens, that mitigating controls alert when a consumer performs an action they are no longer authorised to do and revert the change.

This page reads that as a rule for sessions too: a removed role has to stop working in sessions that are already open, not only in new ones.

When a session must end

ASVS 5.0 requirement 7.4.1, at Level 1, sets the standard for every ending: once termination is triggered, by logout or expiry for example, the application disallows any further use of the session, which for server-side sessions means invalidating the session data at the backend.

CWE-1464 states the same rule from the other side: invalidate sessions on the server for every security-related event relevant to the application, without relying on the client to delete anything, because identifiers and cookies can be captured for later reuse.

Sign-out has to end the session on the server

Clearing the cookie is not signing out. 1 min read

Idle and absolute timeouts protect against different things

An idle timeout ends a session nobody is using. 2 min read

A cookie expiry date is not a timeout

NIST says session cookies SHOULD expire at or soon after the session's validity period, and that this SHALL NOT be relied on to enforce session timeouts. 1 min read

What a password reset or change should do to other sessions

A password reset can be the moment someone discovers their account was used by somebody else. 2 min read

Disabled accounts and sessions an administrator ends

Two endings come from outside the session. 1 min read

Session revocation: what sign out everywhere has to reach

Sign out everywhere is only as complete as the list of things that can still act for the user.

With server-side sessions the job is direct: delete every session record for the account, as the lab's fixed version does after a reset. Four other things can outlive that deletion.

  • Self-contained tokens. ASVS 5.0 says that for self-contained tokens, additional measures are required to revoke or block them, as they may otherwise remain valid until expiration. Its requirement 7.4.1 says applications using them will need a solution such as maintaining a list of terminated tokens, disallowing tokens produced before a per-user date and time, or rotating a per-user signing key. Django's documentation gives a concrete case: with its signed cookie session backend, sessions are not invalidated when the user logs out, so a stolen cookie keeps working until it is older than SESSION_COOKIE_AGE. Token formats and signature checks are covered in the JWT security guide.
  • Access and refresh tokens. NIST SP 800-63B-4 says an access token and any associated refresh tokens could be valid long after the authentication session has ended, and that a relying party SHALL NOT treat the presence of an access token as a sign the user is present, in the absence of other signals. RFC 9700, the OAuth 2.0 security best current practice, says authorisation servers MAY revoke refresh tokens automatically in case of a security event, such as a password change or a logout at the authorisation server. A self-contained access token already issued falls under the previous point: without one of those measures it can stay usable until it expires, so its lifetime is how long sign out everywhere can take to reach it. The OAuth and OIDC security guide covers revoking delegated tokens.
  • Federated sign-in. NIST says an identity provider and a relying party manage sessions separately, so ending one does not end the other, and a new assertion could be issued from a live identity provider session without the user signing in again. WSTG-SESS-06 makes the same point about single sign-on and expects a logout in the application or in the SSO system to end all sessions. ASVS 5.0 requirements 7.1.3 and 7.6.1, both at Level 2, ask that the systems in a federated sign-in setup are documented along with the controls that coordinate session lifetimes and termination, and that lifetime and termination between relying parties and identity providers behave as documented.
  • Remembered devices. A remember-this-device option leaves a long-lived token in the browser that changes how the next sign-in works. NIST says cookies and similar remember-my-browser features SHALL NOT be used instead of authentication, except for reauthentication at AAL2 when the inactivity limit has passed but the overall limit has not. The OWASP Authentication Cheat Sheet says to use remembered-device cookies as risk signals, not substitutes for authentication, and its own example is an application that asks for multi-factor authentication on the first sign-in from a device but not on later ones. Whatever a device token unlocks, treat it as a credential: sign out everywhere, a password reset and removing the device from the account page all have to reach it, along with the sessions started on that device. Check 6 in the lab tests the last of these.

What a person should be able to see and end from their account page

People can only end sessions they can see.

ASVS 5.0 requirement 7.5.2, at Level 2, asks that users can view their active sessions and, after authenticating again with at least one factor, terminate any or all of them. The OWASP cheat sheet recommends letting users check the details of active sessions at any time, alerting them about concurrent logons, letting them end sessions remotely, and keeping an account activity history with details such as IP address, User-Agent, login date and time and idle time.

OWASP's Credential Stuffing Prevention Cheat Sheet adds that details of recent logins should be visible, and that where concurrent sessions are supported the user should be able to view all of them and terminate any that are not legitimate.

Whether one account may be signed in on several devices at once is a design decision, the cheat sheet says, and if it is not allowed, the application must act after each new sign-in, by ending the earlier session or asking the user which one stays.

ASVS requirement 7.1.2, at Level 2, asks that the documentation sets how many concurrent sessions one account may have and what happens when it reaches that number. Within those decisions, these are the parts of an account page that make session control usable.

  • One row per session: the device or browser name, when it signed in, when it was last active, and a marker on the session the person is looking from.
  • A handle for each row that is not the session's token, so nobody reading the page, and no script injected into it, can copy another session's identifier from it.
  • End this session, and sign out of all other sessions, each asking for the password or another factor first, as ASVS 7.5.2 describes.
  • A separate list of remembered devices, each with a remove action that also ends the sessions started on that device.
  • Endings that take effect on the server. A row that disappears from the list while the session still works is the flaw check 5 in the lab catches.

Session hijacking: what limits a stolen identifier

Session hijacking is using someone else's identifier.

WSTG-SESS-09 puts it plainly: an attacker who gets access to a user's session cookies can impersonate them by presenting those cookies. Presenting the value is enough, which is why the controls on this page aim at limiting what a leaked identifier can do. The ways an identifier leaks each have their own coverage: injected script in the XSS guide, which also covers HttpOnly; cookies sent over plain HTTP in WSTG-SESS-09 and the cookie security guide; identifiers in URLs, logs and caches in WSTG-SESS-04; and predictable values in WSTG-SESS-01.

Several controls on this page shrink what a stolen identifier is worth. The absolute timeout caps how long it can be used, and the renewal timeout makes a copied value go stale while the real user stays signed in. Rotation at every privilege change means a value taken before sign-in or before reauthentication does not carry the new rights.

Reauthentication before changes to the email address or multi-factor settings (ASVS 7.5.1) means a stolen session alone cannot change them. Ending sessions on reset, on request and on device removal gives the owner a way to cut an intruder off.

Detection helps, within limits. The cheat sheet recommends binding the session to properties such as the client IP address or User-Agent and treating a change mid-session as a sign of hijacking, and warns that a skilled attacker can share the victim's IP address on the same network or copy the User-Agent.

NIST says session monitoring MAY be performed, evaluating characteristics such as usage patterns, device and browser characteristics, geolocation and IP address; that when it detects possible fraud the relying party SHOULD reauthenticate, terminate the session or notify support staff; and that collecting those characteristics SHALL be included in a privacy risk assessment.

NIST also names the emerging device bound session credentials specification as one way to prove possession of a session secret instead of presenting it as a bearer token.

Log the lifecycle, not the secret. The cheat sheet recommends recording when identifiers are created, renewed and destroyed, along with logins, logouts, privilege changes and timeouts, and logging a salted hash of the session ID rather than the ID itself, so the logs do not become one more place to find it.

How to test session management on an application you own

OWASP's Web Security Testing Guide v4.2 has nine session management tests, WSTG-SESS-01 to WSTG-SESS-09.

Run them only on an application you own or are authorised in writing to test, with test accounts you created for the purpose. Reset, lockout, disable and deletion checks run only on test accounts and test records, where the rules of engagement allow them.

  • WSTG-SESS-01, session management schema: gather identifiers for the same user and, where possible, for different users, check there is enough randomness to stop forging, and look for unsigned cookies holding data that can be changed.
  • WSTG-SESS-02, cookie attributes: review the session cookie's attributes and prefixes with an intercepting proxy or in the browser's cookie jar; the cookie security guide covers what to look for.
  • WSTG-SESS-03, session fixation: follow the authentication flow, then present the cookies held before sign-in and see whether they now act for the signed-in user.
  • WSTG-SESS-04, exposed session variables: check that the identifier only travels encrypted, is not cached, and is not sent in GET requests.
  • WSTG-SESS-05, cross-site request forgery: covered in the CSRF guide.
  • WSTG-SESS-06, logout: store the session cookie values, log out and try a page from the signed-in area; if logout set the cookies to a new value, restore the old values and reload. It should show nothing that only a signed-in user should see.
  • WSTG-SESS-07, session timeout: confirm a timeout exists and that the server, not the cookie, enforces it.
  • WSTG-SESS-08, session puzzling: list the session variables, then reach entry points such as password reset in an order the developers did not plan, and see whether values they set unlock other pages.
  • WSTG-SESS-09, session hijacking: with two test accounts, find the session cookies that are not protected against travelling over plain HTTP and check whether presenting them is enough to act as the user.
  • Beyond the WSTG list, from ASVS 5.0 chapter V7: reset a test account's password and confirm the option to end its other sessions is offered and, when taken, ends them (7.4.3); end one session from another and confirm it is refused (7.5.2); keep a session busy past the absolute limit (7.3.2); have an administrator end a test user's sessions and disable a test account, and confirm both take effect (7.4.5 and 7.4.2). Then remove a remembered device and confirm its token and its sessions stop working.

Running the session lifecycle checks on your own machine

The checks in the asset on this page are an illustration.

They run a small account service with one invented account in two versions: a flawed one with six deliberate mistakes, each marked FLAWED in the code, and a fixed one. The server binds to 127.0.0.1, lets the system pick a free port, keeps everything in memory and uses only modules that ship with Node.js plus its built-in fetch(), so there is nothing to install.

The service reads time from a clock the checks move forward, so the idle and absolute timeouts are tested in milliseconds rather than hours.

It was written and run on Node.js 26.0.0, and every output in the asset was copied from that run.

The checks only send HTTP requests, the way a browser would, so the same checks can later drive your own application. They are for applications you own or are authorised in writing to test.

  • Save the four files into an empty folder and open a terminal there.
  • Run node run-checks.mjs. It runs the six checks against the flawed version and then the fixed version, printing every request, the cookies it carried by label (S1 for a session identifier, D1 for a device token), the response and the result. The flawed version fails all six and the fixed version passes all six.
  • Run node session-checks.test.mjs. Node's built-in test runner runs the six checks against the fixed version, and all pass. Run node session-checks.test.mjs flawed and all six fail with their reasons; when any test fails the runner sets the exit code to 1, so a pipeline step running the test fails.

Pointing the checks at your own application

Start your application on a test port with a test account, and replace the lab's paths with your own sign-in, sign-out, account, session list, device and reset routes. Then give the checks a way to move time: a clock your application reads through one function that a test can replace, or idle and absolute limits set very short in the test environment.

Keep the labels in the log in place of real identifiers, and run the checks against a local or staging copy, not against real customers' accounts.

Where an outside test fits

Your own checks cover the routes you know about.

A Clever Ops web application and API penetration test covers authentication and sessions alongside what each role can reach: whether sign-in replaces the session issued before it, whether a signed-out or timed-out session is still accepted when its old value is sent again, and whether suspending an account ends its open sessions.

Each finding comes with evidence, a fix written for your stack and the criteria the retest will use. If Clever Ops built or hosts your application, the work is a security review rather than an independent test, and the report says who built it and who hosts it.

Frequently asked questions

It is how an application keeps someone signed in between requests: issuing a random identifier at sign-in, recognising it on each request, changing it when privileges change, and ending it on sign-out, timeout, password reset or when the user or an administrator ends it. The OWASP Session Management Cheat Sheet says an authenticated session ID is temporarily equivalent to the strongest authentication method the application uses, which is why each step matters.

In hijacking, someone obtains an identifier the server already issued to the user and presents it. In fixation, the attacker knows an identifier first and the user's sign-in attaches to it; the OWASP cheat sheet describes the attacker setting the session ID in the victim's browser rather than collecting it. A new identifier at sign-in, as ASVS 5.0 requirement 7.2.4 asks, stops fixation. It does not stop the new identifier being stolen later.

ASVS 5.0 leaves it to a documented risk analysis, because no single pattern suits all applications. As examples, the OWASP cheat sheet gives common idle timeout ranges of 2 to 5 minutes for high-value applications and 15 to 30 minutes for low-risk ones, and NIST SP 800-63B-4 says that at AAL2 the inactivity timeout SHOULD be no more than 1 hour and the overall timeout SHOULD be no more than 24 hours. Enforce both limits on the server.

Only if the server ends it. Clearing the cookie removes the browser's copy, but WSTG-SESS-06 describes applications where the old cookie value still works after logout. Test it by keeping a copy of the identifier before signing out and sending it again afterwards: it should be refused.

The OWASP Forgot Password Cheat Sheet says to ask the user whether to invalidate all existing sessions, or to do it automatically, and after recovering a possibly compromised account to invalidate existing sessions and outstanding reset links. ASVS 5.0 requirement 7.4.3 asks that the option to end all other sessions is given after any authentication factor changes, and CWE-1466 is the weakness when sessions survive a credential change.

Not by deleting a server record alone, because a self-contained token carries its own validity. ASVS 5.0 says such tokens may otherwise remain valid until expiration, and its requirement 7.4.1 says applications using them will need a solution such as a list of terminated tokens, disallowing tokens produced before a per-user date and time, or rotating a per-user signing key.

It should not replace signing in. NIST SP 800-63B-4 says remember-my-browser features SHALL NOT be used instead of authentication, except for reauthentication at AAL2 after the inactivity limit has passed but the overall limit has not, and the OWASP Authentication Cheat Sheet treats remembered-device cookies as risk signals. Whatever the token allows, removing the device should revoke it and end the sessions it started.

Scope the right test for your application

Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.