diff --git a/h11/_connection.py b/h11/_connection.py index e37d82a..5e9b749 100644 --- a/h11/_connection.py +++ b/h11/_connection.py @@ -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 @@ -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 diff --git a/h11/tests/test_connection.py b/h11/tests/test_connection.py index 01260dc..4f2cb35 100644 --- a/h11/tests/test_connection.py +++ b/h11/tests/test_connection.py @@ -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 diff --git a/newsfragments/219.bugfix.rst b/newsfragments/219.bugfix.rst new file mode 100644 index 0000000..85577e1 --- /dev/null +++ b/newsfragments/219.bugfix.rst @@ -0,0 +1,2 @@ +Dropped a ``Content-Length`` header from a request that also had a +``Transfer-Encoding`` header, instead of sending both