Skip to content

Commit 9f21959

Browse files
committed
Improve safety of exception text by not repeating an unsafe input character in the message and add a new test for various unsafe characters
1 parent 84a2413 commit 9f21959

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

email_validator/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
354354
if not m:
355355
# It's not a valid internationalized address either. Report which characters were not valid.
356356
bad_chars = ', '.join(sorted(set(
357-
c for c in local if not re.match(u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8) + u"]", c)
357+
unicodedata.name(c, repr(c)) for c in local if not re.match(u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8) + u"]", c)
358358
)))
359359
raise EmailSyntaxError("The email address contains invalid characters before the @-sign: %s." % bad_chars)
360360

tests/test_main.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,16 @@ def test_email_valid(email_input, output):
227227
('my@example\n.com',
228228
'The domain name example\n.com contains invalid characters (Codepoint U+000A at position 8 of '
229229
'\'example\\n\' not allowed).'),
230-
('[email protected]', 'The email address contains invalid characters before the @-sign: ..'),
231-
('[email protected]', 'The email address contains invalid characters before the @-sign: ..'),
232-
('[email protected]', 'The email address contains invalid characters before the @-sign: ..'),
230+
('[email protected]', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
231+
('[email protected]', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
232+
('[email protected]', 'The email address contains invalid characters before the @-sign: FULL STOP.'),
233233
('me@⒈wouldbeinvalid.com',
234234
"The domain name ⒈wouldbeinvalid.com contains invalid characters (Codepoint U+2488 not allowed "
235235
"at position 1 in '⒈wouldbeinvalid.com')."),
236236
('@example.com', 'There must be something before the @-sign.'),
237-
('\n[email protected]', 'The email address contains invalid characters before the @-sign: \n.'),
238-
('m\n[email protected]', 'The email address contains invalid characters before the @-sign: \n.'),
239-
('my\n@example.com', 'The email address contains invalid characters before the @-sign: \n.'),
237+
('\n[email protected]', 'The email address contains invalid characters before the @-sign: \'\\n\'.'),
238+
('m\n[email protected]', 'The email address contains invalid characters before the @-sign: \'\\n\'.'),
239+
('my\n@example.com', 'The email address contains invalid characters before the @-sign: \'\\n\'.'),
240240
('11111111112222222222333333333344444444445555555555666666666677777@example.com', 'The email address is too long before the @-sign (1 character too many).'),
241241
('111111111122222222223333333333444444444455555555556666666666777777@example.com', 'The email address is too long before the @-sign (2 characters too many).'),
242242
('me@1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.1111111111222222222233333333334444444444555555555.6666666666777777777788888888889999999999000000000.111111111122222222223333333333444444444455555555556.com', 'The email address is too long after the @-sign.'),
@@ -278,6 +278,18 @@ def test_email_invalid_reserved_domain(email_input):
278278
# print(f'({email_input!r}, {str(exc_info.value)!r}),')
279279
assert "is a special-use or reserved name" in str(exc_info.value)
280280

281+
@pytest.mark.parametrize(
282+
'email_input',
283+
[
284+
('white space@test'),
285+
('\n@test'),
286+
],
287+
)
288+
def test_email_unsafe_character(email_input):
289+
# Check for various unsafe characters:
290+
with pytest.raises(EmailSyntaxError) as exc_info:
291+
validate_email(email_input, test_environment=True)
292+
assert "invalid character" in str(exc_info.value)
281293

282294
def test_email_test_domain_name_in_test_environment():
283295
validate_email("anything@test", test_environment=True)

0 commit comments

Comments
 (0)