Skip to content

Validate hex input strictly - #33

Merged
sc0Vu merged 1 commit into
web3p:masterfrom
sc0Vuai:fix/strict-hex-validation
Sep 23, 2026
Merged

sc0Vu merged 1 commit into
web3p:masterfrom
sc0Vuai:fix/strict-hex-validation

Conversation

@sc0Vuai

@sc0Vuai sc0Vuai commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

decode(), Str::encode(…, 'hex') and Str::decodeHex() accepted malformed hex:

  • Any input containing a single hex digit passed validation, and invalid characters were silently dropped: decode("0x8zz1") → "8z", encode("0xzz") → "zz".
  • Every 0x in the string was removed, not just the prefix, so 0x0x12 was accepted.
  • decode() padded odd-length input: 0x8 → 08.

Now only a leading 0x is stripped and the rest must be hex digits; a trailing newline is also rejected. decode() rejects odd-length input. Encoding still pads odd-length hex ("0x0" → "00"), and uppercase digits are still accepted.

Behavior change: these inputs now throw InvalidArgumentException. That includes encode("0xhello"), which used to produce invalid output (830hello).

Adds regression vectors for invalid and still-valid hex input.

🤖 Generated with Claude Code (https://claude.com/claude-code)

decode(), Str::encode(..., 'hex') and Str::decodeHex() only required
one hex digit somewhere in the input, removed every "0x" occurrence
rather than just the prefix, and let hexdec() silently drop invalid
characters (e.g. decode("0x8zz1") -> "8z", encode("0xzz") -> "zz").
decode() also padded odd-length input (0x8 -> 08).

Strip only a leading 0x, require the rest to be hex digits (anchored
with \A...\z so a trailing newline is rejected), and reject odd-length
input in decode(). Encoding still pads odd-length hex ("0x0" -> "00").

Add regression vectors for invalid and still-valid hex input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.44%. Comparing base (a22b1ba) to head (4114d21).

Additional details and impacted files
@@              Coverage Diff              @@
##             master      #33       +/-   ##
=============================================
+ Coverage     84.27%   94.44%   +10.16%     
- Complexity       59       61        +2     
=============================================
  Files             3        3               
  Lines           159      162        +3     
=============================================
+ Hits            134      153       +19     
+ Misses           25        9       -16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sc0Vu
sc0Vu merged commit 1478705 into web3p:master Sep 23, 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.

2 participants