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(