Skip to content

fix(ans): reject non-hex input in fromHex instead of silently decoding to 0#91

Merged
ralyodio merged 1 commit into
profullstack:masterfrom
AliaksandrNazaruk:fix/ans-fromhex-validation
Jul 2, 2026
Merged

fix(ans): reject non-hex input in fromHex instead of silently decoding to 0#91
ralyodio merged 1 commit into
profullstack:masterfrom
AliaksandrNazaruk:fix/ans-fromhex-validation

Conversation

@AliaksandrNazaruk

Copy link
Copy Markdown
Contributor

Problem

fromHex() (packages/ans/src/bytes.ts) validated only that the input was even-length, then used parseInt(pair, 16) per byte. parseInt returns NaN for a non-hex pair, and a Uint8Array stores NaN as 0 — so fromHex('zzzz') silently returned [0, 0] instead of failing (see #90).

Fix

Reject non-hex characters before decoding (the * quantifier keeps empty input valid):

if (!/^[0-9a-fA-F]*$/.test(clean)) throw new Error('invalid hex string');

Tests

Added bytes.test.ts covering valid decode (with/without 0x), toHex/fromHex round-trip, odd-length input, and non-hex input ('zzzz', '00gg', '0xnothex').

Fixes #90.

…g to 0

fromHex validated only that the string was even-length, then used
parseInt(pair, 16) per byte. parseInt returns NaN for a non-hex pair, and a
Uint8Array stores NaN as 0 — so fromHex('zzzz') silently returned [0, 0]
instead of failing. Add a hex-charset check (allowing empty input) so bad
input throws. Add bytes.test.ts covering valid decode, round-trip, odd-length
and non-hex cases.
@ralyodio ralyodio merged commit 8107140 into profullstack:master Jul 2, 2026
5 checks passed
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.

fromHex silently decodes non-hex input to zero bytes instead of throwing

2 participants