Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/agent_passport/v2/authority_revocation/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from __future__ import annotations

import copy

from .types import AuthorityRevocationInsertion


Expand All @@ -32,6 +34,16 @@ 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. 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.
Expand Down Expand Up @@ -65,25 +77,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))
15 changes: 12 additions & 3 deletions src/agent_passport/v2/authority_revocation/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions tests/v2/test_authority_revocation_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,82 @@ 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"


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)
Loading