Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6b3cb9

Browse files
committed
[1.6.x] Fixed an infinite loop possibility in strip_tags().
This is a security fix; disclosure to follow shortly.
1 parent 581a439 commit b6b3cb9

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

django/utils/html.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ def strip_tags(value):
156156
if not ('<' in value or '>' in value):
157157
return value
158158
new_value = _strip_once(value)
159-
if new_value == value:
160-
# _strip_once was not able to detect more tags
159+
if len(new_value) >= len(value):
160+
# _strip_once was not able to detect more tags or length increased
161+
# due to http://bugs.python.org/issue20288
162+
# (affects Python 2 < 2.7.7 and Python 3 < 3.3.5)
161163
return value
162164
else:
163165
value = new_value

docs/releases/1.6.11.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,20 @@ Django 1.6.11 release notes
55
*March 18, 2015*
66

77
Django 1.6.11 fixes two security issues in 1.6.10.
8+
9+
Denial-of-service possibility with ``strip_tags()``
10+
===================================================
11+
12+
Last year :func:`~django.utils.html.strip_tags` was changed to work
13+
iteratively. The problem is that the size of the input it's processing can
14+
increase on each iteration which results in an infinite loop in
15+
``strip_tags()``. This issue only affects versions of Python that haven't
16+
received `a bugfix in HTMLParser <http://bugs.python.org/issue20288>`_; namely
17+
Python < 2.7.7 and 3.3.5. Some operating system vendors have also backported
18+
the fix for the Python bug into their packages of earlier versions.
19+
20+
To remedy this issue, ``strip_tags()`` will now return the original input if
21+
it detects the length of the string it's processing increases. Remember that
22+
absolutely NO guarantee is provided about the results of ``strip_tags()`` being
23+
HTML safe. So NEVER mark safe the result of a ``strip_tags()`` call without
24+
escaping it first, for example with :func:`~django.utils.html.escape`.

tests/utils_tests/test_html.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def test_strip_tags(self):
8080
('a<p a >b</p>c', 'abc'),
8181
('d<a:b c:d>e</p>f', 'def'),
8282
('<strong>foo</strong><a href="http://example.com">bar</a>', 'foobar'),
83+
# caused infinite loop on Pythons not patched with
84+
# http://bugs.python.org/issue20288
85+
('&gotcha&#;<>', '&gotcha&#;<>'),
8386
)
8487
for value, output in items:
8588
self.check_output(f, value, output)

0 commit comments

Comments
 (0)