Rotate the session when sign-in succeeds

A session id that survives sign-in was known before the person proved who they are. Rotation makes the authenticated id a new secret.

Lesson 22 of 29 · 14 min read · practice only with authorization

Diagram

From a pre-login cookie to a new session id

Follow the path. One example at a time.

Step 1 of 4

Before

The browser may already hold a session id from a visit, a planted cookie, or an anonymous cart.

Illustration: the id from before sign-in is not the session
# A session may exist before the password check.
# That id must not survive a successful sign-in.
What changes the outcome
Before

An id exists.

After

The same id is now authenticated.

Cookie

Flags may also be missing.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Fixation is a known id

    What fails
    If the id does not change at login, whoever set or saw it is signed in as the person who just authenticated.
    Who is affected
    The person who logged in, and the data their new authority exposes.
    What you would notice
    The Set-Cookie value is unchanged across the login response.
    What you change
    Call regenerate, or issue a new token, in the success path only.
  2. Anonymous sessions

    What fails
    Carts and pre-login state can move to a new id. Keeping the old id for convenience keeps the bug.
    Who is affected
    The shopper, if a planted id becomes their authenticated cart and account.
    What you would notice
    A comment that says the id is preserved so the cart is not lost.
    What you change
    Copy the cart onto the new session server-side. Do not keep the id.
  3. Flags are the other half

    What fails
    Rotation does not stop script or a clear-text network from reading the new cookie if the flags are wrong.
    Who is affected
    The same person, once the new cookie leaks.
    What you would notice
    A new id set without HttpOnly or Secure.
    What you change
    Set the flags on every session cookie, including the one issued at login.
  4. Revocation has to see the new id

    What fails
    A logout or a password reset that only deletes “the” session in a single browser misses other copies and the old id if you did not invalidate it.
    Who is affected
    Anyone still holding a copy.
    What you would notice
    A session table that is never updated on login.
    What you change
    Delete the old row. Give the person a way to revoke every session for the account.

The id from before login is not a secret anymore

Session fixation is the failure where the identifier does not change when the person proves who they are. The id might have been set by an earlier visit, copied from a log, or supplied by someone else. After login it stands for the account. Whoever already had it is the account.

Rotation is the fix: the success path creates a new id and forgets the old one. Do it for cookie sessions and for bearer tokens you issue yourself. A token that was printed before login should not become the authenticated token by adding a claim.

Move the data, not the identifier

Anonymous carts and “remember this form” are why developers keep the id. The cart is rows in a table. Point those rows at the new session inside the regenerate callback. The person keeps the cart. The old id, if anyone else holds it, keeps nothing authenticated.

Do the same for any pre-login object: a CSRF token can be reissued with the new session, an OAuth state value should already be single-use. Nothing that was a capability before login should remain a capability after, except data you explicitly copied.

Flags and revocation

Set HttpOnly so script cannot read the cookie, Secure so it is not sent in clear text, and SameSite so some cross-site requests do not carry it. Rotation without those flags produces a fresh secret that script or the network can still take. The pages on cookie theft and CSRF are the neighbors. This page is the moment the secret is born.

Invalidate the old row so a retry of the previous cookie fails. Logout, password change, and “sign out everywhere” have to delete every session for the account, not only the browser that asked. You would notice a login handler that sets a user id on the existing session object and never calls regenerate. That is the review comment.

What you change

Put regenerate, or your token issuer’s rotation, on the only success path. Cover password login, passkey login, and SSO callback. Add a test that the id after login is not the id from the anonymous request, and that the old id is rejected. Assert the cookie flags in the same test.

The person affected is whoever logged in while someone else knew the old id, and then everyone that account can reach. The finding is the missing rotation. It does not include a captured cookie.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

The login response sets the same session id the browser sent, now marked authenticated. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapSession hijacking and cookie theft: how sessions are stolen and how to make them worthlessThe ways a session token leaves the user — network sniffing, script access, infostealers, logs, and fixation — how cookie flags and server-side session design limit each one, and how to read a session in a capture or proxy.
  1. 01Where a bearer token lives in the browserA token in local storage is readable by any script on the page.

Practice

  1. LabCookie flag simulatorSix Set-Cookie headers, six kinds of request. See which cookies the browser sends, which scripts can read, and which it rejects outright.

Glossary

  1. CardSession hijackingA bearer token or cookie is copied and replayed, so the server treats the attacker as the signed-in user.