Blame SOURCES/00303-CVE-2018-1060-1.patch

925e6b
diff --git a/Lib/difflib.py b/Lib/difflib.py
925e6b
index 1c6fbdbedcb7..788a92df3f89 100644
925e6b
--- a/Lib/difflib.py
925e6b
+++ b/Lib/difflib.py
925e6b
@@ -1103,7 +1103,7 @@ def _qformat(self, aline, bline, atags, btags):
925e6b
 
925e6b
 import re
925e6b
 
925e6b
-def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
925e6b
+def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
925e6b
     r"""
925e6b
     Return 1 for ignorable line: iff `line` is blank or contains a single '#'.
925e6b
 
925e6b
diff --git a/Lib/poplib.py b/Lib/poplib.py
925e6b
index b91e5f72d2ca..a238510b38fc 100644
925e6b
--- a/Lib/poplib.py
925e6b
+++ b/Lib/poplib.py
925e6b
@@ -274,7 +274,7 @@ def rpop(self, user):
925e6b
         return self._shortcmd('RPOP %s' % user)
925e6b
 
925e6b
 
925e6b
-    timestamp = re.compile(r'\+OK.*(<[^>]+>)')
925e6b
+    timestamp = re.compile(br'\+OK.[^<]*(<.*>)')
925e6b
 
925e6b
     def apop(self, user, secret):
925e6b
         """Authorisation
925e6b
diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py
925e6b
index 35f2c36ca70a..d8277b79b880 100644
925e6b
--- a/Lib/test/test_difflib.py
925e6b
+++ b/Lib/test/test_difflib.py
925e6b
@@ -269,13 +269,33 @@ def test_range_format_context(self):
925e6b
         self.assertEqual(fmt(3,6), '4,6')
925e6b
         self.assertEqual(fmt(0,0), '0')
925e6b
 
925e6b
+class TestJunkAPIs(unittest.TestCase):
925e6b
+    def test_is_line_junk_true(self):
925e6b
+        for line in ['#', '  ', ' #', '# ', ' # ', '']:
925e6b
+            self.assertTrue(difflib.IS_LINE_JUNK(line), repr(line))
925e6b
+
925e6b
+    def test_is_line_junk_false(self):
925e6b
+        for line in ['##', ' ##', '## ', 'abc ', 'abc #', 'Mr. Moose is up!']:
925e6b
+            self.assertFalse(difflib.IS_LINE_JUNK(line), repr(line))
925e6b
+
925e6b
+    def test_is_line_junk_REDOS(self):
925e6b
+        evil_input = ('\t' * 1000000) + '##'
925e6b
+        self.assertFalse(difflib.IS_LINE_JUNK(evil_input))
925e6b
+
925e6b
+    def test_is_character_junk_true(self):
925e6b
+        for char in [' ', '\t']:
925e6b
+            self.assertTrue(difflib.IS_CHARACTER_JUNK(char), repr(char))
925e6b
+
925e6b
+    def test_is_character_junk_false(self):
925e6b
+        for char in ['a', '#', '\n', '\f', '\r', '\v']:
925e6b
+            self.assertFalse(difflib.IS_CHARACTER_JUNK(char), repr(char))
925e6b
 
925e6b
 def test_main():
925e6b
     difflib.HtmlDiff._default_prefix = 0
925e6b
     Doctests = doctest.DocTestSuite(difflib)
925e6b
     run_unittest(
925e6b
         TestWithAscii, TestAutojunk, TestSFpatches, TestSFbugs,
925e6b
-        TestOutputFormat, Doctests)
925e6b
+        TestOutputFormat, TestJunkAPIs)
925e6b
 
925e6b
 if __name__ == '__main__':
925e6b
     test_main()
925e6b
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
925e6b
index 23d688724b95..d2143759ba66 100644
925e6b
--- a/Lib/test/test_poplib.py
925e6b
+++ b/Lib/test/test_poplib.py
925e6b
@@ -211,6 +211,16 @@ def test_noop(self):
925e6b
     def test_rpop(self):
925e6b
         self.assertOK(self.client.rpop('foo'))
925e6b
 
925e6b
+    def test_apop_REDOS(self):
925e6b
+        # Replace welcome with very long evil welcome.
925e6b
+        # NB The upper bound on welcome length is currently 2048.
925e6b
+        # At this length, evil input makes each apop call take
925e6b
+        # on the order of milliseconds instead of microseconds.
925e6b
+        evil_welcome = b'+OK' + (b'<' * 1000000)
925e6b
+        with test_support.swap_attr(self.client, 'welcome', evil_welcome):
925e6b
+            # The evil welcome is invalid, so apop should throw.
925e6b
+            self.assertRaises(poplib.error_proto, self.client.apop, 'a', 'kb')
925e6b
+
925e6b
     def test_top(self):
925e6b
         expected =  ('+OK 116 bytes',
925e6b
                      ['From: postmaster@python.org', 'Content-Type: text/plain',