From e7ae5baa16b50d6e83fe8bd3fc01054bf17471ba Mon Sep 17 00:00:00 2001 From: Harshil-1603 <181649964+Harshil-1603@users.noreply.github.com> Date: Tue, 13 Oct 2026 00:38:45 +0530 Subject: [PATCH 1/2] fix: validate BIP32 derivation path indices --- src/embit/bip32.py | 15 +++++++--- tests/tests/__init__.py | 1 + tests/tests/test_bip32_paths.py | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/tests/test_bip32_paths.py diff --git a/src/embit/bip32.py b/src/embit/bip32.py index 6895afc4..e7bdadd9 100644 --- a/src/embit/bip32.py +++ b/src/embit/bip32.py @@ -283,10 +283,17 @@ def detect_version(path, default="xprv", network=None) -> bytes: def _parse_der_item(e: str) -> int: - if e[-1] in {"h", "H", "'"}: - return int(e[:-1]) + HARDENED_INDEX - else: - return int(e) + if not e: + raise ValueError("Empty derivation index") + hardened = e[-1] in {"h", "H", "'"} + digits = e[:-1] if hardened else e + if not digits or any(c not in "0123456789" for c in digits): + raise ValueError("Invalid derivation index") + index = int(digits) + limit = HARDENED_INDEX if hardened else 2 * HARDENED_INDEX + if index >= limit: + raise ValueError("Derivation index out of range") + return index + HARDENED_INDEX if hardened else index def parse_path(path: str) -> list: diff --git a/tests/tests/__init__.py b/tests/tests/__init__.py index 5049803f..ad4db69f 100644 --- a/tests/tests/__init__.py +++ b/tests/tests/__init__.py @@ -3,6 +3,7 @@ from .test_base58 import * from .test_bech32 import * from .test_bip32 import * +from .test_bip32_paths import * from .test_psbt import * from .test_psbt_versions import * from .test_bip39 import * diff --git a/tests/tests/test_bip32_paths.py b/tests/tests/test_bip32_paths.py new file mode 100644 index 00000000..1c4f490a --- /dev/null +++ b/tests/tests/test_bip32_paths.py @@ -0,0 +1,53 @@ +from unittest import TestCase + +from embit.bip32 import HDKey, parse_path + + +class Bip32PathTest(TestCase): + def test_valid_paths(self): + vectors = [ + ("m", []), + ("m/", []), + ("0/1", [0, 1]), + ("m/0/1/", [0, 1]), + ("m/44h/1H/0'/0/32", [0x8000002C, 0x80000001, 0x80000000, 0, 32]), + ("m/2147483647", [0x7FFFFFFF]), + ("m/2147483648", [0x80000000]), + ("m/4294967295", [0xFFFFFFFF]), + ("m/2147483647h", [0xFFFFFFFF]), + ("m/2147483647H", [0xFFFFFFFF]), + ("m/2147483647'", [0xFFFFFFFF]), + ] + for path, expected in vectors: + self.assertEqual(parse_path(path), expected) + + def test_invalid_paths(self): + paths = [ + "", + "/0", + "m//1/", + "m/-1", + "m/-10h", + "m/-10H", + "m/-10'", + "m/2147483648h", + "m/2147483648H", + "m/2147483648'", + "m/4294967296", + "m/+1/", + "m/ 1", + "m/1 h", + "m/1h ", + "m/\n1/2", + "m/h", + "m/1.0", + "m/\u0661", + ] + for path in paths: + with self.assertRaises(ValueError): + parse_path(path) + + def test_derive_rejects_negative_hardened_index(self): + root = HDKey.from_seed(bytes.fromhex("000102030405060708090a0b0c0d0e0f")) + with self.assertRaises(ValueError): + root.derive("m/-10h") From 7f78a55d56ea4b9ee1e5685834b6f62854c88195 Mon Sep 17 00:00:00 2001 From: Harshil-1603 <181649964+Harshil-1603@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:06:00 +0530 Subject: [PATCH 2/2] Use MicroPython-compatible decoding in BIP32 path tests Apply the concrete reviewer request without changing runtime behavior. Assisted-by: OpenAI Codex --- tests/tests/test_bip32_paths.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/tests/test_bip32_paths.py b/tests/tests/test_bip32_paths.py index 1c4f490a..4aa26498 100644 --- a/tests/tests/test_bip32_paths.py +++ b/tests/tests/test_bip32_paths.py @@ -1,3 +1,4 @@ +from binascii import unhexlify from unittest import TestCase from embit.bip32 import HDKey, parse_path @@ -48,6 +49,6 @@ def test_invalid_paths(self): parse_path(path) def test_derive_rejects_negative_hardened_index(self): - root = HDKey.from_seed(bytes.fromhex("000102030405060708090a0b0c0d0e0f")) + root = HDKey.from_seed(unhexlify("000102030405060708090a0b0c0d0e0f")) with self.assertRaises(ValueError): root.derive("m/-10h")