Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
72 changes: 72 additions & 0 deletions cheroot/test/test_header_values.py
Original file line number Diff line number Diff line change
@@ -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''
2 changes: 2 additions & 0 deletions docs/changelog-fragments.d/720.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rejected NUL and embedded CR characters in HTTP header values with
``400 Bad Request``, including in continuation lines (:issue:`720`).
Loading