CVE-2019-16792: Inconsistent Interpretation of HTTP Requests ('HTTP Request/Response Smuggling') (CWE-444)
value: Waitress through version 1.3.1 allows request smuggling by sending the Content-Length header twice. Waitress would header fold a double Content-Length header and due to being unable to cast the now comma separated value to an integer would set the Content-Length to 0 internally. If two Content-Length headers are sent in a single request, Waitress would treat the request as having no body, thereby treating the body of the request as a new request in HTTP pipelining. This issue is fixed in Waitress 1.4.0.
CWE description: The product acts as an intermediary HTTP agent
         (such as a proxy or firewall) in the data flow between two
         entities such as a client and server, but it does not
         interpret malformed HTTP requests or responses in ways that
         are consistent with how the messages will be processed by
         those entities that are at the ultimate destination.
CVSS v3 base score: 7.5
Published: 2020-01-22T19:15Z
Repository: https://github.com/Pylons/waitress
Language: Python
Files changed: waitress/parser.py, waitress/tests/test_parser.py
Commit date: 2019-12-19 15:59:58 +0100
Commit message: Upon receiving invalid Content-Length bail

Instead of attempting to continue processing the request, we instead
raise a ParsingError and return a HTTP Bad Request to the client.

This also catches the case where two Content-Length's are sent, and are
folded together using HTTP header folding.

Diff: diff --git a/waitress/parser.py b/waitress/parser.py
index e2970cb..c537964 100644
--- a/waitress/parser.py
+++ b/waitress/parser.py
@@ -252,11 +252,12 @@ class HTTPRequestParser(object):
 
         if not self.chunked:
             try:
                 cl = int(headers.get("CONTENT_LENGTH", 0))
             except ValueError:
-                cl = 0
+                raise ParsingError("Content-Length is invalid")
+
             self.content_length = cl
             if cl > 0:
                 buf = OverflowableBuffer(self.adj.inbuf_overflow)
                 self.body_rcv = FixedStreamReceiver(cl, buf)
 
diff --git a/waitress/tests/test_parser.py b/waitress/tests/test_parser.py
index 463e0b2..aa01d9d 100644
--- a/waitress/tests/test_parser.py
+++ b/waitress/tests/test_parser.py
@@ -165,13 +165,32 @@ class TestHTTPRequestParser(unittest.TestCase):
             pass
         else:  # pragma: nocover
             self.assertTrue(False)
 
     def test_parse_header_bad_content_length(self):
+        from waitress.parser import ParsingError
+
         data = b"GET /foobar HTTP/8.4\r\ncontent-length: abc\r\n"
-        self.parser.parse_header(data)
-        self.assertEqual(self.parser.body_rcv, None)
+
+        try:
+            self.parser.parse_header(data)
+        except ParsingError as e:
+            self.assertIn("Content-Length is invalid", e.args[0])
+        else:  # pragma: nocover
+            self.assertTrue(False)
+
+    def test_parse_header_multiple_content_length(self):
+        from waitress.parser import ParsingError
+
+        data = b"GET /foobar HTTP/8.4\r\ncontent-length: 10\r\ncontent-length: 20\r\n"
+
+        try:
+            self.parser.parse_header(data)
+        except ParsingError as e:
+            self.assertIn("Content-Length is invalid", e.args[0])
+        else:  # pragma: nocover
+            self.assertTrue(False)
 
     def test_parse_header_11_te_chunked(self):
         # NB: test that capitalization of header value is unimportant
         data = b"GET /foobar HTTP/1.1\r\ntransfer-encoding: ChUnKed\r\n"
         self.parser.parse_header(data)

Vulnerable code:                 cl = 0
        self.parser.parse_header(data)
        self.assertEqual(self.parser.body_rcv, None)
Fixed code:                 raise ParsingError("Content-Length is invalid")

        from waitress.parser import ParsingError


        try:
            self.parser.parse_header(data)
        except ParsingError as e:
            self.assertIn("Content-Length is invalid", e.args[0])
        else:  # pragma: nocover
            self.assertTrue(False)

    def test_parse_header_multiple_content_length(self):
        from waitress.parser import ParsingError

        data = b"GET /foobar HTTP/8.4\r\ncontent-length: 10\r\ncontent-length: 20\r\n"

        try:
            self.parser.parse_header(data)
        except ParsingError as e:
            self.assertIn("Content-Length is invalid", e.args[0])
        else:  # pragma: nocover
            self.assertTrue(False)