Skip to content

Commit

Permalink
Fixed CVE-2018-7537 -- Fixed catastrophic backtracking in django.util…
Browse files Browse the repository at this point in the history
…s.text.Truncator.

Thanks James Davis for suggesting the fix.
  • Loading branch information
timgraham committed Mar 6, 2018
1 parent 8618271 commit 97b7dd5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def capfirst(x):
# Set up regular expressions
re_words = re.compile(r'<.*?>|((?:\w[-\w]*|&.*?;)+)', re.S)
re_chars = re.compile(r'<.*?>|(.)', re.S)
re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S)
re_tag = re.compile(r'<(/)?(\S+?)(?:(\s*/)|\s.*?)?>', re.S)
re_newlines = re.compile(r'\r\n|\r') # Used in normalize_newlines
re_camel_case = re.compile(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))')

Expand Down
12 changes: 12 additions & 0 deletions docs/releases/1.11.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ expressions. The ``urlize()`` function is used to implement the ``urlize`` and

The problematic regular expressions are replaced with parsing logic that
behaves similarly.

CVE-2018-7537: Denial-of-service possibility in ``truncatechars_html`` and ``truncatewords_html`` template filters
==================================================================================================================

If ``django.utils.text.Truncator``'s ``chars()`` and ``words()`` methods were
passed the ``html=True`` argument, they were extremely slow to evaluate certain
inputs due to a catastrophic backtracking vulnerability in a regular
expression. The ``chars()`` and ``words()`` methods are used to implement the
``truncatechars_html`` and ``truncatewords_html`` template filters, which were
thus vulnerable.

The backtracking problem in the regular expression is fixed.
12 changes: 12 additions & 0 deletions docs/releases/1.8.19.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,15 @@ expression. The ``urlize()`` function is used to implement the ``urlize`` and

The problematic regular expression is replaced with parsing logic that behaves
similarly.

CVE-2018-7537: Denial-of-service possibility in ``truncatechars_html`` and ``truncatewords_html`` template filters
==================================================================================================================

If ``django.utils.text.Truncator``'s ``chars()`` and ``words()`` methods were
passed the ``html=True`` argument, they were extremely slow to evaluate certain
inputs due to a catastrophic backtracking vulnerability in a regular
expression. The ``chars()`` and ``words()`` methods are used to implement the
``truncatechars_html`` and ``truncatewords_html`` template filters, which were
thus vulnerable.

The backtracking problem in the regular expression is fixed.
12 changes: 12 additions & 0 deletions docs/releases/2.0.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ expressions. The ``urlize()`` function is used to implement the ``urlize`` and
The problematic regular expressions are replaced with parsing logic that
behaves similarly.

CVE-2018-7537: Denial-of-service possibility in ``truncatechars_html`` and ``truncatewords_html`` template filters
==================================================================================================================

If ``django.utils.text.Truncator``'s ``chars()`` and ``words()`` methods were
passed the ``html=True`` argument, they were extremely slow to evaluate certain
inputs due to a catastrophic backtracking vulnerability in a regular
expression. The ``chars()`` and ``words()`` methods are used to implement the
``truncatechars_html`` and ``truncatewords_html`` template filters, which were
thus vulnerable.

The backtracking problem in the regular expression is fixed.

Bugfixes
========

Expand Down
4 changes: 4 additions & 0 deletions tests/utils_tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def test_truncate_html_words(self):
truncator = text.Truncator('<p>I &lt;3 python, what about you?</p>')
self.assertEqual('<p>I &lt;3 python...</p>', truncator.words(3, '...', html=True))

re_tag_catastrophic_test = ('</a' + '\t' * 50000) + '//>'
truncator = text.Truncator(re_tag_catastrophic_test)
self.assertEqual(re_tag_catastrophic_test, truncator.words(500, html=True))

def test_wrap(self):
digits = '1234 67 9'
self.assertEqual(text.wrap(digits, 100), '1234 67 9')
Expand Down

1 comment on commit 97b7dd5

@rohieb
Copy link

@rohieb rohieb commented on 97b7dd5 Sep 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to understand how this fix works. Can anybody give me a hint what is the difference here between \S and [^ ] when it comes to backtracking?

Please sign in to comment.