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.
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
- ID length and storage. A decoded ID is at least 16 bytes. This length check and 1,000-sample uniqueness check do not measure entropy; the randomness claim comes from using secrets. The raw ID is not a key in the store, every stored key is a 64-character hash, and 1,000 generated IDs are all different.
- Fixation. The "attacker" creates an anonymous session. The "victim" logs in with that ID. The returned ID is different, the planted one now returns nothing, and the new one holds the user. An ID the server never issued returns nothing and leaves the store the same size.
- Login with no ID. A first-time login with no cookie works and returns a working session.
- Idle timeout. With a 900-second idle limit as an example, activity just before the limit keeps the session alive twice in a row. Silence past the limit ends it, and the row is deleted from the store.
- Absolute timeout. With an 8-hour cap, a client that makes a request every ten minutes stays alive through the exact cap and loses the session once it passes; every step is asserted.
- Boundary. A request exactly at the idle limit is still valid in this fixture. Pick that edge on purpose.
- Logout. After logout the old ID returns nothing. Non-string input, an empty string and bytes also return nothing.
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
- OWASP Session Management Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html