From 3ab9efecced9b3535affd321f4890bfeefb2e0e4 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 03:49:27 -0600 Subject: [PATCH] =?UTF-8?q?fix(id):=20keep=5Fprior=20re-add=20starts=20cle?= =?UTF-8?q?an=20=E2=80=94=20pre-removal=20grants=20do=20not=20revive=20(#1?= =?UTF-8?q?69)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements the 2026-11-03 ruling on issue #169 (revival refused): in the delegation chain search, a keep_prior grant whose grantor or grantee was removed after the grant and later re-added is cut at every position that has seen the re-add. Grants issued after the re-add are untouched, and positions before the re-add judge the grant as before. Updates the KeepPrior docs and adds deterministic tests for both directions plus re-issue; the delegation property now mirrors the cut. Queue task 6887 (triage task 2016). Refs #2852. --- pkgs/id/src/groups/delegation.rs | 61 ++++++++++++++++ pkgs/id/src/groups/policy.rs | 6 +- pkgs/id/src/groups/tests.rs | 120 +++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+), 1 deletion(-) diff --git a/pkgs/id/src/groups/delegation.rs b/pkgs/id/src/groups/delegation.rs index 52dd519595..416f69b209 100644 --- a/pkgs/id/src/groups/delegation.rs +++ b/pkgs/id/src/groups/delegation.rs @@ -16,6 +16,11 @@ //! `Transfer` reissues them from a permitted successor; `KeepPrior` retains the original grantor; //! and `ReassignToGroup` makes the group the root. Strong/weak removal still decides what happens //! to grants concurrent with the removal. +//! One limit on `KeepPrior` (issue #169, ruling 2026-11-03): a re-add starts clean. A grant an +//! account made or received before its removal does not revive when the account is re-added - +//! at positions whose past contains the re-add, such a grant is no link, and authority flows +//! only from grants issued after the re-add. Positions before the re-add are judged as before: +//! there the grant still stands, as a fact about the past. use std::collections::{BTreeMap, BTreeSet}; @@ -70,6 +75,15 @@ pub(super) const fn removed_member(event: &Event) -> Option { } } +/// The account a member-add event adds, if the event is one. A re-add is an add whose causal +/// past contains a removal of the same account. +pub(super) const fn added_member(event: &Event) -> Option { + match &event.body { + Body::Member(MemberOp::Add { member, .. }) => Some(*member), + _ => None, + } +} + /// One link of a chain at a position: `from` may give `scope` to `to`. `None` is the group root. A /// reissued link is a grant from a successor. It stands for the removed member's grant `origin`. #[derive(Clone, Debug)] @@ -357,6 +371,13 @@ impl Engine { }) .collect(); if superseding.is_empty() { + // Issue #169: an endpoint removed under KeepPrior and later re-added starts + // clean, so this grant is no link at a position that has seen the re-add. + // Only the grantee endpoint can arise here: a grantor removal that contains + // the grant in its past would supersede the grant, not leave this branch. + if self.cut_by_readd(by_id, &grant, &removals, &past) { + continue; + } // A non-removal role change (a demotion) carries no RemoveMode: under // Cascade delegation the link stands only while the grantor still holds // the scope at `after`, so demoting the grantor below the grant's scope @@ -471,6 +492,10 @@ impl Engine { }); continue; } + // Issue #169: the retained grant does not revive when an endpoint is re-added. + if self.cut_by_readd(by_id, &grant, &removals, &past) { + continue; + } links.push(Link { origin: grant.id, from: grant.from, @@ -485,6 +510,42 @@ impl Engine { Ok(links) } + /// Whether `KeepPrior` must cut this grant at this position because an endpoint was + /// re-added (issue #169): for the grantor or the grantee, a settled `KeepPrior` + /// removal in `past` has the grant in its causal past, and a settled re-add of the + /// same account in `past` has that removal in its causal past. The re-add starts the + /// account clean, so the pre-removal grant is no link at any position that has seen + /// the re-add. Grants issued after the re-add are untouched: no removal that follows + /// them contains them in its past together with a later re-add, until the account is + /// removed and re-added again. + fn cut_by_readd( + &self, + by_id: &BTreeMap, + grant: &Grant, + removals: &[Removal], + past: &BTreeSet, + ) -> bool { + [grant.from, Some(grant.to)].iter().any(|member| { + removals + .iter() + .filter(|removal| { + Some(removal.member) == *member + && removal.mode == RemoveMode::KeepPrior + && causal_past(by_id, &[removal.id]).contains(&grant.id) + }) + .any(|removal| { + past.iter().any(|id| { + self.settled.contains(id) + && by_id.get(id).is_some_and(|event| { + event.group == Some(grant.group) + && added_member(event) == *member + && causal_past(by_id, &[*id]).contains(&removal.id) + }) + }) + }) + }) + } + /// Whether a settled removal of the grantor is concurrent with the grant: neither is in the /// other's causal past, and the group voids such a grant. Strong remove voids it (design §6.2 /// and §7.3). Weak remove stands it, and only the authority check decides later events diff --git a/pkgs/id/src/groups/policy.rs b/pkgs/id/src/groups/policy.rs index 32ae89f4bc..601154013a 100644 --- a/pkgs/id/src/groups/policy.rs +++ b/pkgs/id/src/groups/policy.rs @@ -74,7 +74,9 @@ pub enum RemoveMode { Revoke, /// Reissue the grants to a successor. Transfer(TransferOpts), - /// Retain prior grants under their original grantor. + /// Retain prior grants under their original grantor. A re-add starts clean: + /// pre-removal grants the account made or received do not revive when it is + /// re-added (issue #169); they must be re-issued. KeepPrior, /// Re-anchor grants at the group. ReassignToGroup, @@ -214,6 +216,8 @@ pub enum Removal { #[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum, Serialize, Deserialize)] pub enum Delegation { /// Legacy preference to keep grants made before a removal. + /// A re-add starts clean: pre-removal grants the account made or received do not revive + /// when it is re-added (issue #169); they must be re-issued. #[value(name = "keep_prior")] KeepPrior, /// Legacy preference to cascade a grant when its grantor loses the scope. The default. diff --git a/pkgs/id/src/groups/tests.rs b/pkgs/id/src/groups/tests.rs index e53ca774dc..ff09198fc2 100644 --- a/pkgs/id/src/groups/tests.rs +++ b/pkgs/id/src/groups/tests.rs @@ -1071,6 +1071,102 @@ fn test_keep_prior_lets_a_grant_made_before_the_removal_stand() { assert!(retained.retained); } +#[test] +fn test_keep_prior_readd_does_not_revive_grants_the_member_made() { + // Issue #169 (ruling 2026-11-03): revival is refused. 2 is removed after d1 and then + // re-added; at the head d1 is no link, so 3 has no chain. + let g = id("g"); + let mut events = chain_base(Some(Delegation::KeepPrior)); + events.push(member( + "x0", + 1, + g, + MemberOp::Remove { + member: key(2), + mode: RemoveMode::KeepPrior, + }, + &["d1"], + )); + events.push(add("a2", 1, g, 2, Role::Write, &["x0"])); + events.push(delegate("d2", 3, g, 4, Role::Read, false, &["a2"])); + let p = project(&events); + assert_eq!( + outcome(&p, "a2"), + Outcome::Applied, + "the re-add itself applies" + ); + assert!( + !p.grant_in_force(&id("d1")), + "a grant made before the removal does not revive on re-add" + ); + assert_eq!( + outcome(&p, "d2"), + Outcome::Refused(Refusal::NoChain), + "3 has no chain once d1 is cut" + ); +} + +#[test] +fn test_keep_prior_readd_does_not_revive_grants_the_member_received() { + // The other direction of issue #169: d0 is the owner's grant TO 2. 2's removal and + // re-add cut it too - the re-added member starts clean, holding only its role. + let g = id("g"); + let mut events = chain_base(Some(Delegation::KeepPrior)); + events.push(member( + "x0", + 1, + g, + MemberOp::Remove { + member: key(2), + mode: RemoveMode::KeepPrior, + }, + &["d1"], + )); + events.push(add("a2", 1, g, 2, Role::Write, &["x0"])); + let p = project(&events); + assert!( + !p.grant_in_force(&id("d0")), + "a grant received before the removal does not revive on re-add" + ); +} + +#[test] +fn test_keep_prior_readd_starts_clean_and_a_reissued_grant_stands() { + // After the re-add the owner re-issues the grant; the new chain carries authority + // and the pre-removal grant stays cut. + let g = id("g"); + let mut events = chain_base(Some(Delegation::KeepPrior)); + events.push(member( + "x0", + 1, + g, + MemberOp::Remove { + member: key(2), + mode: RemoveMode::KeepPrior, + }, + &["d1"], + )); + events.push(add("a2", 1, g, 2, Role::Write, &["x0"])); + events.push(delegate("d3", 1, g, 2, Role::Write, true, &["a2"])); + events.push(delegate("d4", 2, g, 3, Role::Write, true, &["d3"])); + events.push(delegate("d5", 3, g, 4, Role::Read, false, &["d4"])); + let p = project(&events); + assert_eq!( + outcome(&p, "d5"), + Outcome::Applied, + "the re-issued chain carries 3" + ); + assert!( + p.grant_in_force(&id("d3")), + "a grant issued after the re-add stands" + ); + assert!(p.grant_in_force(&id("d4"))); + assert!( + !p.grant_in_force(&id("d1")), + "the pre-removal grant stays cut after the re-issue" + ); +} + #[test] fn test_cascade_removes_the_grants_the_removed_member_made() { let g = id("g"); @@ -1742,6 +1838,25 @@ proptest! { _ => None, }) .collect(); + // Issue #169 mirror: a KeepPrior removal of the grantor that contains the + // grant, followed by a re-add of the grantor, cuts the grant at the head. + // Mirrors Engine::cut_by_readd for the grantor endpoint (this property + // tracks only the grantor's removals). + let cut_by_readd = !superseding_modes.is_empty() + && superseding_modes + .iter() + .all(|mode| *mode == RemoveMode::KeepPrior) + && removals + .iter() + .filter(|r| pasts[&r.id].contains(&grant.id)) + .any(|r| { + events.iter().any(|a| { + a.group == Some(group) + && matches!(a.body, Body::Member(MemberOp::Add { member: m, .. }) if m == grantor) + && p.takes_effect(&a.id) + && pasts[&a.id].contains(&r.id) + }) + }); if !vetoed && !concurrent && !superseding_modes.is_empty() { if superseding_modes.contains(&RemoveMode::Revoke) || superseding_modes @@ -1749,6 +1864,11 @@ proptest! { .any(|mode| matches!(mode, RemoveMode::Transfer(_))) { prop_assert!(!in_force, "revoke/transfer supersedes the original grant"); + } else if cut_by_readd { + prop_assert!( + !in_force, + "keep-prior: a re-add cuts pre-removal grants (issue #169)" + ); } else { prop_assert!(in_force, "keep-prior/reassign retains the grant"); }