feat: make signatures single-use with a nonce - #42
Closed
thomas-waite wants to merge 1 commit into
Closed
Conversation
Every signature now carries a random nonce in its signed params. Core verifyRequest accepts an atomic tryRecordNonce dependency (record after the signature is proven, before the AgentBook RPC), and the x402 hooks enforce single use through the new AgentKitStorage.tryRecordNonce — one atomic check-and-record call, replacing v1's racy hasUsedNonce/recordNonce pair, with expiry-based pruning so the store only ever holds the last five minutes of nonces. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
thomas-waite
requested review from
Takaros999,
m1guelpf and
paolodamico
as code owners
August 25, 2026 00:08
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #39. Adds the nonce that #39 deferred, making every AgentKit signature valid for exactly one request.
Why
#39's bindings already prevent replay against a different service, endpoint, payload, or after 5 minutes. The remaining gap: a byte-identical request replays successfully within the window — anyone inside the receiving side's boundary (logs, middleware, a compromised component) can re-execute it, burning the human's shared free-trial quota and duplicating non-idempotent side effects.
expiresbounds replay in time; the nonce bounds it in count — to one.Design
noncein its signed params (created;expires;nonce;keyid;tag). Generated automatically bycreateSignatureHeaders— no CLI or client interface change.verify(request)stays stateless (replay bounded by the 5-minute window, as before).tryRecordNonce(nonce, expiresAt): Promise<boolean>— check and record in one atomic step (Redis:SET nonce 1 NX EX 300), fixing the TOCTOU in v1'shasUsedNonce/recordNoncewhere two concurrent replays could both pass.expiresAtlets stores TTL entries, so a nonce store only ever holds the last five minutes of nonces (v1's grew unboundedly).AgentKitStorage.tryRecordNonceright after verification;InMemoryAgentKitStorageimplements it with expiry pruning for single-process servers.Changes
nonceparam in the profile (serialize/parse/generate);verifyRequestreturns it and accepts an atomictryRecordNoncedependency (NONCE_REUSEDon replay).AgentKitStorage.tryRecordNonce(nonce, expiresAt); hooks reject replays with avalidation_failedevent; in-memory reference implementation.Testing
102 tests (68 core / 24 x402 / 10 cli): nonce parser adversarial cases, record-once semantics with details, no-store-poisoning on failed signatures, hooks replay rejection, storage pruning, and an e2e where an identical replay is rejected while the first use and a tampered-body attempt behave correctly.
🤖 Generated with Claude Code