Skip to content

Commit a9fe98d

Browse files
JaeHyuckSasarahboyce
authored andcommitted
Fixed #35533 -- Prevented urlize creating broken links given a markdown link input.
Signed-off-by: SaJH <wogur981208@gmail.com>
1 parent 05bac8c commit a9fe98d

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

django/utils/html.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from django.conf import settings
1212
from django.core.exceptions import SuspiciousOperation, ValidationError
13-
from django.core.validators import EmailValidator
13+
from django.core.validators import DomainNameValidator, EmailValidator
1414
from django.utils.deprecation import RemovedInDjango70Warning
1515
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
1616
from django.utils.http import MAX_URL_LENGTH, RFC3986_GENDELIMS, RFC3986_SUBDELIMS
@@ -296,7 +296,9 @@ class Urlizer:
296296

297297
simple_url_re = _lazy_re_compile(r"^https?://\[?\w", re.IGNORECASE)
298298
simple_url_2_re = _lazy_re_compile(
299-
r"^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)($|/.*)$", re.IGNORECASE
299+
rf"^www\.|^(?!http)(?:{DomainNameValidator.hostname_re})"
300+
r"\.(com|edu|gov|int|mil|net|org)($|/.*)$",
301+
re.IGNORECASE,
300302
)
301303
word_split_re = _lazy_re_compile(r"""([\s<>"']+)""")
302304

tests/template_tests/filter_tests/test_urlize.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,8 @@ def test_brackets(self):
359359
"www.example.com</a>]",
360360
)
361361
self.assertEqual(
362-
urlize("see test[at[example.com"),
363-
'see <a href="https://test[at[example.com" rel="nofollow">'
364-
"test[at[example.com</a>",
362+
urlize("see test[at[example.com"), # Invalid hostname.
363+
"see test[at[example.com",
365364
)
366365
self.assertEqual(
367366
urlize("[http://168.192.0.1](http://168.192.0.1)"),

tests/utils_tests/test_html.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def test_urlize_unchanged_inputs(self):
489489
"foo@localhost.",
490490
"test@example?;+!.com",
491491
"email me@example.com,then I'll respond",
492+
"[a link](https://www.djangoproject.com/)",
492493
# trim_punctuation catastrophic tests
493494
"(" * 100_000 + ":" + ")" * 100_000,
494495
"(" * 100_000 + "&:" + ")" * 100_000,

0 commit comments

Comments
 (0)