Session fixation
| Class | Authentication |
|---|---|
| CWE | CWE-384: Session Fixation |
| OWASP Top 10 | A07:2025 Authentication Failures |
| Typical severity | Medium |
| Affects | Applications that give anonymous visitors a session and then mark that same session as signed in |
A visitor who opens a sign-in page often receives a session cookie before typing anything.1 In session fixation, the attacker obtains a valid session ID, gets it into the victim's browser, and waits. When the victim signs in and the application records the user in the existing session instead of starting a new one, the ID the attacker already holds now opens the victim's account.2 Fixation is one of five ways to end up holding someone else's session and ends in the same place as Session hijacking. Disclosure, capture, prediction and brute force go after an ID the victim was given; here the attacker picks it.3
Why the ID outlives sign-in
Sessions serve purposes other than sign-in, the secret behind a CSRF token among them, so applications often create one for requests from visitors who have not signed in. The ID is fixed when it is generated and carries over across sign-in and sign-out.1 A sign-in handler that writes userId into that session promotes an anonymous ID, possibly known to someone else, to a signed-in one.
A second weakness makes planting easier. A permissive session manager accepts any ID the browser sends and creates a session under it; a strict one accepts only IDs it issued. PHP is permissive by default.3 Its session.use_strict_mode setting defaults to 0, and the PHP manual calls enabling it "mandatory for general session security".4 Under permissive handling the attacker does not need to visit the site first, because any made-up value becomes a session.
Planting the ID
Getting the victim to use a known ID is the hard step.2 The documented routes:
- A shared computer. The attacker opens the site on a library or kiosk machine, writes down the session ID and leaves the browser on the sign-in page. The next person to sign in there hands over the account for as long as the session lasts.21
- An ID in the address. Where the application accepts a session ID from a URL parameter or form field, a link with
;jsessionid=orPHPSESSID=in it is enough.31 - Script on the site. A cross-site scripting flaw or HTTP response splitting lets the attacker write the cookie into the victim's browser.2
- A sibling subdomain. A page on
recipes.example.comcan set a cookie for all ofexample.com, so a flaw there plants an ID thatbank.example.comthen reads.2
The subdomain route also works in reverse. An attacker who controls bad.example.com plants a session from their own account; the victim visits good.example.com, is signed in as the attacker without noticing, and may type card details into the attacker's account.5
A new ID at every privilege change
Discard the old session ID and issue a new one when a user signs in.2 The same applies to sign-out6 and to every other change of privilege: a password change, a change of permissions, a switch to an administrator role. The server destroys the old ID as well, so a copy of it stops working.3 In Express with express-session, the vulnerable form writes the user into whatever session arrived:6
app.post("/login", async (req, res) => {
const user = await checkPassword(req.body.email, req.body.password);
if (!user) return res.redirect("/login");
req.session.userId = user.id; // same ID as before sign-in
res.redirect("/");
});req.session.regenerate() replaces it with a new ID and an empty session before the user is stored:7
app.post("/login", async (req, res, next) => {
const user = await checkPassword(req.body.email, req.body.password);
if (!user) return res.redirect("/login");
req.session.regenerate((err) => {
if (err) return next(err);
req.session.userId = user.id;
req.session.save((err) => {
if (err) return next(err);
res.redirect("/");
});
});
});| Stack | Call or setting |
|---|---|
| Express, express-session | req.session.regenerate(cb) before storing the user |
| Passport.js | Version 0.6.0 or later renews the session on req.login() and req.logout() |
| PHP | session_regenerate_id(true) after sign-in; session.use_strict_mode = 1 |
| Django | django.contrib.auth.login(), which calls cycle_key() |
| Java servlets | HttpSession.invalidate(), then request.getSession(true) |
PHP's session_regenerate_id() keeps the session data and only deletes the old session file when passed true.8 Django's cycle_key() also keeps the data, and update_session_auth_hash() rotates the key after a password change.59 Values tied to the old session, such as a CSRF token secret, need renewing as well.1
Narrowing the routes
Accept the session ID only from the cookie and refuse it in URLs and form fields; an ID the server never issued is replaced with a fresh one and raises an alert.3 In PHP, session.use_only_cookies is on by default and turning it off is deprecated since PHP 8.4.0.4 A session cookie named with the __Host- prefix must carry Secure, Path=/ and no Domain, so it is sent only to the host that set it and a sibling subdomain cannot set it for the whole domain; browsers without prefix support accept the cookie without these checks.10 Cookie attributes covers the other flags. TLS stops a network attacker from injecting the cookie in transit, but renewal is needed over HTTPS as well.3
Passport.js before 0.6.0
Passport, a sign-in library for Node.js, kept the session ID across sign-in and sign-out until version 0.6.0, released in May 2022. Its maintainer had held that renewal belonged in each application, because sessions are used for more than sign-in, and changed course after GitHub's code scanning began flagging the pattern and a research group at TU Wien looked into it. Since 0.6.0 the library issues a new session at every sign-in and sign-out and drops the old session data unless the keepSessionInfo option is passed.1 The flaw was published as CVE-2022-25896, rated Moderate with a CVSS score of 4.8 and high attack complexity.11
Testing
Open the site in a private window, note the session cookie in the developer tools, sign in, and look again. An unchanged value means the application is open to fixation. For PHP, also send a made-up session ID and check whether the server accepts it or replaces it. In JavaScript code, the CodeQL query js/session-fixation flags sign-in handlers that write to the session without calling regenerate().6
Prompt for a coding agent
Find every place in this project where a user signs in, signs out, changes their password or gains a role. In each one, make sure the session ID is replaced with a new one before the user is stored: req.session.regenerate() with express-session, Passport 0.6.0 or later, session_regenerate_id(true) in PHP, django.contrib.auth.login() in Django. Make the server accept session IDs only from the cookie, never from the URL or a form field. In PHP set session.use_strict_mode = 1. Keep the rest of the sign-in flow the same. List each file and line you changed.
In SecFlash
SecFlash reports a session ID carried in the address, one of the routes for planting an ID, as SF-SEC-654 on the Security page.
See also
- Session hijacking
- Session management
- Cookie attributes
- Cross-site scripting
- Authentication
- Category: Authentication
References
- a b c d e f Hanson, Jared (20 May 2022). "Fixing Session Fixation". Passport.js.
- a b c d e f "CWE-384: Session Fixation". Common Weakness Enumeration. MITRE.
- a b c d e f "Session Management Cheat Sheet". OWASP Cheat Sheet Series. OWASP Foundation.
- a b PHP Manual. "Runtime Configuration" (sessions). php.net.
- a b Django Software Foundation. "How to use sessions". Django 5.2 documentation.
- a b c GitHub. "Failure to abandon session" (js/session-fixation). CodeQL query help.
- back expressjs. "express-session" README. GitHub.
- back PHP Manual. "session_regenerate_id". php.net.
- back Django Software Foundation. "Using the Django authentication system". Django 5.2 documentation.
- back MDN contributors. "Set-Cookie header". MDN Web Docs.
- back GitHub (2 July 2022). "Passport vulnerable to session regeneration when a users logs in or out" (CVE-2022-25896). GitHub Advisory Database.
External links
- Session Management Cheat Sheet, OWASP Cheat Sheet Series