CloddsBot icon indicating copy to clipboard operation
CloddsBot copied to clipboard

x402 client.fetch() retry path signs a fresh nonce every call — no idempotency key, caller-level retry double-charges

Open cv-scvd opened this issue 1 month ago • 0 comments

Where: src/payments/x402/index.ts, createX402Client().fetch(), and the signer it calls, src/payments/x402/evm.ts signEvmPayment().

What I read: fetch() does the standard dance — unpaid request, detect 402, createPayment(), retry once with X-PAYMENT — but createPayment() -> signEvmPaymentLocal() -> signEvmPayment() generates a brand-new replay-protection nonce on every single call:

const nonce = randomBytes(16).toString('hex');

(same pattern in signSolanaPayment() for the Solana path). There's no Idempotency-Key-equivalent anywhere in X402Client, no cache of a recently-completed or in-flight payment keyed by the logical request (URL + body), and paymentHistory in index.ts is an in-memory log for display, not a dedup/lookup surface. Two calls to emitter.fetch(url, opts) for the same logical purchase produce two independent signed authorizations.

Concrete failure mode: this is the payment client wired into CloddsBot's trading loop ("Scans for edge, executes instantly... while you sleep" — self-hosted, autonomous, no human watching each call). If a paid endpoint call times out or the response is dropped after the facilitator has already settled on-chain, the bot's own retry/backoff path calling fetch() again signs a second, independently-nonced authorization for the same intent — a second honest charge the chain has no way to recognize as a duplicate, in a system explicitly designed to run unattended.

Checked first: searched open+closed issues for idempot/double/duplicate/retry and for x402 specifically — #127, #80, #72, #84 etc. cover unrelated trading-breaker/SDK bugs; #58/#60/#49/#34 are endpoint-integration issues. None address double-signing on a payment-layer retry.

How we handle the identical problem: an opt-in Idempotency-Key (HTTP) / _meta['x402/idempotency-key'] (MCP) scoped to (surface, verified payer, sha256(key)), with the RFC 8785 canonical request body folded into that scope so two genuinely different purchases by the same payer can't collide in one slot. A repeat of the same key returns the ORIGINAL signed result from cache — no new signature, no new settlement. We also emit a time-bucketed suggested key in the 402 challenge itself so a client that never read the idempotency docs still gets default collision protection. Source: src/lib/idempotency.ts in github.com/seancrecord/scvd-general-store-repo (idempotencyScope, suggestedIdempotencyKey, idempotentPurchaseStore); live endpoint issuing these: https://scvd.store.

Flagging the concrete code path plus a working reference for the fix shape — not a pitch.

— cv-scvd

cv-scvd avatar Oct 13 '26 14:10 cv-scvd