티스토리 수익 글 보기

티스토리 수익 글 보기

[5.1.x] Fixed CVE-2024-53907 — Mitigated potential DoS in strip_tags(). · django/django@bbc74a7 · GitHub
Skip to content

Commit bbc74a7

Browse files
committed
[5.1.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 5b4d949 commit bbc74a7

File tree

5 files changed

+63
2
lines changed

5 files changed

+63
2
lines changed

django/utils/html.py

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

10+
from django.core.exceptions import SuspiciousOperation
1011
from django.utils.deprecation import RemovedInDjango60Warning
1112
from django.utils.encoding import punycode
1213
from django.utils.functional import Promise, cached_property, keep_lazy, keep_lazy_text
@@ -39,6 +40,7 @@
3940
)
4041

4142
MAX_URL_LENGTH = 2048
43+
MAX_STRIP_TAGS_DEPTH = 50
4244

4345

4446
@keep_lazy(SafeString)
@@ -205,15 +207,19 @@ def _strip_once(value):
205207
@keep_lazy_text
206208
def strip_tags(value):
207209
"""Return the given HTML with all tags stripped."""
208-
# Note: in typical case this loop executes _strip_once once. Loop condition
209-
# is redundant, but helps to reduce number of executions of _strip_once.
210210
value = str(value)
211+
# Note: in typical case this loop executes _strip_once twice (the second
212+
# execution does not remove any more tags).
213+
strip_tags_depth = 0
211214
while "<" in value and ">" in value:
215+
if strip_tags_depth >= MAX_STRIP_TAGS_DEPTH:
216+
raise SuspiciousOperation
212217
new_value = _strip_once(value)
213218
if value.count("<") == new_value.count("<"):
214219
# _strip_once wasn't able to detect more tags.
215220
break
216221
value = new_value
222+
strip_tags_depth += 1
217223
return value
218224

219225

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`.

docs/releases/5.0.10.txt

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

77
Django 5.0.10 fixes one security issue with severity "high" and one security
88
issue with severity "moderate" in 5.0.9.
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`.

docs/releases/5.1.4.txt

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

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`.
25+
1026
Bugfixes
1127
========
1228

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.deprecation import RemovedInDjango60Warning
@@ -124,12 +125,18 @@ def test_strip_tags(self):
124125
("<script>alert()</script>&h", "alert()h"),
125126
("><!" + ("&" * 16000) + "D", "><!" + ("&" * 16000) + "D"),
126127
("X<<<<br>br>br>br>X", "XX"),
128+
("<" * 50 + "a>" * 50, ""),
127129
)
128130
for value, output in items:
129131
with self.subTest(value=value, output=output):
130132
self.check_output(strip_tags, value, output)
131133
self.check_output(strip_tags, lazystr(value), output)
132134

135+
def test_strip_tags_suspicious_operation(self):
136+
value = "<" * 51 + "a>" * 51, "<a>"
137+
with self.assertRaises(SuspiciousOperation):
138+
strip_tags(value)
139+
133140
def test_strip_tags_files(self):
134141
# Test with more lengthy content (also catching performance regressions)
135142
for filename in ("strip_tags1.html", "strip_tags2.txt"):

0 commit comments

Comments
 (0)