Skip to content

Commit 07041ff

Browse files
committed
Accept escaped printable characters in quoted email local parts
1 parent 22786cc commit 07041ff

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎src/validators/email.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ def email(
9292
# quoted-string
9393
+ r'|^"('
9494
+ r"[\u0100-\u017F\u0180-\u024F\u00A0-\u00FF\001-\010\013\014\016-\037"
95-
+ r"!#-\[\]-\177]|\\[\011.]"
95+
# quoted-pair: a backslash followed by a tab or any printable ASCII
96+
# character (RFC 5322 section 3.2.1); (?-i:...) keeps case-insensitive
97+
# matching from also admitting non-ASCII characters such as the Kelvin sign
98+
+ r"!#-\[\]-\177]|\\(?-i:[\011\040-\176])"
9699
+ r')*")$',
97100
username_part,
98101
re.IGNORECASE,

‎tests/test_email.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ def test_rejects_newline_after_local_part(local_part: str):
6868
assert isinstance(email(f"{local_part}\n@example.com"), ValidationError)
6969

7070

71+
@pytest.mark.parametrize("local_part", ['"a\\"b"', '"a\\\\b"', '"a\\.b"', '"a\\ b"', '"\\z"'])
72+
def test_returns_true_on_quoted_pair_in_quoted_local_part(local_part: str):
73+
"""Test returns true when a quoted local part escapes a printable character."""
74+
# RFC 5322 section 3.2.1: a quoted-pair is a backslash followed by VCHAR or WSP.
75+
assert email(f"{local_part}@example.com") is True
76+
77+
78+
@pytest.mark.parametrize("local_part", ['"\\\000"', '"\\\177"', '"\\\u212a"'])
79+
def test_returns_failed_validation_on_invalid_quoted_pair(local_part: str):
80+
"""Test returns failed validation when a quoted-pair escapes a control or non-ASCII char."""
81+
assert isinstance(email(f"{local_part}@example.com"), ValidationError)
82+
83+
7184
@pytest.mark.parametrize("domain_part", ["example.\uff43\uff4f\uff4d", "exam\u00adple.com"])
7285
def test_returns_failed_validation_on_idna_mapped_lookalike_domain(domain_part: str):
7386
"""Test returns failed validation on email domains that only IDNA 2003 mapping makes valid."""

0 commit comments

Comments
 (0)