Skip to content

fix(server): accept $2x$ in load-time hash check and equalize timing for unusable password hashes #356

Closed

Description

@inureyes

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 with 2x, put it on a user, and assert is_supported_hash_format returns true and verifier.verify(user, "pw2x") returns Ok(true) (and Ok(false) for a wrong password). Use this rather than the crate's $2x$12$ vector at lib.rs:593, since cost 12 is slow. The is_supported_hash_format assertion 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 else branch (:281-286): *, !, empty, $6$...
  • a malformed $argon2... hash: PasswordHash::new fails in verify_argon2 (:306-307) and the Err propagates through ? at :280; AuthProvider::verify_password (:389-400) maps it to AuthResult::Reject, so it is fail-closed today but fast
  • a malformed $2... hash (for example $2z$... or a truncated $2b$): bcrypt::verify fails in split_hash before hashing and verify_bcrypt (:319-327) returns Ok(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 the else branch;
  • in the Argon2 branch, replace the ? with a match that on Err(e) calls verify_dummy_hash and then returns Err(e) unchanged (keep the error and its error-level log; do not convert to Ok(false));
  • in verify_bcrypt's Err arm, before returning Ok(false) (verify_bcrypt needs no new parameter; it already takes password).

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_internal directly (private but reachable from the in-file test module) with Zeroizing::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 verify return Err and verify_password return AuthResult::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

  1. added
    type:bug
    status:ready
    priority:low
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Nov 9, 2026
  2. added
    status:done
    and removed
    status:review
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:lowstatus:donetype:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions