From 8b92a10ec85ec01bd575a9f2b3340c061ac38578 Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Wed, 4 Nov 2026 15:43:05 -0500 Subject: [PATCH] Forgot password and sign-up give one answer for every email Security fix. Since #464 forgot-password on a self-hosted install answered with a different status per case (has an account, on the team without one, invited, not on this install), so anyone could learn which emails have accounts or are on the team. Forgot password now answers the same status, body and headers for every email on every install, and no sooner than 2.5 seconds after the ask, so the case can't be timed either: the work (the Cognito reset code, or the set-up email for a team member with no account here) runs while the answer waits, and a slow send carries on after it. Send failures are logged, never answered. The set-up email keeps its once-an-hour limit and the 10/h per IP and 5/h per email limits stay. Self-hosted sign-up answers the same way for invited, existing and refused emails, with the password rules checked for every email first; an email that signed up but never confirmed gets a fresh code. The app client now has preventUserExistenceErrors on. Pins the dashboard to d26225b (CoderHelm/coderhelm #314): the sign-in page shows one message for every email and names both senders. --- docs/self-host/sign-in.md | 26 +- infra/lib/api-stack.ts | 3 + selfhost/versions.json | 2 +- services/gateway/src/routes/auth.rs | 507 ++++++++++++++++--------- services/gateway/src/routes/sign_in.rs | 3 +- 5 files changed, 359 insertions(+), 182 deletions(-) diff --git a/docs/self-host/sign-in.md b/docs/self-host/sign-in.md index c1ee00c5d..568217c14 100644 --- a/docs/self-host/sign-in.md +++ b/docs/self-host/sign-in.md @@ -32,25 +32,31 @@ Each person gets at most one set-up email an hour and five a week, however it wa ### Forgot password -**Forgot password** on the sign-in page says plainly what happened for the email typed in: +**Forgot password** on the sign-in page gives everyone the same answer, whatever the email typed in: "If the email can sign in here, we've sent a reset code or a set-up link." The page then asks for the reset code, with a note that a set-up link may have come instead. It never says whether the email has an account, is on the team, is invited, or isn't on the install, so it can't be used to find out who is. -| The email | What happens | What the page says | -|---|---|---| -| Has an account here | Amazon Cognito sends a reset code | We sent a reset code to the email. | -| On the team, no account here yet (moved from another CoderHelm) | The set-up email goes instead | You don't have an account here yet. We sent you a set-up link instead. | -| Invited, not signed up yet | Nothing is sent | Use the link in the invite email, or ask an admin to send it again. | -| Not on this install | Nothing is sent | There's no account for the email on this CoderHelm. Ask an admin to add you. | +What is sent depends on the email: + +| The email | What is sent | +|---|---| +| Has an account here | Amazon Cognito sends a reset code | +| On the team, no account here yet (moved from another CoderHelm) | The set-up email (at most one an hour and five a week, as above) | +| Invited, not signed up yet | Nothing (their invite email has the link; an admin can send it again) | +| Not on this install | Nothing | + +The answer takes the same time for every email too: the work happens while the page waits a fixed couple of seconds, so a slow send can't be told apart from no send. If a send fails, the page still gives the same answer; the failure is in the gateway's log, and set-up emails show in **Settings, Deliveries**. + +Forgot password is limited to ten asks an hour from one network address and five an hour for one email, for every email alike. -Because the page says whether an email has an account, it's limited so it can't be used to check addresses quickly: ten asks an hour from one network address, and five an hour for one email. +The account form behaves the same way on a self-hosted install: whether the email is invited, already has an account, or isn't let in, it says "If that email can join here, we've sent a verification code." A code goes only to an invited email, one at an allowed domain, or one that signed up before and never entered its code. Someone who already has an account gets nothing new: they sign in, or use **Forgot password**. The password rules (at least 8 characters, with an upper-case letter, a lower-case letter and a number) are checked for every email alike. ### Where these emails come from -The emails come from two different senders. After sending, the sign-in page names the one to look for, so a person can ask IT to let it through a mail filter (Mimecast, Proofpoint, Microsoft Defender and so on): +The emails come from two different senders. The sign-in page names both after **Forgot password**, so a person can ask IT to let them through a mail filter (Mimecast, Proofpoint, Microsoft Defender and so on): - **Reset codes** come from Amazon Cognito: `no-reply@verificationemail.com` while the user pool sends its own email (the default), or the install's own address once `email.cognitoUsesSes` is on (see [Email](./email.md#sign-up-and-password-emails)). CoderHelm reads this from the user pool's email settings. - **Set-up and invite emails** come from the install's own email: its Amazon SES address, or the address of CoderHelm's mail relay when the install sends through it (**Settings, Email**). -If someone says the email never came, check **Settings, Deliveries** for set-up emails, then ask IT to search for, and allow, the sender above. +If someone says the email never came, check **Settings, Deliveries** for set-up emails, then ask IT to search for, and allow, the senders above. ### At install time diff --git a/infra/lib/api-stack.ts b/infra/lib/api-stack.ts index 012742b9f..1f70961cb 100644 --- a/infra/lib/api-stack.ts +++ b/infra/lib/api-stack.ts @@ -198,6 +198,9 @@ export class ApiStack extends cdk.Stack { const userPoolClient = new cognito.UserPoolClient(this, "UserPoolClient", { userPool, userPoolClientName: `${prefix}-dashboard`, + // Cognito answers an email it doesn't have the same as a wrong password + // or a sent code, so its own errors can't tell who has an account. + preventUserExistenceErrors: true, authFlows: { userPassword: true, userSrp: true, diff --git a/selfhost/versions.json b/selfhost/versions.json index 8abb6e040..39cf02ffa 100644 --- a/selfhost/versions.json +++ b/selfhost/versions.json @@ -1,4 +1,4 @@ { "about": "The dashboard commit each platform release ships: a full 40-character commit SHA of CoderHelm/coderhelm, never a branch or tag (scripts/check-dashboard-ref.mjs fails CI and the release otherwise). The release workflow builds exactly this commit (dashboard/) into dashboard.tar.gz. Move it with scripts/bump-dashboard.sh in a reviewed PR, in the same PR as any API change the dashboard needs.", - "dashboardRef": "c599a92ad0c85b9354dbbe6dcf024761eb4f9534" + "dashboardRef": "d26225b43726f3b115826d9bb03cf64724934ee1" } diff --git a/services/gateway/src/routes/auth.rs b/services/gateway/src/routes/auth.rs index 447b84c95..c4d8c3cb2 100644 --- a/services/gateway/src/routes/auth.rs +++ b/services/gateway/src/routes/auth.rs @@ -131,7 +131,8 @@ pub async fn signup( State(state): State>, ip: crate::middleware::rate_limit::ClientIp, Json(body): Json, -) -> Result, (StatusCode, Json)> { +) -> Result)> { + let started = tokio::time::Instant::now(); let email = body.email.trim().to_lowercase(); if email.is_empty() || body.password.len() < 8 { return Err(( @@ -143,19 +144,72 @@ pub async fn signup( } // A self-hosted install: one organization, joined only by invite or an - // allowed email domain (the first admin starts it). + // allowed email domain (the first admin starts it). Its answer is the same + // for every email (one_signup_answer), so sign-up can't be used to learn + // who is invited or already has an account. if sign_in::applies() { - let join = sign_in::may_sign_up(&state, &email).await.map_err(|e| { - error!("Sign-up check failed: {e}"); - ( - StatusCode::SERVICE_UNAVAILABLE, - Json(serde_json::json!({ "error": "Sign-up is unavailable just now. Try again shortly." })), - ) - })?; + if sign_in::email_domain(&email).is_none() { + return Err(( + StatusCode::BAD_REQUEST, + Json(serde_json::json!({ "error": "Invalid email or password format" })), + )); + } + // Checked here, for every email alike: Cognito checks it only for the + // ones it gets, so its answer would say which those were. + if !password_fits(&body.password) { + return Err(( + StatusCode::BAD_REQUEST, + Json(serde_json::json!({ "error": WEAK_PASSWORD })), + )); + } + let join = match sign_in::may_sign_up(&state, &email).await { + Ok(j) => j, + Err(e) => { + error!("Sign-up check failed: {e}"); + return Err(( + StatusCode::SERVICE_UNAVAILABLE, + Json( + serde_json::json!({ "error": "Sign-up is unavailable just now. Try again shortly." }), + ), + )); + } + }; if !join.allowed() { info!(outcome = join.key(), "Self-hosted sign-up refused"); - return Err(not_invited_json()); + return Ok(one_signup_answer(started).await); } + match cognito_sign_up(&state, &email, &body).await { + Ok(sub) => { + info!(email = %email, cognito_sub = %sub, "User signed up, verification pending") + } + Err(e) if e.contains("UsernameExistsException") => { + // Signed up before but never confirmed: a fresh code. Cognito + // refuses it for a confirmed account, and nothing is sent. + let hash = cognito_secret_hash( + &state.cognito_client_secret, + &email, + &state.config.cognito_client_id, + ); + let resent = state + .cognito + .resend_confirmation_code() + .client_id(&state.config.cognito_client_id) + .secret_hash(&hash) + .username(&email) + .send() + .await + .is_ok(); + info!( + resent, + "Self-hosted sign-up for an email that has an account" + ) + } + Err(e) if e.contains("UserLambdaValidationException") => { + info!("Self-hosted sign-up refused by the pre-sign-up trigger") + } + Err(e) => error!("Cognito signup failed: {e}"), + } + return Ok(one_signup_answer(started).await); } else if super::open_signup::allow_new_team(&state, &ip.0) .await .is_err() @@ -169,9 +223,83 @@ pub async fn signup( )); } + let cognito_sub = cognito_sign_up(&state, &email, &body) + .await + .map_err(|msg| { + // The pre-sign-up trigger said no (a self-hosted install's rules). + if msg.contains("UserLambdaValidationException") { + return not_invited_json(); + } + if msg.contains("UsernameExistsException") { + return ( + StatusCode::CONFLICT, + Json(serde_json::json!({ + "error": "An account with this email already exists" + })), + ); + } + error!("Cognito signup failed: {msg}"); + let user_msg = if msg.contains("InvalidPasswordException") + || msg.contains("Password did not conform") + { + WEAK_PASSWORD + } else if msg.contains("InvalidParameterException") { + "Invalid email or password format" + } else { + "Signup failed — please try again" + }; + ( + StatusCode::BAD_REQUEST, + Json(serde_json::json!({ "error": user_msg })), + ) + })?; + + info!(email = %email, cognito_sub = %cognito_sub, "User signed up, verification pending"); + + Ok(Json(serde_json::json!({ + "status": "verification_required", + "message": "Check your email for a verification code" + })) + .into_response()) +} + +const WEAK_PASSWORD: &str = "Password must contain at least 8 characters, including uppercase, lowercase, a number, and a special character"; + +/// The user pool's password rules (infra: at least 8 characters, an upper-case and a +/// lower-case letter, and a digit). +pub fn password_fits(p: &str) -> bool { + p.chars().count() >= 8 + && p.chars().any(|c| c.is_uppercase()) + && p.chars().any(|c| c.is_lowercase()) + && p.chars().any(|c| c.is_ascii_digit()) +} + +/// A self-hosted install's sign-up answer, the same for every email: invited or allowed (a +/// code was sent), already with an account, or not let in (nothing was sent). It goes out +/// [`FORGOT_ANSWER_AFTER`] after the ask, so the time doesn't say which either. +pub async fn one_signup_answer(started: tokio::time::Instant) -> Response { + tokio::time::sleep_until(started + FORGOT_ANSWER_AFTER).await; + signup_answer() +} + +/// The words of [`one_signup_answer`]. +pub fn signup_answer() -> Response { + Json(serde_json::json!({ + "status": "verification_required", + "message": "If that email can join here, we've sent a verification code." + })) + .into_response() +} + +/// Cognito sign-up: the new user's sub, or Cognito's error (Debug form). +async fn cognito_sign_up( + state: &AppState, + email: &str, + body: &SignupRequest, +) -> Result { let hash = cognito_secret_hash( &state.cognito_client_secret, - &email, + email, &state.config.cognito_client_id, ); @@ -180,12 +308,12 @@ pub async fn signup( .sign_up() .client_id(&state.config.cognito_client_id) .secret_hash(&hash) - .username(&email) + .username(email) .password(&body.password) .user_attributes( aws_sdk_cognitoidentityprovider::types::AttributeType::builder() .name("email") - .value(&email) + .value(email) .build() .unwrap(), ); @@ -200,44 +328,10 @@ pub async fn signup( ); } - let result = req.send().await.map_err(|e| { - let msg = format!("{e:?}"); - // The pre-sign-up trigger said no (a self-hosted install's rules). - if msg.contains("UserLambdaValidationException") { - return not_invited_json(); - } - if msg.contains("UsernameExistsException") { - return ( - StatusCode::CONFLICT, - Json(serde_json::json!({ - "error": "An account with this email already exists" - })), - ); - } - error!("Cognito signup failed: {e}"); - let user_msg = if msg.contains("InvalidPasswordException") - || msg.contains("Password did not conform") - { - "Password must contain at least 8 characters, including uppercase, lowercase, a number, and a special character" - } else if msg.contains("InvalidParameterException") { - "Invalid email or password format" - } else { - "Signup failed — please try again" - }; - ( - StatusCode::BAD_REQUEST, - Json(serde_json::json!({ "error": user_msg })), - ) - })?; - - let cognito_sub = result.user_sub(); - - info!(email = %email, cognito_sub = %cognito_sub, "User signed up, verification pending"); - - Ok(Json(serde_json::json!({ - "status": "verification_required", - "message": "Check your email for a verification code" - }))) + req.send() + .await + .map(|r| r.user_sub().to_string()) + .map_err(|e| format!("{e:?}")) } // ── Verify email ───────────────────────────────────────────────────── @@ -797,8 +891,19 @@ const FORGOT_PER_IP_PER_HOUR: u64 = 10; /// Forgot-password asks for one email: five an hour, from anywhere. const FORGOT_PER_EMAIL_PER_HOUR: u64 = 5; -/// What forgot-password does for an email on a self-hosted install, which says plainly what -/// happened (an install is one organization; its admins add people). +/// Every forgot-password answer goes out this long after the ask, whatever the email: the +/// work for an account (a Cognito reset code) and for a team member with no account here (the +/// set-up email) takes longer than for an email with nothing to send, so without a floor the +/// time to answer would say which case it was. +pub const FORGOT_ANSWER_AFTER: std::time::Duration = std::time::Duration::from_millis(2500); + +/// The one forgot-password answer, for every email on every install: it never says whether +/// the email has an account, is on the team, is invited, or is unknown here. +pub const FORGOT_SENT: &str = + "If that email can sign in here, we've sent a reset code or a set-up link."; + +/// What forgot-password does for an email. The answer is the same for every case +/// ([`forgot_answer`]); the case only decides what is sent, and is logged. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Forgot { /// They have an account here: Cognito sends a reset code. @@ -806,11 +911,12 @@ pub enum Forgot { /// On the team (copied from another CoderHelm) but with no account here: the set-up email /// goes instead, from this team record (`pk`, `sk`). SetUp { team_id: String, sk: String }, - /// As [`Forgot::SetUp`], but one went less than an hour ago (or five this week). + /// As [`Forgot::SetUp`], but one went less than an hour ago (or five this week): nothing + /// is sent. SetUpRecent, - /// Invited and not signed up yet: their invite email has the link. + /// Invited and not signed up yet: nothing is sent (their invite email has the link). Invited, - /// Not on this install. + /// Not on this install: nothing is sent. NoAccount, } @@ -819,6 +925,17 @@ impl Forgot { pub fn sends(&self) -> bool { matches!(self, Forgot::ResetCode | Forgot::SetUp { .. }) } + + /// A name for the log. + pub fn key(&self) -> &'static str { + match self { + Forgot::ResetCode => "reset_code", + Forgot::SetUp { .. } => "setup", + Forgot::SetUpRecent => "setup_recent", + Forgot::Invited => "invited", + Forgot::NoAccount => "no_account", + } + } } /// The one place the case is decided. Pure: `in_pool` is whether the user pool has the email, @@ -853,28 +970,38 @@ pub fn plan_forgot( } } -/// What the sign-in page shows for each case: `status` and a plain sentence. -pub fn forgot_reply(case: &Forgot, email: &str) -> serde_json::Value { - let (status, message) = match case { - Forgot::ResetCode => ("code_sent", format!("We sent a reset code to {email}.")), - Forgot::SetUp { .. } => ( - "setup_sent", - "You don't have an account here yet. We sent you a set-up link instead.".to_string(), - ), - Forgot::SetUpRecent => ( - "setup_recent", - "You don't have an account here yet. We sent you a set-up link less than an hour ago: look for that email, or try again later.".to_string(), - ), - Forgot::Invited => ( - "invited", - format!("{email} has an invite here but no account yet. Use the link in the invite email, or ask an admin to send it again."), - ), - Forgot::NoAccount => ( - "no_account", - format!("There's no account for {email} on this CoderHelm. Ask an admin to add you."), - ), - }; - serde_json::json!({ "status": status, "message": message, "email": email }) +/// The forgot-password answer: one status, one body, one set of headers, for everyone. +pub fn forgot_answer() -> Response { + ( + StatusCode::OK, + Json(serde_json::json!({ "status": "sent", "message": FORGOT_SENT })), + ) + .into_response() +} + +/// Does `work` and answers [`forgot_answer`] no sooner than `floor` after `started`, whatever +/// the work found or how long it took. Work still running at the floor carries on after the +/// answer (dropping its handle doesn't stop it), so a slow send can't be timed either. +pub async fn answer_evenly( + started: tokio::time::Instant, + floor: std::time::Duration, + work: F, +) -> Response +where + F: std::future::Future + Send + 'static, +{ + let task = tokio::spawn(async move { + let case = work.await; + info!( + outcome = case.key(), + sends = case.sends(), + "Forgot password" + ); + case + }); + let _ = tokio::time::timeout_at(started + floor, task).await; + tokio::time::sleep_until(started + floor).await; + forgot_answer() } fn too_many_forgot() -> (StatusCode, Json) { @@ -893,15 +1020,18 @@ fn email_key(email: &str) -> String { d.iter().take(16).map(|b| format!("{b:02x}")).collect() } -/// POST /auth/forgot-password — a reset code, or on a self-hosted install for someone on the -/// team with no account here, the set-up email. A self-hosted install says which (or that the -/// email has no account there); the hosted service answers the same for every email. +/// POST /auth/forgot-password: a reset code for an account, or on a self-hosted install for +/// someone on the team with no account here, the set-up email. The answer is the same for +/// every email (status, body, headers and time), so it can't be used to learn who has an +/// account or is on the team. Only a malformed email or the rate limits (which count every +/// email alike) answer differently. pub async fn forgot_password( State(state): State>, ip: crate::middleware::rate_limit::ClientIp, Json(body): Json, -) -> Result, (StatusCode, Json)> { +) -> Result)> { use crate::middleware::rate_limit::allow_shared; + let started = tokio::time::Instant::now(); let email = normalize_email(&body.email); if sign_in::email_domain(&email).is_none() { return Err(( @@ -922,24 +1052,21 @@ pub async fn forgot_password( .await .map_err(|_| too_many_forgot())?; + Ok(answer_evenly(started, FORGOT_ANSWER_AFTER, forgot_work(state, email)).await) +} + +/// What forgot-password sends for `email`. Every failure is logged, never answered: an answer +/// that differed would say which case it was. +async fn forgot_work(state: Arc, email: String) -> Forgot { if !sign_in::applies() { - // The hosted service: the same answer for every email. - let _ = send_reset_code(&state, &email).await; - return Ok(Json(serde_json::json!({ - "status": "sent", - "message": "If an account exists, a reset code was sent" - }))); + // The hosted service: a reset code when Cognito has the email. + if let Err(e) = send_reset_code(&state, &email).await { + if !e.contains("UserNotFound") { + warn!(error = %e, "Reset code not sent"); + } + } + return Forgot::ResetCode; } - - let unavailable = |what: &str, e: String| { - error!(error = %e, "{what}"); - ( - StatusCode::SERVICE_UNAVAILABLE, - Json( - serde_json::json!({ "error": "Password reset is unavailable just now. Try again shortly." }), - ), - ) - }; let in_pool = match state .cognito .admin_get_user() @@ -951,18 +1078,20 @@ pub async fn forgot_password( Ok(_) => true, Err(e) if format!("{e:?}").contains("UserNotFound") => false, Err(e) => { - return Err(unavailable( - "Forgot password: sign-in not checked", - format!("{e}"), - )) + error!(error = %e, "Forgot password: sign-in not checked"); + return Forgot::NoAccount; } }; let (join, member) = if in_pool { (Join::Refused, None) } else { - let facts = sign_in::facts_for(&state, &email) - .await - .map_err(|e| unavailable("Forgot password: join not checked", e))?; + let facts = match sign_in::facts_for(&state, &email).await { + Ok(f) => f, + Err(e) => { + error!(error = %e, "Forgot password: join not checked"); + return Forgot::NoAccount; + } + }; let join = sign_in::decide_join(&facts, chrono::Utc::now()); let member = match &join { Join::Member { team_id, user_id } => facts.records.iter().find(|i| { @@ -979,50 +1108,31 @@ pub async fn forgot_password( Forgot::ResetCode => { if let Err(e) = send_reset_code(&state, &email).await { warn!(error = %e, "Reset code not sent"); - let limited = e.contains("LimitExceeded") || e.contains("TooManyRequests"); - return Err(( - if limited { - StatusCode::TOO_MANY_REQUESTS - } else { - StatusCode::BAD_GATEWAY - }, - Json(serde_json::json!({ - "error": if limited { - "Too many reset codes for this email just now. Try again in a few minutes." - } else { - "We couldn't send a reset code just now. Try again shortly." - } - })), - )); } } Forgot::SetUp { team_id, .. } => { let Some(item) = member.as_ref() else { - return Err(unavailable("Forgot password: no record", String::new())); + return case; + }; + let team = match super::users::team_items(&state, team_id).await { + Ok(t) => t, + Err(e) => { + error!(%team_id, error = ?e, "Forgot password: team not read"); + return case; + } }; - let team = super::users::team_items(&state, team_id) - .await - .map_err(|_| unavailable("Forgot password: team not read", String::new()))?; let by = super::users::standing_admin(&team).unwrap_or_else(|| email.clone()); match super::users::deliver_setup_email(&state, team_id, item, &by).await { Ok(_) => info!(%team_id, "Set-up email sent from the sign-in page"), - Err(super::users::SetupSend::Wait(_)) => { - return Ok(Json(forgot_reply(&Forgot::SetUpRecent, &email))); - } + Err(super::users::SetupSend::Wait(_)) => return Forgot::SetUpRecent, Err(super::users::SetupSend::Failed(e)) => { - return Err(( - StatusCode::BAD_GATEWAY, - Json(serde_json::json!({ - "error": format!("You don't have an account here yet, and we couldn't send you a set-up link just now ({e}). Ask an admin to send it from Settings, Members and roles.") - })), - )); + warn!(%team_id, error = %e, "Set-up email not sent from the sign-in page"); } } } _ => {} } - info!(outcome = ?std::mem::discriminant(&case), sends = case.sends(), "Forgot password"); - Ok(Json(forgot_reply(&case, &email))) + case } /// Asks Cognito to send `email` a reset code. @@ -3291,12 +3401,6 @@ mod forgot_tests { } ); assert!(case.sends()); - let r = forgot_reply(&case, "jo@acme.dev"); - assert_eq!(r["status"], "setup_sent"); - assert_eq!( - r["message"], - "You don't have an account here yet. We sent you a set-up link instead." - ); } #[test] @@ -3316,12 +3420,6 @@ mod forgot_tests { assert_eq!(case, Forgot::NoAccount, "{join:?}"); assert!(!case.sends()); } - let r = forgot_reply(&Forgot::NoAccount, "who@else.dev"); - assert_eq!(r["status"], "no_account"); - assert_eq!( - r["message"], - "There's no account for who@else.dev on this CoderHelm. Ask an admin to add you." - ); } #[test] @@ -3330,9 +3428,6 @@ mod forgot_tests { for join in [joined(), Join::Refused, Join::Invite] { assert_eq!(plan_forgot(true, &join, Some(&m), now()), Forgot::ResetCode); } - let r = forgot_reply(&Forgot::ResetCode, "jo@acme.dev"); - assert_eq!(r["status"], "code_sent"); - assert_eq!(r["message"], "We sent a reset code to jo@acme.dev."); } #[test] @@ -3351,28 +3446,100 @@ mod forgot_tests { ); } - #[test] - fn each_case_reads_differently() { - let cases = [ - Forgot::ResetCode, - Forgot::SetUp { - team_id: "t".into(), - sk: "s".into(), - }, - Forgot::SetUpRecent, - Forgot::Invited, - Forgot::NoAccount, - ]; - let statuses: std::collections::HashSet = cases + /// Status, headers and body bytes of an answer, and how long it took. + async fn taken( + started: tokio::time::Instant, + r: Response, + ) -> ( + StatusCode, + Vec<(String, Vec)>, + Vec, + std::time::Duration, + ) { + let took = started.elapsed(); + let status = r.status(); + let mut headers: Vec<(String, Vec)> = r + .headers() .iter() - .map(|c| { - forgot_reply(c, "a@acme.dev")["status"] - .as_str() - .unwrap() - .to_string() - }) + .map(|(k, v)| (k.to_string(), v.as_bytes().to_vec())) .collect(); - assert_eq!(statuses.len(), cases.len()); + headers.sort(); + let body = axum::body::to_bytes(r.into_body(), usize::MAX) + .await + .unwrap() + .to_vec(); + (status, headers, body, took) + } + + /// Has an account, on the team without one, invited, not on the install: each case's work + /// takes its own time (the slowest past the floor), and every answer is the same bytes, + /// no sooner than the floor. + #[tokio::test] + async fn every_case_gets_the_same_answer_at_the_same_time() { + let floor = std::time::Duration::from_millis(150); + let cases = [ + (Forgot::ResetCode, 60), + ( + Forgot::SetUp { + team_id: "TEAM#acme".into(), + sk: "USER#u1".into(), + }, + 400, + ), + (Forgot::Invited, 5), + (Forgot::NoAccount, 0), + ]; + let mut answers = Vec::new(); + for (case, ms) in cases { + let started = tokio::time::Instant::now(); + let work = async move { + tokio::time::sleep(std::time::Duration::from_millis(ms)).await; + case + }; + answers.push(taken(started, answer_evenly(started, floor, work).await).await); + } + let (status, headers, body, _) = &answers[0]; + assert_eq!(*status, StatusCode::OK); + assert_eq!( + serde_json::from_slice::(body).unwrap(), + serde_json::json!({ "status": "sent", "message": FORGOT_SENT }) + ); + for (s, h, b, took) in &answers { + assert_eq!(s, status); + assert_eq!(h, headers); + assert_eq!(b, body); + assert!(*took >= floor, "{took:?}"); + // The slow send carries on after the answer instead of holding it back. + assert!( + *took < floor + std::time::Duration::from_millis(150), + "{took:?}" + ); + } + } + + #[test] + fn the_answer_names_no_email_and_no_case() { + let s = FORGOT_SENT.to_lowercase(); + for word in ["account here", "invite", "no account", "@"] { + assert!(!s.contains(word), "{word}"); + } + } + + #[tokio::test] + async fn self_hosted_sign_up_answers_alike_and_on_time() { + let started = tokio::time::Instant::now(); + let (status, headers, body, took) = taken(started, one_signup_answer(started).await).await; + assert!(took >= FORGOT_ANSWER_AFTER); + let (s2, h2, b2, _) = taken(started, signup_answer()).await; + assert_eq!((status, headers, body), (s2, h2, b2)); + } + + #[test] + fn passwords_are_checked_before_anyone_is_looked_up() { + assert!(password_fits("Acme2026x")); + for p in ["acme2026x", "ACME2026X", "AcmeAcmeX", "Ac2"] { + assert!(!password_fits(p), "{p}"); + } } #[test] diff --git a/services/gateway/src/routes/sign_in.rs b/services/gateway/src/routes/sign_in.rs index 7e43b9193..6f05631e3 100644 --- a/services/gateway/src/routes/sign_in.rs +++ b/services/gateway/src/routes/sign_in.rs @@ -3753,7 +3753,8 @@ mod tests { let auth = include_str!("auth.rs"); // Email sign-up: refused before Cognito is asked. let signup = body(auth, "signup"); - assert!(before(signup, "sign_in::may_sign_up", ".sign_up()")); + assert!(before(signup, "sign_in::may_sign_up", "cognito_sign_up(")); + assert!(body(auth, "cognito_sign_up").contains(".sign_up()")); assert!(signup.contains("UserLambdaValidationException")); // Email confirmation makes no personal team. let verify = body(auth, "verify_email");