Skip to content

Commit 5db8b8b

Browse files
fix(email): accept exactly one pair of brackets around address literals
`lstrip("[")`/`rstrip("]")` removed any number of brackets, so `user@[[192.168.0.1]]` and even `user@[[192.168.0.1]` were valid with `ipv4_address=True`. RFC 5321 4.1.3 allows exactly one pair. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 22786cc commit 5db8b8b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎src/validators/email.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def email(
6767
if ipv6_address or ipv4_address:
6868
if domain_part.startswith("[") and domain_part.endswith("]"):
6969
# ref: RFC 5321
70-
domain_part = domain_part.lstrip("[").rstrip("]")
70+
domain_part = domain_part[1:-1]
7171
else:
7272
return False
7373

‎tests/test_email.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,20 @@ def test_rejects_newline_after_local_part(local_part: str):
7272
def test_returns_failed_validation_on_idna_mapped_lookalike_domain(domain_part: str):
7373
"""Test returns failed validation on email domains that only IDNA 2003 mapping makes valid."""
7474
assert isinstance(email(f"user@{domain_part}"), ValidationError)
75+
76+
77+
@pytest.mark.parametrize(
78+
("value", "flag"),
79+
[
80+
("user@[[192.168.0.1]]", "ipv4_address"),
81+
("user@[[192.168.0.1]", "ipv4_address"),
82+
("user@[192.168.0.1]]", "ipv4_address"),
83+
("user@[[[2001:db8::1]]]", "ipv6_address"),
84+
],
85+
)
86+
def test_rejects_extra_brackets_around_address_literal(value: str, flag: str):
87+
"""Test an address literal has exactly one pair of brackets (RFC 5321 4.1.3)."""
88+
assert isinstance(email(value, **{flag: True}), ValidationError)
89+
assert email(
90+
f"user@[{'192.168.0.1' if flag == 'ipv4_address' else '2001:db8::1'}]", **{flag: True}
91+
)

0 commit comments

Comments
 (0)