View Issue Details

IDProjectCategoryView StatusLast Update
0012208Talermerchant backendpublic2026-11-18 16:41
Reporterbot-cg [agent]  
PrioritynormalSeveritymajor 
Status new 
Product Versiongit (master) 
Summary0012208: A non-string entry in a token family's trusted_domains/expected_domains leaves a NULL slot that aborts the merchant
DescriptionTMH_token_family_to_contract() (taler-merchant-httpd_token-keys.c) sets trusted_domains_len / expected_domains_len to json_array_size() of the family's extra_data list, but copies only the elements that are strings, leaving NULL in the slots of the others. The contract serializer (src/util/token_family_serialize.c) then appends json_string (NULL) for every slot up to the length, json_array_append_new() fails, and GNUNET_assert aborts the process. This runs whenever the family is put into a contract (order creation, including through a public template) or into a GET /fountain/info reply.

The lists come from extra_data, which POST and PATCH /private/tokenfamilies store without validation, so one stored family keeps aborting the backend for every instance it serves.

Confirmed by reading the code at 495a791d; not reproduced at runtime.

The attached patch makes POST and PATCH refuse extra_data that is not an object or whose domain lists are not arrays of strings (400), makes GET of a family whose stored extra_data is not an object answer 500 instead of asserting in GNUNET_JSON_pack_object_steal(), and makes TMH_token_family_to_contract() set the length to the number of strings actually copied, so families already stored no longer abort.
Additional InformationRecorded as H6 (and, for the non-object extra_data, H5) in the merchant review notes; the input checks are that review's patch 2527, extended here with the fix for rows already stored.
TagsNo tags attached.
Attached Files
12208-merchant-validate-token-family-extra_data-and-copy-o.patch (7,602 bytes)   
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 17:41:26 +0100
Subject: [PATCH] merchant: validate token family extra_data and copy only
 string domains

A non-string entry in trusted_domains or expected_domains left a NULL
slot that the contract serializer asserts on, so reject such input and
skip it in families already stored.

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

Assisted by LLM
---
 ...-private-tokenfamilies-TOKEN_FAMILY_SLUG.c | 10 ++++++
 src/backend/taler-merchant-httpd_helper.c     | 32 +++++++++++++++++++
 src/backend/taler-merchant-httpd_helper.h     | 12 +++++++
 ...-private-tokenfamilies-TOKEN_FAMILY_SLUG.c | 11 +++++++
 ...erchant-httpd_post-private-tokenfamilies.c | 11 +++++++
 src/backend/taler-merchant-httpd_token-keys.c | 10 ++++--
 6 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/src/backend/taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c b/src/backend/taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
index b3d19980..e489433e 100644
--- a/src/backend/taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
+++ b/src/backend/taler-merchant-httpd_get-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
@@ -62,6 +62,16 @@ TMH_private_get_tokenfamilies_SLUG (const struct TMH_RequestHandler *rh,
                                        TALER_EC_MERCHANT_GENERIC_TOKEN_FAMILY_UNKNOWN,
                                        hc->infix);
   }
+  if ( (NULL != details.extra_data) &&
+       (! json_is_object (details.extra_data)) )
+  {
+    GNUNET_break (0);
+    TALER_MERCHANTDB_token_family_details_free (&details);
+    return TALER_MHD_reply_with_error (connection,
+                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
+                                       TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
+                                       "extra_data");
+  }
   {
     char *kind = NULL;
     enum MHD_Result result;
diff --git a/src/backend/taler-merchant-httpd_helper.c b/src/backend/taler-merchant-httpd_helper.c
index 6be5de5a..334f246a 100644
--- a/src/backend/taler-merchant-httpd_helper.c
+++ b/src/backend/taler-merchant-httpd_helper.c
@@ -1368,3 +1368,35 @@ TMH_compute_order_total (const json_t *contract_terms,
   }
   return GNUNET_OK;
 }
+
+
+bool
+TMH_token_family_extra_data_valid (const json_t *extra_data)
+{
+  static const char *const domain_lists[] = {
+    "trusted_domains",
+    "expected_domains",
+    NULL
+  };
+
+  if (! json_is_object (extra_data))
+    return false;
+  for (unsigned int i = 0; NULL != domain_lists[i]; i++)
+  {
+    const json_t *domains = json_object_get (extra_data,
+                                             domain_lists[i]);
+    const json_t *domain;
+    size_t idx;
+
+    if (NULL == domains)
+      continue;
+    if (! json_is_array (domains))
+      return false;
+    json_array_foreach (domains, idx, domain)
+    {
+      if (! json_is_string (domain))
+        return false;
+    }
+  }
+  return true;
+}
diff --git a/src/backend/taler-merchant-httpd_helper.h b/src/backend/taler-merchant-httpd_helper.h
index 7045ac12..dd5f2bfa 100644
--- a/src/backend/taler-merchant-httpd_helper.h
+++ b/src/backend/taler-merchant-httpd_helper.h
@@ -454,4 +454,16 @@ TMH_check_token (const char *token,
                  const char *instance_id,
                  enum TMH_AuthScope *as);
 
