Skip to content

fix: validate BIP32 derivation path indices - #158

Open
Harshil-1603 wants to merge 2 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/bip32-path-validation-113
Open

Harshil-1603 wants to merge 2 commits into
diybitcoinhardware:masterfrom
Harshil-1603:fix/bip32-path-validation-113

Conversation

@Harshil-1603

Copy link
Copy Markdown

Validate each BIP32 path segment before converting it or adding the hardened offset. Negative hardened indices currently derive a non-hardened child, oversized indices reach serialization, and empty segments raise IndexError.

Reject empty segments, signs, whitespace, non-ASCII digits, and indices outside the uint32 range with ValueError. Hardened suffixes (h, H, and ') accept only values below 2**31. Preserve existing root paths, relative paths, the optional trailing slash, and raw uint32 indices.

Add valid boundary and malformed-path cases, plus a regression through HDKey.derive. Register the new test class with the existing test runner.

Fixes #113.

Validation:

  • The malformed-path and negative-hardened-derivation regressions failed before the fix.
  • PYTHONPATH=src pytest -q: 126 passed, 17 skipped on CPython 3.14 using the Python secp256k1 fallback.
  • Black checks for the changed source and new test file passed; git diff --check passed.
  • MicroPython and the native secp256k1 backend were not tested locally.

@odudex

odudex commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

tACK e7ae5ba

Tested on top of hardened (merges cleanly, full suite passes). Before this change:

  • m/-1h parsed to 2147483647, a valid unhardened index
  • m/2147483648h parsed to 4294967296, outside the 32-bit range
  • m/ 1 and m/1_0 were accepted as 1 and 10

All of these now raise ValueError. Valid paths, and descriptor key origins with and without a path, parse as before.

One small request: the new test uses bytes.fromhex. test_bip32_paths.py is added to tests/tests/__init__.py, so it runs under MicroPython, and none of the tests in that list use bytes.fromhex today. Could you switch it to unhexlify, to be safe on older MicroPython builds?

Non-blocking, possibly for a follow-up: unmarked indices of 2^31 and above are still accepted, as before, so m/2147483648 is hardened index 0. That keeps parse_path backwards compatible, but it also means a descriptor origin like [fp/2147483648]xpub... parses and prints back as 0h.

odudex
odudex previously approved these changes Nov 12, 2026

@odudex odudex left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve this PR, regardless of whether the less significant points raised earlier have been resolved or not.

Apply the concrete reviewer request without changing runtime behavior.

Assisted-by: OpenAI Codex
@jdlcdl

jdlcdl commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

ACK at 7f78a55, intending to merge these 2 commits to "hardened" after pr169
I did resolve a conflict on tests/tests/init.py where test modules are sorted for easier maintenance.

Thank you harshil and odudex. and tanveer

@odudex

odudex commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

ACK at 7f78a55

@notTanveer notTanveer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7f78a55

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BIP32 path parsing inconsistencies: negative indices, hardened overflow and input validation

4 participants