Repository navigation
Description
Feature request: optional limit on message size (including after permessage-deflate inflation)
Greetings!
Some context
I maintain aisstream, a small client for the AISStream WebSocket API built on top of mint_web_socket. Someone recently asked for an option to cap the size of incoming messages, so a misbehaving server can't make the client buffer unbounded data (aisstream_elixir#8). While looking into it I ran into two things I can't really solve from the outside.
The problem
Right now Mint.WebSocket doesn't have a size limit at any stage:
- Buffering: if a frame header declares a huge payload (say 4 GB) and the data keeps trickling in,
decode/2keeps buffering while waiting for the frame to complete. - Decompression: with
Mint.WebSocket.PerMessageDeflate, the whole message gets inflated in one go after the last fragment (:zlib.inflate/2on the full payload), with no upper bound. So a small compressed message can blow up into a very large binary.
I can work around the first one by counting the bytes I pass to decode/2. Mint.WebSocket.t() is opaque and I'd rather not poke at its internals. The second one I can't do anything about: by the time I see the result, inflation has already allocated it.
This matters in practice because some services expect compression. AISStream, for example, applies bandwidth limits to uncompressed connections, so turning permessage-deflate off isn't really an option.
What I'm thinking
An optional limit, something like:
Mint.WebSocket.new(conn, ref, status, headers, max_message_size: 1_048_576)Roughly:
- It applies to the data buffered for an incomplete message (fragments included) and to the inflated size when permessage-deflate is on.
- Inflation stops as soon as the limit is hit, instead of inflating everything first. Maybe
:zlib.safeInflate/2in a loop? - On overflow,
decode/2returns an error (e.g.{:error, websocket, %Mint.WebSocket.WebSocketError{reason: :message_too_large}}), so the caller can close with 1009 and reconnect. - The default stays
:infinity(ornil), so nothing changes for existing users.
Totally open on naming and shape, of course.
Prior art
mintitself recently fixed the same pattern for HTTP/1 chunked bodies, where partial chunk data was buffered without a bound until the declared chunk length arrived (CVE-2026-56810, fixed in 1.9.1). WebSocket frame buffering and inflation seem to have the same shape.- Python's
websocketshasmax_size(1 MiB by default), enforced on the decompressed size too. websocket-driver(Ruby/Node) had the same gap, with the limit checked against compressed frame sizes, and fixed it by checking after extensions run (CVE-2026-54464).
Happy to help
If the approach sounds reasonable to you, I'd be glad to take a stab at a PR!
Thanks for Mint, by the way, it's been a pleasure to build on! 🌱
Activity
the-mikedavis commentedon Oct 27, 2026
This idea makes sense to me 👍
I'm not sure if we would want to set a default (like 1 MiB as you mention the Python library does) since it might be a breaking change for some users passing a lot of data across a websocket connection. Feel free to open up a PR and we can discuss it further!
ericmj commentedon Oct 27, 2026
I would suggest setting a default even if it's a breaking change since no default is inherently unsafe. As an example Plug defaults to 8MB for Plug.Parsers.
https://plug.hexdocs.pm/Plug.Parsers.html
GiuseppeSaluto commentedon Oct 28, 2026
Hello @the-mikedavis , @ericmj
Thanks both!
I'll open a PR with a
:max_message_sizeoption applied to buffered data and to the inflated size.On the default: how about a generous one like Plug's 8 MB, with
:infinityto opt out? That keeps the safe-by-default behaviour while only affecting users with really large messages, who can opt out in one line.I'll make the default a single constant so it's easy to change whichever way you decide.