+
+/**
+ * Check that @a extra_data of a token family is well-formed: it must
+ * be a JSON object, and its "trusted_domains" and "expected_domains"
+ * members, if present, must be arrays of strings.
+ *
+ * @param extra_data extra data to check
+ * @return true if @a extra_data is well-formed
+ */
+bool
+TMH_token_family_extra_data_valid (const json_t *extra_data);
+
 #endif
diff --git a/src/backend/taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c b/src/backend/taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
index 7870118d..2a9ecba3 100644
--- a/src/backend/taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
+++ b/src/backend/taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c
@@ -89,6 +89,17 @@ TMH_private_patch_token_family_SLUG (const struct TMH_RequestHandler *rh,
              : MHD_NO;
   }
 
+  if ( (NULL != details.extra_data) &&
+       (! TMH_token_family_extra_data_valid (details.extra_data)) )
+  {
+    GNUNET_break_op (0);
+    GNUNET_JSON_parse_free (spec);
+    return TALER_MHD_reply_with_error (connection,
+                                       MHD_HTTP_BAD_REQUEST,
+                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
+                                       "extra_data");
+  }
+
   /* Ensure that valid_after is before valid_before */
   if (GNUNET_TIME_timestamp_cmp (details.valid_after,
                                  >=,
diff --git a/src/backend/taler-merchant-httpd_post-private-tokenfamilies.c b/src/backend/taler-merchant-httpd_post-private-tokenfamilies.c
index 796054cc..569bcf52 100644
--- a/src/backend/taler-merchant-httpd_post-private-tokenfamilies.c
+++ b/src/backend/taler-merchant-httpd_post-private-tokenfamilies.c
@@ -149,6 +149,17 @@ TMH_private_post_token_families (const struct TMH_RequestHandler *rh,
              : MHD_NO;
     }
   }
+  if ( (NULL != details.extra_data) &&
+       (! TMH_token_family_extra_data_valid (details.extra_data)) )
+  {
+    GNUNET_break_op (0);
+    GNUNET_JSON_parse_free (spec);
+    return TALER_MHD_reply_with_error (connection,
+                                       MHD_HTTP_BAD_REQUEST,
+                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
+                                       "extra_data");
+  }
+
   if (no_valid_after)
     details.valid_after = now;
 
diff --git a/src/backend/taler-merchant-httpd_token-keys.c b/src/backend/taler-merchant-httpd_token-keys.c
index 77ad52df..a7f852d5 100644
--- a/src/backend/taler-merchant-httpd_token-keys.c
+++ b/src/backend/taler-merchant-httpd_token-keys.c
@@ -773,6 +773,7 @@ TMH_token_family_to_contract (
                                           "trusted_domains");
       json_t *dom;
       size_t i;
+      size_t n = 0;
 
       new_family.kind = TALER_MERCHANT_CONTRACT_TOKEN_KIND_SUBSCRIPTION;
       new_family.critical = true;
@@ -791,9 +792,11 @@ TMH_token_family_to_contract (
         val = json_string_value (dom);
         GNUNET_break (NULL != val);
         if (NULL != val)
-          new_family.details.subscription.trusted_domains[i]
+          new_family.details.subscription.trusted_domains[n++]
             = GNUNET_strdup (val);
       }
+      /* only the strings were copied */
+      new_family.details.subscription.trusted_domains_len = n;
       break;
     }
   case TALER_MERCHANTDB_TFK_Discount:
@@ -802,6 +805,7 @@ TMH_token_family_to_contract (
                                           "expected_domains");
       json_t *dom;
       size_t i;
+      size_t n = 0;
 
       new_family.kind = TALER_MERCHANT_CONTRACT_TOKEN_KIND_DISCOUNT;
       new_family.critical = false;
@@ -820,9 +824,11 @@ TMH_token_family_to_contract (
         val = json_string_value (dom);
         GNUNET_break (NULL != val);
         if (NULL != val)
-          new_family.details.discount.expected_domains[i]
+          new_family.details.discount.expected_domains[n++]
             = GNUNET_strdup (val);
       }
+      /* only the strings were copied */
+      new_family.details.discount.expected_domains_len = n;
       break;
     }
   }
-- 
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 16:41bot-cgNew Issue
2026-11-18 16:41bot-cgRelationship addedchild of 0012186
2026-11-18 16:41bot-cgImplementer => Bohdan
2026-11-18 16:41bot-cgReviewer => sebasjm
2026-11-18 16:41bot-cgRelease signed by => Florian Dold
2026-11-18 16:41bot-cgDeployed by => Bohdan
2026-11-18 16:41bot-cgVerifier => sebasjm
2026-11-18 16:41bot-cgCommunicator => CM_7
2026-11-18 16:41bot-cgFile Added: 12208-merchant-validate-token-family-extra_data-and-copy-o.patch