From 73b22b13ce8a9a26a23e0cd8ee5de04adc4f99bc Mon Sep 17 00:00:00 2001 From: Tymofii Pidlisnyi Date: Sat, 28 Nov 2026 18:31:11 -0800 Subject: [PATCH 1/2] fix(revocation): store owns accepted revocation records InMemoryAuthorityRevocationStore kept the inserted dict and returned it from get() and in the insertion result, so a held reference could rewrite or empty accepted evidence after first-wins acceptance. The store now deep-copies on insert and hands out deep copies from get() and stored. The first-wins check and write become one dict.setdefault on the copied record. First wins, serialized bytes, signatures and resolver behaviour are unchanged. The store protocol documents ownership. Signed-off-by: Tymofii Pidlisnyi --- .../v2/authority_revocation/store.py | 34 ++++++++----- .../v2/authority_revocation/types.py | 15 ++++-- tests/v2/test_authority_revocation_v1.py | 50 +++++++++++++++++++ 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/src/agent_passport/v2/authority_revocation/store.py b/src/agent_passport/v2/authority_revocation/store.py index 327ae18..150e432 100644 --- a/src/agent_passport/v2/authority_revocation/store.py +++ b/src/agent_passport/v2/authority_revocation/store.py @@ -6,6 +6,8 @@ from __future__ import annotations +import copy + from .types import AuthorityRevocationInsertion @@ -32,6 +34,12 @@ class InMemoryAuthorityRevocationStore: in what the store reports, not in the bytes the caller minted: two calls with different nonces mint two different valid records, and the store keeps the first. + - The store owns what it accepts. insert_verified_revocation() keeps a deep + copy of the record, never the caller's dict, and get() and the insertion + result hand out fresh deep copies. Accepted evidence cannot be changed + through a reference the caller passed in or received back. This fixes the + stored bytes, not the verdict: the resolver still verifies on every read, + so a key resolver that changes can still change the answer. - insert_verified_revocation() brings its target into the tracked view. Recording a revocation for a delegation is a statement that this store has an opinion about that delegation. @@ -65,25 +73,27 @@ def tracks(self, delegation_id: str) -> bool: return delegation_id in self._tracked def get(self, delegation_id: str) -> dict | None: - return self._records.get(delegation_id) + record = self._records.get(delegation_id) + return None if record is None else copy.deepcopy(record) def insert_verified_revocation(self, revocation: dict) -> AuthorityRevocationInsertion: """See AuthorityRevocationStore.insert_verified_revocation for the contract, including that ``revocation`` must already have been verified against its target delegation by record_authority_revocation(). - The read of the existing record and the write are one statement sequence - with no await and no callback between them, and CPython evaluates them - under the GIL without a bytecode boundary another thread can use to - interleave a second insert for the same key, so no second caller can - observe ``inserted`` True for a delegation whose slot is already taken. + The record is copied first, before the slot is examined. The check for an + existing record and the write are then one dict.setdefault() call on a + str key, which CPython performs as a single operation under the GIL, so + no second caller can observe ``inserted`` True for a delegation whose + slot is already taken. That is a property of this runtime and this container, not a pattern a durable store may copy; see the protocol docstring for what a durable implementation owes instead. """ - existing = self._records.get(revocation["delegation_id"]) - if existing is not None: - return AuthorityRevocationInsertion(inserted=False, stored=existing) - self._records[revocation["delegation_id"]] = revocation - self._tracked.add(revocation["delegation_id"]) - return AuthorityRevocationInsertion(inserted=True, stored=revocation) + owned = copy.deepcopy(revocation) + key = owned["delegation_id"] + held = self._records.setdefault(key, owned) + if held is not owned: + return AuthorityRevocationInsertion(inserted=False, stored=copy.deepcopy(held)) + self._tracked.add(key) + return AuthorityRevocationInsertion(inserted=True, stored=copy.deepcopy(owned)) diff --git a/src/agent_passport/v2/authority_revocation/types.py b/src/agent_passport/v2/authority_revocation/types.py index 55427fb..dab546b 100644 --- a/src/agent_passport/v2/authority_revocation/types.py +++ b/src/agent_passport/v2/authority_revocation/types.py @@ -93,8 +93,8 @@ class AuthorityRevocationInsertion: """What a store's first-write primitive reports. ``inserted`` is the only way a caller learns whether this call was the write - that took the first-wins slot. ``stored`` is the record passed in when - ``inserted`` is True and the record already held when it is False, so a + that took the first-wins slot. ``stored`` is a copy of the record passed in when + ``inserted`` is True and of the record already held when it is False, so a caller that must tell them apart reads ``inserted`` rather than comparing bytes. """ @@ -130,7 +130,16 @@ def tracks(self, delegation_id: str) -> bool: """ def get(self, delegation_id: str) -> dict | None: - """The revocation recorded for ``delegation_id``, or None when none is.""" + """The revocation recorded for ``delegation_id``, or None when none is. + + A store owns the records it accepts. Neither the dict passed to + insert_verified_revocation() nor any value returned by get() or in + ``stored`` may be a way to change the record held: keep an independent + copy and hand out copies or immutable values. This fixes the stored + evidence, not the verdict. The resolver verifies the record on every + read, and a key resolver whose answers change can still change what it + reports. + """ def insert_verified_revocation(self, revocation: dict) -> AuthorityRevocationInsertion: """Persistence primitive. Write ``revocation`` only when this store holds diff --git a/tests/v2/test_authority_revocation_v1.py b/tests/v2/test_authority_revocation_v1.py index 7ca3496..af828d5 100644 --- a/tests/v2/test_authority_revocation_v1.py +++ b/tests/v2/test_authority_revocation_v1.py @@ -809,3 +809,53 @@ def test_an_untracked_root_yields_indeterminate_never_valid(): assert outcome.valid is False assert outcome.failures[0].code == "REVOCATION_UNKNOWN" assert outcome.failures[0].index == 0 + + +def test_store_owns_accepted_evidence_returned_references_cannot_change_it(): + delegation = _root() + store = InMemoryAuthorityRevocationStore() + resolve = _resolver(store) + original = _revoke_by_issuer(delegation) + expected = canonicalize_jcs(original) + + accepted = _record(store, delegation, original) + assert accepted.inserted is True + assert resolve(delegation) == "revoked" + + # Writes through the insertion result reach nothing the store holds. + accepted.stored["signature"] = "0" * 128 + accepted.stored.clear() + assert canonicalize_jcs(store.get(delegation["delegation_id"])) == expected + assert resolve(delegation) == "revoked" + + # Each get() is a fresh copy: writing to one leaves the next untouched. + got = store.get(delegation["delegation_id"]) + got["reason_code"] = "tampered" + got["revoked_at"] = "2026-07-18T22:50:00.000Z" + assert store.get(delegation["delegation_id"]) is not got + assert canonicalize_jcs(store.get(delegation["delegation_id"])) == expected + assert resolve(delegation) == "revoked" + + +def test_store_ownership_on_the_direct_primitive_keeps_first_wins(): + delegation = _root() + store = InMemoryAuthorityRevocationStore() + first = _revoke_by_issuer(delegation) + second = _revoke_by_issuer( + delegation, revoked_at="2026-07-18T22:40:00.000Z", nonce="d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" + ) + caller = dict(first) + inserted = store.insert_verified_revocation(caller) + assert inserted.inserted is True + assert inserted.stored is not caller + + # The caller's own dict stays the caller's. + caller["signature"] = "0" * 128 + assert canonicalize_jcs(store.get(delegation["delegation_id"])) == canonicalize_jcs(first) + + repeated = store.insert_verified_revocation(second) + assert repeated.inserted is False + assert canonicalize_jcs(repeated.stored) == canonicalize_jcs(first) + repeated.stored.clear() + assert canonicalize_jcs(store.get(delegation["delegation_id"])) == canonicalize_jcs(first) + assert _resolver(store)(delegation) == "revoked" From 002a0890598278ad534f8aecbdbe260b8b9b7f60 Mon Sep 17 00:00:00 2001 From: Tymofii Pidlisnyi Date: Sat, 28 Nov 2026 18:40:47 -0800 Subject: [PATCH 2/2] fix(revocation): scope ownership to plain record data, test concurrent first wins Signed-off-by: Tymofii Pidlisnyi --- .../v2/authority_revocation/store.py | 6 +++- tests/v2/test_authority_revocation_v1.py | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/agent_passport/v2/authority_revocation/store.py b/src/agent_passport/v2/authority_revocation/store.py index 150e432..e71c4fc 100644 --- a/src/agent_passport/v2/authority_revocation/store.py +++ b/src/agent_passport/v2/authority_revocation/store.py @@ -39,7 +39,11 @@ class InMemoryAuthorityRevocationStore: result hand out fresh deep copies. Accepted evidence cannot be changed through a reference the caller passed in or received back. This fixes the stored bytes, not the verdict: the resolver still verifies on every read, - so a key resolver that changes can still change the answer. + so a key resolver that changes can still change the answer. Ownership is + stated for plain record data (dict, list, str, int, float, bool, None), + which is what record_authority_revocation() passes in. copy.deepcopy is + not an isolation guarantee for arbitrary objects with their own copy + behaviour. - insert_verified_revocation() brings its target into the tracked view. Recording a revocation for a delegation is a statement that this store has an opinion about that delegation. diff --git a/tests/v2/test_authority_revocation_v1.py b/tests/v2/test_authority_revocation_v1.py index af828d5..b121ad9 100644 --- a/tests/v2/test_authority_revocation_v1.py +++ b/tests/v2/test_authority_revocation_v1.py @@ -859,3 +859,32 @@ def test_store_ownership_on_the_direct_primitive_keeps_first_wins(): repeated.stored.clear() assert canonicalize_jcs(store.get(delegation["delegation_id"])) == canonicalize_jcs(first) assert _resolver(store)(delegation) == "revoked" + + +def test_store_ownership_first_wins_under_concurrent_inserts(): + import threading + + delegation = _root() + candidates = [ + _revoke_by_issuer(delegation, nonce=f"{i:032x}") for i in range(16) + ] + for _ in range(20): + store = InMemoryAuthorityRevocationStore() + barrier = threading.Barrier(len(candidates)) + results = [None] * len(candidates) + + def insert(i): + barrier.wait() + results[i] = store.insert_verified_revocation(candidates[i]) + + threads = [threading.Thread(target=insert, args=(i,)) for i in range(len(candidates))] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + + winners = [i for i, r in enumerate(results) if r.inserted] + assert len(winners) == 1 + held = canonicalize_jcs(store.get(delegation["delegation_id"])) + assert held == canonicalize_jcs(candidates[winners[0]]) + assert all(canonicalize_jcs(r.stored) == held for r in results)