From 525266e5aa4f7556c0bff506f8f55a53e45c42e6 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Fri, 20 Nov 2026 18:47:42 +0800 Subject: [PATCH] Reject invalid HTTP header names HTTP field names must be nonempty tokens. Accepting whitespace before the colon can make Cheroot interpret message framing differently from an upstream recipient. Validate the original name before normalization as required by RFC 9110 section 5.1 and RFC 9112 section 5.1. https://github.com/cherrypy/cheroot/issues/714 https://github.com/cherrypy/cheroot/issues/721 Assisted-by: LLM --- cheroot/server.py | 2 + cheroot/test/test_header_names.py | 74 +++++++++++++++++++++++ docs/changelog-fragments.d/714.bugfix.rst | 3 + 3 files changed, 79 insertions(+) create mode 100644 cheroot/test/test_header_names.py create mode 100644 docs/changelog-fragments.d/714.bugfix.rst diff --git a/cheroot/server.py b/cheroot/server.py index 89793309aa..626f22bd89 100644 --- a/cheroot/server.py +++ b/cheroot/server.py @@ -246,6 +246,8 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME k, v = line.split(COLON, 1) except ValueError: raise ValueError('Illegal header line.') + if not re.fullmatch(rb"[!#$%&'*+.^_`|~0-9A-Za-z-]+", k): + raise ValueError('Invalid HTTP header name.') v = v.strip() k = self._transform_key(k) hname = k diff --git a/cheroot/test/test_header_names.py b/cheroot/test/test_header_names.py new file mode 100644 index 0000000000..6ed72a4c51 --- /dev/null +++ b/cheroot/test/test_header_names.py @@ -0,0 +1,74 @@ +"""Exercise HTTP header parsing 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( + ('header_name', 'expected_status'), + ( + (b'', HTTPStatus.BAD_REQUEST), + (b'Test ', HTTPStatus.BAD_REQUEST), + (b'Test\t', HTTPStatus.BAD_REQUEST), + (b'Test\v', HTTPStatus.BAD_REQUEST), + (b'Test\f', HTTPStatus.BAD_REQUEST), + (b'Test\r', HTTPStatus.BAD_REQUEST), + (b'Content-Length ', HTTPStatus.BAD_REQUEST), + (b'Te st', HTTPStatus.BAD_REQUEST), + (b'Te(st)', HTTPStatus.BAD_REQUEST), + (b'Te\x00st', HTTPStatus.BAD_REQUEST), + (b'Test\x80', HTTPStatus.BAD_REQUEST), + (b'Test', HTTPStatus.OK), + (b'tEsT', HTTPStatus.OK), + (b"!#$%&'*+-.^_`|~0123456789aZ", HTTPStatus.OK), + ), +) +def test_header_name_grammar( + native_server: HTTPServer, + http_request_timeout: float, + header_name: bytes, + expected_status: HTTPStatus, +) -> None: + """Accept only nonempty HTTP tokens as header names. + + :param native_server: live HTTP server + :param http_request_timeout: socket timeout in seconds + :param header_name: field name to send on the wire + :param expected_status: response required by the HTTP token grammar + """ + native_server.gateway = _AcceptedGateway + host, port = native_server.bind_addr + request = b''.join( + ( + b'GET / HTTP/1.1\r\nHost: localhost\r\n', + header_name, + b': 0\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/714.bugfix.rst b/docs/changelog-fragments.d/714.bugfix.rst new file mode 100644 index 0000000000..b5eef12227 --- /dev/null +++ b/docs/changelog-fragments.d/714.bugfix.rst @@ -0,0 +1,3 @@ +Rejected empty or malformed HTTP header names with ``400 Bad Request``, +including whitespace between the field name and colon +(:issue:`714`, :issue:`721`).