forge
Explore
Log in

Don't you dare go Hollow

Meter link redemption, and stop the address bucket locking people out

Browse files at this commit

6067dfec552e75b81d5a9bbd29dd2ae603c5b597Ahmet Kaan GümüşParent: 0dbf1cf
  • fix
    • meter link redemption, and stop the address bucket locking people out
Show full message
🐛 fix(auth): meter link redemption, and stop the address bucket locking people out

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/<token> 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.rsmodified+50 -12
@@ -191,10 +191,22 @@ impl Accounts {
191 /// Every failure — unknown link, expired link, already-used link — is the191 /// Every failure — unknown link, expired link, already-used link — is the
192 /// same [`AccountError::Rejected`], and the miss path pays for a hash so192 /// same [`AccountError::Rejected`], and the miss path pays for a hash so
193 /// the timing says nothing either.193 /// the timing says nothing either.
194- pub async fn redeem_signin_link(&self, presented: &str, password: &str) -> Result<SignedIn> {194+ ///
195+ /// The limit is bucketed on the client, not on the presented token: a
196+ /// bucket keyed on the token would be fresh for every guess and would never
197+ /// catch anyone walking the token space, while every miss here pays for a
198+ /// full Argon2 hash.
199+ pub async fn redeem_signin_link(
200+ &self,
201+ presented: &str,
202+ password: &str,
203+ client: &str,
204+ ) -> Result<SignedIn> {
195 let now = OffsetDateTime::now_utc();205 let now = OffsetDateTime::now_utc();
206+ let client_bucket = format!("client:{client}");
207+ self.check_rate(&client_bucket).await?;
208+ self.store.record_auth_attempt(&client_bucket, now).await?;
196 let digest = auth::hash_token(presented);209 let digest = auth::hash_token(presented);
197- self.check_rate(&format!("link:{digest}")).await?;
198 210
199 let link = match self.store.signin_link_by_hash(&digest).await? {211 let link = match self.store.signin_link_by_hash(&digest).await? {
200 Some(link) => link,212 Some(link) => link,
@@ -230,7 +242,7 @@ impl Accounts {
230 }242 }
231 243
232 self.store.set_password_hash(&user.id, &hash).await?;244 self.store.set_password_hash(&user.id, &hash).await?;
233- self.store.clear_auth_attempts(&format!("link:{digest}")).await?;245+ self.store.clear_auth_attempts(&client_bucket).await?;
234 let user = self.store.user(&user.id).await?.ok_or(StoreError::NotFound)?;246 let user = self.store.user(&user.id).await?.ok_or(StoreError::NotFound)?;
235 self.start_session(user).await247 self.start_session(user).await
236 }248 }
@@ -240,6 +252,13 @@ impl Accounts {
240 /// Signs a person in. `client` is whatever identifies the caller's machine252 /// Signs a person in. `client` is whatever identifies the caller's machine
241 /// — it only ever becomes a rate-limit bucket.253 /// — it only ever becomes a rate-limit bucket.
242 pub async fn sign_in(&self, email: &str, password: &str, client: &str) -> Result<SignedIn> {254 pub async fn sign_in(&self, email: &str, password: &str, client: &str) -> Result<SignedIn> {
255+ let client_bucket = format!("client:{client}");
256+ // Checked before anything else, including the pre-claim path below,
257+ // which would otherwise be an unmetered dummy-verify.
258+ self.check_rate(&client_bucket).await?;
259+ let now = OffsetDateTime::now_utc();
260+ self.store.record_auth_attempt(&client_bucket, now).await?;
261+
243 let workspace = match self.store.workspace().await? {262 let workspace = match self.store.workspace().await? {
244 Some(workspace) => workspace,263 Some(workspace) => workspace,
245 None => {264 None => {
@@ -248,13 +267,13 @@ impl Accounts {
248 }267 }
249 };268 };
250 let address_bucket = format!("address:{}", email.trim().to_lowercase());269 let address_bucket = format!("address:{}", email.trim().to_lowercase());
251- let client_bucket = format!("client:{client}");270+ // The address bucket counts, but it never refuses on its own: refusing
252- self.check_rate(&address_bucket).await?;271+ // before the verify would let anyone who knows a colleague's address
253- self.check_rate(&client_bucket).await?;272+ // lock them out with ten wrong guesses every fifteen minutes. A correct
254-273+ // password always gets in; a wrong one still pays for its Argon2 and
255- let now = OffsetDateTime::now_utc();274+ // still counts. The client bucket is what caps the work.
275+ let address_over_limit = self.over_limit(&address_bucket).await?;
256 self.store.record_auth_attempt(&address_bucket, now).await?;276 self.store.record_auth_attempt(&address_bucket, now).await?;
257- self.store.record_auth_attempt(&client_bucket, now).await?;
258 277
259 let user = self.store.user_by_email(&workspace.id, email).await?;278 let user = self.store.user_by_email(&workspace.id, email).await?;
260 let hash = user.as_ref().and_then(|u| u.password_hash.clone());279 let hash = user.as_ref().and_then(|u| u.password_hash.clone());
@@ -269,7 +288,11 @@ impl Accounts {
269 }288 }
270 };289 };
271 if !ok {290 if !ok {
272- return Err(AccountError::Rejected);291+ return Err(if address_over_limit {
292+ AccountError::RateLimited
293+ } else {
294+ AccountError::Rejected
295+ });
273 }296 }
274 297
275 let user = user.expect("a hash implies a user");298 let user = user.expect("a hash implies a user");
@@ -286,7 +309,15 @@ impl Accounts {
286 user_id: &str,309 user_id: &str,
287 current: &str,310 current: &str,
288 new: &str,311 new: &str,
312+ client: &str,
289 ) -> Result<SignedIn> {313 ) -> Result<SignedIn> {
314+ // A signed-in browser someone walked away from is still an Argon2
315+ // faucet and a guessing oracle on the current password.
316+ let client_bucket = format!("client:{client}");
317+ self.check_rate(&client_bucket).await?;
318+ self.store
319+ .record_auth_attempt(&client_bucket, OffsetDateTime::now_utc())
320+ .await?;
290 let user = self.store.user(user_id).await?.ok_or(StoreError::NotFound)?;321 let user = self.store.user(user_id).await?.ok_or(StoreError::NotFound)?;
291 let ok = match user.password_hash.as_deref() {322 let ok = match user.password_hash.as_deref() {
292 Some(stored) => verify_password(current, stored),323 Some(stored) => verify_password(current, stored),
@@ -301,6 +332,7 @@ impl Accounts {
301 auth::check_password(new, &user.email, &user.display_name)?;332 auth::check_password(new, &user.email, &user.display_name)?;
302 let hash = hash_password(new)?;333 let hash = hash_password(new)?;
303 self.store.set_password_hash(&user.id, &hash).await?;334 self.store.set_password_hash(&user.id, &hash).await?;
335+ self.store.clear_auth_attempts(&client_bucket).await?;
304 // "Signs out your other devices", as the pane promises.336 // "Signs out your other devices", as the pane promises.
305 self.store337 self.store
306 .revoke_sessions_for_user(&user.id, OffsetDateTime::now_utc())338 .revoke_sessions_for_user(&user.id, OffsetDateTime::now_utc())
@@ -370,10 +402,16 @@ impl Accounts {
370 }402 }
371 403
372 async fn check_rate(&self, bucket: &str) -> Result<()> {404 async fn check_rate(&self, bucket: &str) -> Result<()> {
373- let since = OffsetDateTime::now_utc() - RATE_WINDOW;405+ if self.over_limit(bucket).await? {
374- if self.store.count_auth_attempts(bucket, since).await? >= RATE_LIMIT {
375 return Err(AccountError::RateLimited);406 return Err(AccountError::RateLimited);
376 }407 }
377 Ok(())408 Ok(())
378 }409 }
410+
411+ /// The same question without the refusal, for the bucket that counts but
412+ /// must not lock an account out.
413+ async fn over_limit(&self, bucket: &str) -> Result<bool> {
414+ let since = OffsetDateTime::now_utc() - RATE_WINDOW;
415+ Ok(self.store.count_auth_attempts(bucket, since).await? >= RATE_LIMIT)
416+ }
379 }417 }
crates/dizey-core/tests/store.rsmodified+102 -13
@@ -804,7 +804,7 @@ async fn an_invited_member_chooses_their_own_password() {
804 assert!(matches!(as_them, Err(AccountError::Rejected)));804 assert!(matches!(as_them, Err(AccountError::Rejected)));
805 805
806 let signed_in = accounts806 let signed_in = accounts
807- .redeem_signin_link(invitation.token.expose(), "sextant-and-chart")807+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7")
808 .await808 .await
809 .unwrap();809 .unwrap();
810 assert_eq!(signed_in.user.id, invitation.user.id);810 assert_eq!(signed_in.user.id, invitation.user.id);
@@ -825,7 +825,7 @@ async fn only_an_admin_may_invite() {
825 .await825 .await
826 .unwrap();826 .unwrap();
827 let member = accounts827 let member = accounts
828- .redeem_signin_link(invitation.token.expose(), "sextant-and-chart")828+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7")
829 .await829 .await
830 .unwrap()830 .unwrap()
831 .user;831 .user;
@@ -853,16 +853,16 @@ async fn a_link_works_once_and_a_wrong_one_never_does() {
853 .unwrap();853 .unwrap();
854 854
855 accounts855 accounts
856- .redeem_signin_link(invitation.token.expose(), "sextant-and-chart")856+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7")
857 .await857 .await
858 .unwrap();858 .unwrap();
859 let again = accounts859 let again = accounts
860- .redeem_signin_link(invitation.token.expose(), "another-password")860+ .redeem_signin_link(invitation.token.expose(), "another-password", "198.51.100.7")
861 .await;861 .await;
862 assert!(matches!(again, Err(AccountError::Rejected)));862 assert!(matches!(again, Err(AccountError::Rejected)));
863 863
864 let invented = accounts864 let invented = accounts
865- .redeem_signin_link(&"0".repeat(32), "another-password")865+ .redeem_signin_link(&"0".repeat(32), "another-password", "198.51.100.7")
866 .await;866 .await;
867 assert!(matches!(invented, Err(AccountError::Rejected)));867 assert!(matches!(invented, Err(AccountError::Rejected)));
868 868
@@ -882,16 +882,16 @@ async fn a_rejected_password_does_not_burn_the_invitation() {
882 .unwrap();882 .unwrap();
883 883
884 assert!(matches!(884 assert!(matches!(
885- accounts.redeem_signin_link(invitation.token.expose(), "grace!!").await,885+ accounts.redeem_signin_link(invitation.token.expose(), "grace!!", "198.51.100.7").await,
886 Err(AccountError::Password(PasswordProblem::TooShort))886 Err(AccountError::Password(PasswordProblem::TooShort))
887 ));887 ));
888 assert!(matches!(888 assert!(matches!(
889- accounts.redeem_signin_link(invitation.token.expose(), "grace-hopper-1906").await,889+ accounts.redeem_signin_link(invitation.token.expose(), "grace-hopper-1906", "198.51.100.7").await,
890 Err(AccountError::Password(PasswordProblem::LooksLikeYou))890 Err(AccountError::Password(PasswordProblem::LooksLikeYou))
891 ));891 ));
892 // Still redeemable with a password that passes.892 // Still redeemable with a password that passes.
893 accounts893 accounts
894- .redeem_signin_link(invitation.token.expose(), "sextant-and-chart")894+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7")
895 .await895 .await
896 .unwrap();896 .unwrap();
897 }897 }
@@ -916,7 +916,7 @@ async fn an_expired_link_is_refused_and_resending_opens_the_same_account() {
916 .await916 .await
917 .unwrap();917 .unwrap();
918 assert!(matches!(918 assert!(matches!(
919- accounts.redeem_signin_link(stale.expose(), "sextant-and-chart").await,919+ accounts.redeem_signin_link(stale.expose(), "sextant-and-chart", "198.51.100.7").await,
920 Err(AccountError::Rejected)920 Err(AccountError::Rejected)
921 ));921 ));
922 922
@@ -926,7 +926,7 @@ async fn an_expired_link_is_refused_and_resending_opens_the_same_account() {
926 .unwrap();926 .unwrap();
927 assert_eq!(resent.user.id, invitation.user.id, "same account, new link");927 assert_eq!(resent.user.id, invitation.user.id, "same account, new link");
928 let signed_in = accounts928 let signed_in = accounts
929- .redeem_signin_link(resent.token.expose(), "sextant-and-chart")929+ .redeem_signin_link(resent.token.expose(), "sextant-and-chart", "198.51.100.7")
930 .await930 .await
931 .unwrap();931 .unwrap();
932 assert_eq!(signed_in.user.id, invitation.user.id);932 assert_eq!(signed_in.user.id, invitation.user.id);
@@ -1008,6 +1008,95 @@ async fn sign_in_attempts_are_rate_limited_per_address() {
1008 ));1008 ));
1009 }1009 }
1010 1010
1011+/// The lockout question, decided: an address bucket that refuses before the
1012+/// verify lets anyone who knows a colleague's address keep them out with ten
1013+/// wrong guesses every fifteen minutes, from anywhere, with no account of their
1014+/// own. So the address bucket counts but never refuses a correct password.
1015+#[tokio::test]
1016+async fn a_flooded_address_never_locks_the_owner_out() {
1017+ let (_scratch, accounts, _admin) = claimed().await;
1018+ // Each guess from its own client, so only the address bucket fills.
1019+ for i in 0..(RATE_LIMIT + 5) {
1020+ let _ = accounts
1021+ .sign_in("ada@dizey.sh", "wrong", &format!("203.0.113.{i}"))
1022+ .await;
1023+ }
1024+ // The owner, from a client of their own, still gets in.
1025+ accounts
1026+ .sign_in("ada@dizey.sh", "tide-tables-1892", "198.51.100.7")
1027+ .await
1028+ .unwrap();
1029+}
1030+
1031+/// The client bucket is the one that caps the Argon2 work, and it does refuse.
1032+#[tokio::test]
1033+async fn sign_in_attempts_are_rate_limited_per_client() {
1034+ let (_scratch, accounts, _admin) = claimed().await;
1035+ for i in 0..RATE_LIMIT {
1036+ let _ = accounts
1037+ .sign_in(&format!("nobody{i}@dizey.sh"), "wrong", "203.0.113.9")
1038+ .await;
1039+ }
1040+ // Refused before any Argon2 work, whatever address it asks about.
1041+ assert!(matches!(
1042+ accounts
1043+ .sign_in("ada@dizey.sh", "tide-tables-1892", "203.0.113.9")
1044+ .await,
1045+ Err(AccountError::RateLimited)
1046+ ));
1047+}
1048+
1049+/// `/join/<token>` runs a full Argon2 hash on every miss, so the guess rate has
1050+/// to be capped on the client — a bucket keyed on the presented token would be
1051+/// fresh for every guess and would never fire.
1052+#[tokio::test]
1053+async fn link_redemption_is_rate_limited_per_client() {
1054+ let (_scratch, accounts, admin) = claimed().await;
1055+ let invitation = accounts
1056+ .invite(&admin, "grace@dizey.sh", "Grace", Role::Member)
1057+ .await
1058+ .unwrap();
1059+ for i in 0..RATE_LIMIT {
1060+ let bogus = format!("{i:032x}");
1061+ assert!(matches!(
1062+ accounts
1063+ .redeem_signin_link(&bogus, "sextant-and-chart", "203.0.113.9")
1064+ .await,
1065+ Err(AccountError::Rejected)
1066+ ));
1067+ }
1068+ // Even the real link is refused now, from that client.
1069+ assert!(matches!(
1070+ accounts
1071+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "203.0.113.9")
1072+ .await,
1073+ Err(AccountError::RateLimited)
1074+ ));
1075+ // And the person on another machine is unaffected.
1076+ accounts
1077+ .redeem_signin_link(invitation.token.expose(), "sextant-and-chart", "198.51.100.7")
1078+ .await
1079+ .unwrap();
1080+}
1081+
1082+/// A signed-in browser left unattended is still a guessing oracle on the
1083+/// current password, and still 19 MiB of hashing per try.
1084+#[tokio::test]
1085+async fn changing_a_password_is_rate_limited_per_client() {
1086+ let (_scratch, accounts, admin) = claimed().await;
1087+ for _ in 0..RATE_LIMIT {
1088+ let _ = accounts
1089+ .change_password(&admin.id, "not-it", "chronometer-1761", "203.0.113.9")
1090+ .await;
1091+ }
1092+ assert!(matches!(
1093+ accounts
1094+ .change_password(&admin.id, "tide-tables-1892", "chronometer-1761", "203.0.113.9")
1095+ .await,
1096+ Err(AccountError::RateLimited)
1097+ ));
1098+}
1099+
1011 #[tokio::test]1100 #[tokio::test]
1012 async fn a_successful_sign_in_clears_the_bucket() {1101 async fn a_successful_sign_in_clears_the_bucket() {
1013 let (_scratch, accounts, _admin) = claimed().await;1102 let (_scratch, accounts, _admin) = claimed().await;
@@ -1041,7 +1130,7 @@ async fn changing_a_password_signs_out_every_device() {
1041 assert!(accounts.authenticate(first.session_token.expose()).await.unwrap().is_some());1130 assert!(accounts.authenticate(first.session_token.expose()).await.unwrap().is_some());
1042 1131
1043 let fresh = accounts1132 let fresh = accounts
1044- .change_password(&admin.id, "tide-tables-1892", "chronometer-1761")1133+ .change_password(&admin.id, "tide-tables-1892", "chronometer-1761", "198.51.100.7")
1045 .await1134 .await
1046 .unwrap();1135 .unwrap();
1047 1136
@@ -1062,11 +1151,11 @@ async fn changing_a_password_signs_out_every_device() {
1062 async fn changing_a_password_needs_the_current_one_and_obeys_the_rules() {1151 async fn changing_a_password_needs_the_current_one_and_obeys_the_rules() {
1063 let (_scratch, accounts, admin) = claimed().await;1152 let (_scratch, accounts, admin) = claimed().await;
1064 assert!(matches!(1153 assert!(matches!(
1065- accounts.change_password(&admin.id, "not-it", "chronometer-1761").await,1154+ accounts.change_password(&admin.id, "not-it", "chronometer-1761", "198.51.100.7").await,
1066 Err(AccountError::Rejected)1155 Err(AccountError::Rejected)
1067 ));1156 ));
1068 assert!(matches!(1157 assert!(matches!(
1069- accounts.change_password(&admin.id, "tide-tables-1892", "short").await,1158+ accounts.change_password(&admin.id, "tide-tables-1892", "short", "198.51.100.7").await,
1070 Err(AccountError::Password(PasswordProblem::TooShort))1159 Err(AccountError::Password(PasswordProblem::TooShort))
1071 ));1160 ));
1072 // The old password still works after both refusals.1161 // The old password still works after both refusals.
crates/dizey-web/src/auth.rsmodified+7 -4
@@ -164,10 +164,13 @@ pub async fn redeem_link(
164 token: String,164 token: String,
165 password: String,165 password: String,
166 ) -> Result<Option<Refusal>, ServerFnError> {166 ) -> Result<Option<Refusal>, ServerFnError> {
167- use crate::server::{accounts, set_session_cookie};167+ use crate::server::{accounts, client_label, set_session_cookie};
168 use dizey_core::accounts::SESSION_LIFETIME;168 use dizey_core::accounts::SESSION_LIFETIME;
169 169
170- match accounts().redeem_signin_link(&token, &password).await {170+ match accounts()
171+ .redeem_signin_link(&token, &password, &client_label())
172+ .await
173+ {
171 Ok(signed_in) => {174 Ok(signed_in) => {
172 set_session_cookie(signed_in.session_token.expose(), SESSION_LIFETIME);175 set_session_cookie(signed_in.session_token.expose(), SESSION_LIFETIME);
173 Ok(None)176 Ok(None)
@@ -217,7 +220,7 @@ pub async fn change_password(
217 current: String,220 current: String,
218 new: String,221 new: String,
219 ) -> Result<Option<Refusal>, ServerFnError> {222 ) -> Result<Option<Refusal>, ServerFnError> {
220- use crate::server::{require_user, set_session_cookie};223+ use crate::server::{client_label, require_user, set_session_cookie};
221 use dizey_core::accounts::SESSION_LIFETIME;224 use dizey_core::accounts::SESSION_LIFETIME;
222 225
223 let user = match require_user().await {226 let user = match require_user().await {
@@ -225,7 +228,7 @@ pub async fn change_password(
225 Err(refusal) => return Ok(Some(refusal)),228 Err(refusal) => return Ok(Some(refusal)),
226 };229 };
227 match crate::server::accounts()230 match crate::server::accounts()
228- .change_password(&user.id, &current, &new)231+ .change_password(&user.id, &current, &new, &client_label())
229 .await232 .await
230 {233 {
231 Ok(signed_in) => {234 Ok(signed_in) => {