Skip to content

Reject sign characters in Base16 decoding - #79

Merged
ianopolous merged 1 commit into
multiformats:masterfrom
amalsaidov:fix-base16-reject-sign-chars
Dec 3, 2026
Merged

ianopolous merged 1 commit into
multiformats:masterfrom
amalsaidov:fix-base16-reject-sign-chars

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

Problem

Base16.decode decodes each two-character pair with Integer.parseInt(pair, 16). Integer.parseInt accepts a leading + or -, so pairs containing a sign are parsed as numbers instead of being rejected, even though + and - are not part of the Base16 alphabet (0-9a-f).

Before this change:

Multibase.decode("f-1");  // returns [0xFF] instead of throwing
Multibase.decode("f+a");  // returns [0x0a] instead of throwing

This is inconsistent with the existing behaviour for other invalid characters (e.g. f0g correctly throws) and with the multibase spec, whose base16 alphabet is 0123456789abcdef.

Fix

Decode each hex nibble directly and throw IllegalArgumentException on any character outside 0-9, a-f, A-F. Valid input (including upper-case, as previously accepted by parseInt) is unchanged.

Test

Added f-1 and f+a to MultibaseBadInputsTest. Both fail before the fix (decode returns a value) and pass after (decode throws). Full suite is green (123 tests) and spotless:check passes.

Base16.decode used Integer.parseInt(pair, 16), which accepts a leading
'+' or '-'. As a result Multibase.decode("f-1") returned 0xFF and
"f+a" returned 0x0a, silently accepting characters outside the Base16
alphabet (0-9a-f). Decode each hex nibble directly and throw
IllegalArgumentException on any non-hex character.
@ianopolous

Copy link
Copy Markdown
Member

Thanks!

@ianopolous
ianopolous merged commit d719780 into multiformats:master Dec 3, 2026
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