Skip to content

Commit 7b7b909

Browse files
committed
[5.0.x] Fixed CVE-2024-41990 -- Mitigated potential DoS in urlize and urlizetrunc template filters.
Thanks to MProgrammer for the report.
1 parent 27900fe commit 7b7b909

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

django/utils/html.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,23 +408,21 @@ def trim_punctuation(self, word):
408408
trimmed_something = True
409409
counts[closing] -= strip
410410

411-
rstripped = middle.rstrip(self.trailing_punctuation_chars_no_semicolon)
411+
amp = middle.rfind("&")
412+
if amp == -1:
413+
rstripped = middle.rstrip(self.trailing_punctuation_chars)
414+
else:
415+
rstripped = middle.rstrip(self.trailing_punctuation_chars_no_semicolon)
412416
if rstripped != middle:
413417
trail = middle[len(rstripped) :] + trail
414418
middle = rstripped
415419
trimmed_something = True
416420

417421
if self.trailing_punctuation_chars_has_semicolon and middle.endswith(";"):
418422
# Only strip if not part of an HTML entity.
419-
amp = middle.rfind("&")
420-
if amp == -1:
421-
can_strip = True
422-
else:
423-
potential_entity = middle[amp:]
424-
escaped = html.unescape(potential_entity)
425-
can_strip = (escaped == potential_entity) or escaped.endswith(";")
426-
427-
if can_strip:
423+
potential_entity = middle[amp:]
424+
escaped = html.unescape(potential_entity)
425+
if escaped == potential_entity or escaped.endswith(";"):
428426
rstripped = middle.rstrip(";")
429427
amount_stripped = len(middle) - len(rstripped)
430428
if amp > -1 and amount_stripped > 1:

docs/releases/4.2.15.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ consumption.
1616

1717
To avoid this, decimals with more than 200 digits are now returned as is.
1818

19+
CVE-2024-41990: Potential denial-of-service vulnerability in ``django.utils.html.urlize()``
20+
===========================================================================================
21+
22+
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
23+
denial-of-service attack via very large inputs with a specific sequence of
24+
characters.
25+
1926
Bugfixes
2027
========
2128

docs/releases/5.0.8.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ consumption.
1616

1717
To avoid this, decimals with more than 200 digits are now returned as is.
1818

19+
CVE-2024-41990: Potential denial-of-service vulnerability in ``django.utils.html.urlize()``
20+
===========================================================================================
21+
22+
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
23+
denial-of-service attack via very large inputs with a specific sequence of
24+
characters.
25+
1926
Bugfixes
2027
========
2128

tests/utils_tests/test_html.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ def test_urlize_unchanged_inputs(self):
359359
"[(" * 100_000 + ":" + ")]" * 100_000,
360360
"([[" * 100_000 + ":" + "]])" * 100_000,
361361
"&:" + ";" * 100_000,
362+
"&.;" * 100_000,
363+
".;" * 100_000,
362364
)
363365
for value in tests:
364366
with self.subTest(value=value):

0 commit comments

Comments
 (0)