From d4e87d8fbf7c5d92d9b5722eb71077952f0993aa Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sat, 24 Oct 2026 05:37:14 +0000 Subject: [PATCH 1/4] descriptor: reject malformed ranged derivation suffixes Add regression coverage for /0*, which previously lost the zero and was silently treated as a bare range marker. --- hwilib/descriptor.py | 7 ++++++- test/test_descriptor.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index 1373ad2d2..b546b83da 100644 --- a/hwilib/descriptor.py +++ b/hwilib/descriptor.py @@ -167,7 +167,12 @@ def parse(cls, s: str, key_expr_index: int) -> 'PubkeyProvider': path_str = s[slash_idx + 1:] ranged = path_str.endswith("*") if ranged: - path_str = path_str[:-2] + if path_str == "*": + path_str = "" + elif path_str.endswith("/*"): + path_str = path_str[:-2] + else: + raise ValueError(f"Invalid ranged derivation path: /{path_str}") if len(path_str) > 0: deriv_path = parse_multipath(path_str) diff --git a/test/test_descriptor.py b/test/test_descriptor.py index dca4fbe80..1dc176481 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -165,6 +165,11 @@ 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_invalid_key_expressions(self): + xpub = "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B" + with self.assertRaisesRegex(ValueError, "Invalid ranged derivation path"): + parse_descriptor(f"wpkh({xpub}/0*)") + def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84h/1h/0h]tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" desc = parse_descriptor(d) From 5c0fb65f989570c4c38a550f9c96ff97666e6cf2 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sat, 24 Oct 2026 05:26:38 +0000 Subject: [PATCH 2/4] descriptor: reject empty key expressions Cover an empty entry inside a multisig key list and raise ValueError instead of indexing an empty string. --- hwilib/descriptor.py | 2 ++ test/test_descriptor.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index b546b83da..7ccdac39a 100644 --- a/hwilib/descriptor.py +++ b/hwilib/descriptor.py @@ -155,6 +155,8 @@ def parse(cls, s: str, key_expr_index: int) -> 'PubkeyProvider': deriv_path = None ranged = False + if not s: + raise ValueError("Empty key expression") if s[0] == "[": end = s.index("]") origin = KeyOriginInfo.from_string(s[1:end]) diff --git a/test/test_descriptor.py b/test/test_descriptor.py index 1dc176481..2d9f8ff10 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -169,6 +169,8 @@ def test_parse_invalid_key_expressions(self): xpub = "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B" with self.assertRaisesRegex(ValueError, "Invalid ranged derivation path"): parse_descriptor(f"wpkh({xpub}/0*)") + with self.assertRaisesRegex(ValueError, "Empty key expression"): + parse_descriptor(f"wsh(multi(1,{xpub}/0/*,,{xpub}/1/*))") def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84h/1h/0h]tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" From 83ed8dd658d198de13ab7f4d1dced1e5a168c226 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sat, 24 Oct 2026 05:26:39 +0000 Subject: [PATCH 3/4] descriptor: reject trailing commas in key lists Cover a multisig key list with a trailing comma, which previously dropped the empty final entry. --- hwilib/descriptor.py | 2 ++ test/test_descriptor.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index 7ccdac39a..33120ce9b 100644 --- a/hwilib/descriptor.py +++ b/hwilib/descriptor.py @@ -606,6 +606,8 @@ def parse_pubkey(expr: str, key_expr_index: int) -> Tuple['PubkeyProvider', str, if comma_idx != -1: end = comma_idx next_expr = expr[end + 1:] + if not next_expr: + raise ValueError("Trailing comma after key expression") return PubkeyProvider.parse(expr[:end], key_expr_index), next_expr, (key_expr_index + 1) diff --git a/test/test_descriptor.py b/test/test_descriptor.py index 2d9f8ff10..7b495ec20 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -171,6 +171,8 @@ def test_parse_invalid_key_expressions(self): parse_descriptor(f"wpkh({xpub}/0*)") with self.assertRaisesRegex(ValueError, "Empty key expression"): parse_descriptor(f"wsh(multi(1,{xpub}/0/*,,{xpub}/1/*))") + with self.assertRaisesRegex(ValueError, "Trailing comma"): + parse_descriptor(f"wsh(multi(1,{xpub}/0/*,))") def test_parse_descriptor_replace_h(self): d = "wpkh([00000001/84h/1h/0h]tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0/0)" From 5db59a64e74271944399dd854e6450816de1c1fe Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sat, 24 Oct 2026 05:26:41 +0000 Subject: [PATCH 4/4] descriptor: report truncated taproot trees as invalid Add a regression for a taproot tree missing its closing brace. Report the missing delimiter with ValueError rather than leaking an IndexError. --- hwilib/descriptor.py | 2 ++ test/test_descriptor.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index 33120ce9b..352b49c50 100644 --- a/hwilib/descriptor.py +++ b/hwilib/descriptor.py @@ -565,6 +565,8 @@ def _get_const(s: str, const: str) -> str: :return: The remainder of the string without the constant character :raises: ValueError: if the first character is not the constant character """ + if not s: + raise ValueError(f"Expected '{const}' but reached the end") if s[0] != const: raise ValueError(f"Expected '{const}' but got '{s[0]}'") return s[1:] diff --git a/test/test_descriptor.py b/test/test_descriptor.py index 7b495ec20..865f1507d 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -359,6 +359,11 @@ def check(descriptor, error): "BIP 388 requires all multipath specifiers to be exactly 2 elements" ) + def test_parse_incomplete_taproot_tree(self): + key = "f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9" + with self.assertRaisesRegex(ValueError, "Expected '}' but reached the end"): + parse_descriptor(f"tr({key},{{pk({key}),pk({key}))") + if __name__ == "__main__": unittest.main()