From ff061df124250bceda805c1e9f728a97a3b481dd Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Fri, 20 Nov 2026 18:54:14 +0800 Subject: [PATCH] Reject control characters in HTTP header values CR, LF and NUL in field values can be interpreted differently by HTTP recipients. RFC 9110 section 5.5 requires rejecting or replacing them before processing. Checking before whitespace trimming also catches carriage returns at the edges of a value. https://github.com/cherrypy/cheroot/issues/720 Assisted-by: LLM --- cheroot/server.py | 2 + cheroot/test/test_header_values.py | 72 +++++++++++++++++++++++ docs/changelog-fragments.d/720.bugfix.rst | 2 + 3 files changed, 76 insertions(+) create mode 100644 cheroot/test/test_header_values.py create mode 100644 docs/changelog-fragments.d/720.bugfix.rst diff --git a/cheroot/server.py b/cheroot/server.py index 89793309aa..96852789d1 100644 --- a/cheroot/server.py +++ b/cheroot/server.py @@ -233,6 +233,8 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME break if not line.endswith(CRLF): raise ValueError('HTTP requires CRLF terminators') + if b'\x00' in line or b'\r' in line[:-2]: + raise ValueError('Invalid character in HTTP header.') if line[:1] in {SPACE, TAB}: # NOTE: `type(line[0]) is int` and `type(line[:1]) is bytes`. diff --git a/cheroot/test/test_header_values.py b/cheroot/test/test_header_values.py new file mode 100644 index 0000000000..1df0c23ad3 --- /dev/null +++ b/cheroot/test/test_header_values.py @@ -0,0 +1,72 @@ +"""Check forbidden characters in HTTP field values.""" + +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'Test: a\x00b', HTTPStatus.BAD_REQUEST), + (b'Test: \x00', HTTPStatus.BAD_REQUEST), + (b'Test: a\rb', HTTPStatus.BAD_REQUEST), + (b'Test: \ra', HTTPStatus.BAD_REQUEST), + (b'Test: a\r', HTTPStatus.BAD_REQUEST), + (b'Test: \r a\r ', HTTPStatus.BAD_REQUEST), + (b'Test: a\r\n b\x00c', HTTPStatus.BAD_REQUEST), + (b'Test: a\r\n b\rc', HTTPStatus.BAD_REQUEST), + (b'Test: a\nb', HTTPStatus.BAD_REQUEST), + (b'Test: a\tb\x80', HTTPStatus.OK), + (b'Test: a', HTTPStatus.OK), + (b'Test: a\r\n b', HTTPStatus.OK), + ), +) +def test_header_value_control_characters( + native_server: HTTPServer, + http_request_timeout: float, + header_lines: bytes, + expected_status: HTTPStatus, +) -> None: + """Reject CR, LF and NUL in field values before trimming whitespace. + + :param native_server: live HTTP server + :param http_request_timeout: socket timeout in seconds + :param header_lines: field lines to send on the wire + :param expected_status: response required by the field-value 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_lines, + b'\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/720.bugfix.rst b/docs/changelog-fragments.d/720.bugfix.rst new file mode 100644 index 0000000000..c899efaca7 --- /dev/null +++ b/docs/changelog-fragments.d/720.bugfix.rst @@ -0,0 +1,2 @@ +Rejected NUL and embedded CR characters in HTTP header values with +``400 Bad Request``, including in continuation lines (:issue:`720`).