Skip to content

Commit df852f7

Browse files
committed
Prevent an unhandled exception encoding to UTF-8 if the input has a surrogate code point
1 parent 9f21959 commit df852f7

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

email_validator/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
368368
# so we'll return the normalized local part in the return value.
369369
local = unicodedata.normalize("NFC", local)
370370

371+
# Try encoding to UTF-8. Failure is possible with some characters like
372+
# surrogate code points.
373+
try:
374+
local.encode("utf8")
375+
except ValueError:
376+
raise EmailSyntaxError("The email address contains an invalid character.")
377+
371378
# Flag that SMTPUTF8 will be required for deliverability.
372379
return {
373380
"local_part": local,

tests/test_main.py

+1
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ def test_email_invalid_reserved_domain(email_input):
283283
[
284284
('white space@test'),
285285
('\n@test'),
286+
('\uD800@test'), # surrogate (Cs)
286287
],
287288
)
288289
def test_email_unsafe_character(email_input):

0 commit comments

Comments
 (0)