Skip to content

Commit 657ee3c

Browse files
committed
fix: enforce the email local-part limit in UTF-8 octets
1 parent 22786cc commit 657ee3c

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎src/validators/email.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def email(
6060

6161
username_part, domain_part = value.rsplit("@", 1)
6262

63-
if len(username_part) > 64 or len(domain_part) > 253:
64-
# ref: RFC 1034 and 5231
63+
if len(username_part.encode("utf-8")) > 64 or len(domain_part) > 253:
64+
# ref: RFC 5321 section 4.5.3.1.1 (octets) and RFC 1034
6565
return False
6666

6767
if ipv6_address or ipv4_address:

‎tests/test_email.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ 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("local_part", ["a" * 64, "\u00fc" * 32, "a" * 62 + "\u00fc"])
78+
def test_accepts_local_part_at_utf8_byte_limit(local_part: str):
79+
"""Test local parts at the 64-octet SMTP boundary."""
80+
assert len(local_part.encode("utf-8")) == 64
81+
assert email(f"{local_part}@example.com") is True
82+
83+
84+
@pytest.mark.parametrize("local_part", ["a" * 65, "\u00fc" * 33, "a" * 63 + "\u00fc"])
85+
def test_rejects_local_part_over_utf8_byte_limit(local_part: str):
86+
"""Test that Unicode characters cannot exceed the SMTP octet limit."""
87+
assert len(local_part.encode("utf-8")) > 64
88+
assert isinstance(email(f"{local_part}@example.com"), ValidationError)

0 commit comments

Comments
 (0)