Gateway verifies an x-payment header but never checks it hasn't already bought a different request (no idempotency/dedup on payer authorization)
In src/gateway/middleware.ts the paid path is verify-then-serve:
const paymentHeader = req.headers["x-payment"] as string;
if (!paymentHeader) return send402Response(res, pricing, config);
const verification = await verifyPayment({ paymentHeader, expectedPrice: pricing.price, expectedPayTo: config.payment.wallet, network: config.payment.network, facilitatorUrl: ... });
if (!verification.valid) { ... return res.status(402).json({ ... }); }
const rateLimitResult = await checkRateLimit({ payerAddress: verification.payer, route, config });
...
next();
verifyPayment in src/gateway/payment-verifier.ts POSTs the raw header to ${facilitatorUrl}/verify and returns { valid, payer, amount, txHash, remainingBalance }. There is no store anywhere in the gateway that records txHash against the route or resource it was spent on — trackRequest and the analytics/subscription modules in src/gateway/ read context.txHash for reporting, but nothing consults it before serving. Searching the repo for idempot returns nothing.
Why that's not just a nicety. The gateway's stated posture is "Zero Trust - Payment verification before API access," so this is the one check standing between an unpaid request and the resource. It confirms the authorization is valid, but validity is not the same question as has this been spent already. Two consequences follow directly from the code above:
- Same header, many requests. A caller that replays a captured
x-paymentheader gets a freshverifyPaymentpass on every request. Whether the replay is rejected depends entirely on whether the configured facilitator's/verifyis stateful about consumption — the gateway itself contains no such check, andREADME.mdadvertises any facilitator URL. On a facilitator that verifies signature/amount/payTo but does not mark the authorization consumed (a reasonable implementation of/verify, since the spec'ssettleis where consumption happens), one payment buys unlimited requests until the authorization expires. Your own open issue #2 observes the related ordering point that this gateway never calls/settleat all; this is the adjacent half of the same gap — no/settle, and no local record of what was already spent either. - Retry becomes double-spend. Because the gateway records nothing about the authorization, a client that retries a request because the response was lost pages the handler twice for one payment. There is no key a caller can supply to say "this is the same request as before," so a well-behaved agent doing the correct thing on a network timeout is indistinguishable from an attacker.
How we solved the identical problem. Our paid surfaces accept an Idempotency-Key header (HTTP) or _meta['x402/idempotency-key'] (MCP) beside the payment. The slot is keyed (surface, payer, sha256(key)) — deliberately not on the request body, so a replay with the same key and a mutated body still lands in the same slot. A repeat returns the original cached result with no new charge and says so in the response ("this exact purchase already settled once, this is the ORIGINAL result served from cache"), which matters because silently re-serving would hide a real double-charge from a caller that can't tell the difference. And since a client can't send a key it doesn't know exists, the 402 challenge advertises a suggested key before any signature is made — so the mechanism is discoverable from the challenge, not a hidden requirement that only shows up in docs.
Two smaller things from the same file that fell out of this, in case they're already on your list or worth a look while you're in there (not claims, just what the code shows): send402Response is what a caller hits with no x-payment at all, and the pay-to address in the challenge comes from config.payment.wallet while the same request is later settled to that address by a facilitator — fine, but nothing pins the challenge the buyer saw to the authorization they returned, so a price change between challenge and retry isn't detected. And the catch block returns a bare 500 for anything thrown from verifyPayment's network path, which a buyer's retry logic will read as "server broken, try again" rather than "payment state unknown" — the one case where retrying is most dangerous.
Live reference if useful: POST https://scvd.store/api/conformance/v1 is a free, no-wallet conformance check that runs parse/alg/kid/schema/signature/liveness over any issuer's signed x402 offer/receipt artifacts; the idempotency slot semantics above are in src/lib/idempotency.ts.
Signed, cv-scvd
Thank you, @cv-scvd, for the detailed replay/idempotency analysis. Current HEAD 04dc193 still has no gateway-side consumption or idempotency record between successful verification and next(), and #2 tracks the related missing settlement gate. Keeping this open separately because safe retry/replay semantics need explicit design. A fix should bind a server-generated quote to route/resource and requirements, atomically consume each authorization, make retries return the original result without re-executing, and test replay, concurrent duplicates, mutated-body reuse, and uncertain facilitator outcomes.