Skip to content

Commit d0a82e2

Browse files
committed
[4.2.x] Fixed CVE-2024-41990 -- Mitigated potential DoS in urlize and urlizetrunc template filters.
Thanks to MProgrammer for the report.
1 parent fc76660 commit d0a82e2

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

django/utils/html.py

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

381-
rstripped = middle.rstrip(self.trailing_punctuation_chars_no_semicolon)
381+
amp = middle.rfind("&")
382+
if amp == -1:
383+
rstripped = middle.rstrip(self.trailing_punctuation_chars)
384+
else:
385+
rstripped = middle.rstrip(self.trailing_punctuation_chars_no_semicolon)
382386
if rstripped != middle:
383387
trail = middle[len(rstripped) :] + trail
384388
middle = rstripped
385389
trimmed_something = True
386390

387391
if self.trailing_punctuation_chars_has_semicolon and middle.endswith(";"):
388392
# Only strip if not part of an HTML entity.
389-
amp = middle.rfind("&")
390-
if amp == -1:
391-
can_strip = True
392-
else:
393-
potential_entity = middle[amp:]
394-
escaped = html.unescape(potential_entity)
395-
can_strip = (escaped == potential_entity) or escaped.endswith(";")
396-
397-
if can_strip:
393+
potential_entity = middle[amp:]
394+
escaped = html.unescape(potential_entity)
395+
if escaped == potential_entity or escaped.endswith(";"):
398396
rstripped = middle.rstrip(";")
399397
amount_stripped = len(middle) - len(rstripped)
400398
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

tests/utils_tests/test_html.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ def test_urlize_unchanged_inputs(self):
349349
"[(" * 100_000 + ":" + ")]" * 100_000,
350350
"([[" * 100_000 + ":" + "]])" * 100_000,
351351
"&:" + ";" * 100_000,
352+
"&.;" * 100_000,
353+
".;" * 100_000,
352354
)
353355
for value in tests:
354356
with self.subTest(value=value):

0 commit comments

Comments
 (0)