From 9dae9b044fe873b7d50f098cdf87878610157654 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 11 Nov 2026 11:35:38 -0700 Subject: [PATCH 1/2] 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. (cherry picked from commit 27e6b0a57b057c08d0a7ae27ec1f1381b826f0b8) --- 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 From dcd32909d5c15c37e20672d6a11aeb3800268c8e Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 11 Nov 2026 12:27:05 -0700 Subject: [PATCH 2/2] Keep a deleted-but-held account's audit and journal records under the hold held_accounts() scanned only live accounts, so an account deleted while undelete still kept it -- the case a legal hold is placed for -- was absent from the set the retention purges trust. audit_purge consults it directly, so a deleted-but-held account's audit trail was purged once past the 90-day floor while the hold was active. The journal purge augmented the set at its own call site, so it was affected only when a hold covered no live account. held_accounts() now folds in the accounts undelete keeps that a hold covers, through the domain and tenant their retained record carries, so every purge that trusts it keeps a deleted-but-held account's records. The journal call site's now-redundant augmentation is removed, leaving one source of truth. Covered by legal_hold_tests: a deleted-but-held custodian is now present in held_accounts() (LH-6, AU-7). (cherry picked from commit c884bad555957e75bc6cda8171698abb6b9b36b8) --- crates/common/src/hold.rs | 10 ++++++++++ crates/services/src/task_manager/maintenance.rs | 12 ++---------- tests/src/system/legal_hold.rs | 13 +++++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/crates/common/src/hold.rs b/crates/common/src/hold.rs index 6c8fcfad..558628ad 100644 --- a/crates/common/src/hold.rs +++ b/crates/common/src/hold.rs @@ -301,6 +301,16 @@ impl Server { held.insert(account_id); } } + // LH-6, AU-7: accounts deleted while undelete keeps them can still be + // held, through the domain and tenant their kept record carries. They + // have dropped out of the live-account scan above, so fold them in -- + // otherwise a purge that keeps a held account's records (audit, journal) + // would drop a deleted-but-held account's, the exact case a hold is for. + for (account_id, kept) in undelete_data::kept_accounts(self.store()).await? { + if self.is_kept_held(account_id, &kept).await? { + held.insert(account_id); + } + } Ok(held) } diff --git a/crates/services/src/task_manager/maintenance.rs b/crates/services/src/task_manager/maintenance.rs index 844b352b..32923310 100644 --- a/crates/services/src/task_manager/maintenance.rs +++ b/crates/services/src/task_manager/maintenance.rs @@ -433,16 +433,8 @@ async fn store_maintenance( /// (deleted accounts a hold keeps included), and records how many went. async fn purge_journal(server: &Server) -> trc::Result<()> { use inbuxa_features::audit::{Action, Actor, Outcome, Record, Target}; - let mut held = server.held_accounts().await?; - if !held.is_empty() { - for (account_id, kept) in - inbuxa_features::undelete::data::kept_accounts(server.store()).await? - { - if server.is_kept_held(account_id, &kept).await? { - held.insert(account_id); - } - } - } + // held_accounts() already folds in deleted-but-kept accounts a hold covers. + let held = server.held_accounts().await?; let at = store::write::now(); let purged = inbuxa_features::journal::entries::purge(server.store(), at, |entry| { entry.accounts.iter().any(|account| held.contains(account)) diff --git a/tests/src/system/legal_hold.rs b/tests/src/system/legal_hold.rs index 36c511b4..3da5472a 100644 --- a/tests/src/system/legal_hold.rs +++ b/tests/src/system/legal_hold.rs @@ -962,6 +962,19 @@ pub async fn test(test: &mut TestServer) { summary["sizeHeld"].as_u64().is_some_and(|s| s > 0), "LH-8: the held size is zero for a deleted-then-held account: {response}" ); + + // LH-6, AU-7: a hold must also keep the records ABOUT a deleted-but-held + // account. held_accounts() -- the set the audit and journal purges keep -- + // has to include the gone custodian, or its audit trail is purged out from + // under the hold. On the buggy path the live-account scan never sees it. + assert!( + test.server + .held_accounts() + .await + .unwrap() + .contains(&departed_id.document_id()), + "LH-6/AU-7: the deleted-but-held custodian is missing from held_accounts()" + ); } async fn import( -- 2.54.0