Skip to content

fix: reject underscores in BIP32 path indexes - #860

Open
SashaMIT wants to merge 2 commits into
bitcoin-core:masterfrom
SashaMIT:codered-bip32-reject-underscore
Open

SashaMIT wants to merge 2 commits into
bitcoin-core:masterfrom
SashaMIT:codered-bip32-reject-underscore

Conversation

@SashaMIT

Copy link
Copy Markdown

Summary

parse_path used Python int() on each path component. int("1_0") is 10, so m/1_0 derived index 10 instead of failing. The caller already has the path string. An underscore is not a BIP32 character, and it was changing the index. Components now have to be digits, with the existing h or ' harden suffix.

Test plan

  • parse_path("m/1_0") returned [10] before the change
  • test_parse_path_rejects_underscore and test_deriv_path pass after the change

Made with Cursor

Python int() accepts 1_0 as 10, so parse_path("m/1_0") derived index 10. Path components now have to be digits, with the existing harden suffix.
@fametrano

Copy link
Copy Markdown
Contributor

Underscores are fixed, but str.isdigit() also accepts non-ASCII digits: on 0191bdd, parse_path("m/١") returns [1] and parse_path("m/1h") returns [2147483649].

The range is not checked either: m/2147483648h gives 2147483648, the same index as m/0h, and m/4294967296 gives 4294967296. Bitcoin Core's ParseHDKeypath rejects all four. Checking body.isascii() and body.isdigit() and then index < 2**31 would close both.

isdigit() accepts Arabic and fullwidth digits, and an index at 2^31 collides with a hardened zero.
@SashaMIT

SashaMIT commented Oct 9, 2026

Copy link
Copy Markdown
Author

Those four are rejected now. Arabic and fullwidth digits are not ASCII, and an index at or above 2^31 is rejected before the hardened bit is set. m/2147483647 and m/2147483647h still parse. Commit cee9680. The new test fails on 0191bdd.

@fametrano

Copy link
Copy Markdown
Contributor

Tested ACK cee9680

python3 -m unittest test.test_bip32 passes, and the new non-ASCII and range test fails with 0191bdd's hwilib/key.py. m/1', m/-5 and m/2147483647h still parse; m/+1, m/ 1 and m/0hh are rejected.

@SashaMIT

SashaMIT commented Oct 9, 2026

Copy link
Copy Markdown
Author

Thanks for the tested ACK, and for checking the apostrophe, the sign, and the double harden on top of the non-ASCII cases.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants