Difficulty: Advanced
What is a session fixation attack and how do you prevent it in a web application?
Session fixation is an attack where the attacker plants a session ID they already know into the victim's browser, waits for the victim to log in, and then uses that same ID to act as the victim. It works when the server keeps the pre-login session ID after authentication. The fix is to issue a brand new session ID at every privilege change, especially immediately after a successful login, and destroy the old one.
sid=abc123.abc123. Common ways: a link with the ID in the URL (?PHPSESSID=abc123) on apps that accept URL session IDs, a cookie set from a compromised or attacker-controlled subdomain, or an XSS bug that writes document.cookie.abc123 as authenticated for that user but does not change the ID.abc123, is now logged in as the victim.The difference from session hijacking: in hijacking the attacker steals an ID after login; in fixation the attacker chooses the ID before login, so they never need to steal anything.
After verifying credentials, create a fresh session with a new random ID, copy over only what you need, and invalidate the old one. Most frameworks have a single call:
| Stack | How to rotate |
|---|---|
Express (express-session) | req.session.regenerate(cb) |
| PHP | session_regenerate_id(true) |
| Spring Security | Done by default (changeSessionId strategy) |
| Django | Done automatically by login() via cycle_key() |
| ASP.NET Core | Issue a new auth cookie on sign-in |
Also rotate when privileges change: switching to an admin role, completing step-up MFA, or after a password change. On logout, destroy the session server side, not just the cookie.
session.use_trans_sid in PHP.session.use_strict_mode=1 in PHP). Express-session already ignores unknown IDs and creates a new session.HttpOnly, Secure, SameSite=Lax or Strict, and the __Host- name prefix, which requires Secure, Path=/ and no Domain, so a sibling subdomain cannot overwrite it.Custom auth code is the usual culprit: a developer sets req.session.userId = user.id after a password check and forgets to regenerate. It also appears in SSO callbacks and in apps that keep a guest cart session and then "upgrade" it at login. Merge the cart into the new session instead of promoting the old one.
import session from 'express-session';
app.use(session({
name: '__Host-sid',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { httpOnly: true, secure: true, sameSite: 'lax', path: '/' },
}));
app.post('/login', async (req, res, next) => {
const user = await verifyCredentials(req.body.email, req.body.password);
if (!user) return res.status(401).json({ error: 'Invalid credentials' });
const cart = req.session.cart; // keep only what you need
req.session.regenerate((err) => { // new ID, old one destroyed
if (err) return next(err);
req.session.userId = user.id;
req.session.cart = cart;
req.session.save((err) => (err ? next(err) : res.json({ ok: true })));
});
});
regenerate() discards the pre-login session and issues a new random ID. The __Host- prefix stops subdomains from planting a cookie with the same name.
Session Fixation, Session ID Regeneration, Session Hijacking, Cookie Attributes