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
34 changes: 34 additions & 0 deletions h11/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ def send_with_data_passthrough(self, event: Event) -> Optional[List[bytes]]:
try:
if type(event) is Response:
event = self._clean_up_response_headers_for_sending(event)
elif type(event) is Request:
event = self._clean_up_request_headers_for_sending(event)
# We want to call _process_event before calling the writer,
# because if someone tries to do something invalid then this will
# give a sensible error message, while our writers all just assume
Expand Down Expand Up @@ -584,6 +586,38 @@ def send_failed(self) -> None:
"""
self._process_error(self.our_role)

# When sending a Request, we take responsibility for one thing:
#
# - If the user set both Transfer-Encoding and Content-Length, we drop the
# Content-Length. "A sender MUST NOT send a Content-Length header field
# in any message that contains a Transfer-Encoding header field."
# -- https://tools.ietf.org/html/rfc7230#section-3.3.2
#
# _body_framing always lets Transfer-Encoding win, so passing a
# redundant Content-Length through would put a framing header on the
# wire that disagrees with the framing we actually use -- which is the
# shape request smuggling attacks rely on. Responses already get this
# treatment below; this is the same rule for requests.
#
# Note this is purely a *sending* rule. On the receiving side h11 still
# accepts both and lets Transfer-Encoding win, as the RFC's framing
# algorithm requires (see gh-90).
def _clean_up_request_headers_for_sending(self, request: Request) -> Request:
assert type(request) is Request

headers = request.headers
if not get_comma_header(headers, b"transfer-encoding"):
return request
if not get_comma_header(headers, b"content-length"):
return request

return Request(
method=request.method,
target=request.target,
headers=set_comma_header(headers, b"content-length", []),
http_version=request.http_version,
)

# When sending a Response, we take responsibility for a few things:
#
# - Sometimes you MUST set Connection: close. We take care of those
Expand Down
50 changes: 50 additions & 0 deletions h11/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,56 @@ def test_automatic_transfer_encoding_in_response() -> None:
assert c.send(Data(data=b"12345")) == b"12345"


# A request that carries both Transfer-Encoding and Content-Length is
# illegal to send: "A sender MUST NOT send a Content-Length header field in
# any message that contains a Transfer-Encoding header field."
# https://tools.ietf.org/html/rfc7230#section-3.3.2
#
# _body_framing always lets Transfer-Encoding win, so leaving the redundant
# Content-Length on the wire would make the framing header disagree with the
# framing we actually use. Responses already drop it (see
# test_automatic_transfer_encoding_in_response); requests did not.
def test_request_framing_headers() -> None:
def send(headers: List[Tuple[str, str]], body: bytes = b"") -> bytes:
c = Connection(CLIENT)
out = c.send(Request(method="POST", target="/", headers=headers))
if body:
out += c.send(Data(data=body))
out += c.send(EndOfMessage())
return out

# Neither framing header: no body, nothing added to the wire.
assert send([("Host", "example.com")]) == (
b"POST / HTTP/1.1\r\nHost: example.com\r\n\r\n"
)
# Content-Length only: passed through untouched.
assert send([("Host", "example.com"), ("Content-Length", "5")], b"12345") == (
b"POST / HTTP/1.1\r\nHost: example.com\r\nContent-Length: 5\r\n\r\n" b"12345"
)
# Transfer-Encoding only: passed through untouched.
assert send(
[("Host", "example.com"), ("Transfer-Encoding", "chunked")], b"12345"
) == (
b"POST / HTTP/1.1\r\nHost: example.com\r\n"
b"Transfer-Encoding: chunked\r\n\r\n"
b"5\r\n12345\r\n0\r\n\r\n"
)
# Both: Transfer-Encoding wins for framing, so the Content-Length is
# dropped instead of being sent as a contradiction.
assert send(
[
("Host", "example.com"),
("Content-Length", "5"),
("Transfer-Encoding", "chunked"),
],
b"12345",
) == (
b"POST / HTTP/1.1\r\nHost: example.com\r\n"
b"Transfer-Encoding: chunked\r\n\r\n"
b"5\r\n12345\r\n0\r\n\r\n"
)


def test_automagic_connection_close_handling() -> None:
p = ConnectionPair()
# If the user explicitly sets Connection: close, then we notice and
Expand Down
2 changes: 2 additions & 0 deletions newsfragments/219.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dropped a ``Content-Length`` header from a request that also had a
``Transfer-Encoding`` header, instead of sending both