diff --git a/src/validators/email.py b/src/validators/email.py index 46cc5946..43e699be 100644 --- a/src/validators/email.py +++ b/src/validators/email.py @@ -92,7 +92,7 @@ def email( # quoted-string + r'|^"(' + r"[\u0100-\u017F\u0180-\u024F\u00A0-\u00FF\001-\010\013\014\016-\037" - + r"!#-\[\]-\177]|\\[\011.]" + + r"!#-\[\]-\177]|\\[\001-\011\013\014\016-\177]" + r')*")$', username_part, re.IGNORECASE, diff --git a/src/validators/url.py b/src/validators/url.py index ad42140e..be721291 100644 --- a/src/validators/url.py +++ b/src/validators/url.py @@ -19,7 +19,7 @@ def _username_regex(): # dot-atom + r"|[-!#$%&'*+/=?^_`{}|~0-9a-z]+(\.[-!#$%&'*+/=?^_`{}|~0-9a-z]+)*$" # non-quoted-string - + r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\011.])*$)", + + r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*$)", re.IGNORECASE, ) diff --git a/tests/test_email.py b/tests/test_email.py index 9b82eb46..72fe88bb 100644 --- a/tests/test_email.py +++ b/tests/test_email.py @@ -72,3 +72,9 @@ def test_rejects_newline_after_local_part(local_part: str): 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.""" assert isinstance(email(f"user@{domain_part}"), ValidationError) + + +@pytest.mark.parametrize("local_part", [r'"a\"b"', r'"a\\b"', r'"\a"', r'"a\ b"']) +def test_accepts_quoted_pair_with_any_visible_character(local_part: str): + """Test a backslash may escape any visible character or space (RFC 5322 3.2.1).""" + assert email(f"{local_part}@example.com")