Repository navigation
Description
Problem / Background
Follow-up to #354 / PR #355, found during that PR's security review. Both items live in src/server/auth/password.rs and share its test module, so implement both in one PR, run the local verification pass once at the end, and keep pushes to a minimum.
1. False load-time warning for $2x$ bcrypt hashes
Current behavior. is_supported_hash_format (src/server/auth/password.rs:503-508, added in #355) accepts only the $2a$, $2b$, $2y$ prefixes. verify_internal (:278-283) dispatches every hash starting with $2 to verify_bcrypt, and bcrypt 0.19.3 accepts version 2x in split_hash (bcrypt-0.19.3/src/lib.rs:208). A user with a $2x$... hash can log in, yet reload_users (:186-194) warns "This user cannot log in with a password", which misleads operators.
Fix. Add hash.starts_with("$2x$") to the bcrypt prefixes in is_supported_hash_format. Do not change verify_internal dispatch (narrowing it would change which accounts authenticate). Add $2x$ to the formats listed in the warn message at :191 (bcrypt ($2a$/$2b$/$2x$/$2y$)) and to the UserDefinition::password_hash rustdoc at src/server/config/types.rs:242. The helper rustdoc at :499-502 does not enumerate variants; leave it. No README or docs file lists the variants.
Acceptance criteria
-
test_is_supported_hash_format(:815) also asserts true for a$2x$hash. - New test: take
bcrypt::hash("pw2x", 4)(emits$2b$), replace bytes 1..3 with2x, put it on a user, and assertis_supported_hash_formatreturns true andverifier.verify(user, "pw2x")returnsOk(true)(andOk(false)for a wrong password). Use this rather than the crate's$2x$12$vector atlib.rs:593, since cost 12 is slow. Theis_supported_hash_formatassertion fails on current main.
2. Equalize timing for users whose stored hash is unusable
Current behavior. In verify_internal (:241-302), the invalid-username (:247) and unknown-user (:263) branches call verify_dummy_hash before returning Ok(false). Three failure paths for an existing user skip it and return after no hash work at all:
- the unsupported-format
elsebranch (:281-286):*,!, empty,$6$... - a malformed
$argon2...hash:PasswordHash::newfails inverify_argon2(:306-307) and theErrpropagates through?at:280;AuthProvider::verify_password(:389-400) maps it toAuthResult::Reject, so it is fail-closed today but fast - a malformed
$2...hash (for example$2z$...or a truncated$2b$):bcrypt::verifyfails insplit_hashbefore hashing andverify_bcrypt(:319-327) returnsOk(false)fast
verify (:221-237) pads to a 100 ms floor, which hides this only while real verification stays under 100 ms. On a slow host or with heavier Argon2 parameters, these users respond measurably faster than users with a real hash, revealing accounts that exist but are locked or misconfigured. Severity is low: such accounts still cannot log in.
Fix. Call let _ = self.verify_dummy_hash(password); in each of the three paths, matching the other two failure branches:
- before
return Ok(false)in theelsebranch; - in the Argon2 branch, replace the
?with a match that onErr(e)callsverify_dummy_hashand then returnsErr(e)unchanged (keep the error and its error-level log; do not convert toOk(false)); - in
verify_bcrypt'sErrarm, before returningOk(false)(verify_bcryptneeds no new parameter; it already takespassword).
Test seam. The existing wall-clock test test_password_verifier_timing_attack_mitigation (:640) is #[ignore]d as flaky, so use a deterministic counter instead of timing: add #[cfg(test)] dummy_hash_calls: std::sync::atomic::AtomicUsize to PasswordVerifier (:107-117), initialize it in the only struct literal (new, :135), and fetch_add(1, Ordering::Relaxed) at the top of verify_dummy_hash (:333) under #[cfg(test)].
Acceptance criteria
- New test calls
verify_internaldirectly (private but reachable from the in-file test module) withZeroizing::new(..)and asserts the counter increases by exactly 1 per call for users with*,!,"",$6$rounds=5000$saltsalt$abc,$argon2id$v=19$,$2z$04$followed by 53 base64 chars (60 bytes total), and for an unknown user; and by 0 for a valid Argon2 user and a valid bcrypt user (right and wrong password). This fails on current main. - The malformed-Argon2 user still makes
verifyreturnErrandverify_passwordreturnAuthResult::Reject. - No wall-clock assertion is added; existing tests keep their expectations, and
test_unsupported_hashes_reject_all_password_lengths(:851) passes unchanged.
Out of scope
Changing which formats authenticate (including narrowing $2 dispatch or rejecting $2z$), tightening is_supported_hash_format to validate bcrypt bodies beyond the prefix, SHA-crypt support, and the 100 ms floor value.
Verification
cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --lib server::auth::password
cargo test
The four tests/integration_test.rs tests need localhost SSH and are authoritative on CI.
Activity
fix(server): treat Argon2 hashes without salt or output as malformed