From bd58a123db5b4acc61afd592cbf2dc5f30353d34 Mon Sep 17 00:00:00 2001 From: holistis Date: Mon, 9 Nov 2026 17:54:42 +0100 Subject: [PATCH] fix: settlement cache reuse must match payee/token/amount, not just nonce+chain POST /settle checks findExistingSettlement before doing any real verification. That lookup matches on nonce and chainId only, and when it finds a confirmed record it returns success: true straight away with the stored txHash and payer, using whatever network the current request sent. A confirmed settlement's nonce can be reused in a new /settle request with completely different paymentRequirements (different payTo, asset, or amount), and the cache hit still returned success without checking any of that. GET /settlements?payer= is also unauthenticated, so a nonce from any payer's past confirmed settlement is easy to look up. This adds the missing check: a cached settlement is only reused when its stored payee, token, and amount match the current request's paymentRequirements. Anything else falls through to the normal facilitator.settle() path, which does full verification. --- packages/server/src/handler.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/server/src/handler.ts b/packages/server/src/handler.ts index ee7d3e0..fee86b1 100644 --- a/packages/server/src/handler.ts +++ b/packages/server/src/handler.ts @@ -109,7 +109,18 @@ export function createHandler( ? await findExistingSettlement(body.paymentPayload, body.paymentRequirements, settlementStore) : null; - if (existing && existing.status === "confirmed") { + // Only reuse a cached settlement when it was actually confirmed for + // THIS SAME payee/token/amount. The nonce+chain lookup alone isn't + // enough: without this check, any already-confirmed nonce could be + // replayed against different payment requirements and still come + // back as success. + if ( + existing && + existing.status === "confirmed" && + existing.payee.toLowerCase() === body.paymentRequirements.payTo.toLowerCase() && + existing.token.toLowerCase() === body.paymentRequirements.asset.toLowerCase() && + existing.amount === body.paymentRequirements.amount + ) { return Response.json( { success: true, @@ -168,7 +179,7 @@ async function findExistingSettlement( payload: PaymentPayload, requirements: PaymentRequirements, store: SettlementStore, -): Promise<{ txHash: string; status: string; payer: string } | null> { +): Promise<{ txHash: string; status: string; payer: string; payee: string; token: string; amount: string } | null> { try { const p = payload as Record; const inner = p.payload as Record | undefined; @@ -182,8 +193,15 @@ async function findExistingSettlement( const record = await store.findByNonceAndChain(nonce, chainId); if (!record) return null; - return { txHash: record.txHash, status: record.status, payer: record.payer }; + return { + txHash: record.txHash, + status: record.status, + payer: record.payer, + payee: record.payee, + token: record.token, + amount: record.amount, + }; } catch { return null; } -} \ No newline at end of file +}