From 4a22ec11332841d39d21db8760565173923cbb46 Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Mon, 16 Nov 2026 11:17:40 -0500 Subject: [PATCH] GitHub sign-in lands on the person's one record; copied invites get sent from the install - A GitHub-only record (no email) next to the same person's record by email is folded into it at their GitHub sign-in: the higher built-in role stays and the GitHub-only record is removed. The GitHub email is never written onto a second record on the same team. - The GitHub id lookup reads every record instead of one at random, so an install's own record (or, hosted, the team someone was added to) wins. - An invite dated before the organization was made on the install and never sent from it is offered by forgot-password and the bulk set-up email; sending it restarts its 30 days. - A refused password sign-in is logged by cause; no sign-in here is info. Depends on #727. --- services/gateway/src/routes/auth.rs | 581 ++++++++++++++++++++++--- services/gateway/src/routes/sign_in.rs | 7 + services/gateway/src/routes/users.rs | 123 +++++- 3 files changed, 634 insertions(+), 77 deletions(-) diff --git a/services/gateway/src/routes/auth.rs b/services/gateway/src/routes/auth.rs index 9a46042f9..0a2058dfd 100644 --- a/services/gateway/src/routes/auth.rs +++ b/services/gateway/src/routes/auth.rs @@ -547,20 +547,78 @@ pub async fn login_email( })) .into_response()); } - return Err(if msg.contains("NotAuthorizedException") { - StatusCode::UNAUTHORIZED - } else if msg.contains("UserNotConfirmedException") { - StatusCode::FORBIDDEN - } else { - error!("Cognito auth failed: {e}"); - StatusCode::UNAUTHORIZED - }); + let refused = login_refusal(&msg); + match refused { + LoginRefusal::WrongPassword | LoginRefusal::Unconfirmed => {} + // Expected for a member copied from another CoderHelm who + // hasn't set up their sign-in here: their answer is the + // same as a wrong password, and forgot-password sends the + // set-up email. + LoginRefusal::NoSignIn => info!("Password sign-in: no sign-in here for that email"), + LoginRefusal::Other(code) => error!(code, "Cognito auth failed: {e}"), + } + return Err(refused.status()); } }; finish_password_login(&state, &email, &auth_result, false).await } +/// Why Cognito refused a password sign-in, from its error (Debug form). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum LoginRefusal { + WrongPassword, + Unconfirmed, + /// No sign-in on this install for that email (`UserNotFoundException`, + /// also what a user-migration trigger answers when it can't carry one + /// over). + NoSignIn, + /// Anything else, by Cognito's error name: logged as an error with it. + Other(&'static str), +} + +/// The Cognito error names a password sign-in can meet, for the log. +const COGNITO_ERRORS: &[&str] = &[ + "UserLambdaValidationException", + "TooManyRequestsException", + "PasswordResetRequiredException", + "InvalidParameterException", + "InvalidUserPoolConfigurationException", + "ResourceNotFoundException", + "InternalErrorException", + "UnexpectedLambdaException", + "InvalidLambdaResponseException", +]; + +pub fn login_refusal(err: &str) -> LoginRefusal { + if err.contains("NotAuthorizedException") { + LoginRefusal::WrongPassword + } else if err.contains("UserNotConfirmedException") { + LoginRefusal::Unconfirmed + } else if err.contains("UserNotFoundException") { + LoginRefusal::NoSignIn + } else { + LoginRefusal::Other( + COGNITO_ERRORS + .iter() + .find(|c| err.contains(*c)) + .copied() + .unwrap_or("unknown"), + ) + } +} + +impl LoginRefusal { + /// The answer: the same 401 for a wrong password and no sign-in here, + /// so it can't be used to learn who has one. + pub fn status(self) -> StatusCode { + match self { + LoginRefusal::Unconfirmed => StatusCode::FORBIDDEN, + _ => StatusCode::UNAUTHORIZED, + } + } +} + /// The answer to a password sign-in that Cognito accepted: its own /// authenticator challenge, or the session. async fn finish_password_login( @@ -991,7 +1049,8 @@ impl Forgot { } /// The one place the case is decided. Pure: `in_pool` is whether the user pool has the email, -/// `join` the install's word on it, `member` that member's record when `join` is one. +/// `join` the install's word on it, `member` that member's record when `join` is one, or for +/// an invite, the invite when this install never sent it ([`super::users::copied_invite`]). pub fn plan_forgot( in_pool: bool, join: &Join, @@ -1001,21 +1060,27 @@ pub fn plan_forgot( if in_pool { return Forgot::ResetCode; } + let g = |item: &HashMap, k: &str| { + item.get(k) + .and_then(|v| v.as_s().ok()) + .cloned() + .unwrap_or_default() + }; + let set_up = |item: &HashMap, team_id: String, sk: String| { + if super::users::setup_email_due(item, now) { + Forgot::SetUp { team_id, sk } + } else { + Forgot::SetUpRecent + } + }; match (join, member) { - (Join::Member { team_id, user_id }, Some(item)) - if item - .get("email") - .and_then(|v| v.as_s().ok()) - .is_some_and(|e| !e.is_empty()) => - { - if super::users::setup_email_due(item, now) { - Forgot::SetUp { - team_id: team_id.clone(), - sk: user_id.clone(), - } - } else { - Forgot::SetUpRecent - } + (Join::Member { team_id, user_id }, Some(item)) if !g(item, "email").is_empty() => { + set_up(item, team_id.clone(), user_id.clone()) + } + // Copied from the hosted CoderHelm: their invite email links there, + // which no longer lets them in, so it goes again from here. + (Join::Invite, Some(item)) if !g(item, "email").is_empty() => { + set_up(item, g(item, "pk"), g(item, "sk")) } (Join::Invite, _) => Forgot::Invited, _ => Forgot::NoAccount, @@ -1145,6 +1210,16 @@ async fn forgot_work(state: Arc, email: String) -> Forgot { let g = |k: &str| i.get(k).and_then(|v| v.as_s().ok()).map(String::as_str); g("pk") == Some(team_id.as_str()) && g("sk") == Some(user_id.as_str()) }), + Join::Invite => { + let org = facts.org.as_ref(); + facts.records.iter().find(|i| { + org.is_some_and(|o| { + i.get("pk").and_then(|v| v.as_s().ok()) == Some(&o.team_id) + && super::users::copied_invite(i, o.created_at.as_deref()) + && !super::users::invite_expired(i, chrono::Utc::now()) + }) + }) + } _ => None, } .cloned(); @@ -2151,55 +2226,44 @@ pub(crate) async fn github_sign_in_with_token( let email = email.as_str(); let normalized_email = normalize_email(email); - // 1. Try to find an existing user by GitHub ID (GSI1 across all teams) - let existing_by_github = state - .dynamo - .query() - .table_name(&state.config.users_table_name) - .index_name("gsi1") - .key_condition_expression("gsi1pk = :gpk") - .expression_attribute_values(":gpk", attr_s(&format!("GHUSER#{github_id}"))) - .limit(1) - .send() + // The person's records by GitHub id (every team: GSI1) and by the GitHub + // account's email (GSI2). All of them: one picked at random was how a + // GitHub sign-in landed on the wrong team. + let by_github = query_all(state, "gsi1", "gsi1pk", &format!("GHUSER#{github_id}")).await; + let by_email = if normalized_email.is_empty() { + Vec::new() + } else { + query_all( + state, + "gsi2", + "gsi2pk", + &format!("EMAIL#{normalized_email}"), + ) .await - .ok() - .and_then(|r| r.items().first().cloned()); - - // 2. If not found by GitHub ID, try by email - let existing_by_email = if existing_by_github.is_none() && !normalized_email.is_empty() { - state - .dynamo - .query() - .table_name(&state.config.users_table_name) - .index_name("gsi2") - .key_condition_expression("gsi2pk = :email") - .expression_attribute_values(":email", attr_s(&format!("EMAIL#{normalized_email}"))) - .send() + }; + // A self-hosted install: only the organization's own records, or a join + // its rules allow; never a new team. + let org_team = if sign_in::applies() { + sign_in::ensure_org(state) .await - .ok() - .and_then(|r| { - // Prefer a real team the user joined over their personal team - // (github_id serves as the own-id for the personal-team check). - let items = r.items().to_vec(); - select_session_team(&items, &github_id.to_string()).cloned() - }) + .map_err(|e| SignInFailure::new(P, Reason::Server, "join", e))? + .map(|o| o.team_id) } else { None }; + let landing = github_landing( + &by_github, + &by_email, + &github_id.to_string(), + sign_in::applies(), + org_team.as_deref(), + ); + // Their email goes on the record only when no other record of theirs + // on that team has it (else the team would hold them twice by email). + let email_for_record = if landing.email_taken { "" } else { email }; - let existing_user = existing_by_github.or(existing_by_email); - - // A self-hosted install: only the organization's own records, or a join - // its rules allow; never a new team. let existing_user = if sign_in::applies() { - let org_team = sign_in::ensure_org(state) - .await - .map_err(|e| SignInFailure::new(P, Reason::Server, "join", e))? - .map(|o| o.team_id); - let in_org = existing_user.filter(|i| { - org_team.is_some() && i.get("pk").and_then(|v| v.as_s().ok()) == org_team.as_ref() - }); - let landed = match in_org { + let landed = match landing.record.clone() { Some(rec) => Some(rec), None if !email.is_empty() => selfhost_landing( state, @@ -2218,7 +2282,7 @@ pub(crate) async fn github_sign_in_with_token( }; Some(rec) } else { - existing_user + landing.record.clone() }; let has_existing_record = existing_user.is_some(); @@ -2335,7 +2399,7 @@ pub(crate) async fn github_sign_in_with_token( &GithubProfile { id: github_id, login: github_login, - email, + email: email_for_record, avatar_url: user["avatar_url"].as_str().unwrap_or(""), name: user["name"].as_str().unwrap_or(""), }, @@ -2357,6 +2421,14 @@ pub(crate) async fn github_sign_in_with_token( { error!(github_login, team_id = %team_id, "Failed to upsert GitHub user record: {e}"); } + // The same person's GitHub-only record, beside the record by email this + // sign-in landed on: its higher role kept, then removed. + let role = match &landing.fold { + Some(dup) if existing_user.is_some() => { + fold_github_record(state, &team_id, &user_id, &role, dup).await + } + _ => role, + }; // The session carries the member's own email when GitHub has none. let email = existing_user .as_ref() @@ -2567,6 +2639,210 @@ struct RecordUpdate { values: HashMap, } +/// Every record on a users-table index under one key (paged). A failed read +/// is logged and reads as none. +async fn query_all( + state: &AppState, + index: &str, + key: &str, + value: &str, +) -> Vec> { + let mut out = Vec::new(); + let mut start = None; + for _ in 0..20 { + let r = state + .dynamo + .query() + .table_name(&state.config.users_table_name) + .index_name(index) + .key_condition_expression("#k = :v") + .expression_attribute_names("#k", key) + .expression_attribute_values(":v", attr_s(value)) + .set_exclusive_start_key(start.take()) + .send() + .await; + match r { + Ok(r) => { + out.extend(r.items().iter().cloned()); + match r.last_evaluated_key() { + Some(k) if !k.is_empty() => start = Some(k.clone()), + _ => break, + } + } + Err(e) => { + warn!(index, error = %e, "Member records not read for a GitHub sign-in"); + break; + } + } + } + out +} + +/// Where a GitHub sign-in lands ([`github_landing`]). +#[derive(Debug, Clone, PartialEq)] +pub(crate) struct GithubLanding { + /// The record to sign in to, if they have one. + pub record: Option>, + /// Another record of the same person on that team, with only their + /// GitHub id and no email: folded into `record` and removed. + pub fold: Option>, + /// Another record on `record`'s team already has their email, so the + /// GitHub account's email isn't written onto `record`. + pub email_taken: bool, +} + +/// Which record a GitHub sign-in lands on, from the person's records by +/// GitHub id (`by_github`, every team) and by the GitHub account's email +/// (`by_email`). Pure. +/// +/// - Self-hosted (`org_team`): only the organization's records. A record +/// with this GitHub id and an email wins. A GitHub-only record (no email, +/// e.g. copied from the hosted CoderHelm) next to a record for their email +/// that has no other GitHub account: the email record wins and the +/// GitHub-only one is folded in, so the person isn't on the team twice. +/// - Hosted: their records by GitHub id, else by email, preferring a team +/// they were added to over their own ([`select_session_team`]). +pub(crate) fn github_landing( + by_github: &[HashMap], + by_email: &[HashMap], + github_id: &str, + self_hosted: bool, + org_team: Option<&str>, +) -> GithubLanding { + let g = |i: &HashMap, k: &str| { + i.get(k) + .and_then(|v| v.as_s().ok()) + .cloned() + .unwrap_or_default() + }; + let member = |i: &&HashMap| { + g(i, "sk").starts_with("USER#") && g(i, "status") != "invited" + }; + let gh_of = |i: &HashMap| match i.get("github_id") { + Some(AttributeValue::N(n)) => n.clone(), + Some(AttributeValue::S(v)) => v.clone(), + _ => String::new(), + }; + // Another member record on `rec`'s team with this email. + let email_beside = |rec: &HashMap| { + by_email + .iter() + .filter(member) + .find(|e| g(e, "pk") == g(rec, "pk") && g(e, "sk") != g(rec, "sk")) + .cloned() + }; + let landing = |record: Option>| { + let email_taken = record + .as_ref() + .is_some_and(|r| g(r, "email").is_empty() && email_beside(r).is_some()); + GithubLanding { + record, + fold: None, + email_taken, + } + }; + + if !self_hosted { + let pick = select_session_team(by_github, github_id) + .or_else(|| select_session_team(by_email, github_id)) + .cloned(); + return landing(pick); + } + let Some(org) = org_team else { + return landing(None); + }; + let in_org = |i: &&HashMap| g(i, "pk") == org; + let mine: Vec<&HashMap> = + by_github.iter().filter(member).filter(in_org).collect(); + if let Some(rec) = mine.iter().find(|i| !g(i, "email").is_empty()) { + return landing(Some((*rec).clone())); + } + if let Some(dup) = mine.first() { + return match email_beside(dup) { + Some(rec) if gh_of(&rec).is_empty() || gh_of(&rec) == github_id => GithubLanding { + record: Some(rec), + fold: Some((*dup).clone()), + email_taken: false, + }, + _ => landing(Some((*dup).clone())), + }; + } + landing(by_email.iter().filter(member).find(|i| in_org(i)).cloned()) +} + +/// Built-in roles from least to most (a custom role ranks as a member). +fn role_rank(role: &str) -> u8 { + match role { + "owner" => 3, + "admin" => 2, + _ => 1, + } +} + +/// The role a folded pair keeps: the higher built-in one, else the landing +/// record's own (a custom role there stays). +pub(crate) fn folded_role<'a>(landing: &'a str, dup: &'a str) -> &'a str { + if role_rank(dup) > role_rank(landing) { + dup + } else { + landing + } +} + +/// Folds a GitHub-only record into the record this sign-in landed on (see +/// [`github_landing`]): the higher role is kept on the landing record, then +/// the GitHub-only record is removed (only while it still has no email). +/// Returns the role the session carries. +async fn fold_github_record( + state: &AppState, + team_id: &str, + user_id: &str, + role: &str, + dup: &HashMap, +) -> String { + let g = |k: &str| { + dup.get(k) + .and_then(|v| v.as_s().ok()) + .map_or("", |v| v.as_str()) + }; + let kept = folded_role(role, g("role")).to_string(); + if kept != role { + if let Err(e) = state + .dynamo + .update_item() + .table_name(&state.config.users_table_name) + .key("pk", attr_s(team_id)) + .key("sk", attr_s(user_id)) + .update_expression("SET #r = :r") + .condition_expression("attribute_exists(pk)") + .expression_attribute_names("#r", "role") + .expression_attribute_values(":r", attr_s(&kept)) + .send() + .await + { + warn!(%team_id, error = %e, "GitHub-only record not folded: role not kept"); + return role.to_string(); + } + } + match state + .dynamo + .delete_item() + .table_name(&state.config.users_table_name) + .key("pk", attr_s(g("pk"))) + .key("sk", attr_s(g("sk"))) + .condition_expression( + "attribute_exists(pk) AND (attribute_not_exists(email) OR email = :none)", + ) + .expression_attribute_values(":none", attr_s("")) + .send() + .await + { + Ok(_) => info!(%team_id, "GitHub-only record folded into the member's record"), + Err(e) => warn!(%team_id, error = %e, "GitHub-only record not removed"), + } + kept +} + /// The write a GitHub sign-in makes to a member record. It always refreshes the /// GitHub id/login, GSI1 keys, avatar and timestamps. On an existing record it /// never touches role, status, invited_by, or an email/name already set: it @@ -3962,3 +4238,174 @@ mod unconfirmed_tests { assert_eq!(sign_up_again(PoolUser::Account), SignUpAgain::Nothing); } } + +#[cfg(test)] +mod landing_tests { + use super::*; + + type Item = HashMap; + + fn rec(pk: &str, sk: &str, email: &str, role: &str, github_id: Option<&str>) -> Item { + let mut i = HashMap::new(); + i.insert("pk".to_string(), attr_s(pk)); + i.insert("sk".to_string(), attr_s(sk)); + i.insert("email".to_string(), attr_s(email)); + i.insert("role".to_string(), attr_s(role)); + i.insert("status".to_string(), attr_s("active")); + if let Some(g) = github_id { + i.insert("github_id".to_string(), AttributeValue::N(g.to_string())); + } + i + } + + fn sk(i: &Option) -> String { + i.as_ref() + .and_then(|i| i.get("sk")) + .and_then(|v| v.as_s().ok()) + .cloned() + .unwrap_or_default() + } + + const ORG: &str = "TEAM#org"; + + #[test] + fn a_github_only_copy_beside_their_email_record_is_folded_into_it() { + // The failure: a person copied from the hosted CoderHelm with two + // records on the team, one by email (their sign-in, role and history) + // and one with only their GitHub id. A GitHub sign-in landed on the + // GitHub-only one, and wrote their email onto it, so the team held + // them twice by email and a password sign-in could land on either. + let gh_only = rec(ORG, "USER#4242", "", "member", Some("4242")); + let by_email = rec(ORG, "USER#hosted-sub", "pat@acme.test", "admin", None); + let l = github_landing( + std::slice::from_ref(&gh_only), + std::slice::from_ref(&by_email), + "4242", + true, + Some(ORG), + ); + assert_eq!(sk(&l.record), "USER#hosted-sub"); + assert_eq!(sk(&l.fold), "USER#4242"); + assert!(!l.email_taken); + } + + #[test] + fn a_github_only_record_alone_is_where_they_land() { + let gh_only = rec(ORG, "USER#4242", "", "member", Some("4242")); + let l = github_landing(std::slice::from_ref(&gh_only), &[], "4242", true, Some(ORG)); + assert_eq!(sk(&l.record), "USER#4242"); + assert_eq!(l.fold, None); + assert!(!l.email_taken); + } + + #[test] + fn an_email_record_with_another_github_account_is_not_folded_and_its_email_not_copied() { + let gh_only = rec(ORG, "USER#4242", "", "member", Some("4242")); + let by_email = rec( + ORG, + "USER#hosted-sub", + "pat@acme.test", + "admin", + Some("777"), + ); + let l = github_landing( + std::slice::from_ref(&gh_only), + std::slice::from_ref(&by_email), + "4242", + true, + Some(ORG), + ); + assert_eq!(sk(&l.record), "USER#4242"); + assert_eq!(l.fold, None); + // Their email stays on the record that has it: never two by email. + assert!(l.email_taken); + } + + #[test] + fn a_github_record_on_another_team_never_hides_the_organization_one() { + // The failure: the GitHub id lookup took one record at random + // (limit 1). On an install with a record left on another team, a + // GitHub-only member could be sent away as not invited. + let elsewhere = rec("TEAM#old", "USER#4242", "", "owner", Some("4242")); + let mine = rec(ORG, "USER#4242", "", "member", Some("4242")); + let l = github_landing(&[elsewhere, mine], &[], "4242", true, Some(ORG)); + assert_eq!( + l.record + .as_ref() + .and_then(|i| i.get("pk")) + .and_then(|v| v.as_s().ok()) + .map(String::as_str), + Some(ORG) + ); + } + + #[test] + fn hosted_github_sign_in_prefers_the_team_they_were_added_to() { + let own = rec("TEAM#mine", "USER#4242", "", "owner", Some("4242")); + let added = rec("TEAM#theirs", "USER#4242", "", "member", Some("4242")); + let l = github_landing(&[own, added], &[], "4242", false, None); + assert_eq!( + l.record + .as_ref() + .and_then(|i| i.get("pk")) + .and_then(|v| v.as_s().ok()) + .map(String::as_str), + Some("TEAM#theirs") + ); + assert_eq!(l.fold, None); + } + + #[test] + fn a_fold_keeps_the_higher_built_in_role() { + assert_eq!(folded_role("member", "admin"), "admin"); + assert_eq!(folded_role("admin", "member"), "admin"); + assert_eq!(folded_role("owner", "admin"), "owner"); + assert_eq!(folded_role("role_custom", "member"), "role_custom"); + } + + #[test] + fn a_password_sign_in_with_no_sign_in_here_is_named_in_the_log() { + // The failure: every refusal but a wrong password logged "service + // error", so 21 failed sign-ins in two weeks had no cause. + let r = login_refusal( + "ServiceError { source: UserNotFoundException { message: \"User does not exist.\" } }", + ); + assert_eq!(r, LoginRefusal::NoSignIn); + assert_eq!(r.status(), StatusCode::UNAUTHORIZED); + assert_eq!( + login_refusal("NotAuthorizedException").status(), + StatusCode::UNAUTHORIZED + ); + assert_eq!( + login_refusal("UserNotConfirmedException").status(), + StatusCode::FORBIDDEN + ); + assert_eq!( + login_refusal("UserLambdaValidationException: PreSignUp failed"), + LoginRefusal::Other("UserLambdaValidationException") + ); + assert_eq!(login_refusal("timeout"), LoginRefusal::Other("unknown")); + } + + #[test] + fn a_copied_invite_gets_its_invite_again_from_forgot_password() { + // The failure: forgot-password sent nothing to an invite ("their + // invite email has the link"), but a copied invite's only email + // links to the hosted CoderHelm, which no longer lets them in. + let mut inv = rec(ORG, "USER#invite_1", "new@acme.test", "admin", None); + inv.insert("status".to_string(), attr_s("invited")); + let now = chrono::Utc::now(); + assert_eq!( + plan_forgot(false, &Join::Invite, Some(&inv), now), + Forgot::SetUp { + team_id: ORG.into(), + sk: "USER#invite_1".into() + } + ); + // An invite made here keeps pointing at its own email. + assert_eq!( + plan_forgot(false, &Join::Invite, None, now), + Forgot::Invited + ); + } +} diff --git a/services/gateway/src/routes/sign_in.rs b/services/gateway/src/routes/sign_in.rs index 9f77a9b48..a0cace217 100644 --- a/services/gateway/src/routes/sign_in.rs +++ b/services/gateway/src/routes/sign_in.rs @@ -225,6 +225,10 @@ pub struct Org { pub require_2fa: bool, /// When the rule was last turned on. pub require_2fa_since: Option, + /// When the organization was made on this install. Anything dated + /// before it (an invite copied from the hosted CoderHelm) was never + /// sent from here. + pub created_at: Option, } impl Org { @@ -254,6 +258,9 @@ impl Org { require_2fa_since: Some(s(item, "require_2fa_since")) .filter(|v| !v.is_empty()) .map(str::to_string), + created_at: Some(s(item, "created_at")) + .filter(|v| !v.is_empty()) + .map(str::to_string), }) } } diff --git a/services/gateway/src/routes/users.rs b/services/gateway/src/routes/users.rs index 23a66d805..054605d8e 100644 --- a/services/gateway/src/routes/users.rs +++ b/services/gateway/src/routes/users.rs @@ -9,7 +9,7 @@ use serde::Deserialize; use serde_json::{json, Value}; use std::collections::HashMap; use std::sync::Arc; -use tracing::{error, info}; +use tracing::{error, info, warn}; use crate::models::Claims; use crate::permissions::{ @@ -173,6 +173,19 @@ pub(crate) fn invite_expired(item: &Item, now: chrono::DateTime) -> invite_expires_at(item).is_some_and(|t| t <= now) } +/// An invite copied from the hosted CoderHelm that this install never sent: +/// dated before the organization was made here (`org_since`) and with no +/// set-up email since. Its only email links to the hosted CoderHelm, which +/// no longer lets them in, so it is sent from here like a set-up email. +pub(crate) fn copied_invite(item: &Item, org_since: Option<&str>) -> bool { + let Some(since) = org_since.and_then(rfc3339) else { + return false; + }; + status_of(item) == "invited" + && setup_email_log(item).is_empty() + && invite_sent_at(item).is_some_and(|t| t < since) +} + /// What the team page shows for one record. fn member_view(item: &Item, self_id: &str, now: chrono::DateTime) -> Value { let user_id = bare_id(s(item, "sk")); @@ -589,14 +602,22 @@ pub async fn list_users( .filter_map(|u| u["email"].as_str().map(normalize_email)) .filter(|e| !e.is_empty()) .collect(); + let since = org_since(&state).await; + let copied: std::collections::HashSet<&str> = items + .iter() + .filter(|i| copied_invite(i, since.as_deref())) + .map(|i| bare_id(s(i, "sk"))) + .collect(); match pool_emails(&state, emails).await { Ok(pool) => { for u in users.iter_mut() { let email = u["email"].as_str().map(normalize_email).unwrap_or_default(); + let id = u["user_id"].as_str().unwrap_or(""); u["needs_setup"] = json!(needs_setup( u["status"].as_str().unwrap_or(""), &email, - &pool + &pool, + copied.contains(id) )); } } @@ -672,13 +693,21 @@ pub(crate) async fn pool_user(state: &AppState, email: &str) -> Result, + copied_invite: bool, ) -> bool { - status != "invited" && !email.is_empty() && !pool.contains(email) + if email.is_empty() { + return false; + } + if status == "invited" { + return copied_invite; + } + !pool.contains(email) } // ── Invite user ────────────────────────────────────────────────────── @@ -1070,7 +1099,7 @@ pub(crate) async fn deliver_setup_email( .table_name(&state.config.users_table_name) .key("pk", attr_s(team_id)) .key("sk", attr_s(s(item, "sk"))) - .update_expression("SET setup_email_sent_at = :now, setup_email_log = :log") + .update_expression(setup_record_expression(status_of(item))) .condition_expression("attribute_exists(pk)") .expression_attribute_values(":now", attr_s(&stamp)) .expression_attribute_values(":log", AttributeValue::L(kept)) @@ -1080,6 +1109,28 @@ pub(crate) async fn deliver_setup_email( Ok(stamp) } +/// What sending a set-up email writes on the record. To an invite it is the +/// invite sent again, so its 30 days start now ([`INVITE_VALID_DAYS`]). +pub(crate) fn setup_record_expression(status: &str) -> &'static str { + if status == "invited" { + "SET setup_email_sent_at = :now, setup_email_log = :log, invited_at = :now" + } else { + "SET setup_email_sent_at = :now, setup_email_log = :log" + } +} + +/// When the organization was made on this install (see [`copied_invite`]). +/// Unknown (no organization, or a failed read) copies nothing. +pub(crate) async fn org_since(state: &AppState) -> Option { + match super::sign_in::load_org(state).await { + Ok(org) => org.and_then(|o| o.created_at), + Err(e) => { + warn!(error = %e, "Organization not read; copied invites not offered set-up emails"); + None + } + } +} + /// Whether a set-up email may go to this record now (an hour since the last one, five a week). pub(crate) fn setup_email_due(item: &Item, now: chrono::DateTime) -> bool { setup_email_allowed(&setup_email_log(item), now).is_ok() @@ -1159,11 +1210,13 @@ pub async fn send_setup_emails_all( let pool = pool_emails(&state, emails) .await .map_err(|e| internal("Sign-ins not checked", e))?; + let since = org_since(&state).await; let mut sent = serde_json::Map::new(); let (mut skipped, mut failed) = (0usize, 0usize); for item in &items { let email = normalize_email(s(item, "email")); - if !needs_setup(status_of(item), &email, &pool) { + let copied = copied_invite(item, since.as_deref()); + if !needs_setup(status_of(item), &email, &pool, copied) { continue; } if bulk_step(item, now) == BulkStep::SkipRecent { @@ -2141,10 +2194,60 @@ mod tests { #[test] fn needs_setup_means_a_member_with_no_sign_in_here() { let pool: std::collections::HashSet = ["in@example.com".to_string()].into(); - assert!(needs_setup("active", "out@example.com", &pool)); - assert!(!needs_setup("active", "in@example.com", &pool)); - assert!(!needs_setup("invited", "out@example.com", &pool)); - assert!(!needs_setup("active", "", &pool)); + assert!(needs_setup("active", "out@example.com", &pool, false)); + assert!(!needs_setup("active", "in@example.com", &pool, false)); + assert!(!needs_setup("invited", "out@example.com", &pool, false)); + assert!(!needs_setup("active", "", &pool, false)); + } + + fn copied(invited_at: &str, sent: &[&str]) -> Item { + let mut i = Item::new(); + i.insert("pk".into(), attr_s("TEAM#acme")); + i.insert("sk".into(), attr_s("USER#invite_1")); + i.insert("email".into(), attr_s("new@acme.test")); + i.insert("status".into(), attr_s("invited")); + i.insert("invited_at".into(), attr_s(invited_at)); + if !sent.is_empty() { + i.insert( + "setup_email_log".into(), + AttributeValue::L(sent.iter().map(|t| attr_s(t)).collect()), + ); + } + i + } + + #[test] + fn an_invite_copied_from_hosted_is_sent_from_here() { + // The failure: an invite made on the hosted CoderHelm before the team + // moved was copied over with its hosted email only. Nothing on the + // install ever sent it, and the bulk set-up email skipped invites. + let since = Some("2026-11-03T02:12:09+00:00"); + let pool = std::collections::HashSet::new(); + let hosted = copied("2026-10-27T14:33:25+00:00", &[]); + assert!(copied_invite(&hosted, since)); + assert!(needs_setup("invited", "new@acme.test", &pool, true)); + // Sent from here once: no longer copied (its own resend takes over). + assert!(!copied_invite( + &copied("2026-10-27T14:33:25+00:00", &["2026-11-16T12:00:00Z"]), + since + )); + // Made on this install: its invite email links here. + assert!(!copied_invite( + &copied("2026-11-05T10:00:00+00:00", &[]), + since + )); + // No organization date: nothing counts as copied. + assert!(!copied_invite(&hosted, None)); + // A member is never a copied invite. + let mut m = hosted.clone(); + m.insert("status".into(), attr_s("active")); + assert!(!copied_invite(&m, since)); + } + + #[test] + fn a_set_up_email_to_an_invite_restarts_its_thirty_days() { + assert!(setup_record_expression("invited").contains("invited_at = :now")); + assert!(!setup_record_expression("active").contains("invited_at")); } #[test]