Answer#
If "no such user" and "wrong password" look different, an attacker can test a list of e-mail addresses and learn who has an account. There are two differences to remove: the message, and the time. Hashing a password takes noticeable time on purpose, so a request for an unknown user that skips the hash would return much faster than one for a real user.
The fix is to always run one verification. When the account is missing, verify against a dummy hash created once at start-up.
use argon2::{
password_hash::{rand_core::OsRng, PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
Argon2,
};
use std::collections::HashMap;
struct Auth {
users: HashMap<String, String>, // name -> PHC hash
dummy: String, // a hash nobody can match, made once at start-up
}
impl Auth {
fn new() -> Auth {
let salt = SaltString::generate(&mut OsRng);
let dummy = Argon2::default().hash_password(b"unused", &salt).unwrap().to_string();
Auth { users: HashMap::new(), dummy }
}
fn add(&mut self, name: &str, password: &str) {
let salt = SaltString::generate(&mut OsRng);
let h = Argon2::default().hash_password(password.as_bytes(), &salt).unwrap();
self.users.insert(name.to_string(), h.to_string());
}
/// Same work and same answer whether the account exists or not.
fn login(&self, name: &str, password: &str) -> Result<(), &'static str> {
let (stored, exists) = match self.users.get(name) {
Some(h) => (h.as_str(), true),
None => (self.dummy.as_str(), false),
};
let parsed = PasswordHash::new(stored).unwrap();
let ok = Argon2::default().verify_password(password.as_bytes(), &parsed).is_ok();
if ok && exists { Ok(()) } else { Err("invalid user name or password") }
}
}
fn main() {
let mut auth = Auth::new();
auth.add("alice", "correct horse");
println!("{:?}", auth.login("alice", "correct horse"));
println!("{:?}", auth.login("alice", "wrong"));
println!("{:?}", auth.login("nobody", "wrong"));
}Ok(())
Err("invalid user name or password")
Err("invalid user name or password")This program shows that the answers are identical. It does not measure time: measure your own login path, in a release build, to confirm that the two cases take comparable time.
How to do it#
- Create the dummy hash with the same parameters as real ones, so the work is the same.
- Look the user up, choose the stored hash or the dummy, verify, then decide.
- Return one generic message for every failure.
- Apply the same rule to "forgot password": answer "if that address exists, we sent a message" in every case.
- Add rate limiting and lockout or delays per account and per address, because enumeration is not the only guessing risk.
Common mistakes#
- Different HTTP status codes or wording for the two cases.
- Skipping the hash for unknown users "to save work".
- Revealing existence on the registration form ("this address is taken"). Where you cannot avoid that, protect the form with rate limits.
Related questions#
Does this fully hide that an account exists?#
Not perfectly. Registration, e-mail delivery and timing noise can still leak it. The goal is to remove the obvious differences and to make the rest slow and costly to probe.
Does a login limit also help here?#
Yes. Limit attempts per account and per address, and slow down repeated failures. That reduces both guessing and the use of the login form to probe for names.
Sources#
- OWASP Authentication Cheat Sheet, "Authentication Responses": https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html#authentication-responses
- OWASP Forgot Password Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Forgot_Password_Cheat_Sheet.html