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..05d98e4c75 --- /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 following 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`).