From 37e967144c658194895a58fd2276da7f74797ebf Mon Sep 17 00:00:00 2001 From: Ferdinando Ametrano Date: Tue, 8 Sep 2026 19:23:56 +0200 Subject: [PATCH] descriptor: reject trailing characters after the closing parenthesis The parser located a descriptor's closing parenthesis with rindex but did not verify it was the final character, so trailing bytes after it were silently discarded and pkh()garbage was accepted as pkh(). Require the closing parenthesis to end the expression and raise ValueError naming the trailing text. --- electrum/descriptor.py | 4 +++- tests/test_descriptor.py | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/electrum/descriptor.py b/electrum/descriptor.py index d23213e64d26..4311112f0af5 100644 --- a/electrum/descriptor.py +++ b/electrum/descriptor.py @@ -841,9 +841,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 closing parenthesis".format(s[end + 1:])) + return s[0:start], s[start + 1:end] def _get_const(s: str, const: str) -> str: diff --git a/tests/test_descriptor.py b/tests/test_descriptor.py index 35bd50493b31..b1f61b977ab8 100644 --- a/tests/test_descriptor.py +++ b/tests/test_descriptor.py @@ -181,6 +181,14 @@ def test_parse_descriptor_with_key_at_end_without_origin(self): def test_parse_empty_descriptor(self): self.assertRaises(ValueError, parse_descriptor, "") + @as_testnet + def test_parse_descriptor_rejects_trailing_characters(self): + key = "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B" + # trailing characters after the closing parenthesis must not be silently ignored + self.assertRaises(ValueError, parse_descriptor, "wpkh({}/0/0)abc".format(key)) + # the same applies to the closing parenthesis of a nested descriptor + self.assertRaises(ValueError, parse_descriptor, "sh(wpkh({}/0/0))x".format(key)) + @as_testnet def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84'/1'/0']tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)"