Repository navigation
Description
Problem
HttpStream forwards request/response trailers to the peer's protocol layer unconditionally, but Http1Client.send/Http1Server.send have no RequestTrailers/ResponseTrailers branch and fall through to raise AssertionError. An h2 or h3 peer that sends legal trailers causes an unhandled AssertionError whenever the other side is HTTP/1, which escapes layer.handle_event and lands in the server.py catch-all ("mitmproxy has crashed!").
Details
mitmproxy/proxy/layers/http/__init__.py:351-356, 405-410(request trailers) and:528-530(response trailers) emitSendHttp(...Trailers...)wheneverflow.request.trailers/flow.response.trailersis set, with no check of the peer protocol.mitmproxy/proxy/layers/http/_http1.py:349-395(Http1Client.send) and:235-279(Http1Server.send) end inraise AssertionError(f"Unexpected event: {event}").- h1 inbound never produces trailers (
_http1.py:118raisesNotImplementedErroron a trailer section), so the trigger is specifically h2/h3 on one side and h1 on the other.
Consequences observed at runtime:
- The assembled h1 request head and body have already been sent upstream when the crash hits.
RequestEndOfMessageis never dispatched to the h1 layer, somark_done(request=True)never runs — the upstream connection is left inread_bodystate, not reusable, and pinned in the connection pool until idle timeout. If the request had noContent-Length(h2 requests routinely omit it), the upstream additionally waits forever for a body terminator.- Repeated requests let a client pin upstream connections (bounded only by connection throttling) and flood the log with
mitmproxy has crashed!tracebacks.
Symmetric: ResponseTrailers from an h2/h3 upstream crashes Http1Server.send toward h1 clients.
Repro
Using the existing playbook harness (against HttpLayer, real h2 frames, h1 upstream):
tctx.client.alpn = b"h2"
pb = Playbook(http.HttpLayer(tctx, HTTPMode.regular))
pb << SendData(tctx.client, Placeholder()) # server settings
pb >> DataReceived(tctx.client, cff.preamble())
pb >> DataReceived(tctx.client, cff.build_settings_frame({}, ack=True).serialize())
pb >> DataReceived(tctx.client, cff.build_headers_frame([
(b":method", b"POST"), (b":scheme", b"http"), (b":path", b"/"),
(b":authority", b"example.com"), (b"content-length", b"5"),
]).serialize())
pb << http.HttpRequestHeadersHook(flow); pb >> reply()
pb >> DataReceived(tctx.client, cff.build_data_frame(b"hello").serialize())
pb >> DataReceived(tctx.client,
cff.build_headers_frame([(b"x-trailer", b"t")], flags=["END_STREAM"]).serialize())
pb << http.HttpRequestHook(flow); pb >> reply()
pb << OpenConnection(server); pb >> reply(None) # upstream: h1 (alpn stays None)
pb << SendData(server, head)Result:
<< SendData(server, b'POST / HTTP/1.1\r\nHost: example.com\r\ncontent-length: 5\r\n\r\n')
<< SendData(server, b'hello')
...
File "mitmproxy/proxy/layers/http/_http1.py", line 395, in send
raise AssertionError(f"Unexpected event: {event}")
AssertionError: Unexpected event: RequestTrailers(stream_id=1, trailers=Headers[(b'x-trailer', b't')])
Existing trailer tests (test_request_trailers, test_response_trailers in test_http2.py) all use open_h2_server_conn — the h2↔h1 direction is uncovered.
Expected behavior
One of:
- Drop trailers when the peer speaks HTTP/1 (simplest; trailers are optional metadata), or
- Emit them as a chunked trailer section when the body was sent chunked (and the peer is h1.1).
Also consider replacing the blanket raise AssertionError in send() with a logged protocol error so that unhandled event types degrade the stream gracefully instead of crashing mid-dispatch.
Tested on
main @ 992052ee9 (13.0.0.dev).
Activity
fix(http1): drop trailers when the peer speaks HTTP/1 instead of cras…
Fix crash when forwarding HTTP/2 or HTTP/3 trailers to an HTTP/1 peer