Skip to content

Add :max_message_size option to limit incoming message size - #44

Merged
whatyouhide merged 3 commits into
elixir-mint:mainfrom
GiuseppeSaluto:max-message-size
Nov 24, 2026
Merged

whatyouhide merged 3 commits into
elixir-mint:mainfrom
GiuseppeSaluto:max-message-size

Conversation

@GiuseppeSaluto

@GiuseppeSaluto GiuseppeSaluto commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #43

This adds a :max_message_size option to Mint.WebSocket.new/5. When an incoming message goes over the limit, decode/2 returns {:error, websocket, %Mint.WebSocketError{reason: :message_too_large}}.

{:ok, conn, websocket} =
  Mint.WebSocket.new(conn, ref, status, headers, max_message_size: 1_048_576)

Where the limit is enforced

  1. Frame header: a frame whose declared payload length is over the limit is rejected as soon as the length is read, before any payload gets buffered.
  2. Fragmented messages: the accumulated size of the fragments is checked in combine/3, before concatenating.
  3. permessage-deflate: :zlib.inflate/2 is replaced by a :zlib.safeInflate/2 loop that checks the inflated size after each chunk and stops as soon as it goes over the limit. A small compressed message can no longer inflate into an unbounded binary.

All three use the same check (Frame.check_message_size/2) and return the same error. The limit reaches PerMessageDeflate through the extension's opts, so the Extension behaviour is unchanged.

Default (breaking change)

As discussed in #43, the default is 8_000_000 bytes (same as Plug.Parsers), with :infinity to opt out. Users receiving messages larger than 8 MB need to set the option, e.g. max_message_size: :infinity. This is visible in the Autobahn suite: cases 9.1.5/9.1.6/9.2.5/9.2.6 send 8 and 16 MiB messages, so the test client now passes :infinity.

If you'd rather keep :infinity as the default to avoid the breaking change, it's a one-line change in @default_max_message_size.

Known limitations

  • Inflation overshoot: the inflated size is checked after each safeInflate chunk, so up to one chunk (16 KB on OTP 27) may be inflated past the limit before stopping.
  • Compressed frames near the limit: the header check uses the frame's (compressed) payload length. For incompressible data, the compressed frame can be a few bytes larger than the original message, so a message within a few bytes of the limit may be rejected.
  • No validation of the option value, consistent with :mode. Happy to add a check if you'd like.

Tests

New tests in test/mint/web_socket_test.exs, with a 100-byte limit:

  • a frame declaring a 4 GB payload is rejected from its header;
  • a fragmented message over the limit (60 + 60 bytes) is rejected, and one exactly at the limit (60 + 40) is delivered;
  • a compressed message inflating over the limit (1,000 bytes, compressed under 100) is rejected, and one inflating exactly to the limit is delivered (against the local Cowboy server with compression).

mix test (including Autobahn), mix dialyzer, mix format --check-formatted and mix compile --warnings-as-errors all pass locally.

Reject messages over the limit while decoding: from a frame's declared
length, from the accumulated size of a fragmented message, and while
inflating with permessage-deflate (chunked :zlib.safeInflate/2).

The default is 8_000_000 bytes; pass :infinity to disable the limit.

Closes elixir-mint#43
@GiuseppeSaluto GiuseppeSaluto changed the title Add :max_message_size option to limit incoming message size Max_message_size option to limit incoming message size Oct 28, 2026
@GiuseppeSaluto GiuseppeSaluto changed the title Max_message_size option to limit incoming message size Add :max_message_size option to limit incoming message size Oct 28, 2026
@whatyouhide

Copy link
Copy Markdown
Collaborator

Control frames: the header check also applies to ping/pong/close frames (up to 125 bytes), so a limit below 125 bytes could reject them.

Should we consider setting up a minimum limit that prevents things like these happening?

@GiuseppeSaluto

Copy link
Copy Markdown
Contributor Author

Hello @whatyouhide!
We could opt for a minimum value: new/5 would raise an ArgumentError. Unless :max_message_size is either :infinity or an integer ≥ 125 (which is the maximum payload for control frames according to RFC 6455). This ensures that ping, pong, or close frames can never reach that limit. I can also update the typespec and the documentation, and add a test case.

@GiuseppeSaluto

Copy link
Copy Markdown
Contributor Author

Hello @whatyouhide
Pushed: new/5 now raises ArgumentError unless :max_message_size is :infinity or an integer >= 125. Updated the typespec and docs, and added a test. Existing tests now use a 200-byte limit.

@whatyouhide whatyouhide left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After :message_too_large, the inflater can still have data from the rejected message. However, the next valid message can fail too. Can we document that callers must close the WebSocket after this error and update the GenServer example to do that?

Also, should we check the final text or binary payload size after all extensions run? A custom extension that expands 150 bytes to 300 currently delivers the result with a 200-byte limit.

Comment thread lib/mint/web_socket.ex Outdated
# extensions which buffer or inflate data (e.g. permessage-deflate) read the limit from their opts
extensions =
for extension <- get_private(conn, :extensions) do
%{extension | opts: Keyword.put(extension.opts, :max_message_size, max_message_size)}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set this option only on PerMessageDeflate? Keyword.put/3 currently replaces any :max_message_size the caller already set on a custom extension.

@whatyouhide

Copy link
Copy Markdown
Collaborator

Also, some small messages can be larger when compressed. Either we document that the limit is after decompression and then check only that, or that it's the min between (size(compressed), size(uncompressed)) (which is what we have today basically).

@GiuseppeSaluto

Copy link
Copy Markdown
Contributor Author

Hi @whatyouhide, thanks for the review, and sorry for the delay!

  • The limit is now set only on PerMessageDeflate, so custom extensions keep their own options.
  • The docs say the connection should be closed after :message_too_large, and the GenServer example now closes it on decode errors.
  • decode/2 checks the final text/binary payload size after all extensions run, with a test using an extension that doubles the payload.
  • On compressed vs uncompressed: I documented that the limit applies to both the size on the wire and the size after extensions. Checking only after decompression would mean skipping the header check for compressed frames, so a frame declaring a huge compressed payload would be buffered in full before inflating. The edge case only affects incompressible messages right at the limit.

@whatyouhide
whatyouhide merged commit 6ce9fab into elixir-mint:main Nov 24, 2026
@whatyouhide

Copy link
Copy Markdown
Collaborator

Thank you so much @GiuseppeSaluto! 🫶

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional limit on message size

2 participants