From 5afc92d8a11a1d8b5945c8f0d8af3b3363670ea4 Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Tue, 8 Sep 2026 19:23:44 +0200 Subject: [PATCH] descriptor: reject trailing characters after closing parenthesis _get_func_expr located the closing parenthesis with rindex but never checked that it was the final character, so any content following it was silently discarded and a descriptor such as pkh()garbage parsed as pkh(). Require the closing parenthesis to be the last character and raise ValueError naming the trailing text otherwise, matching Bitcoin Core's rejection of trailing bytes. --- hwilib/descriptor.py | 4 +++- test/test_descriptor.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index 1373ad2d2..1c05ad225 100644 --- a/hwilib/descriptor.py +++ b/hwilib/descriptor.py @@ -543,9 +543,11 @@ def _get_func_expr(s: str) -> Tuple[str, str]: try: start = s.index("(") end = s.rindex(")") - return s[0:start], s[start + 1:end] except ValueError: raise ValueError("A matching pair of parentheses cannot be found") + if end != len(s) - 1: + raise ValueError("Trailing characters '{}' after descriptor".format(s[end + 1:])) + return s[0:start], s[start + 1:end] def _get_const(s: str, const: str) -> str: diff --git a/test/test_descriptor.py b/test/test_descriptor.py index dca4fbe80..1fd01c93f 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -165,6 +165,13 @@ def test_parse_descriptor_with_key_at_end_without_origin(self): def test_parse_empty_descriptor(self): self.assertRaises(ValueError, parse_descriptor, "") + def test_parse_descriptor_trailing_characters(self): + key = "02c97dc3f4420402e01a113984311bf4a1b8de376cac0bdcfaf1b3ac81f13433c7" + with self.assertRaisesRegex(ValueError, "Trailing characters"): + parse_descriptor("pkh({})abc".format(key)) + with self.assertRaisesRegex(ValueError, "Trailing characters"): + parse_descriptor("sh(wpkh({}))x".format(key)) + def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84h/1h/0h]tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" desc = parse_descriptor(d)