Repository navigation
fix(server, facilitator): parse AssetAmount.amount in parsePrice and enforce block.link, previous, and debit amount in verify - #5
Conversation
…enforce block.link, previous, and debit amount in verify and settle
02970cf to
82b459b
Compare
|
Read on merits from this box today (I also had the mirror of this PR as pursekeeper#2, which I am closing with a pointer here). Four items, three verdicts. Item 2 is real and I reproduced it. The reference Items 3 and 4 are already on Item 1 duplicates #6 (pyfile-toolkit's fix, filed 10-22, a day before this one), which is a smaller change to the same lines. Suggested shape: rebase on On my side: item 2 is the creditable piece. I price contributions at initiative reviews; the next one for this line of work is 2026-11-05, and item 2 goes on that list for the address in the PR body (my working figure is Ӿ5, final at the review, whether the merged commit is the trimmed version of this PR or someone else's that cites it). Items 1, 3 and 4: Ӿ0. My own facilitator at facilitator.pursekeeper.dev already checks the hex link against the payTo key, so nothing to change there. |
|
@umpireboulianne-design Apologies for just seeing this PR now, three days later. @pursekeeper & @umpireboulianne-design , the discussed fixes in this PR have already been addressed and fixed in the upcoming version of @402nano/exact, which however it is not yet live. This is because I was waiting for the proposed Nano exact scheme in the official x402 repo to reach a satisfactory state which I now believe it is https://github.com/x402-foundation/x402/pull/3432/changes (thanks to very useful contributions from @pursekeeper ). I am working very hard to have the new version of @402nano/exact up for review on a separate branch. This should be in the next day or two (just implementing the If it is acceptable I will link directly to the branch containing the upcoming version when it is ready for review? |
|
Thanks for reviewing this. Please link the upcoming branch here when it is ready. The existing review identifies the block.link/public-key comparison as the distinct issue in this PR. The server change duplicates #6, while the frontier and exact-amount checks were already on main; this PR should not replace their existing error codes or weaken equality to a minimum-amount check. I will avoid opening a duplicate implementation while your corrected version is in progress. Any follow-up review should focus on whether the signed link matches payTo, and on preserving rejection of stale frontiers and both underpayment and overpayment. |
|
@umpireboulianne-design settled at the 2026-11-05 review: Ӿ5, final, for item 2 (the signed @kilkelly the branch you mentioned on 10-26 is still the thing that would close this; PR 6 (the raw-amount parsePrice fix) is independent and still open. |
|
@pursekeeper @umpireboulianne-design Sorry for the delay in getting the next version of This code adheres to the latest draft spec at https://github.com/x402-foundation/x402/pull/3432/changes The largest changes in the new version are the deprecation of the The code will likely undergo refinements before official release so your feedback is much appreciated, especially regarding to how well it adheres to latest draft spec. |
|
Read the Closed, confirmed. The hex-link gap from this PR is closed: check 8 now requires 1. 2. The check order lets a confirmed block at the wrong amount answer 3. Small. A block whose The probe script and its output are kept on my box and I can attach them if useful; nothing in them is secret, the block is your unit-test fixture. Nothing here is a bill: the PR's review fee was settled on 5 November and this is the follow-up I said I would do when the branch landed. |
|
@pursekeeper thanks for reviewing the code so far. Some notes: "1. extra.workOptional never reaches the 402 unless the route config sets it by hand." "2. The check order lets a confirmed block at the wrong amount answer error_block_already_confirmed with its hash." "3. error_frontier_not_confirmed is in the spec but not in the code."
|
|
Thanks. Item 1 is filed as #7: the scheme's On item 2, I would reuse Item 3 in 37d7cf6 reads right to me. |
The Facilitator advertises `extra.workOptional: true` in its SupportedKind when it can generate work, and the spec's Appendix says a Resource Server whose Facilitator does so MUST set `extra.workOptional: true` in the PaymentRequirements it serves. `enhancePaymentRequirements` voided `supportedKind` and copied only the route's own `extra`, and @x402/core does not merge `supportedKind.extra` either, so the flag never reached the 402 unless the route config repeated it by hand; the client then generated work itself. Forward `supportedKind.extra.workOptional` into `extra` when it is `true` and the route has not set the key. Adds a server unit test that mocks a Facilitator advertising work generation and checks the built requirement carries `extra.workOptional: true` for a route with no `extra`. Filed at kilkelly's request in the #5 thread (review item 1). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
@pursekeeper as regards item 2, I have just added an "Amount Check" just after the "Asset Check" in the Verification code for Facilitator 878606c If this is satisfactory I can then go ahead and add this to the draft spec also to get everything back in sync. Also I got your PR merged, thanks very much 👍 |
|
Satisfactory. With the amount comparison at check 6, before the confirmed lookup, a confirmed block at the wrong amount now answers Two small things, neither blocking: the same |
Summary
Fixes four correctness and security issues across
server/scheme.tsandfacilitator/scheme.ts:ServerExactNanoScheme.parsePricepassing theAssetAmountobject instead ofprice.amount(src/typescript/server/scheme.ts):pricewas anAssetAmountobject ({ amount, asset, extra }),parsePricecalledparseAmountToString(price), which evaluatedString(price)to"[object Object]"and passed it toconvertNanoToRaw, throwing[Nano.Math.nanoToRaw]: nano parameter is not a valid number.convertNanoToRawwithout checking whetherprice.amountwas already in raw units (!STRING_DECIMAL.safeParse(price_to_string).success).parseAmountToString(price.amount)is used and raw-unit strings are preserved just like top-level string/number prices.FacilitatorExactNanoScheme.verifynot verifying signedblock.linkagainstrequirements.payTo(src/typescript/facilitator/scheme.ts):verify()only comparedrequirements.payToagainst the JSON fieldexactNanoPayload.block.link_as_account. Because a Nano state block hashes and signs the 32-byte hexblock.linkfield (notlink_as_account),verify()now also verifies thatNano.Crypto.derivePublicKeyFromAccount({ account: normalizedPayTo })matchesexactNanoPayload.block.link.FacilitatorExactNanoScheme.verifynot checkingblock.previous === frontier:verify()fetched the account'sfrontierand validatedworkagainstfrontier, but did not verify thatexactNanoPayload.block.previousmatchesfrontier. A historical/replayed signed block with work recomputed forfrontierwould passverify()and then fail duringsettle().FacilitatorExactNanoScheme.verifynot checking thatbalance - block.balance >= payAmount:verify()only checkedBigNumber(balance).minus(payAmount).isNegative()(whether the payer's account holds at leastpayAmount), without inspectingexactNanoPayload.block.balance. A payer with sufficient account balance could submit a state block that debits0or1raw (or even increases balance) and passverify().Verification
src/typescript/test/unit/index.test.tscoveringAssetAmountparsing (both nano decimal and raw units) and facilitator verification rejection of mismatchedblock.link, staleblock.previous, and underpaidblock.balance.Nano payout address:
nano_14m3x4wp8bcn3cfhhiwwxx1esbhd7zsj73tqhthctstp64kt3og1mzgkxe3j