Course contentsShow
← Security
Lesson 735 of 3,05215. Web Security - Authentication and SessionsFree lesson

Session Regeneration After Privilege Changes

Master session ID regeneration after authentication, authorization changes, and other sensitive state transitions.

Session Regeneration After Privilege Changes

You'll learn why changing a user's session ID after authentication or privilege changes is critical to prevent session fixation and hijacking attacks.

The Core Problem

Imagine you receive a hotel room key card before you check in. A malicious actor could have given you that card, knowing its number. Once you check in using that card, they could use a duplicate to access your room. That's essentially session fixation: an attacker tricks you into using a session ID they already know, then exploits it after you authenticate.

What is Session Regeneration?

Session regeneration means creating a brand new session ID and invalidating the old one whenever a user's privilege level changes. This includes:

  • After login (moving from anonymous to authenticated)
  • After logout (back to anonymous)
  • After privilege escalation (user becomes admin)
  • After password reset or security changes

When you regenerate, any attacker holding the old session ID is locked out—their known token becomes worthless.

How It Protects You

Session fixation attacks depend on the session ID remaining unchanged across authentication. If an attacker sets a session cookie on your browser (via XSS or a malicious link), then you log in with that session, they can hijack your authenticated session. Regeneration breaks this chain.

Session hijacking mitigation: Even if an attacker obtains a pre-authentication session token through network sniffing, regeneration ensures it can't be used post-login.

Implementation Principle

On successful login or privilege change:

  1. Generate a completely new session ID
  2. Copy over necessary session data to the new session
  3. Invalidate and delete the old session
  4. Send the new session ID to the client

Key Takeaway: Always regenerate session IDs after authentication or authorization changes to prevent attackers from exploiting known or fixed session tokens.