From 92810ea285aa50661ca0a739f8a6d2335a961d34 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Fri, 20 Nov 2026 18:51:18 +0800 Subject: [PATCH] Reject whitespace after HTTP request versions The request-line grammar places CRLF directly after HTTP-version. Stripping the entire line and letting int() trim its input accepts extra whitespace, which can disagree with other HTTP recipients. https://github.com/cherrypy/cheroot/issues/717 Assisted-by: LLM --- cheroot/server.py | 7 +- cheroot/test/test_request_line_whitespace.py | 71 ++++++++++++++++++++ docs/changelog-fragments.d/717.bugfix.rst | 2 + 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 cheroot/test/test_request_line_whitespace.py create mode 100644 docs/changelog-fragments.d/717.bugfix.rst diff --git a/cheroot/server.py b/cheroot/server.py index 89793309aa..00b5a49bc6 100644 --- a/cheroot/server.py +++ b/cheroot/server.py @@ -801,8 +801,11 @@ def read_request_line(self): # noqa: C901 # FIXME return False try: - method, uri, req_protocol = request_line.strip().split(SPACE, 2) - if not req_protocol.startswith(b'HTTP/'): + method, uri, req_protocol = request_line[:-2].split(SPACE, 2) + if ( + not req_protocol.startswith(b'HTTP/') + or req_protocol[-1:].isspace() + ): self.simple_response( '400 Bad Request', 'Malformed Request-Line: bad protocol', diff --git a/cheroot/test/test_request_line_whitespace.py b/cheroot/test/test_request_line_whitespace.py new file mode 100644 index 0000000000..60d5dec387 --- /dev/null +++ b/cheroot/test/test_request_line_whitespace.py @@ -0,0 +1,71 @@ +"""Check request-line terminators over real connections.""" + +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( + ('version', 'expected_status'), + ( + (b'1.1 ', HTTPStatus.BAD_REQUEST), + (b'1.1\t', HTTPStatus.BAD_REQUEST), + (b'1.1\v', HTTPStatus.BAD_REQUEST), + (b'1.1\f', HTTPStatus.BAD_REQUEST), + (b'1.1\r', HTTPStatus.BAD_REQUEST), + (b'1.1 \t', HTTPStatus.BAD_REQUEST), + (b'1.0 ', HTTPStatus.BAD_REQUEST), + (b'1.2 ', HTTPStatus.BAD_REQUEST), + (b'1.0', HTTPStatus.OK), + (b'1.1', HTTPStatus.OK), + (b'1.2', HTTPStatus.HTTP_VERSION_NOT_SUPPORTED), + ), +) +def test_request_line_trailing_whitespace( + native_server: HTTPServer, + http_request_timeout: float, + version: bytes, + expected_status: HTTPStatus, +) -> None: + """Require the HTTP version to be followed immediately by CRLF. + + :param native_server: live HTTP server + :param http_request_timeout: socket timeout in seconds + :param version: version text including any extra whitespace + :param expected_status: response required by the request-line grammar + """ + native_server.gateway = _AcceptedGateway + host, port = native_server.bind_addr + request = b''.join( + ( + b'GET / HTTP/', + version, + b'\r\nHost: localhost\r\nConnection: close\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/717.bugfix.rst b/docs/changelog-fragments.d/717.bugfix.rst new file mode 100644 index 0000000000..b923fb8431 --- /dev/null +++ b/docs/changelog-fragments.d/717.bugfix.rst @@ -0,0 +1,2 @@ +Rejected whitespace between the HTTP version and the request-line CRLF +terminator with ``400 Bad Request`` (:issue:`717`).