Skip to content

Commit f9ce4b8

Browse files
committed
Reject trailing characters in BTC and ETH address matches
1 parent 6ffd862 commit f9ce4b8

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/validators/crypto_addresses/btc_address.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def btc_address(value: str, /):
5050

5151
return (
5252
# segwit pattern
53-
re.compile(r"^(bc|tb)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
53+
re.compile(r"^(bc|tb)[0-3][02-9ac-hj-np-z]{14,74}$").fullmatch(value)
5454
if value[:2] in ("bc", "tb")
5555
else _validate_old_btc_address(value)
5656
)

‎src/validators/crypto_addresses/eth_address.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ def eth_address(value: str, /):
5959
if not value:
6060
return False
6161

62-
return re.compile(r"^0x[0-9a-f]{40}$|^0x[0-9A-F]{40}$").match(
62+
return re.compile(r"^0x[0-9a-f]{40}$|^0x[0-9A-F]{40}$").fullmatch(
6363
value
6464
) or _validate_eth_checksum_address(value)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Require complete BTC and ETH addresses."""
2+
3+
from typing import Callable
4+
5+
import pytest
6+
7+
from validators import ValidationError, btc_address, eth_address
8+
9+
10+
@pytest.mark.parametrize(
11+
"validate,value",
12+
[
13+
(btc_address, "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"),
14+
(btc_address, "tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx"),
15+
(eth_address, "0x9cc14ba4f9f68ca159ea4ebf2c292a808aaeb598"),
16+
(eth_address, "0x9CC14BA4F9F68CA159EA4EBF2C292A808AAEB598"),
17+
],
18+
)
19+
@pytest.mark.parametrize("suffix", ["", "\n", "\r\n", " ", "\nextra"])
20+
def test_complete_address(validate: Callable[[str], object], value: str, suffix: str):
21+
"""Accept the address alone and reject trailing characters."""
22+
if suffix:
23+
assert isinstance(validate(value + suffix), ValidationError)
24+
else:
25+
assert validate(value) is True

0 commit comments

Comments
 (0)