티스토리 수익 글 보기

티스토리 수익 글 보기

Fixed CVE-2024-45230 — Mitigated potential DoS in urlize and urlizet… · django/django@320dd27 · GitHub
Skip to content

Commit 320dd27

Browse files
sarahboycenessita
authored andcommitted
Fixed CVE-2024-45230 — Mitigated potential DoS in urlize and urlizetrunc template filters.
Thanks MProgrammer (https://hackerone.com/mprogrammer) for the report.
1 parent f5ddd54 commit 320dd27

File tree

7 files changed

+46
9
lines changed

7 files changed

+46
9
lines changed

django/utils/html.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,17 @@ def trim_punctuation(self, word):
434434
potential_entity = middle[amp:]
435435
escaped = html.unescape(potential_entity)
436436
if escaped == potential_entity or escaped.endswith(";"):
437-
rstripped = middle.rstrip(";")
438-
amount_stripped = len(middle) - len(rstripped)
439-
if amp > -1 and amount_stripped > 1:
440-
# Leave a trailing semicolon as might be an entity.
441-
trail = middle[len(rstripped) + 1 :] + trail
442-
middle = rstripped + ";"
437+
rstripped = middle.rstrip(self.trailing_punctuation_chars)
438+
trail_start = len(rstripped)
439+
amount_trailing_semicolons = len(middle) - len(middle.rstrip(";"))
440+
if amp > -1 and amount_trailing_semicolons > 1:
441+
# Leave up to most recent semicolon as might be an entity.
442+
recent_semicolon = middle[trail_start:].index(";")
443+
middle_semicolon_index = recent_semicolon + trail_start + 1
444+
trail = middle[middle_semicolon_index:] + trail
445+
middle = rstripped + middle[trail_start:middle_semicolon_index]
443446
else:
444-
trail = middle[len(rstripped) :] + trail
447+
trail = middle[trail_start:] + trail
445448
middle = rstripped
446449
trimmed_something = True
447450

docs/ref/templates/builtins.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,17 @@ Django's built-in :tfilter:`escape` filter. The default value for
29222922
email addresses that contain single quotes (``'``), things won't work as
29232923
expected. Apply this filter only to plain text.
29242924

2925+
.. warning::
2926+
2927+
Using ``urlize`` or ``urlizetrunc`` can incur a performance penalty, which
2928+
can become severe when applied to user controlled values such as content
2929+
stored in a :class:`~django.db.models.TextField`. You can use
2930+
:tfilter:`truncatechars` to add a limit to such inputs:
2931+
2932+
.. code-block:: html+django
2933+
2934+
{{ value|truncatechars:500|urlize }}
2935+
29252936
.. templatefilter:: urlizetrunc
29262937

29272938
``urlizetrunc``

docs/releases/4.2.16.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ Django 4.2.16 release notes
77
Django 4.2.16 fixes one security issue with severity "moderate" and one
88
security issue with severity "low" in 4.2.15.
99

10-
...
10+
CVE-2024-45230: Potential denial-of-service vulnerability in ``django.utils.html.urlize()``
11+
===========================================================================================
12+
13+
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
14+
denial-of-service attack via very large inputs with a specific sequence of
15+
characters.

docs/releases/5.0.9.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ Django 5.0.9 release notes
77
Django 5.0.9 fixes one security issue with severity "moderate" and one security
88
issue with severity "low" in 5.0.8.
99

10-
...
10+
CVE-2024-45230: Potential denial-of-service vulnerability in ``django.utils.html.urlize()``
11+
===========================================================================================
12+
13+
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
14+
denial-of-service attack via very large inputs with a specific sequence of
15+
characters.

docs/releases/5.1.1.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ Django 5.1.1 release notes
77
Django 5.1.1 fixes one security issue with severity "moderate", one security
88
issue with severity "low", and several bugs in 5.1.
99

10+
CVE-2024-45230: Potential denial-of-service vulnerability in ``django.utils.html.urlize()``
11+
===========================================================================================
12+
13+
:tfilter:`urlize` and :tfilter:`urlizetrunc` were subject to a potential
14+
denial-of-service attack via very large inputs with a specific sequence of
15+
characters.
16+
1017
Bugfixes
1118
========
1219

tests/template_tests/filter_tests/test_urlize.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ def test_trailing_semicolon(self):
321321
'<a href="http://example.com?x=" rel="nofollow">'
322322
"http://example.com?x=&amp;</a>;;",
323323
)
324+
self.assertEqual(
325+
urlize("http://example.com?x=&amp.;...;", autoescape=False),
326+
'<a href="http://example.com?x=" rel="nofollow">'
327+
"http://example.com?x=&amp</a>.;...;",
328+
)
324329

325330
def test_brackets(self):
326331
"""

tests/utils_tests/test_html.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def test_urlize_unchanged_inputs(self):
396396
"&:" + ";" * 100_000,
397397
"&.;" * 100_000,
398398
".;" * 100_000,
399+
"&" + ";:" * 100_000,
399400
)
400401
for value in tests:
401402
with self.subTest(value=value):

0 commit comments

Comments
 (0)