Skip to content

Require the 0x prefix in mixed-case ETH addresses - #571

Open
hemtilva wants to merge 1 commit into
python-validators:masterfrom
hemtilva:eth-address-require-0x-prefix
Open

hemtilva wants to merge 1 commit into
python-validators:masterfrom
hemtilva:eth-address-require-0x-prefix

Conversation

@hemtilva

Copy link
Copy Markdown

Fixes #570.

The EIP-55 checksum path in eth_address() began with addr.replace("0x", ""), which strips every "0x" in the value, not just a leading prefix. As a result a mixed-case checksum address was accepted with no prefix at all, with a doubled prefix, or with "0x" inserted anywhere in the string, for example "0x5AEDA56215b1670x893e80B4fE645BA6d5Bab767DE". The lowercase and uppercase forms already required the prefix, so the behaviour was also inconsistent between the two paths.

This change matches the full value against 0x[0-9a-fA-F]{40} before computing the checksum and then drops only the first two characters. Every address that was valid before is still valid.

I added four invalid cases to tests/crypto_addresses/test_eth_address.py (no prefix, doubled prefix, "0x" in the middle, trailing "0x"). All four fail on master and pass with the change. The full suite passes locally (1520 passed, including doctests), and ruff check, ruff format --check and bandit report no issues. pyright reports the same four eth_hash import errors with and without this change in my environment, so nothing new there.

The EIP-55 checksum path stripped every "0x" occurrence with
str.replace before checking for 40 hex digits, so eth_address()
accepted checksum addresses with no prefix, a doubled prefix, or
"0x" inserted anywhere in the string. Match the full 0x-prefixed
address first and drop only the leading prefix.
Copilot AI balanced review requested due to automatic review settings November 15, 2026 22:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

eth_address() accepts checksum addresses without the 0x prefix or with extra "0x" inserted

2 participants