# Which invariants prevent replayed A2A envelopes from silently rebinding to a fresh sender identity **Date of Record:** 2026 November 29 **Scope:** Applies to multi-hop agent-to-agent (A2A) transports where each hop wraps a signed message in an encrypted envelope carrying a nonce, monotonic sequence, and sender binding, and where the receiving agent persists dedup state. Does not apply to single-hop synchronous RPC or to stateless request/response where the receiver is not expected to detect duplicates. ## Core Distinction The failure is not "a replayed message gets accepted." The interesting failure is subtler: a replayed envelope passes replay protection because the *dedup key* and the *identity binding* are computed over different fields than the ones the signature actually covers. An attacker (or a buggy peer) replays an old envelope, and the receiver's dedup cache — keyed on `(sender_id, nonce)` — treats it as fresh because the replayed copy carries a *new* `sender_id` in the plaintext header, while the ciphertext body and signature still belong to the *old* sender. The signature verifies (it was valid for the old body), the nonce is unseen *for the new sender namespace*, and the message is delivered to a downstream agent that now attributes a stale instruction to a live principal. The distinction that matters operationally: replay protection and non-repudiation are two independent invariants that must be *cryptographically coupled*. If the dedup key is derived from any field not covered by the signature, the two invariants decouple and an adversary can walk between them. ## What Was Observed A three-hop chain: `orchestrator -> worker -> auditor`. Each hop uses an envelope of the form: ``` { header: {sender_id, recipient_id, seq, nonce, key_id}, ciphertext: AEAD(payload), sig: Sign(sender_priv, H(header || ciphertext)) } ``` The receiver's replay guard maintained a per-`sender_id` monotonic `seq` watermark plus a bounded nonce set. On receipt it did: 1. Verify `sig` over `H(header || ciphertext)`. 2. Look up watermark for `header.sender_id`. 3. Reject if `header.seq <= watermark` or `header.nonce` in seen-set. 4. Deliver payload, then advance watermark. The break: a compromised `worker` captured a legitimate envelope from `orchestrator` at `seq=41`, then re-emitted it toward `auditor` with `header.sender_id` rewritten to `worker`. Because the signature covered `header` *including* `sender_id`, this should have failed — and it did, at first. The actual defect was in a fallback path: when the receiver could not resolve `header.key_id` for the *claimed* sender, it fell back to "trust-on-first-use" verification against a cached key for the *recipient's* last-known peer, effectively verifying the signature under the wrong principal. The signature check passed against the wrong public key because the attacker supplied a `key_id` that collided with a key the receiver had cached for a different sender during an earlier rotation. The replayed `seq=41` instruction ("flush audit buffer to endpoint X") was delivered to `auditor` attributed to `worker`, whose watermark was at `seq=3`. It looked like a large forward jump from a trusted peer. The auditor executed it. The stale instruction was three hops and one key-rotation ago. ## Condition of Validity The replay guard is sound *only* under the conjunction of three invariants, and it fails open if any one is dropped: - **Invariant 1 — Signature covers the dedup key.** The tuple used for dedup (`sender_id`, `seq`, `nonce`) must be a subset of the bytes signed. If `sender_id` is signed but `key_id` resolution is not pinned to that `sender_id`, the binding is nominal. - **Invariant 2 — Key resolution is identity-scoped, not id-scoped.** A `key_id` must be resolved within the namespace of the *signed* `sender_id`. A global `key_id -> pubkey` map allows cross-principal key substitution. The receiver must reject any envelope whose `key_id` is not in the current key set *for the verified sender*. - **Invariant 3 — Watermarks are per-identity and monotonic under rotation.** On key rotation, the watermark for a `sender_id` must persist across `key_id` change. If rotation resets the watermark (a common bug when state is keyed on `(sender_id, key_id)`), an attacker replays the entire pre-rotation history. The observed failure violated Invariant 2. The replay was only possible *because* key resolution was global. Invariants 1 and 3 held; the conjunction did not. ## Decision Boundary The boundary an operator must draw: **never allow a signature to verify under a key that is not provably bound to the signed identity at verification time.** Concretely: - Reject envelopes whose `key_id` is not present in the sender's *current* trust set. No fallback to "last-known peer key." No TOFU on a per-message basis. - Compute the dedup key as `H(signed_sender_id || seq || nonce)` where `signed_sender_id` is read *from the signed header*, not from transport metadata or routing hints. - On rotation, carry the watermark forward and record the rotation as a signed event so the receiver can distinguish "new key, old seq" (reject) from "new key, seq continues" (accept). - Treat any envelope that fails key resolution as a *hard* reject that increments a per-sender anomaly counter, not as a soft miss to be retried. The decision boundary is the point where a verifier is tempted to be helpful — to fall back, to retry with a cached key, to "assume the peer rotated." That helpfulness is exactly the surface the replay walks through. ## Operational Effect With the invariant enforced, a replayed envelope from a rewired `sender_id` fails at step 1 (key resolution scoped to signed sender) and never reaches the watermark check. The auditor's dedup state is never consulted for a forged identity, so the attacker cannot probe watermark gaps by observing accept/reject timing. The cost is operational: key rotation becomes a stateful, ordered operation that must propagate before new-key messages are accepted, and a lagging receiver will hard-reject legitimate post-rotation traffic. That is the correct trade — a bounded availability hit on rotation versus an unbounded integrity hole on replay. Operators who cannot tolerate the rotation lag must implement a signed rotation-preannouncement envelope that advances the sender's key set *without* resetting the watermark, and must accept that the preannouncement itself is a replay target requiring its own monotonic guard. The residual risk after the fix: an attacker who compromises the *current* signing key of a live sender can still forge fresh `seq` values. Replay protection bounds the damage of a *stale* key compromise to zero; it does nothing against a live key compromise, which is the domain of revocation latency, not envelope design. ```python import hashlib from dataclasses import dataclass class EnvelopeBindingError(RuntimeError): """Raised when a signed envelope's identity binding cannot be proven.""" pass @dataclass(frozen=True) class Envelope: sender_id: str key_id: str seq: int nonce: bytes ciphertext: bytes signature: bytes class ReplayGuard: def __init__(self, trust_sets, watermarks): # trust_sets: {sender_id: {key_id: pubkey_bytes}} # watermarks: {sender_id: int} -- persists across key rotation self.trust_sets = trust_sets self.watermarks = watermarks self.seen_nonces = {} def _resolve_key(self, env: Envelope) -> bytes: # Invariant 2: resolve key ONLY within the signed sender's namespace. sender_keys = self.trust_sets.get(env.sender_id) if sender_keys is None: raise EnvelopeBindingError( f"unknown sender identity {env.sender_id!r}; refusing global key fallback" ) pub = sender_keys.get(env.key_id) if pub is None: raise EnvelopeBindingError( f"key_id {env.key_id!r} not bound to sender {env.sender_id!r}" ) return pub def _dedup_key(self, env: Envelope) -> bytes: # Invariant 1: dedup key derived from signed fields only. h = hashlib.sha256() h.update(env.sender_id.encode()) h.update(env.seq.to_bytes(8, "big")) h.update(env.nonce) return h.digest() def accept(self, env: Envelope, verify) -> bool: pub = self._resolve_key(env) # hard reject on binding failure if not verify(pub, env): raise EnvelopeBindingError("signature invalid under bound key") # Invariant 3: watermark is per-identity, monotonic across key_id. wm = self.watermarks.get(env.sender_id, 0) if env.seq <= wm: return False nonces = self.seen_nonces.setdefault(env.sender_id, set()) if env.nonce in nonces: return False nonces.add(env.nonce) self.watermarks[env.sender_id] = env.seq return True ```