Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-32981: Fix catastrophic backtracking vulns #5955

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ def _qformat(self, aline, bline, atags, btags):

import re

def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
r"""
Return 1 for ignorable line: iff `line` is blank or contains a single '#'.

Expand Down
2 changes: 1 addition & 1 deletion Lib/poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def rpop(self, user):
return self._shortcmd('RPOP %s' % user)


timestamp = re.compile(br'\+OK.*(<[^>]+>)')
timestamp = re.compile(br'\+OK.[^<]*(<.*>)')

def apop(self, user, password):
"""Authorisation
Expand Down
22 changes: 21 additions & 1 deletion Lib/test/test_difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,33 @@ def _assert_type_error(self, msg, generator, *args):
list(generator(*args))
self.assertEqual(msg, str(ctx.exception))

class TestJunkAPIs(unittest.TestCase):
def test_is_line_junk_true(self):
for line in ['#', ' ', ' #', '# ', ' # ', '']:
self.assertTrue(difflib.IS_LINE_JUNK(line), repr(line))

def test_is_line_junk_false(self):
for line in ['##', ' ##', '## ', 'abc ', 'abc #', 'Mr. Moose is up!']:
self.assertFalse(difflib.IS_LINE_JUNK(line), repr(line))

def test_is_line_junk_REDOS(self):
evil_input = ('\t' * 1000000) + '##'
self.assertFalse(difflib.IS_LINE_JUNK(evil_input))

def test_is_character_junk_true(self):
for char in [' ', '\t']:
self.assertTrue(difflib.IS_CHARACTER_JUNK(char), repr(char))

def test_is_character_junk_false(self):
for char in ['a', '#', '\n', '\f', '\r', '\v']:
self.assertFalse(difflib.IS_CHARACTER_JUNK(char), repr(char))

def test_main():
difflib.HtmlDiff._default_prefix = 0
Doctests = doctest.DocTestSuite(difflib)
run_unittest(
TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
TestOutputFormat, TestBytes, Doctests)
TestOutputFormat, TestBytes, TestJunkAPIs, Doctests)

if __name__ == '__main__':
test_main()
12 changes: 11 additions & 1 deletion Lib/test/test_poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,19 @@ def test_noop(self):
def test_rpop(self):
self.assertOK(self.client.rpop('foo'))

def test_apop(self):
def test_apop_normal(self):
self.assertOK(self.client.apop('foo', 'dummypassword'))

def test_apop_REDOS(self):
# Replace welcome with very long evil welcome.
# NB The upper bound on welcome length is currently 2048.
# At this length, evil input makes each apop call take
# on the order of milliseconds instead of microseconds.
evil_welcome = b'+OK' + (b'<' * 1000000)
with test_support.swap_attr(self.client, 'welcome', evil_welcome):
# The evil welcome is invalid, so apop should throw.
self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')

def test_top(self):
expected = (b'+OK 116 bytes',
[b'From: postmaster@python.org', b'Content-Type: text/plain',
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ Jonathan Dasteel
Pierre-Yves David
A. Jesse Jiryu Davis
Jake Davis
Jamie (James C.) Davis
Ratnadeep Debnath
Merlijn van Deen
John DeGood
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Regexes in difflib and poplib were vulnerable to catastrophic backtracking.
These regexes formed potential DOS vectors (REDOS). They have been
refactored. This resolves CVE-2018-1060 and CVE-2018-1061.
Copy link
Member

Choose a reason for hiding this comment

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

Add "Patch by your name."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Patch by Jamie Davis.