diff --git a/crates/common/src/hold.rs b/crates/common/src/hold.rs index 3d910ea9..558628ad 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); @@ -285,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 e23bbb56..3da5472a 100644 --- a/tests/src/system/legal_hold.rs +++ b/tests/src/system/legal_hold.rs @@ -874,6 +874,107 @@ 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}" + ); + + // 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(