Skip to content

Commit 52479ac

Browse files
apollo13carltongibson
authored andcommitted
[1.11.x] Fixed CVE-2019-14233 -- Prevented excessive HTMLParser recursion in strip_tags() when handling incomplete HTML entities.
Thanks to Guido Vranken for initial report.
1 parent 42a66e9 commit 52479ac

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

django/utils/html.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ def strip_tags(value):
169169
value = force_text(value)
170170
while '<' in value and '>' in value:
171171
new_value = _strip_once(value)
172-
if len(new_value) >= len(value):
173-
# _strip_once was not able to detect more tags or length increased
172+
if len(new_value) >= len(value) or value.count('<') == new_value.count('<'):
173+
# _strip_once wasn't able to detect more tags, or line length increased.
174174
# due to http://bugs.python.org/issue20288
175175
# (affects Python 2 < 2.7.7 and Python 3 < 3.3.5)
176176
break

docs/releases/1.11.23.txt

+17
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,20 @@ filters, which were thus vulnerable.
1919
The regular expressions used by ``Truncator`` have been simplified in order to
2020
avoid potential backtracking issues. As a consequence, trailing punctuation may
2121
now at times be included in the truncated output.
22+
23+
CVE-2019-14233: Denial-of-service possibility in ``strip_tags()``
24+
=================================================================
25+
26+
Due to the behavior of the underlying ``HTMLParser``,
27+
:func:`django.utils.html.strip_tags` would be extremely slow to evaluate
28+
certain inputs containing large sequences of nested incomplete HTML entities.
29+
The ``strip_tags()`` method is used to implement the corresponding
30+
:tfilter:`striptags` template filter, which was thus also vulnerable.
31+
32+
``strip_tags()`` now avoids recursive calls to ``HTMLParser`` when progress
33+
removing tags, but necessarily incomplete HTML entities, stops being made.
34+
35+
Remember that absolutely NO guarantee is provided about the results of
36+
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
37+
``strip_tags()`` call without escaping it first, for example with
38+
:func:`django.utils.html.escape`.

tests/utils_tests/test_html.py

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def test_strip_tags(self):
8686
# caused infinite loop on Pythons not patched with
8787
# http://bugs.python.org/issue20288
8888
('&gotcha&#;<>', '&gotcha&#;<>'),
89+
('><!' + ('&' * 16000) + 'D', '><!' + ('&' * 16000) + 'D'),
90+
('X<<<<br>br>br>br>X', 'XX'),
8991
)
9092
for value, output in items:
9193
self.check_output(f, value, output)

0 commit comments

Comments
 (0)