Skip to content

Unbounded buffering: HTTP/1 head (client, upstream, CONNECT-proxy) and WebSocket messages #8574

Open

Description

@Black1hp

Problem

HTTP/1 request and response heads are buffered without any size limit: Http1Connection.read_headers keeps appending to self.buf (h11.ReceiveBuffer) until a blank line terminates the head. A peer that never sends \r\n\r\n can grow the buffer arbitrarily per connection. This affects:

  • mitmproxy/proxy/layers/http/_http1.py:319 (client request head — already marked # FIXME: protect against header size DoS),
  • mitmproxy/proxy/layers/http/_http1.py:435 (upstream response head — same FIXME),
  • mitmproxy/proxy/layers/http/_upstream_proxy.py:91-92 (CONNECT response head from an upstream proxy — same maybe_extract_lines loop, so a malicious/compromised upstream proxy can do the same to mitmproxy).

The same unbounded-buffering pattern exists for WebSocket messages: WebsocketLayer.relay_messages (mitmproxy/proxy/layers/websocket.py:172-194) accumulates all fragments of a message into frame_buf and joins them before the websocket_message hook runs — a client or upstream can send an arbitrarily large message (wsproto has no message-size limit; permessage-deflate payloads additionally inflate), all buffered.

Runtime evidence

Driving the real Http1Server layer directly:

srv = Http1Server(ctx.fork())
list(srv._handle_event(Start()))
chunk = b'GET / HTTP/1.1\r\n' + b'X-Pad: ' + b'a'*1_000_000 + b'\r\n'
for _ in range(50):
    list(srv._handle_event(DataReceived(client, chunk)))

Result: srv.buf holds 50 MB, state is still read_headers, no error, no cap. Growth is monotonic in bytes received; multiply by N connections for linear memory amplification.

Impact

Any client that can reach a listener (any proxy mode) can make mitmproxy buffer unlimited bytes per connection without authentication; a malicious upstream server or upstream HTTP proxy can do the same on the response side. Since the head never terminates, body_size_limit/stream_large_bodies cannot help — they only apply to bodies. Similarly, no option bounds a WebSocket message.

Suggestion

  • Cap the head buffer (e.g. 64 KiB, ideally an option) in the else: branch of read_headers — exceed → 431/502 + CloseConnection.
  • Apply the same cap to the upstream-proxy handshake reader.
  • Bound frame_buf in the WebSocket layer (option, kill/forward-behavior on exceed).

Tested on

main @ 992052ee9 (13.0.0.dev).

Activity

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