Skip to content

Commit 790eb05

Browse files
committed
[4.2.x] Fixed CVE-2024-53907 -- Mitigated potential DoS in strip_tags().
Thanks to jiangniao for the report, and Shai Berger and Natalia Bidart for the reviews.
1 parent f663277 commit 790eb05

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

django/utils/html.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from html.parser import HTMLParser
77
from urllib.parse import parse_qsl, quote, unquote, urlencode, urlsplit, urlunsplit
88

9+
from django.core.exceptions import SuspiciousOperation
910
from django.utils.encoding import punycode
1011
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
1112
from django.utils.http import RFC3986_GENDELIMS, RFC3986_SUBDELIMS
@@ -14,6 +15,7 @@
1415
from django.utils.text import normalize_newlines
1516

1617
MAX_URL_LENGTH = 2048
18+
MAX_STRIP_TAGS_DEPTH = 50
1719

1820

1921
@keep_lazy(SafeString)
@@ -172,15 +174,19 @@ def _strip_once(value):
172174
@keep_lazy_text
173175
def strip_tags(value):
174176
"""Return the given HTML with all tags stripped."""
175-
# Note: in typical case this loop executes _strip_once once. Loop condition
176-
# is redundant, but helps to reduce number of executions of _strip_once.
177177
value = str(value)
178+
# Note: in typical case this loop executes _strip_once twice (the second
179+
# execution does not remove any more tags).
180+
strip_tags_depth = 0
178181
while "<" in value and ">" in value:
182+
if strip_tags_depth >= MAX_STRIP_TAGS_DEPTH:
183+
raise SuspiciousOperation
179184
new_value = _strip_once(value)
180185
if value.count("<") == new_value.count("<"):
181186
# _strip_once wasn't able to detect more tags.
182187
break
183188
value = new_value
189+
strip_tags_depth += 1
184190
return value
185191

186192

docs/releases/4.2.17.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,19 @@ Django 4.2.17 release notes
66

77
Django 4.2.17 fixes one security issue with severity "high" and one security
88
issue with severity "moderate" in 4.2.16.
9+
10+
CVE-2024-53907: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be extremely slow to evaluate
14+
certain inputs containing large sequences of nested incomplete HTML entities.
15+
The ``strip_tags()`` method is used to implement the corresponding
16+
:tfilter:`striptags` template filter, which was thus also vulnerable.
17+
18+
``strip_tags()`` now has an upper limit of recursive calls to ``HTMLParser``
19+
before raising a :exc:`.SuspiciousOperation` exception.
20+
21+
Remember that absolutely NO guarantee is provided about the results of
22+
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
23+
``strip_tags()`` call without escaping it first, for example with
24+
:func:`django.utils.html.escape`.

tests/utils_tests/test_html.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from datetime import datetime
33

4+
from django.core.exceptions import SuspiciousOperation
45
from django.core.serializers.json import DjangoJSONEncoder
56
from django.test import SimpleTestCase
67
from django.utils.functional import lazystr
@@ -113,12 +114,18 @@ def test_strip_tags(self):
113114
("<script>alert()</script>&h", "alert()h"),
114115
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
115116
("X<<<<br>br>br>br>X", "XX"),
117+
("<" * 50 + "a>" * 50, ""),
116118
)
117119
for value, output in items:
118120
with self.subTest(value=value, output=output):
119121
self.check_output(strip_tags, value, output)
120122
self.check_output(strip_tags, lazystr(value), output)
121123

124+
def test_strip_tags_suspicious_operation(self):
125+
value = "<" * 51 + "a>" * 51, "<a>"
126+
with self.assertRaises(SuspiciousOperation):
127+
strip_tags(value)
128+
122129
def test_strip_tags_files(self):
123130
# Test with more lengthy content (also catching performance regressions)
124131
for filename in ("strip_tags1.html", "strip_tags2.txt"):

0 commit comments

Comments
 (0)