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
7 changes: 5 additions & 2 deletions cheroot/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
71 changes: 71 additions & 0 deletions cheroot/test/test_request_line_whitespace.py
Original file line number Diff line number Diff line change
@@ -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''
2 changes: 2 additions & 0 deletions docs/changelog-fragments.d/717.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rejected whitespace between the HTTP version and the request-line CRLF
terminator with ``400 Bad Request`` (:issue:`717`).
Loading