From c8c62f4c26b3b685ab8b428390cc574f8648d1bb Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Fri, 20 Nov 2026 18:57:48 +0800 Subject: [PATCH 1/2] Reject duplicate Content-Length fields Using the last of several Content-Length fields can disagree with an upstream HTTP recipient and leave a different request boundary. Reject duplicates before dispatch, including differently cased field names. RFC 9112 section 6.3 permits rejecting identical duplicate values too. https://github.com/cherrypy/cheroot/issues/737 Assisted-by: LLM --- cheroot/server.py | 2 + cheroot/test/test_duplicate_content_length.py | 66 +++++++++++++++++++ docs/changelog-fragments.d/737.bugfix.rst | 3 + 3 files changed, 71 insertions(+) create mode 100644 cheroot/test/test_duplicate_content_length.py create mode 100644 docs/changelog-fragments.d/737.bugfix.rst diff --git a/cheroot/server.py b/cheroot/server.py index 89793309aa..bbf6f962d0 100644 --- a/cheroot/server.py +++ b/cheroot/server.py @@ -248,6 +248,8 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME raise ValueError('Illegal header line.') v = v.strip() k = self._transform_key(k) + if k == b'Content-Length' and k in hdict: + raise ValueError('Multiple Content-Length headers.') hname = k if not self._allow_header(k): diff --git a/cheroot/test/test_duplicate_content_length.py b/cheroot/test/test_duplicate_content_length.py new file mode 100644 index 0000000000..2e8ddcb6ce --- /dev/null +++ b/cheroot/test/test_duplicate_content_length.py @@ -0,0 +1,66 @@ +"""Reject ambiguous HTTP request body lengths.""" + +from __future__ import annotations + +import socket +from http import HTTPStatus +from http.client import HTTPResponse + +import pytest + +from cheroot.server import Gateway, HTTPServer +from cheroot.test import webtest + + +class _AcceptedGateway(Gateway): + """Acknowledge requests that reach the application.""" + + def respond(self) -> None: + """Send an empty successful response.""" + self.req.status = b'200 OK' + self.req.outheaders = [(b'Content-Length', b'0')] + + +@pytest.mark.parametrize( + ('header_lines', 'expected_status'), + ( + (b'Content-Length: 0\r\nContent-Length: 1', HTTPStatus.BAD_REQUEST), + (b'Content-Length: 1\r\nContent-Length: 0', HTTPStatus.BAD_REQUEST), + (b'Content-Length: 1\r\nContent-Length: 1', HTTPStatus.BAD_REQUEST), + (b'Content-Length: 0\r\ncontent-length: 1', HTTPStatus.BAD_REQUEST), + (b'content-length: 0\r\nCONTENT-LENGTH: 1', HTTPStatus.BAD_REQUEST), + (b'Content-Length: 1\r\nConnection: close', HTTPStatus.OK), + ), +) +def test_duplicate_content_length( + native_server: HTTPServer, + http_request_timeout: float, + header_lines: bytes, + expected_status: HTTPStatus, +) -> None: + """Reject duplicate lengths and close before any pipelined request. + + :param native_server: live HTTP server + :param http_request_timeout: socket timeout in seconds + :param header_lines: length fields to send on the wire + :param expected_status: response required by the framing rules + """ + native_server.gateway = _AcceptedGateway + host, port = native_server.bind_addr + request = b''.join( + ( + b'POST / HTTP/1.1\r\nHost: localhost\r\n', + header_lines, + b'\r\n\r\naGET / HTTP/1.1\r\nHost: localhost\r\n\r\n', + ), + ) + with socket.create_connection( + (webtest.interface(host), port), + timeout=http_request_timeout, + ) as connection: + connection.sendall(request) + with HTTPResponse(connection) as response: + response.begin() + assert response.status == expected_status + response.read() + assert connection.recv(1) == b'' diff --git a/docs/changelog-fragments.d/737.bugfix.rst b/docs/changelog-fragments.d/737.bugfix.rst new file mode 100644 index 0000000000..3d95f9e277 --- /dev/null +++ b/docs/changelog-fragments.d/737.bugfix.rst @@ -0,0 +1,3 @@ +Rejected requests with multiple ``Content-Length`` fields with +``400 Bad Request`` and closed the connection, preventing inconsistent +request-body framing between HTTP recipients (:issue:`737`). From 99fd4017b7482058681efdf0f3225a03dd8439ae Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Sat, 21 Nov 2026 18:32:59 +0800 Subject: [PATCH 2/2] Reword request regression description The documentation spelling check does not recognize the verb form of pipeline. Describe the following request with ordinary wording while preserving the test behavior. Assisted-by: LLM --- cheroot/test/test_duplicate_content_length.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cheroot/test/test_duplicate_content_length.py b/cheroot/test/test_duplicate_content_length.py index 2e8ddcb6ce..05d98e4c75 100644 --- a/cheroot/test/test_duplicate_content_length.py +++ b/cheroot/test/test_duplicate_content_length.py @@ -38,7 +38,7 @@ def test_duplicate_content_length( header_lines: bytes, expected_status: HTTPStatus, ) -> None: - """Reject duplicate lengths and close before any pipelined request. + """Reject duplicate lengths and close before any following request. :param native_server: live HTTP server :param http_request_timeout: socket timeout in seconds