From 27e6b0a57b057c08d0a7ae27ec1f1381b826f0b8 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 11 Nov 2026 11:35:38 -0700 Subject: [PATCH] Freeze a deleted-but-kept account's archived mail under a legal hold settle_archive's item loop collapsed any gone account to keeping=None, so an archived item belonging to an account that was deleted while undelete still retains it kept its finite deadline no matter what holds covered it. The second loop froze the account's own kept_until but never the item deadlines, so the scheduled purge destroyed individually-deleted messages of a departed custodian while a hold was asserting over them (LH-8, LH-6). A gone-but-kept account now resolves its Keeping from the holds covering the domain and tenant its kept record carries, so its archived items freeze and release on the same path a live account's do. A gone-and-not- kept account still gets None (release only). Covered by legal_hold_tests: a late, domain-scoped hold over a deleted custodian now freezes the message they had deleted before leaving. --- crates/common/src/hold.rs | 20 +++++++- tests/src/system/legal_hold.rs | 88 ++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/crates/common/src/hold.rs b/crates/common/src/hold.rs index 3d910ea9..6c8fcfad 100644 --- a/crates/common/src/hold.rs +++ b/crates/common/src/hold.rs @@ -185,18 +185,34 @@ impl Server { let now = now(); let mut keeping: AHashMap> = AHashMap::new(); let mut settled = Settled::default(); + // LH-8: the accounts undelete still keeps after deletion. A gone account + // can't be looked up to find its domain or tenant, so its kept record + // carries them; its archived items have to follow the holds all the same. + let kept_items_retention = inbuxa_features::undelete::settings::retention(registry) + .await? + .items; + let kept_accounts: AHashMap = undelete_data::kept_accounts(data) + .await? + .into_iter() + .collect(); for id in records::all(data, registry).await? { let Some(item) = registry.object::(id).await? else { continue; }; let account_id = item.account_id().document_id(); if !keeping.contains_key(&account_id) { - // An account that's gone can't be placed in a domain or - // tenant any more: None, and its items are left as they are let known = self.account(account_id).await.is_ok(); let value = if known { Some(self.keeping(account_id).await?) + } else if let Some(kept) = kept_accounts.get(&account_id) { + // Gone but kept by undelete: follow the holds through the + // domain and tenant the kept record carries, so its items + // freeze and release just like a live account's (LH-8). + let ranges = hold::covering(data, &kept_member(account_id, kept)).await?; + Some(Keeping::new(kept_items_retention, &ranges)) } else { + // Gone and not kept: leave items as they are. The None + // branch below releases them once no hold is active. None }; keeping.insert(account_id, value); diff --git a/tests/src/system/legal_hold.rs b/tests/src/system/legal_hold.rs index e23bbb56..36c511b4 100644 --- a/tests/src/system/legal_hold.rs +++ b/tests/src/system/legal_hold.rs @@ -874,6 +874,94 @@ pub async fn test(test: &mut TestServer) { .is_some_and(|l| l.iter().any(|r| r["target"]["name"] == "held@example.com")), "an account isn't named by its address: {accounts}" ); + + // LH-8, LH-6: a hold placed AFTER an account was deleted must still freeze + // the account's archived mail. The account is gone, so settle_archive finds + // no live keeping for it; its already-archived items have to follow the hold + // all the same, or the next purge destroys mail a hold is asserting over. + admin + .registry_update_setting( + DataRetention { + archive_deleted_items_for: Some(registry::schema::prelude::Duration( + std::time::Duration::from_secs(30 * 86_400), + )), + archive_deleted_accounts_for: Some(registry::schema::prelude::Duration( + std::time::Duration::from_secs(30 * 86_400), + )), + ..Default::default() + }, + &[ + Property::ArchiveDeletedItemsFor, + Property::ArchiveDeletedAccountsFor, + ], + ) + .await; + // A dedicated domain with no other account, so a domain-scoped hold covers + // exactly this one custodian and its held-item count can't be inflated by + // live accounts that merely share a domain. + let departed = admin + .create_user_account( + "departed@custodian.example.net", + "departed-secret-7700", + "Departed", + &[], + vec![], + ) + .await; + let departed_id = departed.id(); + let departed_client = departed.jmap_client().await; + // A message the custodian deleted before they left: undelete archives it on + // a finite deadline. This is the already-archived mail a late hold has to + // reach once the account itself is gone. + let doomed = import(&departed_client, "Mail of a departed custodian", None).await; + departed_client.email_destroy(&doomed).await.unwrap(); + test.wait_for_tasks_skip_not_due().await; + + // The account is removed while undelete keeps it (and its archived mail). + admin + .registry_destroy(ObjectType::Account, [departed_id]) + .await + .assert_destroyed(&[departed_id]); + test.wait_for_tasks_skip_not_due().await; + + // The hold arrives after the deletion. It can't name the gone account by id + // (that scope is refused once the account is missing), so it reaches the + // custodian through the custodian.example.net domain its kept record still + // carries. + let custodian_dom = admin.find_or_create_domain("custodian.example.net").await; + let response = admin + .hold_set(json!({"reason": "Matter 9100, post-departure", "create": { + "late": {"name": "Matter 9100", "scope": {"domains": [custodian_dom.to_string()]}}}})) + .await; + let late = response["created"]["late"]["id"] + .as_str() + .unwrap_or_else(|| panic!("the post-departure hold wasn't placed: {response}")) + .to_string(); + test.wait_for_tasks_skip_not_due().await; + + let (_, response) = admin + .hold_call( + "inbuxa:LegalHold/get", + json!({"ids": [late], "properties": ["accountsCovered", "itemsHeld", "sizeHeld"]}), + ) + .await; + let summary = &response["list"][0]; + // Setup sanity: the hold reaches the gone custodian through its domain. + assert_eq!( + summary["accountsCovered"], 1, + "LH-8: the post-departure hold doesn't cover the deleted custodian: {response}" + ); + // The bug: a deleted account's archived mail is left on its finite deadline, + // so the hold shows the account covered but freezes none of its mail; the + // scheduled purge then destroys it while the hold is active. + assert!( + summary["itemsHeld"].as_u64().is_some_and(|n| n >= 1), + "LH-8/LH-6: a hold placed after the account was deleted didn't freeze its archived mail: {response}" + ); + assert!( + summary["sizeHeld"].as_u64().is_some_and(|s| s > 0), + "LH-8: the held size is zero for a deleted-then-held account: {response}" + ); } async fn import( -- 2.54.0