ClawRouter icon indicating copy to clipboard operation
ClawRouter copied to clipboard

Pre-auth catch spans the payment-carrying send, so a lost response signs a second payment (payment-preauth.ts:131)

Open aurumflux20 opened this issue 2 months ago • 4 comments

Summary

In src/payment-preauth.ts the try at line 116 wraps not only the payment signing but the network send at line 123 that already carries a signed payment. Its catch at line 131 is commented "Pre-auth signing failed — invalidate and fall through", which reads as though only createPaymentPayload / encodePaymentSignatureHeader can land there. await baseFetch(preAuthRequest) is inside the same block, so a rejected send lands there too — and the fall-through path signs and sends a second, distinct payment.

The path

if (preAuthCovers) {
  try {
    const payload = await client.createPaymentPayload(cached.paymentRequired);   // 117
    const headers = httpClient.encodePaymentSignatureHeader(payload);            // 118
    …
    const response = await baseFetch(preAuthRequest);   // 123  ← send, payment attached
    if (response.status !== 402) return response;
    cache.delete(cacheKey);
  } catch (err) {                                       // 131
    cache.delete(cacheKey);
    if (err instanceof SpendPolicyError) throw err;
    // Pre-auth signing failed — invalidate and fall through.
  }
}
// clean, un-paid request → 402 → sign a NEW payload → send
const response = await baseFetch(request);              // 145
…
const payload = await client.createPaymentPayload(paymentRequired);   // 178  ← second authorization
return baseFetch(clonedRequest);                                       // 183

If baseFetch at 123 rejects after the gateway received and settled that payment — a connection reset, a socket hang-up, a TLS error, an upstream that closes after settling — the client cannot tell that from "the request never arrived." It takes the second reading, falls through, and pays again. One user request, two USDC charges.

Why the existing guards don't cover this one

I checked both before writing this up, because you clearly already thought about double-charging:

  • The 30s response dedup (proxy.ts header, line 16-17) keys on the request body and catches OpenClaw retrying after a timeout — a second proxy-level request. This double payment happens inside one payFetch invocation, so there is never a second proxy request and the dedup cache is never consulted.
  • The client-abort path is safe. clientAbort.signal is passed into payFetch (proxy.ts:2106) and survives request.clone(), so once aborted the fall-through baseFetch(request) at 145 rejects immediately and nothing is re-signed. The exposure is specifically non-abort rejections, where the client is still connected and the fall-through proceeds normally.
  • Nonce replay protection cannot catch it. Line 178 is a fresh createPaymentPayload, so the second charge is a genuinely new authorization with an unused nonce — authorizationState returns false for it and every guard correctly passes it. There is no Idempotency-Key anywhere in the repo (I grepped), and on the x402 wallet path a key would not help regardless, since a fresh signed authorization is a new payment whatever the header says.

The Solana comment at line 105-107 shows the same failure mode was already reasoned about in another form ("causing double charges"), which is why I think this one is an oversight rather than a decision.

Test coverage

payment-preauth.test.ts covers a pre-auth rejected by a 402 response (rejectNextPaid returns challenge402()). Nothing in the harness makes baseFetch reject as a promise, so the line-131 branch is exercised only for signing failures — which is consistent with the comment describing it that way.

Suggested fix

Narrow the ambiguity instead of resolving it toward "did not happen." Track whether the send was attempted with a payment attached:

let sent = false;
try {
  const payload = await client.createPaymentPayload(cached.paymentRequired);
  const headers = httpClient.encodePaymentSignatureHeader(payload);
  …
  sent = true;                                  // a signed payment is now in flight
  const response = await baseFetch(preAuthRequest);
  if (response.status !== 402) return response;
  cache.delete(cacheKey);
} catch (err) {
  cache.delete(cacheKey);
  if (err instanceof SpendPolicyError) throw err;
  if (sent) throw err;   // indeterminate: may have settled — never re-pay blindly
  // signing failed, nothing was sent: safe to fall through
}

if (sent) throw err is the minimal change and is strictly safer: it surfaces an ambiguous outcome instead of silently paying twice. If you would rather not fail the request, the stronger version reconciles before re-paying — read authorizationState(authorizer, nonce) for the authorization that was in flight, and only sign a new payment when the chain says the old one was never used.

