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)