Skip to content

Commit

Permalink
[2.2.x] Fixed CVE-2019-14233 -- Prevented excessive HTMLParser recurs…
Browse files Browse the repository at this point in the history
…ion in strip_tags() when handling incomplete HTML entities.

Thanks to Guido Vranken for initial report.
  • Loading branch information
apollo13 authored and carltongibson committed Jul 29, 2019
1 parent c328971 commit e34f3c0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django/utils/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def strip_tags(value):
value = str(value)
while '<' in value and '>' in value:
new_value = _strip_once(value)
if len(new_value) >= len(value):
# _strip_once was not able to detect more tags
if value.count('<') == new_value.count('<'):
# _strip_once wasn't able to detect more tags.
break
value = new_value
return value
Expand Down
17 changes: 17 additions & 0 deletions docs/releases/1.11.23.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ filters, which were thus vulnerable.
The regular expressions used by ``Truncator`` have been simplified in order to
avoid potential backtracking issues. As a consequence, trailing punctuation may
now at times be included in the truncated output.

CVE-2019-14233: Denial-of-service possibility in ``strip_tags()``
=================================================================

Due to the behavior of the underlying ``HTMLParser``,
:func:`django.utils.html.strip_tags` would be extremely slow to evaluate
certain inputs containing large sequences of nested incomplete HTML entities.
The ``strip_tags()`` method is used to implement the corresponding
:tfilter:`striptags` template filter, which was thus also vulnerable.

``strip_tags()`` now avoids recursive calls to ``HTMLParser`` when progress
removing tags, but necessarily incomplete HTML entities, stops being made.

Remember that absolutely NO guarantee is provided about the results of
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
``strip_tags()`` call without escaping it first, for example with
:func:`django.utils.html.escape`.
17 changes: 17 additions & 0 deletions docs/releases/2.1.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ filters, which were thus vulnerable.
The regular expressions used by ``Truncator`` have been simplified in order to
avoid potential backtracking issues. As a consequence, trailing punctuation may
now at times be included in the truncated output.

CVE-2019-14233: Denial-of-service possibility in ``strip_tags()``
=================================================================

Due to the behavior of the underlying ``HTMLParser``,
:func:`django.utils.html.strip_tags` would be extremely slow to evaluate
certain inputs containing large sequences of nested incomplete HTML entities.
The ``strip_tags()`` method is used to implement the corresponding
:tfilter:`striptags` template filter, which was thus also vulnerable.

``strip_tags()`` now avoids recursive calls to ``HTMLParser`` when progress
removing tags, but necessarily incomplete HTML entities, stops being made.

Remember that absolutely NO guarantee is provided about the results of
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
``strip_tags()`` call without escaping it first, for example with
:func:`django.utils.html.escape`.
17 changes: 17 additions & 0 deletions docs/releases/2.2.4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ The regular expressions used by ``Truncator`` have been simplified in order to
avoid potential backtracking issues. As a consequence, trailing punctuation may
now at times be included in the truncated output.

CVE-2019-14233: Denial-of-service possibility in ``strip_tags()``
=================================================================

Due to the behavior of the underlying ``HTMLParser``,
:func:`django.utils.html.strip_tags` would be extremely slow to evaluate
certain inputs containing large sequences of nested incomplete HTML entities.
The ``strip_tags()`` method is used to implement the corresponding
:tfilter:`striptags` template filter, which was thus also vulnerable.

``strip_tags()`` now avoids recursive calls to ``HTMLParser`` when progress
removing tags, but necessarily incomplete HTML entities, stops being made.

Remember that absolutely NO guarantee is provided about the results of
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
``strip_tags()`` call without escaping it first, for example with
:func:`django.utils.html.escape`.

Bugfixes
========

Expand Down
2 changes: 2 additions & 0 deletions tests/utils_tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def test_strip_tags(self):
('&gotcha&#;<>', '&gotcha&#;<>'),
('<sc<!-- -->ript>test<<!-- -->/script>', 'ript>test'),
('<script>alert()</script>&h', 'alert()h'),
('><!' + ('&' * 16000) + 'D', '><!' + ('&' * 16000) + 'D'),
('X<<<<br>br>br>br>X', 'XX'),
)
for value, output in items:
with self.subTest(value=value, output=output):
Expand Down

0 comments on commit e34f3c0

Please sign in to comment.