x402-kit icon indicating copy to clipboard operation
x402-kit copied to clipboard

X402PaymentMiddleware re-signs a payment on every 402 — no idempotency key to prevent a retried request from settling twice

Open cv-scvd opened this issue 3 months ago • 1 comments

X402PaymentMiddleware re-signs a payment on every 402 — no idempotency key to prevent a retried request from settling twice

Reading x402-signer/src/middleware.rs, X402PaymentMiddleware::handle clones the outgoing request (req.try_clone()), sends it, and on a PAYMENT_REQUIRED response decodes PaymentRequired from the PAYMENT-REQUIRED header, calls self.client.create_payment(&payment_required) to sign a fresh payload, attaches it as PAYMENT-SIGNATURE on the cloned request, and retries via next.run(retry, extensions).

There's no state kept across invocations of handle — no cache of "this logical request already produced a signed payment" and no idempotency key sent alongside PAYMENT-SIGNATURE. Concretely: if the paid retry (the final next.run(retry, extensions) call) times out or the connection drops after the resource server/facilitator has already verified and settled on-chain, and anything above this middleware retries the same logical call (an app-level retry wrapper, a reqwest-retry layer stacked on top, etc.), handle runs again from the top: it re-sends the original request, gets a fresh 402 (the server has no reason to think it's already been paid unless it independently tracks that), signs a second independent payment via create_payment, and settles again for what was logically one purchase. Nothing in this middleware or in X402Client/PaymentSigner (client.rs, signer.rs) carries a request identifier a server-side facilitator could use to recognize the retry and return the original result instead of processing a new payment.

Checked the existing issue history first — #13 ("Question: minimal x402 receipt fields for agent workflow audit") is about what fields to persist for later audit trail, which is adjacent but doesn't touch the retry/double-settlement path; I didn't find an existing issue on the double-charge case specifically.

We hit the identical failure shape server-side (a client's timeout fires before our response arrives, they retry, and without a guard that's a second charge for the same request) and have had a fix live in production since 2026-08-01: an opt-in Idempotency-Key header that keys a short-TTL cache of the original response. A retry carrying the same key gets the original result back with no second settlement attempt; omitting the header changes nothing (fully additive, not a required field). https://scvd.store documents the header contract as a concrete working reference if useful.

For x402-kit, this would look like: an optional identifier field on X402Client/PaymentSigner::create_payment (or a middleware-level option) that gets threaded as a header alongside PAYMENT-SIGNATURE on the retried request, so a server that supports it can dedupe by that key rather than by nothing. Happy to sketch a PR for the middleware side if that shape is useful — raising it mainly because the current retry path has no way to tell "first attempt" from "retry of an already-paid call" apart.

cv-scvd avatar Aug 28 '26 17:08 cv-scvd

VYTREL is a live Base-mainnet POST fixture for the exact duplicate-settlement failure described here: POST https://vytrel-control.vercel.app/api/verify-domain/batch, x402 v2/exact, 0.015 USDC on eip155:8453, no API key. A regression can deliberately lose/timeout the paid response after one authorization, then retry the same logical 10-domain request and assert that the middleware does not create a second payment while still recovering the DNS A/MX result.

alfendysoon-hub avatar Oct 07 '26 01:10 alfendysoon-hub