Blame SOURCES/00368-CVE-2021-3737.patch

956e31
From f7fb35b563a9182c22fbdd03c72ec3724dafe918 Mon Sep 17 00:00:00 2001
956e31
From: Gen Xu <xgbarry@gmail.com>
956e31
Date: Wed, 5 May 2021 15:42:41 -0700
956e31
Subject: [PATCH] bpo-44022: Fix http client infinite line reading (DoS) after
956e31
 a HTTP 100 Continue (GH-25916)
956e31
956e31
Fixes http.client potential denial of service where it could get stuck reading lines from a malicious server after a 100 Continue response.
956e31
956e31
Co-authored-by: Gregory P. Smith <greg@krypto.org>
956e31
(cherry picked from commit 47895e31b6f626bc6ce47d175fe9d43c1098909d)
956e31
956e31
Co-authored-by: Gen Xu <xgbarry@gmail.com>
956e31
---
956e31
 Lib/http/client.py                            | 38 ++++++++++---------
956e31
 Lib/test/test_httplib.py                      | 10 ++++-
956e31
 .../2021-05-05-17-37-04.bpo-44022.bS3XJ9.rst  |  2 +
956e31
 3 files changed, 32 insertions(+), 18 deletions(-)
956e31
 create mode 100644 Misc/NEWS.d/next/Security/2021-05-05-17-37-04.bpo-44022.bS3XJ9.rst
956e31
956e31
diff --git a/Lib/http/client.py b/Lib/http/client.py
956e31
index 53581eca20587..07e675fac5981 100644
956e31
--- a/Lib/http/client.py
956e31
+++ b/Lib/http/client.py
956e31
@@ -205,15 +205,11 @@ def getallmatchingheaders(self, name):
956e31
                 lst.append(line)
956e31
         return lst
956e31
 
956e31
-def parse_headers(fp, _class=HTTPMessage):
956e31
-    """Parses only RFC2822 headers from a file pointer.
956e31
-
956e31
-    email Parser wants to see strings rather than bytes.
956e31
-    But a TextIOWrapper around self.rfile would buffer too many bytes
956e31
-    from the stream, bytes which we later need to read as bytes.
956e31
-    So we read the correct bytes here, as bytes, for email Parser
956e31
-    to parse.
956e31
+def _read_headers(fp):
956e31
+    """Reads potential header lines into a list from a file pointer.
956e31
 
956e31
+    Length of line is limited by _MAXLINE, and number of
956e31
+    headers is limited by _MAXHEADERS.
956e31
     """
956e31
     headers = []
956e31
     while True:
956e31
@@ -225,6 +221,19 @@ def parse_headers(fp, _class=HTTPMessage):
956e31
             raise HTTPException("got more than %d headers" % _MAXHEADERS)
956e31
         if line in (b'\r\n', b'\n', b''):
956e31
             break
956e31
+    return headers
956e31
+
956e31
+def parse_headers(fp, _class=HTTPMessage):
956e31
+    """Parses only RFC2822 headers from a file pointer.
956e31
+
956e31
+    email Parser wants to see strings rather than bytes.
956e31
+    But a TextIOWrapper around self.rfile would buffer too many bytes
956e31
+    from the stream, bytes which we later need to read as bytes.
956e31
+    So we read the correct bytes here, as bytes, for email Parser
956e31
+    to parse.
956e31
+
956e31
+    """
956e31
+    headers = _read_headers(fp)
956e31
     hstring = b''.join(headers).decode('iso-8859-1')
956e31
     return email.parser.Parser(_class=_class).parsestr(hstring)
956e31
 
956e31
@@ -312,15 +321,10 @@ def begin(self):
956e31
             if status != CONTINUE:
956e31
                 break
956e31
             # skip the header from the 100 response
956e31
-            while True:
956e31
-                skip = self.fp.readline(_MAXLINE + 1)
956e31
-                if len(skip) > _MAXLINE:
956e31
-                    raise LineTooLong("header line")
956e31
-                skip = skip.strip()
956e31
-                if not skip:
956e31
-                    break
956e31
-                if self.debuglevel > 0:
956e31
-                    print("header:", skip)
956e31
+            skipped_headers = _read_headers(self.fp)
956e31
+            if self.debuglevel > 0:
956e31
+                print("headers:", skipped_headers)
956e31
+            del skipped_headers
956e31
 
956e31
         self.code = self.status = status
956e31
         self.reason = reason.strip()
956e31
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
956e31
index 03e049b13fd21..0db287507c7bf 100644
956e31
--- a/Lib/test/test_httplib.py
956e31
+++ b/Lib/test/test_httplib.py
956e31
@@ -971,6 +971,14 @@ def test_overflowing_header_line(self):
956e31
         resp = client.HTTPResponse(FakeSocket(body))
956e31
         self.assertRaises(client.LineTooLong, resp.begin)
956e31
 
956e31
+    def test_overflowing_header_limit_after_100(self):
956e31
+        body = (
956e31
+            'HTTP/1.1 100 OK\r\n'
956e31
+            'r\n' * 32768
956e31
+        )
956e31
+        resp = client.HTTPResponse(FakeSocket(body))
956e31
+        self.assertRaises(client.HTTPException, resp.begin)
956e31
+
956e31
     def test_overflowing_chunked_line(self):
956e31
         body = (
956e31
             'HTTP/1.1 200 OK\r\n'
956e31
@@ -1377,7 +1385,7 @@ def readline(self, limit):
956e31
 class OfflineTest(TestCase):
956e31
     def test_all(self):
956e31
         # Documented objects defined in the module should be in __all__
956e31
-        expected = {"responses"}  # White-list documented dict() object
956e31
+        expected = {"responses"}  # Allowlist documented dict() object
956e31
         # HTTPMessage, parse_headers(), and the HTTP status code constants are
956e31
         # intentionally omitted for simplicity
956e31
         blacklist = {"HTTPMessage", "parse_headers"}
956e31
diff --git a/Misc/NEWS.d/next/Security/2021-05-05-17-37-04.bpo-44022.bS3XJ9.rst b/Misc/NEWS.d/next/Security/2021-05-05-17-37-04.bpo-44022.bS3XJ9.rst
956e31
new file mode 100644
956e31
index 0000000000000..cf6b63e396155
956e31
--- /dev/null
956e31
+++ b/Misc/NEWS.d/next/Security/2021-05-05-17-37-04.bpo-44022.bS3XJ9.rst
956e31
@@ -0,0 +1,2 @@
956e31
+mod:`http.client` now avoids infinitely reading potential HTTP headers after a
956e31
+``100 Continue`` status response from the server.