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

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