Freeze a deleted-but-kept account's archived mail under a legal hold #377

Merged
jcoffey-dev merged 1 commits from fix/legal-hold-deleted-account into main 2026-11-11 18:52:21 +00:00
2 changed files with 106 additions and 2 deletions

No files matched your search

+18 -2
View File
@@ -185,18 +185,34 @@ impl Server {
let now = now();
let mut keeping: AHashMap<u32, Option<Keeping>> = 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<u32, KeptAccount> = undelete_data::kept_accounts(data)
.await?
.into_iter()
.collect();
for id in records::all(data, registry).await? {
let Some(item) = registry.object::<ArchivedItem>(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);
+88
View File
@@ -874,6 +874,94 @@ pub async fn test(test: &mut TestServer) {
.is_some_and(|l| l.iter().any(|r| r["target"]["name"] == "[email protected]")),
"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(
"[email protected]",
"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(