Skip to content

Reject non-ASCII characters in Bech32 strings - #1375

Open
allenpiscitello wants to merge 1 commit into
MetacoSA:masterfrom
allenpiscitello:fix/bech32-reject-non-ascii
Open

allenpiscitello wants to merge 1 commit into
MetacoSA:masterfrom
allenpiscitello:fix/bech32-reject-non-ascii

Conversation

@allenpiscitello

Copy link
Copy Markdown
Contributor

BIP 173 says each character of a Bech32 string must be US-ASCII in the range 33-126, and BIP 350 keeps that rule for Bech32m. Bech32Encoder.DecodeDataCore never checks it. It lowercases the string, then turns it into bytes with ASCIIEncoder.DecodeData, which keeps only the low byte of each character. So a non-ASCII character whose low byte is a valid Bech32 character decodes as that character. For example, U+0171 'ű' becomes 0x71 'q', and bcrt1űqqqqp399… decodes to the same witness program as bcrt1qqqqqp399… with a valid checksum. ToLowerInvariant gives a second path: U+212A KELVIN SIGN lowercases to 'k'. The BIP 173 and BIP 350 vectors with 0x20, 0x7F and 0x80 in the HRP also decode today. The existing test only rejects them because it changes a data character first.

DecodeDataCore now throws a FormatException for any character outside 33-126, before the case check and the lowercasing. Decode, DecodeDataRaw, segwit and taproot address parsing and Network.Parse all go through it.

Blech32Encoder in NBitcoin.Altcoins already had this check, but it ran on the bytes after the ASCII conversion, so it had the same hole. It now checks the characters.

I left ASCIIEncoder alone. It is also used for the PSBT magic bytes, P2P command names and user agents, so making it strict would change behaviour well outside Bech32.

New tests cover the out-of-range BIP 173 and BIP 350 vectors, the 'ű' alias of a regtest address (through the encoder and BitcoinAddress.Create), a Kelvin sign in the HRP, and the 'ű' alias of a Liquid blinded address. They also check that the valid addresses still decode. The Bech32, address and Taproot tests pass on net10.0, except the ones that download bitcoind, which fail the same way without this change.

🤖 Generated with Claude Code

BIP 173 requires every character of a Bech32 string to be US-ASCII in
the range 33-126, and BIP 350 keeps that rule for Bech32m.
Bech32Encoder.DecodeDataCore did not check it. It lowercased the string
and converted it with the ASCII encoder, which keeps only the low byte
of each character. A non-ASCII character whose low byte is a valid
Bech32 character, such as U+0171 (low byte 0x71, 'q'), was decoded as
that character with a valid checksum. ToLowerInvariant also maps some
non-ASCII characters to ASCII, such as U+212A KELVIN SIGN to 'k'.

DecodeDataCore now rejects any character outside 33-126 before the case
check and the lowercasing. Blech32Encoder already had this check, but
ran it on the converted bytes, so it now checks the characters instead.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant