OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

Renew the session ID at login, and test that the old one is dead

Model session ID renewal at login and test that the old ID is refused. Fake-clock fixtures cover fixation, idle and absolute timeouts, hashed storage and logout.

OOmnistu
Reviewed 2026-11-18 · 4 min read

Session fixation is a quiet attack. The attacker does not steal anyone's session. They get a session ID from your site, plant it in the victim's browser, and wait for the victim to log in. If the site keeps the same ID through login, the attacker's copy of that ID is now a logged-in session.

The OWASP Session Management Cheat Sheet says the session ID must be renewed after any privilege level change, and names login as the most common case, where the user moves from anonymous to authenticated. It calls regeneration mandatory to prevent fixation. The same sheet describes a strict mechanism, where the server only accepts IDs it created, as opposed to a permissive one that accepts whatever ID the client sends and creates a session for it. It also asks for idle and absolute timeouts, and for active server-side invalidation on expiry and logout, not only clearing the cookie.

This guide builds a small in-memory session store and tests those rules with a fake clock. It was prepared with AI assistance and reviewed on 18 November 2026. It tests logic, not a web framework.

The store

IDs come from secrets.token_urlsafe(16), which is 128 random bits. The sheet requires at least 64 bits and says it is the entropy, not the length, that matters. The store keeps a SHA-256 hash of each ID instead of the ID. The sheet says storing random tokens as they are can be acceptable in a tightly controlled store, but anyone who can read it can reuse unexpired tokens. Hashing is the author's choice here, not an OWASP requirement.

def get(self, sid):
    if not isinstance(sid, str):
        return None
    key = self._h(sid)
    row = self.rows.get(key)      # unknown ID: no session is created for it
    if not row:
        return None
    if (self.clock.now - row['seen'] > IDLE
            or self.clock.now - row['born'] > ABSOLUTE):
        del self.rows[key]
        return None
    row['seen'] = self.clock.now
    return row

def login(self, sid, user):
    if isinstance(sid, str):
        self.rows.pop(self._h(sid), None)   # old ID dies at the privilege change
    return self.create(user)

Looking up an unknown ID returns nothing and creates nothing. That is the strict behavior. Login deletes the old row and issues a new ID.

What the run asserts

All pass on Python 3.10.12. The 900-second and 8-hour values are examples. The cheat sheet says timeouts depend on how critical the application is and gives ranges, not a single answer.

What this does not test

There are no cookies here. The fixture does not check the Secure, HttpOnly or SameSite attributes, that the ID is kept out of URLs, or that TLS is on. The sheet notes TLS does not stop fixation or prediction by itself. It does not test two requests racing at the moment of login, or renewal on other privilege changes such as a password change or a switch to an admin role. It does not use a real framework, whose regenerate call may keep or drop session data in its own way. The fixture also drops any data held by the old session, which a real login flow may need to carry over deliberately.

Treat it as a regression test for one rule: the ID that exists before login must not exist after it.

Sources