diff --git a/hwilib/descriptor.py b/hwilib/descriptor.py index 1373ad2d2..352b49c50 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]) @@ -167,7 +169,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) @@ -558,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:] @@ -599,6 +608,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 dca4fbe80..865f1507d 100755 --- a/test/test_descriptor.py +++ b/test/test_descriptor.py @@ -165,6 +165,15 @@ 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*)") + 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)" desc = parse_descriptor(d) @@ -350,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()