fix(types): reject odd-length hex strings - #100
Open
UgwujaGeorge wants to merge 1 commit into
Open
UgwujaGeorge wants to merge 1 commit into
UgwujaGeorge wants to merge 1 commit into
Conversation
eth_utils.to_bytes left-pads an odd-length hex string with a zero nibble, so an address that lost a character decoded to 20 bytes and passed the length check as a different, well-formed address. as_bytes, as_address, as_hash32 and as_selector now reject odd-length hex instead, and add regression tests.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
as_bytes,as_addressandas_hash32pass hex strings straight toeth_utils.to_bytes, which left-pads an odd-length string with a zero nibble. The byte-length check that follows then passes, because the padding has already produced the expected length:So an address that lost a single character is accepted as a valid 20-byte address — a different one, with every byte shifted:
No error is raised at any point. The same shift applies to
as_hash32(63 digits) andas_selector(7 digits), and reachesCall.to,AccessListItem.address, itsstorage_keys, and the access-key call scopes inkeychain.py, all of which use these asattrsconverters. The helpers are also exported frompytempo, so callers use them directly.This is the same class as #90 — a malformed value silently reinterpreted rather than rejected — except here the corruption happens at input coercion rather than at decode.
Fix
Reject an odd number of hex digits in
_hex_to_bytes, shared by the three string branches.as_selectoralready routes throughas_bytes, so it is covered too. Even-length input decodes exactly as before.Tests
Adds six cases to
TestTypes: odd-length rejected foras_address,as_hash32,as_bytesandas_selector, rejected with the0xprefix omitted, and a guard that canonical even-length input still decodes unchanged.The five rejection tests fail on
mainand pass on this branch. Full suite 287 passed, 37 skipped (+6);ruff check .andruff format --check .clean.