Session fixation: give users a new session ID at login
Short answer: Session fixation is an attack where someone gets a victim to use a session ID the attacker already knows, waits for the victim to log in, then uses that same ID to act as them. The fix is simple and well established: issue a brand-new session ID whenever someone logs in, and again whenever their privileges change, such as becoming an admin or confirming a sensitive action. Throw away the old ID on the server, not just in the browser.
How the attack works
Most web apps give every visitor a session ID in a cookie, often before they log in, so a shopping basket or language choice can follow them around. When they log in, the app marks that session as belonging to their account.
The weakness appears if the session ID stays the same across login. OWASP's description of session fixation explains the idea: the attacker fixes a user's session ID before the user logs in, so they don't need to steal it afterwards. In outline:
- The attacker obtains a valid session ID from your site, for example by visiting it.
- They get the victim's browser to use that ID. Common routes include a subdomain that can set cookies for the parent domain, a cross-site scripting hole, or an app that accepts session IDs in URLs.
- The victim logs in normally.
- Because the ID didn't change, the attacker's copy now belongs to a logged-in session.
The victim sees nothing unusual. That's what makes it worth closing off properly.
The core rule: new ID at login
OWASP's Session Management Cheat Sheet states that the session ID must be renewed or regenerated after any privilege level change within the user's session, and names authentication as the most common case.
In practice, at the moment a login succeeds:
- Create a new session with a fresh, random ID.
- Copy across any anonymous data you want to keep, such as the basket.
- Destroy the old session on the server, so the old ID is worthless.
- Send the new ID in a cookie, replacing the old one.
Most frameworks have a built-in call for this. Common examples include session_regenerate_id(true) in PHP, request.session.regenerate() with express-session in Node.js, and cycle_key() in Django, which its login() helper already calls for you. Check your framework's documentation for the exact behaviour, especially whether it deletes the old session server-side.
Other moments that need a new ID
Login isn't the only privilege change. Regenerate the session when:
- A user's role goes up. For example, an admin switches into an elevated mode.
- A user re-authenticates for something sensitive, such as changing their email or payout details.
- A user switches accounts within the same browser, such as a staff member logging in as a different location.
- A user logs out. Destroy the session completely, and start a fresh anonymous one if needed.
If you support "log in as customer" for support staff, treat entering and leaving that mode as privilege changes too.
Reject session IDs you didn't issue
Some apps accept any session ID a browser presents and create a session for it if none exists. That lets an attacker choose an ID in advance. OWASP calls this a permissive session mechanism and recommends strict handling, where the app only accepts IDs it has generated itself.
Also avoid putting session IDs in URLs. They leak through referrer headers, browser history, shared links and server logs, and they make fixation easy, because the attacker only needs to send a link.
Harden the cookie as well
Regeneration is the main fix, but cookie settings reduce the routes an attacker can use:
HttpOnlystops page scripts reading the cookie.Securekeeps it off unencrypted connections.SameSitelimits when browsers send it with cross-site requests.- The
__Host-prefix on the cookie name tells browsers to accept it only if it'sSecure, has noDomainattribute and usesPath=/, which blocks a subdomain from setting it for your main site.
That last point matters if you run marketing pages, a blog or customer microsites on subdomains that others can control.
How to check your own app
You can test this in a few minutes with browser developer tools:
- Open your site in a private window and note the session cookie value before logging in.
- Log in.
- Check the cookie again. It should be different.
- Try the old value: replace the cookie with the pre-login value and reload a logged-in page. You should be treated as logged out.
Repeat for logout and for any elevated or re-authentication step. If the value stays the same at any of these points, that's the fix to prioritise.
Common implementation mistakes
- Regenerating but not deleting. Creating a new ID while leaving the old session alive on the server still lets the old ID work.
- Regenerating only on the main login form. Social logins, magic links, single sign-on callbacks and "remember me" paths need it too.
- Losing the basket. Teams sometimes skip regeneration because it empties the cart. Copy the data you need into the new session instead.
- Race conditions. Apps that fire several requests at once around login can briefly send the old ID. Make the client wait for the login response before continuing.
A worked example
Consider a hypothetical Toronto furniture shop whose storefront kept the same session cookie from first visit through checkout login. A security review found that a promotional microsite on a subdomain could set cookies for the main domain. The team switched to their framework's regenerate call at every login path, including social login and magic links, deleted old sessions server-side, renamed the cookie with the __Host- prefix and copied the basket across on login. Customers noticed nothing, and the old-cookie test now shows a logged-out page.
FAQ
Do token-based APIs have the same problem?
Less often, because tokens are usually issued fresh at login. The same idea applies, though: never upgrade a token or identifier that existed before authentication.
Does HTTPS prevent session fixation?
No. HTTPS protects the cookie in transit, but fixation works by getting the victim to use a known ID, not by intercepting it.
Will regenerating the session log users out of other tabs?
Not usually. The browser shares the new cookie with every tab on your site. Tabs that send requests during the switch may need a reload.
Is this covered by framework defaults?
Often, but not always for every login path. Test each one yourself.
If you'd like your app's login and session handling checked, tell us which framework you use.