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 @@ -248,6 +248,8 @@ def __call__(self, rfile, hdict=None): # noqa: C901 # FIXME
raise ValueError('Illegal header line.')
v = v.strip()
k = self._transform_key(k)
if k == b'Content-Length' and k in hdict:
raise ValueError('Multiple Content-Length headers.')
hname = k

if not self._allow_header(k):
Expand Down
66 changes: 66 additions & 0 deletions cheroot/test/test_duplicate_content_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Reject ambiguous HTTP request body lengths."""

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'Content-Length: 0\r\nContent-Length: 1', HTTPStatus.BAD_REQUEST),
(b'Content-Length: 1\r\nContent-Length: 0', HTTPStatus.BAD_REQUEST),
(b'Content-Length: 1\r\nContent-Length: 1', HTTPStatus.BAD_REQUEST),
(b'Content-Length: 0\r\ncontent-length: 1', HTTPStatus.BAD_REQUEST),
(b'content-length: 0\r\nCONTENT-LENGTH: 1', HTTPStatus.BAD_REQUEST),
(b'Content-Length: 1\r\nConnection: close', HTTPStatus.OK),
),
)
def test_duplicate_content_length(
native_server: HTTPServer,
http_request_timeout: float,
header_lines: bytes,
expected_status: HTTPStatus,
) -> None:
"""Reject duplicate lengths and close before any pipelined request.

:param native_server: live HTTP server
:param http_request_timeout: socket timeout in seconds
:param header_lines: length fields to send on the wire
:param expected_status: response required by the framing rules
"""
native_server.gateway = _AcceptedGateway
host, port = native_server.bind_addr
request = b''.join(
(
b'POST / HTTP/1.1\r\nHost: localhost\r\n',
header_lines,
b'\r\n\r\naGET / HTTP/1.1\r\nHost: localhost\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''
3 changes: 3 additions & 0 deletions docs/changelog-fragments.d/737.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rejected requests with multiple ``Content-Length`` fields with
``400 Bad Request`` and closed the connection, preventing inconsistent
request-body framing between HTTP recipients (:issue:`737`).
Loading