Answer#
In a fixation attack the attacker obtains a valid, anonymous session ID from your site, gets the victim's browser to use it (a link, a sub-domain cookie, or a flaw elsewhere), and waits for the victim to sign in. If the server keeps the same ID after authentication, the attacker's copy of the ID is now signed in too.
The defence is simple: whenever the privilege level changes (login, sign-out, a switch to an administrator role, a password change), issue a new ID and invalidate the old one.
use std::collections::HashMap;
fn random_id() -> String {
let mut raw = [0u8; 16];
getrandom::getrandom(&mut raw).unwrap();
hex::encode(raw)
}
#[derive(Default)]
struct Sessions {
data: HashMap<String, Option<u32>>, // id -> user id (None = not signed in)
}
impl Sessions {
fn start_anonymous(&mut self) -> String {
let id = random_id();
self.data.insert(id.clone(), None);
id
}
/// On login, drop the old id and hand out a new one, so an id an attacker knew is now useless.
fn sign_in(&mut self, old_id: &str, user: u32) -> String {
self.data.remove(old_id);
let id = random_id();
self.data.insert(id.clone(), Some(user));
id
}
fn sign_out(&mut self, id: &str) {
self.data.remove(id);
}
fn user(&self, id: &str) -> Option<u32> {
self.data.get(id).copied().flatten()
}
}
fn main() {
let mut s = Sessions::default();
let before = s.start_anonymous();
let after = s.sign_in(&before, 42);
println!("old id still works: {}", s.user(&before).is_some());
println!("new id works: {}", s.user(&after) == Some(42));
s.sign_out(&after);
println!("after sign-out: {}", s.user(&after).is_some());
}old id still works: false
new id works: true
after sign-out: falseHow to do it#
- Create an anonymous session only when you need it (for example, to hold a CSRF token).
- In the login handler, after the password check, call something like
sign_in(old, user)and set the new cookie. - In the logout handler, delete the server-side session and send the expiring cookie.
- After a password change, delete all other sessions for the user.
- Never accept a session ID from a URL parameter.
Common mistakes#
- Deleting only the cookie on logout. A copy of the ID would still work until it expires.
- Reusing the ID "to keep the shopping cart". Copy the data into the new session instead.
- Forgetting the login flows that bypass the main handler (single sign-on callbacks, "remember me").
Related questions#
Does HTTPS prevent fixation?#
No. It protects the cookie in transit; fixation is about what the server does with an ID it already knows.
What about "remember me"?#
Treat it as a separate, long-lived token with its own storage, rotation after each use and a clear way to revoke. It should not be the normal session ID with a long lifetime.
Sources#
- OWASP Session Management Cheat Sheet, "Renewing the Session ID": https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#renew-the-session-id-after-any-privilege-level-change
- OWASP Session Fixation: https://owasp.org/www-community/attacks/Session_fixation