View Issue Details

IDProjectCategoryView StatusLast Update
0012194Talermerchant backendpublic2026-11-18 15:02
Reporterbot-cg [agent]  
PrioritynormalSeverityminor 
Status new 
Summary0012194: POST /orders/$ID/pay records surplus input tokens as spent without validating them
Descriptionphase_validate_tokens() (taler-merchant-httpd_post-orders-ORDER_ID-pay.c) validates the first sum(input.count) entries of the wallet's tokens array, through find_valid_input_tokens(), and none at all for a v0 contract. phase_execute_pay_transaction() then calls insert_used_token() for all parse_pay.tokens_cnt entries. Nothing compares tokens_cnt with the number of tokens the selected choice takes, so up to 64 extra entries are stored as spent tokens although their issue and use signatures were never checked. The only check they meet is that h_issue names a token family key of the instance.

This is not a memory-safety problem, but it lets a payment write unverified used-token rows, which can mark a token_pub as spent without its owner's signature.

The attached patch refuses the payment with TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_COUNT_MISMATCH when the wallet sends more tokens than the choice takes, or any for a v0 contract. wallet-core sends exactly the tokens it selected for the choice's inputs, so it is not affected.
TagsNo tags attached.
Attached Files
12194-merchant-refuse-pay-with-more-tokens-than-the-choice.patch (2,322 bytes)   
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 16:01:54 +0100
Subject: [PATCH] merchant: refuse /pay with more tokens than the choice takes

Every token recorded as spent must have been validated against one of
the choice's inputs.

Issue: https://bugs.taler.net/n/12194

Assisted by LLM
---
 ...-merchant-httpd_post-orders-ORDER_ID-pay.c | 26 ++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c b/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
index 38c2854a..f6a9f728 100644
--- a/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
+++ b/src/backend/taler-merchant-httpd_post-orders-ORDER_ID-pay.c
@@ -4220,7 +4220,18 @@ phase_validate_tokens (struct PayContext *pc)
   switch (pc->check_contract.contract_terms->pc->base->version)
   {
   case TALER_MERCHANT_CONTRACT_VERSION_0:
-    /* No tokens to validate */
+    /* No tokens to validate, so none may be spent */
+    if (0 != pc->parse_pay.tokens_cnt)
+    {
+      GNUNET_break_op (0);
+      pay_end (pc,
+               TALER_MHD_reply_with_error (
+                 pc->connection,
+                 MHD_HTTP_BAD_REQUEST,
+                 TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_COUNT_MISMATCH,
+                 "contract takes no tokens"));
+      return;
+    }
     pc->phase = PP_COMPUTE_MONEY_POTS;
     pc->validate_tokens.max_fee
       = pc->check_contract.contract_terms->pc->details.v0.max_fee;
@@ -4295,6 +4306,19 @@ phase_validate_tokens (struct PayContext *pc)
           wallet_off += input->details.token.count;
         }
       }
+      if (wallet_off != pc->parse_pay.tokens_cnt)
+      {
+        /* every token we record as spent must have been validated
+           against one of the inputs above */
+        GNUNET_break_op (0);
+        pay_end (pc,
+                 TALER_MHD_reply_with_error (
+                   pc->connection,
+                   MHD_HTTP_BAD_REQUEST,
+                   TALER_EC_MERCHANT_POST_ORDERS_ID_PAY_TOKEN_COUNT_MISMATCH,
+                   "more tokens than the choice takes"));
+        return;
+      }
 
       /* calculate pc->output_tokens_len */
       output_off = 0;
-- 
2.47.3

Required notifications

Address or linkRemarkAdded byNotifiedActions

Relationships

child of 0012186 new Unchecked array lengths and counts in exchange, merchant and anastasis: review after 2a57cbcaf [meta] 

Activities

Issue History

Date ModifiedUsernameFieldChange
2026-11-18 15:01bot-cgNew Issue
2026-11-18 15:01bot-cgRelationship addedchild of 0012186
2026-11-18 15:01bot-cgImplementer => Bohdan
2026-11-18 15:01bot-cgReviewer => sebasjm
2026-11-18 15:01bot-cgRelease signed by => Florian Dold
2026-11-18 15:01bot-cgDeployed by => Bohdan
2026-11-18 15:01bot-cgVerifier => sebasjm
2026-11-18 15:01bot-cgCommunicator => CM_7
2026-11-18 15:02bot-cgFile Added: 12194-merchant-refuse-pay-with-more-tokens-than-the-choice.patch