FAQ - Help and questions

Why must I issue a new session ID at login, and what must logout do?

#security#sessions#authenticationpublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

If a session ID that existed before login stays valid after it, an attacker who planted that ID in the victim's browser gains the account (session fixation). Create a new ID when privilege changes, delete the old one on the server, and make logout delete the server-side session, not only the cookie.

Checked with rustc 1.93.0, getrandom 0.2.17, hex 0.4.3, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

src/main.rsrust
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());
}
text
old id still works: false
new id works:       true
after sign-out:     false

How to do it#

  1. Create an anonymous session only when you need it (for example, to hold a CSRF token).
  2. In the login handler, after the password check, call something like sign_in(old, user) and set the new cookie.
  3. In the logout handler, delete the server-side session and send the expiring cookie.
  4. After a password change, delete all other sessions for the user.
  5. 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").

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet