commit 1badd237f1c254dcb4b4244f1f7cb400eb712451
parent 77cda0dc78ef8d0dd714cf48753452d38f84ff36
Author: Florian Dold <dold@taler.net>
Date: Thu, 26 Nov 2026 04:11:48 +0100
wallet-core: reject a payment if the debit changed after review
Choosing coins at confirmation can give a different total from the one
the payer saw. Let the client supply the expected debit and check it
before spending the coins.
Diffstat:
4 files changed, 68 insertions(+), 2 deletions(-)
diff --git a/packages/taler-util/src/types-taler-wallet.ts b/packages/taler-util/src/types-taler-wallet.ts
@@ -3101,6 +3101,8 @@ export const codecForPreparePayTemplateRequest =
export interface ConfirmPayRequest {
transactionId: TransactionIdStr;
+ /** When supplied, the effective debit of a new payment must match the reviewed amount. */
+ expectedAmountEffective?: AmountString;
useDonau?: boolean;
sessionId?: string;
forcedCoinSel?: ForcedCoinSel;
@@ -3129,6 +3131,7 @@ export interface ConfirmPayRequest {
export const codecForConfirmPayRequest = (): Codec<ConfirmPayRequest> =>
buildCodecForObject<ConfirmPayRequest>()
.property("transactionId", codecForTransactionIdStr())
+ .property("expectedAmountEffective", codecOptional(codecForAmountString()))
.property("sessionId", codecOptional(codecForString()))
.property("forcedCoinSel", codecOptional(codecForForcedCoinSel()))
.property("forcedTokenSel", codecOptional(codecForBoolean()))
diff --git a/packages/taler-wallet-core/src/pay-merchant.test.ts b/packages/taler-wallet-core/src/pay-merchant.test.ts
@@ -93,6 +93,7 @@ import {
applyPaySessionRequest,
classifyMerchantAbortRecoveryEvidence,
calculateDefaultChoice,
+ confirmPay,
cleanupFailedClaimsOnStartup,
computePayMerchantTransactionActions,
computePayMerchantTransactionState,
@@ -238,6 +239,7 @@ function makeDownloadedPurchase(
async function upsertTestContractTerms(
tx: WalletDbTransaction,
purchase: WalletPurchase,
+ amount: AmountString = "TESTKUDOS:1",
): Promise<void> {
if (!purchase.download) {
throw Error("test purchase must be downloaded");
@@ -246,7 +248,7 @@ async function upsertTestContractTerms(
h: purchase.download.contractTermsHash,
contractTermsRaw: {
version: MerchantContractVersion.V0,
- amount: "TESTKUDOS:1",
+ amount,
max_fee: "TESTKUDOS:0",
order_id: purchase.orderId,
nonce: "nonce",
@@ -267,6 +269,54 @@ async function upsertTestContractTerms(
});
}
+test("merchant confirmation binds the reviewed wallet debit before spending", async () => {
+ const runner = await makeIdbRunner();
+ try {
+ const purchase = makeDownloadedPurchase(
+ "reviewed-debit",
+ PurchaseStatus.DialogProposed,
+ );
+ delete purchase.payInfo;
+ await runner.runReadWriteTx(async (tx) => {
+ await upsertTestContractTerms(tx, purchase, "TESTKUDOS:0");
+ await tx.upsertPurchase(purchase);
+ });
+ const wex = {
+ ws: { devExperimentState: {}, config: { testing: {} } },
+ runWalletDbTx: runner.runReadWriteTx.bind(runner),
+ taskScheduler: { resetTask() {}, startShepherdTask() {} },
+ } as unknown as WalletExecutionContext;
+ const transactionId = "txn:payment:reviewed-debit";
+ const load = () =>
+ runner.runReadWriteTx((tx) => tx.getPurchase(purchase.proposalId));
+ for (const expectedAmountEffective of ["TESTKUDOS:1", "EUR:0"] as const) {
+ await assert.rejects(
+ confirmPay(wex, {
+ transactionId,
+ expectedAmountEffective,
+ noWait: true,
+ }),
+ (cause: unknown) =>
+ cause instanceof TalerError &&
+ cause.errorDetail.code ===
+ TalerErrorCode.WALLET_CORE_API_BAD_REQUEST &&
+ cause.errorDetail.parameter === "expectedAmountEffective",
+ );
+ const stored = await load();
+ assert.equal(stored?.payInfo, undefined);
+ assert.equal(stored?.purchaseStatus, PurchaseStatus.DialogProposed);
+ }
+ await confirmPay(wex, {
+ transactionId,
+ expectedAmountEffective: "TESTKUDOS:0.00",
+ noWait: true,
+ });
+ assert.equal((await load())?.payInfo?.totalPayCost, "TESTKUDOS:0");
+ } finally {
+ await runner.close();
+ }
+});
+
function makeTestRefundGroup(
proposalId: string,
refundGroupId: string,
diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts
@@ -3539,6 +3539,7 @@ export async function confirmPay(
wex: WalletExecutionContext,
args: {
transactionId: string;
+ expectedAmountEffective?: AmountString;
sessionIdOverride?: string;
forcedCoinSel?: ForcedCoinSel;
choiceIndex?: number;
@@ -3810,6 +3811,17 @@ export async function confirmPay(
logger.trace("coin selection result", selectCoinsResult);
const payCostInfo = await getTotalPaymentCostInTx(wex, tx, currency, coins);
+ const totalPayCost = Amounts.stringify(payCostInfo);
+ if (
+ args.expectedAmountEffective !== undefined &&
+ totalPayCost !== Amounts.stringify(args.expectedAmountEffective)
+ ) {
+ throw TalerError.fromDetail(
+ TalerErrorCode.WALLET_CORE_API_BAD_REQUEST,
+ { parameter: "expectedAmountEffective" },
+ "the effective payment amount changed since review",
+ );
+ }
const confRes = await tx.getConfig(ConfigRecordKey.DonauConfig);
@@ -3842,7 +3854,7 @@ export async function confirmPay(
}
p.payInfo = {
- totalPayCost: Amounts.stringify(payCostInfo),
+ totalPayCost,
};
if (selectTokensResult?.type === "success") {
const tokens = selectTokensResult.tokens;
diff --git a/packages/taler-wallet-core/src/requests.ts b/packages/taler-wallet-core/src/requests.ts
@@ -1456,6 +1456,7 @@ async function handleConfirmPay(
): Promise<ConfirmPayResult> {
return await confirmPay(wex, {
transactionId: req.transactionId,
+ expectedAmountEffective: req.expectedAmountEffective,
choiceIndex: req.choiceIndex,
forcedCoinSel: undefined,
sessionIdOverride: req.sessionId,