From 07041ff1f64d0d53278819ab22e513f05fa5a420 Mon Sep 17 00:00:00 2001 From: uvrvuoon <0524yar@gmail.com> Date: Sat, 21 Nov 2026 20:13:10 +0900 Subject: [PATCH] Accept escaped printable characters in quoted email local parts --- src/validators/email.py | 5 ++++- tests/test_email.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/validators/email.py b/src/validators/email.py index 46cc5946..279b5fb1 100644 --- a/src/validators/email.py +++ b/src/validators/email.py @@ -92,7 +92,10 @@ def email( # quoted-string + r'|^"(' + r"[\u0100-\u017F\u0180-\u024F\u00A0-\u00FF\001-\010\013\014\016-\037" - + r"!#-\[\]-\177]|\\[\011.]" + # quoted-pair: a backslash followed by a tab or any printable ASCII + # character (RFC 5322 section 3.2.1); (?-i:...) keeps case-insensitive + # matching from also admitting non-ASCII characters such as the Kelvin sign + + r"!#-\[\]-\177]|\\(?-i:[\011\040-\176])" + r')*")$', username_part, re.IGNORECASE, diff --git a/tests/test_email.py b/tests/test_email.py index 9b82eb46..bc3aa1ac 100644 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -68,6 +68,19 @@ def test_rejects_newline_after_local_part(local_part: str): assert isinstance(email(f"{local_part}\n@example.com"), ValidationError) +@pytest.mark.parametrize("local_part", ['"a\\"b"', '"a\\\\b"', '"a\\.b"', '"a\\ b"', '"\\z"']) +def test_returns_true_on_quoted_pair_in_quoted_local_part(local_part: str): + """Test returns true when a quoted local part escapes a printable character.""" + # RFC 5322 section 3.2.1: a quoted-pair is a backslash followed by VCHAR or WSP. + assert email(f"{local_part}@example.com") is True + + +@pytest.mark.parametrize("local_part", ['"\\\000"', '"\\\177"', '"\\\u212a"']) +def test_returns_failed_validation_on_invalid_quoted_pair(local_part: str): + """Test returns failed validation when a quoted-pair escapes a control or non-ASCII char.""" + assert isinstance(email(f"{local_part}@example.com"), ValidationError) + + @pytest.mark.parametrize("domain_part", ["example.\uff43\uff4f\uff4d", "exam\u00adple.com"]) def test_returns_failed_validation_on_idna_mapped_lookalike_domain(domain_part: str): """Test returns failed validation on email domains that only IDNA 2003 mapping makes valid."""