Skip to content

Commit 61dae11

Browse files
gmaOCRsarahboyce
authored andcommitted
Fixed #36017 -- Used EmailValidator in urlize to detect emails.
1 parent ec8cbea commit 61dae11

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

django/utils/html.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from html.parser import HTMLParser
88
from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit, urlunsplit
99

10-
from django.core.exceptions import SuspiciousOperation
10+
from django.core.exceptions import SuspiciousOperation, ValidationError
11+
from django.core.validators import EmailValidator
1112
from django.utils.encoding import punycode
1213
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
1314
from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS
@@ -455,20 +456,9 @@ def trim_punctuation(self, word):
455456
@staticmethod
456457
def is_email_simple(value):
457458
"""Return True if value looks like an email address."""
458-
# An @ must be in the middle of the value.
459-
if "@" not in value or value.startswith("@") or value.endswith("@"):
460-
return False
461459
try:
462-
p1, p2 = value.split("@")
463-
except ValueError:
464-
# value contains more than one @.
465-
return False
466-
# Max length for domain name labels is 63 characters per RFC 1034.
467-
# Helps to avoid ReDoS vectors in the domain part.
468-
if len(p2) > 63:
469-
return False
470-
# Dot must be in p2 (e.g. example.com)
471-
if "." not in p2 or p2.startswith("."):
460+
EmailValidator(allowlist=[])(value)
461+
except ValidationError:
472462
return False
473463
return True
474464

tests/utils_tests/test_html.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -374,15 +374,9 @@ def test_urlize(self):
374374
(
375375
# RFC 6068 requires a mailto URI to percent-encode a number of
376376
# characters that can appear in <addr-spec>.
377-
"yes;this=is&a%[email protected]",
378-
'<a href="mailto:yes%3Bthis%3Dis%26a%25valid%[email protected]"'
379-
">yes;this=is&a%[email protected]</a>",
380-
),
381-
(
382-
# Urlizer shouldn't urlize the "?org" part of this. But since
383-
# it does, RFC 6068 requires percent encoding the "?".
384-
385-
'<a href="mailto:[email protected]%3Forg">[email protected]?org</a>',
377+
"yes+this=is&a%[email protected]",
378+
'<a href="mailto:yes%2Bthis%3Dis%26a%25valid%[email protected]"'
379+
">yes+this=is&a%[email protected]</a>",
386380
),
387381
)
388382
for value, output in tests:
@@ -402,6 +396,8 @@ def test_urlize_unchanged_inputs(self):
402396
403397
"foo@localhost",
404398
"foo@localhost.",
399+
"test@example?;+!.com",
400+
"email [email protected],then I'll respond",
405401
# trim_punctuation catastrophic tests
406402
"(" * 100_000 + ":" + ")" * 100_000,
407403
"(" * 100_000 + "&:" + ")" * 100_000,

0 commit comments

Comments
 (0)