Skip to content

HTTP/2/3 trailers forwarded to an HTTP/1 peer crash the stream (AssertionError) #8573

Open

Description

@Black1hp

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) emit SendHttp(...Trailers...) whenever flow.request.trailers/flow.response.trailers is set, with no check of the peer protocol.
  • mitmproxy/proxy/layers/http/_http1.py:349-395 (Http1Client.send) and :235-279 (Http1Server.send) end in raise AssertionError(f"Unexpected event: {event}").
  • h1 inbound never produces trailers (_http1.py:118 raises NotImplementedError on a trailer section), so the trigger is specifically h2/h3 on one side and h1 on the other.

Consequences observed at runtime:

  1. The assembled h1 request head and body have already been sent upstream when the crash hits.
  2. RequestEndOfMessage is never dispatched to the h1 layer, so mark_done(request=True) never runs — the upstream connection is left in read_body state, not reusable, and pinned in the connection pool until idle timeout. If the request had no Content-Length (h2 requests routinely omit it), the upstream additionally waits forever for a body terminator.
  3. 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

  1. added a commit that references this issue on Nov 19, 2026
    15a8a51
  2. added a commit that references this issue on Nov 19, 2026
    7fb0a27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions