From 6067dfec552e75b81d5a9bbd29dd2ae603c5b597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 08:47:26 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(auth):=20meter=20link=20rede?= =?UTF-8?q?mption,=20and=20stop=20the=20address=20bucket=20locking=20peopl?= =?UTF-8?q?e=20out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The redemption limit never fired: nothing recorded an attempt for its bucket, so the count was always zero, and the bucket was keyed on the presented token's digest — fresh for every guess, so it would not have caught a token walk even if it had recorded. Every miss on that route runs a full Argon2 hash, which made /join/ the unbounded faucet the limit was meant to close. Redemption is now bucketed on the client, recorded before the work and cleared on success. The address bucket was also an account-lockout lever: ten wrong guesses every fifteen minutes, from any address, kept the owner out — verified by the earlier slice's own drive, where a correct password was refused. It now counts without refusing, so a correct password always gets in while a wrong one still pays for its Argon2 and still counts. The client bucket is what caps the work, and it refuses outright. Also: change_password is metered on the client (an unattended browser is a guessing oracle on the current password), and sign_in checks the client bucket before the pre-claim path, which previously ran an unmetered dummy verify. --- crates/dizey-core/src/accounts.rs | 62 ++++++++++++---- crates/dizey-core/tests/store.rs | 115 ++++++++++++++++++++++++++---- crates/dizey-web/src/auth.rs | 11 +-- 3 files changed, 159 insertions(+), 29 deletions(-) diff --git a/crates/dizey-core/src/accounts.rs b/crates/dizey-core/src/accounts.rs index e6257bc..0495c93 100644 --- a/crates/dizey-core/src/accounts.rs +++ b/crates/dizey-core/src/accounts.rs @@ -191,10 +191,22 @@ impl Accounts { /// Every failure — unknown link, expired link, already-used link — is the /// same [`AccountError::Rejected`], and the miss path pays for a hash so /// the timing says nothing either. - pub async fn redeem_signin_link(&self, presented: &str, password: &str) -> Result { + /// + /// The limit is bucketed on the client, not on the presented token: a + /// bucket keyed on the token would be fresh for every guess and would never + /// catch anyone walking the token space, while every miss here pays for a + /// full Argon2 hash. + pub async fn redeem_signin_link( + &self, + presented: &str, + password: &str, + client: &str, + ) -> Result { let now = OffsetDateTime::now_utc(); + let client_bucket = format!("client:{client}"); + self.check_rate(&client_bucket).await?; + self.store.record_auth_attempt(&client_bucket, now).await?; let digest = auth::hash_token(presented); - self.check_rate(&format!("link:{digest}")).await?; let link = match self.store.signin_link_by_hash(&digest).await? { Some(link) => link, @@ -230,7 +242,7 @@ impl Accounts { } self.store.set_password_hash(&user.id, &hash).await?; - self.store.clear_auth_attempts(&format!("link:{digest}")).await?; + self.store.clear_auth_attempts(&client_bucket).await?; let user = self.store.user(&user.id).await?.ok_or(StoreError::NotFound)?; self.start_session(user).await } @@ -240,6 +252,13 @@ impl Accounts { /// Signs a person in. `client` is whatever identifies the caller's machine /// — it only ever becomes a rate-limit bucket. pub async fn sign_in(&self, email: &str, password: &str, client: &str) -> Result { + let client_bucket = format!("client:{client}"); + // Checked before anything else, including the pre-claim path below, + // which would otherwise be an unmetered dummy-verify. + self.check_rate(&client_bucket).await?; + let now = OffsetDateTime::now_utc(); + self.store.record_auth_attempt(&client_bucket, now).await?; + let workspace = match self.store.workspace().await? { Some(workspace) => workspace, None => { @@ -248,13 +267,13 @@ impl Accounts { } }; let address_bucket = format!("address:{}", email.trim().to_lowercase()); - let client_bucket = format!("client:{client}"); - self.check_rate(&address_bucket).await?; - self.check_rate(&client_bucket).await?; - - let now = OffsetDateTime::now_utc(); + // The address bucket counts, but it never refuses on its own: refusing + // before the verify would let anyone who knows a colleague's address + // lock them out with ten wrong guesses every fifteen minutes. A correct + // password always gets in; a wrong one still pays for its Argon2 and + // still counts. The client bucket is what caps the work. + let address_over_limit = self.over_limit(&address_bucket).await?; self.store.record_auth_attempt(&address_bucket, now).await?; - self.store.record_auth_attempt(&client_bucket, now).await?; let user = self.store.user_by_email(&workspace.id, email).await?; let hash = user.as_ref().and_then(|u| u.password_hash.clone()); @@ -269,7 +288,11 @@ impl Accounts { } }; if !ok { - return Err(AccountError::Rejected); + return Err(if address_over_limit { + AccountError::RateLimited + } else { + AccountError::Rejected + }); } let user = user.expect("a hash implies a user"); @@ -286,7 +309,15 @@ impl Accounts { user_id: &str, current: &str, new: &str, + client: &str, ) -> Result { + // A signed-in browser someone walked away from is still an Argon2 + // faucet and a guessing oracle on the current password. + let client_bucket = format!("client:{client}"); + self.check_rate(&client_bucket).await?; + self.store + .record_auth_attempt(&client_bucket, OffsetDateTime::now_utc()) + .await?; let user = self.store.user(user_id).await?.ok_or(StoreError::NotFound)?; let ok = match user.password_hash.as_deref() { Some(stored) => verify_password(current, stored), @@ -301,6 +332,7 @@ impl Accounts { auth::check_password(new, &user.email, &user.display_name)?; let hash = hash_password(new)?; self.store.set_password_hash(&user.id, &hash).await?; + self.store.clear_auth_attempts(&client_bucket).await?; // "Signs out your other devices", as the pane promises. self.store .revoke_sessions_for_user(&user.id, OffsetDateTime::now_utc()) @@ -370,10 +402,16 @@ impl Accounts { } async fn check_rate(&self, bucket: &str) -> Result<()> { - let since = OffsetDateTime::now_utc() - RATE_WINDOW; - if self.store.count_auth_attempts(bucket, since).await? >= RATE_LIMIT { + if self.over_limit(bucket).await? { return Err(AccountError::RateLimited); } Ok(()) } + + /// The same question without the refusal, for the bucket that counts but + /// must not lock an account out. + async fn over_limit(&self, bucket: &str) -> Result { + let since = OffsetDateTime::now_utc() - RATE_WINDOW; + Ok(self.store.count_auth_attempts(bucket, since).await? >= RATE_LIMIT) + } } diff --git a/crates/dizey-core/tests/store.rs b/crates/dizey-core/tests/store.rs index 83a449e..2a2ca7d 100644 --- a/crates/dizey-core/tests/store.rs +++ b/crates/dizey-core/tests/store.rs @@ -804,7 +804,7 @@ async fn an_invited_member_chooses_their_own_password() { assert!(matches!(as_them, Err(AccountError::Rejected))); let signed_in = accounts - .redeem_signin_link(invitation.token.expose(), "sextant-and-chart") + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7") .await .unwrap(); assert_eq!(signed_in.user.id, invitation.user.id); @@ -825,7 +825,7 @@ async fn only_an_admin_may_invite() { .await .unwrap(); let member = accounts - .redeem_signin_link(invitation.token.expose(), "sextant-and-chart") + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7") .await .unwrap() .user; @@ -853,16 +853,16 @@ async fn a_link_works_once_and_a_wrong_one_never_does() { .unwrap(); accounts - .redeem_signin_link(invitation.token.expose(), "sextant-and-chart") + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7") .await .unwrap(); let again = accounts - .redeem_signin_link(invitation.token.expose(), "another-password") + .redeem_signin_link(invitation.token.expose(), "another-password", "198.51.100.7") .await; assert!(matches!(again, Err(AccountError::Rejected))); let invented = accounts - .redeem_signin_link(&"0".repeat(32), "another-password") + .redeem_signin_link(&"0".repeat(32), "another-password", "198.51.100.7") .await; assert!(matches!(invented, Err(AccountError::Rejected))); @@ -882,16 +882,16 @@ async fn a_rejected_password_does_not_burn_the_invitation() { .unwrap(); assert!(matches!( - accounts.redeem_signin_link(invitation.token.expose(), "grace!!").await, + accounts.redeem_signin_link(invitation.token.expose(), "grace!!", "198.51.100.7").await, Err(AccountError::Password(PasswordProblem::TooShort)) )); assert!(matches!( - accounts.redeem_signin_link(invitation.token.expose(), "grace-hopper-1906").await, + accounts.redeem_signin_link(invitation.token.expose(), "grace-hopper-1906", "198.51.100.7").await, Err(AccountError::Password(PasswordProblem::LooksLikeYou)) )); // Still redeemable with a password that passes. accounts - .redeem_signin_link(invitation.token.expose(), "sextant-and-chart") + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7") .await .unwrap(); } @@ -916,7 +916,7 @@ async fn an_expired_link_is_refused_and_resending_opens_the_same_account() { .await .unwrap(); assert!(matches!( - accounts.redeem_signin_link(stale.expose(), "sextant-and-chart").await, + accounts.redeem_signin_link(stale.expose(), "sextant-and-chart", "198.51.100.7").await, Err(AccountError::Rejected) )); @@ -926,7 +926,7 @@ async fn an_expired_link_is_refused_and_resending_opens_the_same_account() { .unwrap(); assert_eq!(resent.user.id, invitation.user.id, "same account, new link"); let signed_in = accounts - .redeem_signin_link(resent.token.expose(), "sextant-and-chart") + .redeem_signin_link(resent.token.expose(), "sextant-and-chart", "198.51.100.7") .await .unwrap(); assert_eq!(signed_in.user.id, invitation.user.id); @@ -1008,6 +1008,95 @@ async fn sign_in_attempts_are_rate_limited_per_address() { )); } +/// The lockout question, decided: an address bucket that refuses before the +/// verify lets anyone who knows a colleague's address keep them out with ten +/// wrong guesses every fifteen minutes, from anywhere, with no account of their +/// own. So the address bucket counts but never refuses a correct password. +#[tokio::test] +async fn a_flooded_address_never_locks_the_owner_out() { + let (_scratch, accounts, _admin) = claimed().await; + // Each guess from its own client, so only the address bucket fills. + for i in 0..(RATE_LIMIT + 5) { + let _ = accounts + .sign_in("ada@dizey.sh", "wrong", &format!("203.0.113.{i}")) + .await; + } + // The owner, from a client of their own, still gets in. + accounts + .sign_in("ada@dizey.sh", "tide-tables-1892", "198.51.100.7") + .await + .unwrap(); +} + +/// The client bucket is the one that caps the Argon2 work, and it does refuse. +#[tokio::test] +async fn sign_in_attempts_are_rate_limited_per_client() { + let (_scratch, accounts, _admin) = claimed().await; + for i in 0..RATE_LIMIT { + let _ = accounts + .sign_in(&format!("nobody{i}@dizey.sh"), "wrong", "203.0.113.9") + .await; + } + // Refused before any Argon2 work, whatever address it asks about. + assert!(matches!( + accounts + .sign_in("ada@dizey.sh", "tide-tables-1892", "203.0.113.9") + .await, + Err(AccountError::RateLimited) + )); +} + +/// `/join/` runs a full Argon2 hash on every miss, so the guess rate has +/// to be capped on the client — a bucket keyed on the presented token would be +/// fresh for every guess and would never fire. +#[tokio::test] +async fn link_redemption_is_rate_limited_per_client() { + let (_scratch, accounts, admin) = claimed().await; + let invitation = accounts + .invite(&admin, "grace@dizey.sh", "Grace", Role::Member) + .await + .unwrap(); + for i in 0..RATE_LIMIT { + let bogus = format!("{i:032x}"); + assert!(matches!( + accounts + .redeem_signin_link(&bogus, "sextant-and-chart", "203.0.113.9") + .await, + Err(AccountError::Rejected) + )); + } + // Even the real link is refused now, from that client. + assert!(matches!( + accounts + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "203.0.113.9") + .await, + Err(AccountError::RateLimited) + )); + // And the person on another machine is unaffected. + accounts + .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7") + .await + .unwrap(); +} + +/// A signed-in browser left unattended is still a guessing oracle on the +/// current password, and still 19 MiB of hashing per try. +#[tokio::test] +async fn changing_a_password_is_rate_limited_per_client() { + let (_scratch, accounts, admin) = claimed().await; + for _ in 0..RATE_LIMIT { + let _ = accounts + .change_password(&admin.id, "not-it", "chronometer-1761", "203.0.113.9") + .await; + } + assert!(matches!( + accounts + .change_password(&admin.id, "tide-tables-1892", "chronometer-1761", "203.0.113.9") + .await, + Err(AccountError::RateLimited) + )); +} + #[tokio::test] async fn a_successful_sign_in_clears_the_bucket() { let (_scratch, accounts, _admin) = claimed().await; @@ -1041,7 +1130,7 @@ async fn changing_a_password_signs_out_every_device() { assert!(accounts.authenticate(first.session_token.expose()).await.unwrap().is_some()); let fresh = accounts - .change_password(&admin.id, "tide-tables-1892", "chronometer-1761") + .change_password(&admin.id, "tide-tables-1892", "chronometer-1761", "198.51.100.7") .await .unwrap(); @@ -1062,11 +1151,11 @@ async fn changing_a_password_signs_out_every_device() { async fn changing_a_password_needs_the_current_one_and_obeys_the_rules() { let (_scratch, accounts, admin) = claimed().await; assert!(matches!( - accounts.change_password(&admin.id, "not-it", "chronometer-1761").await, + accounts.change_password(&admin.id, "not-it", "chronometer-1761", "198.51.100.7").await, Err(AccountError::Rejected) )); assert!(matches!( - accounts.change_password(&admin.id, "tide-tables-1892", "short").await, + accounts.change_password(&admin.id, "tide-tables-1892", "short", "198.51.100.7").await, Err(AccountError::Password(PasswordProblem::TooShort)) )); // The old password still works after both refusals. diff --git a/crates/dizey-web/src/auth.rs b/crates/dizey-web/src/auth.rs index 409d21f..729fe95 100644 --- a/crates/dizey-web/src/auth.rs +++ b/crates/dizey-web/src/auth.rs @@ -164,10 +164,13 @@ pub async fn redeem_link( token: String, password: String, ) -> Result, ServerFnError> { - use crate::server::{accounts, set_session_cookie}; + use crate::server::{accounts, client_label, set_session_cookie}; use dizey_core::accounts::SESSION_LIFETIME; - match accounts().redeem_signin_link(&token, &password).await { + match accounts() + .redeem_signin_link(&token, &password, &client_label()) + .await + { Ok(signed_in) => { set_session_cookie(signed_in.session_token.expose(), SESSION_LIFETIME); Ok(None) @@ -217,7 +220,7 @@ pub async fn change_password( current: String, new: String, ) -> Result, ServerFnError> { - use crate::server::{require_user, set_session_cookie}; + use crate::server::{client_label, require_user, set_session_cookie}; use dizey_core::accounts::SESSION_LIFETIME; let user = match require_user().await { @@ -225,7 +228,7 @@ pub async fn change_password( Err(refusal) => return Ok(Some(refusal)), }; match crate::server::accounts() - .change_password(&user.id, ¤t, &new) + .change_password(&user.id, ¤t, &new, &client_label()) .await { Ok(signed_in) => {