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

0b7fb1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
0b7fb1
From: Lumir Balhar <lbalhar@redhat.com>
0b7fb1
Date: Fri, 17 Sep 2021 07:56:50 +0200
0b7fb1
Subject: [PATCH] 00368-CVE-2021-3737.patch
0b7fb1
0b7fb1
00368 #
0b7fb1
CVE-2021-3737: http client infinite line reading (DoS) after a HTTP 100 Continue
0b7fb1
0b7fb1
Fixes http.client potential denial of service where it could get stuck reading
0b7fb1
lines from a malicious server after a 100 Continue response.
0b7fb1
0b7fb1
Backported from Python 3.
0b7fb1
0b7fb1
Co-authored-by: Gregory P. Smith <greg@krypto.org>
0b7fb1
Co-authored-by: Gen Xu <xgbarry@gmail.com>
0b7fb1
---
0b7fb1
 Lib/httplib.py           | 32 +++++++++++++++++++++++---------
0b7fb1
 Lib/test/test_httplib.py |  8 ++++++++
0b7fb1
 2 files changed, 31 insertions(+), 9 deletions(-)
0b7fb1
0b7fb1
diff --git a/Lib/httplib.py b/Lib/httplib.py
0b7fb1
index a63677477d5..f9a27619e62 100644
0b7fb1
--- a/Lib/httplib.py
0b7fb1
+++ b/Lib/httplib.py
0b7fb1
@@ -365,6 +365,25 @@ class HTTPMessage(mimetools.Message):
0b7fb1
                 # It's not a header line; skip it and try the next line.
0b7fb1
                 self.status = 'Non-header line where header expected'
0b7fb1
 
0b7fb1
+
0b7fb1
+def _read_headers(fp):
0b7fb1
+    """Reads potential header lines into a list from a file pointer.
0b7fb1
+    Length of line is limited by _MAXLINE, and number of
0b7fb1
+    headers is limited by _MAXHEADERS.
0b7fb1
+    """
0b7fb1
+    headers = []
0b7fb1
+    while True:
0b7fb1
+        line = fp.readline(_MAXLINE + 1)
0b7fb1
+        if len(line) > _MAXLINE:
0b7fb1
+            raise LineTooLong("header line")
0b7fb1
+        headers.append(line)
0b7fb1
+        if len(headers) > _MAXHEADERS:
0b7fb1
+            raise HTTPException("got more than %d headers" % _MAXHEADERS)
0b7fb1
+        if line in (b'\r\n', b'\n', b''):
0b7fb1
+            break
0b7fb1
+    return headers
0b7fb1
+
0b7fb1
+
0b7fb1
 class HTTPResponse:
0b7fb1
 
0b7fb1
     # strict: If true, raise BadStatusLine if the status line can't be
0b7fb1
@@ -453,15 +472,10 @@ class HTTPResponse:
0b7fb1
             if status != CONTINUE:
0b7fb1
                 break
0b7fb1
             # skip the header from the 100 response
0b7fb1
-            while True:
0b7fb1
-                skip = self.fp.readline(_MAXLINE + 1)
0b7fb1
-                if len(skip) > _MAXLINE:
0b7fb1
-                    raise LineTooLong("header line")
0b7fb1
-                skip = skip.strip()
0b7fb1
-                if not skip:
0b7fb1
-                    break
0b7fb1
-                if self.debuglevel > 0:
0b7fb1
-                    print "header:", skip
0b7fb1
+            skipped_headers = _read_headers(self.fp)
0b7fb1
+            if self.debuglevel > 0:
0b7fb1
+                print("headers:", skipped_headers)
0b7fb1
+            del skipped_headers
0b7fb1
 
0b7fb1
         self.status = status
0b7fb1
         self.reason = reason.strip()
0b7fb1
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
0b7fb1
index b5fec9aa1ec..d05c0fc28d2 100644
0b7fb1
--- a/Lib/test/test_httplib.py
0b7fb1
+++ b/Lib/test/test_httplib.py
0b7fb1
@@ -700,6 +700,14 @@ class BasicTest(TestCase):
0b7fb1
         resp = httplib.HTTPResponse(FakeSocket(body))
0b7fb1
         self.assertRaises(httplib.LineTooLong, resp.begin)
0b7fb1
 
0b7fb1
+    def test_overflowing_header_limit_after_100(self):
0b7fb1
+        body = (
0b7fb1
+            'HTTP/1.1 100 OK\r\n'
0b7fb1
+            'r\n' * 32768
0b7fb1
+        )
0b7fb1
+        resp = httplib.HTTPResponse(FakeSocket(body))
0b7fb1
+        self.assertRaises(httplib.HTTPException, resp.begin)
0b7fb1
+
0b7fb1
     def test_overflowing_chunked_line(self):
0b7fb1
         body = (
0b7fb1
             'HTTP/1.1 200 OK\r\n'