Skip to content

Commit 2ed3aad

Browse files
committed
Require the 0x prefix in mixed-case ETH addresses
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.
1 parent 22786cc commit 2ed3aad

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/validators/crypto_addresses/eth_address.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
def _validate_eth_checksum_address(addr: str):
1818
"""Validate ETH type checksum address."""
19-
addr = addr.replace("0x", "")
20-
21-
if not re.fullmatch(r"[0-9a-fA-F]{40}", addr):
19+
if not re.fullmatch(r"0x[0-9a-fA-F]{40}", addr):
2220
return False
2321

22+
addr = addr[2:]
23+
2424
addr_hash = keccak.new(addr.lower().encode("ascii")).digest().hex() # type: ignore
2525

2626
for i in range(0, 40):

‎tests/crypto_addresses/test_eth_address.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def test_returns_true_on_valid_eth_address(value: str):
3939
"validators.eth",
4040
# non-hex characters (the checksum path used to accept these)
4141
"0x" + "*" * 40,
42+
# checksum addresses without the 0x prefix or with extra 0x markers
43+
"5AEDA56215b167893e80B4fE645BA6d5Bab767DE",
44+
"0x0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE",
45+
"0x5AEDA56215b1670x893e80B4fE645BA6d5Bab767DE",
46+
"0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE0x",
4247
],
4348
)
4449
def test_returns_failed_validation_on_invalid_eth_address(value: str):

0 commit comments

Comments
 (0)