Skip to content

Configurable maximum message size for the method stream WebSocket #5990

Open

Description

@t-kietzmann

Problem to Solve

HTTP method calls are bounded by maxRequestSize (default 524,288 bytes = 512 KiB, configurable in the config and through an environment variable; serverpod_shared/lib/src/config.dart:50, :139, :1680-1690; enforced in server.dart:417-421, mapped to 413 at :239-244). The method stream WebSocket has no equivalent: MethodWebsocketRequestHandler reads every event (await for (final event in webSocket.events)), decodes binary data with utf8.decode(event.data) and parses it with WebSocketMessage.fromJsonString(jsonData, ...) before anything can check its size (serverpod/lib/src/server/websocket_request_handlers/method_websocket_request_handler.dart:40-52). maxRequestSize applies only to _endpoints (server.dart:417-421), and the config has no WebSocket limit. This happens for any connected socket, including before the stream's authentication, since the open command is itself such a message.

dart:io can reject oversized frames (WebSocketTransformer.upgrade(..., maxPayloadLength:)), but relic does not pass the option (relic_io/lib/src/adapter/io_relic_web_socket.dart:48), the SDK default is unlimited (_http/websocket_impl.dart:18-21), and the limit applies per frame (:462-466): an uncompressed message made of many frames is collected without a total limit (:393-400, :514-516).

The client reads every message without a limit as well (serverpod_client/lib/src/client_method_stream_manager.dart:519-530); that is less critical because the server is trusted.

Today there is no limit; a client can make the server buffer an arbitrarily large message.

Proposal

A config value, e.g. maxWebSocketMessageSize (default could follow maxRequestSize), that bounds every incoming WebSocket message before it is materialized; an oversized message closes the connection with close code 1009 (Message Too Big).

  1. relic: expose maxPayloadLength on WebSocketUpgrade (filed as serverpod/relic#(link)).
  2. Serverpod: a config value passed to the upgrade, plus a length check in the handler as a second line (covers fragmented, uncompressed messages, which dart:io collects without a total limit, websocket_impl.dart:393-400, :514-516).

Use Case

Our sync stream checks its 64 KiB command limit only inside the endpoint method, after the message is fully materialized. Ten messages of 64 MiB each would exceed the whole memory budget of an instance. That applies today to every signed-in client with an open stream that takes input, and to opening a stream even before authentication (the message is read before the server knows who sent it).

Alternatives

  • A reverse proxy that limits WebSocket messages or frames.
  • Our own small transport adapter.

Both move a basic safety limit out of the framework; until one is in place, an unbounded path stays open.

Additional context

  • Checked against serverpod 4.1.0, serverpod_shared 4.1.0, relic_io 2.0.0-rc.2 to 2.0.0-rc.6 and Dart SDK 3.13.7; unchanged in 4.1.1.
  • Related: #3423 (open; per-module maxRequestSize override, HTTP only).

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this
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

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions