← All articlesWeb security

How to renew session IDs at login and invalidate the old ID

At login, a user crosses an authentication boundary: before it, the application uses an anonymous session; afterward, it grants access to account data. At this transition, the session ID must be renewed and the old one destroyed. But seeing a new value in the client is not enough for acceptance. You need to confirm that the new session opens the intended account area and that the old one no longer works. We will examine this using an entirely fictional store, an owned test account, and synthetic data.

Why login requires a new session ID

The session ID must be renewed when the privilege level changes. Authentication is a mandatory case: an anonymous visitor becomes a user with account access. The previous anonymous ID must not become a valid pass to private data.

Regeneration is necessary to prevent session fixation. Issuing a new value is only part of the requirement: the previous ID must be destroyed, and sensitive pages must ignore it. Record these conditions together in the task so that changing the ID does not become the sole acceptance criterion.

A fictional store: defining acceptance criteria

Imagine an entirely fictional store called “Practice Cart” in an owned test environment. The team has a test account with a prepared entry in its account area: “Test Order 001.” All products, orders, and account details are synthetic. We will call the anonymous session A and the session after successful login B. Actual identifier values stay within the test and are not published.

Before login, A must not open the account area. After login, the application must issue B, which differs from A, and provide access through B to the prepared test account entry. A separate requirement is that A must then have no access to the same area and the previous session must be invalidated. This verifies the outcome of crossing the authentication boundary.

The cart: agree in advance on what may carry over

In our example, the visitor adds two synthetic products to the cart before logging in. For acceptance, the team defines in advance which state may be preserved—for example, selected items and their quantities. It then records the expected cart contents after the A → B transition. This is a product decision and a test design proposed by the editorial team; the cited sources do not establish a rule for transferring cart contents.

Preserving the agreed cart contents must not keep A valid for the account area. The identifier value and its associated business logic must be stored on the server. It is therefore useful to frame the task as transferring permitted state and verifying the result, while also requiring the previous session to be destroyed.

Original editorial checks for the A → B transition

The following is a verification scenario proposed by the editorial team, not a quotation or an official procedure from the documentation. Run it only in your own test environment. Check the login page and account area over HTTPS: login and subsequent authenticated pages must be available exclusively over TLS or another secure transport.

  • Create anonymous session A. Confirm that it cannot open the synthetic account area or display the prepared account entry.
  • Log in with your own test account. Record within the test that B was issued and that its value differs from A.
  • Open the account area with B. Check specifically for the prepared “Test Order 001.” An ID change without working access through the new session does not satisfy the task.
  • From a separate test client, request the same area using only A, without B or any other authentication credentials. Verify that access and private data are unavailable. Also confirm that the previous session has been invalidated in server-side storage or through the framework’s mechanisms.
  • With B, verify the agreed cart contents: the specific synthetic products and quantities. Compare the result with the expectation recorded before the test.

Check state, not just the HTTP response

A successful HTTP response alone proves neither that access was granted nor that it was denied. In the proposed scenario, the positive control is retrieving the prepared data from your own account through B. The negative control is the absence of access and that data when using only A, supplemented by confirmation that the previous session was invalidated.

The cart also needs an observable result: its items and quantities must match the agreed expectation. If the ID changes but the account area does not work with B, the task fails acceptance. If B works but A still opens private data, the task also fails acceptance. These criteria give developers and product owners a shared understanding of when the change is complete.

What to fix and in what order

The first task is to use the chosen framework’s built-in session renewal mechanism after successful login, ensure that the previous session is destroyed, and verify that access using the previous ID is denied. Built-in framework mechanisms are recommended over implementing your own from scratch. Choose the specific API and call order using your stack’s documentation: the supplied excerpts do not prescribe a universal sequence.

Include the entire scenario in the acceptance criteria: A → B, B’s access to the prepared data, denial of access for A, confirmation of invalidation, and the agreed cart contents. Make it a regression check. Separately verify secure transport for login and authenticated pages. HTTPS is required for these pages, but the regeneration requirement applies regardless of whether HTTP or HTTPS is used.

Verification limits and the next step

This scenario confirms the behavior of a specific transition in the tested environment. It does not guarantee the security of every login flow or privilege change. The general requirement to renew the ID applies to any change in privilege level; invalidating sessions and rotating tokens after reauthentication is also recommended. Such transitions need separate, agreed checks.

The result is a concrete developer task and reproducible acceptance criteria using your own data. If you need help checking the implementation, you can start with a free audit of your website or the web portion of your product at CheckMyApp. Assess its results within the agreed scope of testing, without promises of complete security.

Sources and documentation

  1. Session Management Cheat Sheet
  2. Authentication Cheat Sheet
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free