The same shape sits at line 183 (return baseFetch(clonedRequest)), where a rejection propagates to proxy.ts:2102; worth a look at what the caller does with it, though I did not trace that one to a conclusion.

Scope of this report, honestly

This is a line-by-line read of payment-preauth.ts, proxy.ts and the tests at c41b8df — not a live reproduction. I have not driven a connection reset against the gateway mid-settle and watched two USDC transfers land. The control flow, the absence of idempotency, and the two guards not covering it are verified from the code; the settled-then-lost first payment is the consequence that follows.

The general rule I am applying: an outcome that could not be determined is terminal — it is never "did not happen." Filed as one finding, no CVE theatre, and nothing about this is public beyond this issue.

aurumflux20 avatar Sep 02 '26 23:09 aurumflux20

Fixed and released in v0.12.267.

Your read of the code was exact, and so was the part that took the most work to establish — that neither the dedup, the abort path, nor replay protection covers it. I verified each of those independently before touching anything, and each held up.

The fix tracks whether a request carrying a signed payment has left the process. Once it has, the transport error surfaces instead of falling through. A 402 still falls through — that is an answer, the gateway declined and nothing settled — and so does a signing failure before the send, which is unambiguous. Both cases are pinned by tests now.

Your point about the test harness was the sharpest part of the report: it only ever made a paid request return a 402, never reject, so the branch really was exercised for signing failures only, which is why the comment describing it that way went unchallenged. The new test makes baseFetch reject, and without the fix it resolves 200 having paid twice — red-green verified rather than assumed.

Thanks for the writeup.

VickyXAI avatar Sep 03 '26 04:09 VickyXAI

Thank you — and for verifying each of the three guards independently before touching anything; that's the part most fixes skip.

The shape you landed on is the right one: a 402 is an answer (declined, nothing settled), a pre-send signing failure is unambiguous, and only "a signed payment left the process and we never heard back" is the case that must surface instead of fall through. Pinning that with a test that makes baseFetch reject — and confirming it resolves 200-having-paid-twice without the fix — is exactly the red-green check I'd want in place; the branch can't quietly drift back to "signing failed only" now.

Closing this from my side too. Glad it was useful.

aurumflux20 avatar Sep 03 '26 04:09 aurumflux20

One question, and it's genuinely a question — not a lead-in to anything.

You fixed this in 4.3 hours, and Franklin's before it. That's faster than any other team I've reported to, and it's made me realise I don't actually know what the thing I'm reporting is worth to the person receiving it.

So: had this shipped and a customer been double-charged, what would that actually have cost you? Not the refund — the whole thing. The support thread, the reconciliation, the trust conversation, whoever's afternoon disappeared into it.

I'm asking because I've spent three weeks reading money paths and filing findings, and I've been guessing at that number. I'd rather have one real answer from someone who has run the payment path than ten more guesses of my own.

Two related, if you have the patience — ignore either:

  • Would you have caught this yourselves eventually? I genuinely don't know whether I found something that was going to surface anyway, or something that would have sat there.
  • Is this the kind of thing that has a budget line at your size, or is it just "engineering handles it"? An honest "nobody would pay for this" is more useful to me than a polite maybe.

No pitch attached, and nothing changes on the public index either way — you're on it with the 4.3 hours regardless. I'm trying to find out whether what I'm doing is worth anything to anyone, and you're the person best placed to tell me it isn't.

aurumflux20 avatar Sep 04 '26 16:09 aurumflux20

Last note from me on this thread, and it is a commercial one — I would rather say that plainly than dress it up as another question.

Two findings now, Franklin and this one. Both real, both yours in under five hours, and neither cost you anything. That was deliberate: the finding is free because it is the only honest way to show the work is worth something.

What I have not looked at is the rest of your settlement path. The two defects were the same class in two different services, and when that happens the pattern is usually upstream of both — in how a retry gets decided, not in either handler. Finding it in two places is not the same as knowing it is only in two places.

That read is what I sell: every settlement path in the repo graded safe / unsafe / undetermined, a failing test in your own harness for each unsafe one, and the patch. Fixed fee, $1,200, one week, and I will send the scope before you commit to anything.

If that is useful, reply here or at [email protected]. If it is not, that is a completely fine answer and it is the last you will hear about it from me — the two findings stand either way, and I would file the next one regardless.

— A. Kaur, AurumFlux

aurumflux20 avatar Sep 04 '26 17:09 aurumflux20