Skip to content

Commit 9cd8028

Browse files
sarahboycenessita
authored andcommitted
[4.2.x] Fixed CVE-2025-32873 -- Mitigated potential DoS in strip_tags().
Thanks to Elias Myllymäki for the report, and Shai Berger and Jake Howard for the reviews. Co-authored-by: Natalia <124304+nessita@users.noreply.github.com> Backport of 9f3419b from main.
1 parent ca31ca0 commit 9cd8028

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

django/utils/html.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
MAX_URL_LENGTH = 2048
1818
MAX_STRIP_TAGS_DEPTH = 50
1919

20+
# HTML tag that opens but has no closing ">" after 1k+ chars.
21+
long_open_tag_without_closing_re = _lazy_re_compile(r"<[a-zA-Z][^>]{1000,}")
22+
2023

2124
@keep_lazy(SafeString)
2225
def escape(text):
@@ -175,6 +178,9 @@ def _strip_once(value):
175178
def strip_tags(value):
176179
"""Return the given HTML with all tags stripped."""
177180
value = str(value)
181+
for long_open_tag in long_open_tag_without_closing_re.finditer(value):
182+
if long_open_tag.group().count("<") >= MAX_STRIP_TAGS_DEPTH:
183+
raise SuspiciousOperation
178184
# Note: in typical case this loop executes _strip_once twice (the second
179185
# execution does not remove any more tags).
180186
strip_tags_depth = 0

docs/releases/4.2.21.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ Django 4.2.21 release notes
77
Django 4.2.21 fixes a security issue with severity "moderate", a data loss bug,
88
and a regression in 4.2.20.
99

10+
CVE-2025-32873: Denial-of-service possibility in ``strip_tags()``
11+
=================================================================
12+
13+
:func:`~django.utils.html.strip_tags` would be slow to evaluate certain inputs
14+
containing large sequences of incomplete HTML tags. This function is used to
15+
implement the :tfilter:`striptags` template filter, which was thus also
16+
vulnerable.
17+
18+
:func:`~django.utils.html.strip_tags` now raises a :exc:`.SuspiciousOperation`
19+
exception if it encounters an unusually large number of unclosed opening tags.
20+
1021
Bugfixes
1122
========
1223

tests/utils_tests/test_html.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,30 @@ def test_strip_tags(self):
115115
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
116116
("X<<<<br>br>br>br>X", "XX"),
117117
("<" * 50 + "a>" * 50, ""),
118+
(">" + "<a" * 500 + "a", ">" + "<a" * 500 + "a"),
119+
("<a" * 49 + "a" * 951, "<a" * 49 + "a" * 951),
120+
("<" + "a" * 1_002, "<" + "a" * 1_002),
118121
)
119122
for value, output in items:
120123
with self.subTest(value=value, output=output):
121124
self.check_output(strip_tags, value, output)
122125
self.check_output(strip_tags, lazystr(value), output)
123126

124-
def test_strip_tags_suspicious_operation(self):
127+
def test_strip_tags_suspicious_operation_max_depth(self):
125128
value = "<" * 51 + "a>" * 51, "<a>"
126129
with self.assertRaises(SuspiciousOperation):
127130
strip_tags(value)
128131

132+
def test_strip_tags_suspicious_operation_large_open_tags(self):
133+
items = [
134+
">" + "<a" * 501,
135+
"<a" * 50 + "a" * 950,
136+
]
137+
for value in items:
138+
with self.subTest(value=value):
139+
with self.assertRaises(SuspiciousOperation):
140+
strip_tags(value)
141+
129142
def test_strip_tags_files(self):
130143
# Test with more lengthy content (also catching performance regressions)
131144
for filename in ("strip_tags1.html", "strip_tags2.txt"):

0 commit comments

Comments
 (0)