diff --git a/guides/websocket_guide.md b/guides/websocket_guide.md index 4bbd1386..2e3c591a 100644 --- a/guides/websocket_guide.md +++ b/guides/websocket_guide.md @@ -40,6 +40,8 @@ hackney:ws_close(Conn). | `protocols` | `[]` | Sec-WebSocket-Protocol values | | `active` | `false` | Active mode: false, true, once | | `ssl_options` | `[]` | SSL options for wss:// | +| `max_frame_size` | 16 MiB | Maximum frame payload (bytes) | +| `max_message_size` | 64 MiB | Maximum message size across fragments (bytes) | ## Sending Messages diff --git a/src/hackney.erl b/src/hackney.erl index 50293976..6491fe66 100644 --- a/src/hackney.erl +++ b/src/hackney.erl @@ -906,6 +906,9 @@ resume_stream(ConnPid) when is_pid(ConnPid) -> %%
  • recv_timeout: Receive timeout in ms (default infinity)
  • %%
  • connect_options: Options passed to transport connect
  • %%
  • ssl_options: Additional SSL options
  • +%%
  • max_frame_size: Maximum frame payload in bytes (default 16 MiB)
  • +%%
  • max_message_size: Maximum message size in bytes across fragments +%% (default 64 MiB)
  • %% %% %% Returns `{ok, WsPid}' on success, where WsPid is the hackney_ws process. @@ -941,7 +944,7 @@ ws_connect(URL, Options) when is_binary(URL) orelse is_list(URL) -> ProxyConfig = get_ws_proxy_config(Scheme, Host, Options), %% Build connection options - WsOpts = #{ + WsOpts0 = #{ host => Host, port => Port, transport => Transport, @@ -956,6 +959,18 @@ ws_connect(URL, Options) when is_binary(URL) orelse is_list(URL) -> proxy => ProxyConfig }, + %% Frame and message limits keep the hackney_ws defaults unless given + WsOpts = lists:foldl( + fun(Key, Acc) -> + case proplists:lookup(Key, Options) of + {Key, Value} -> Acc#{Key => Value}; + none -> Acc + end + end, + WsOpts0, + [max_frame_size, max_message_size] + ), + %% Start WebSocket process and connect case hackney_ws:start_link(WsOpts) of {ok, WsPid} -> diff --git a/test/hackney_ws_tests.erl b/test/hackney_ws_tests.erl index 83e5e954..0fb2f9be 100644 --- a/test/hackney_ws_tests.erl +++ b/test/hackney_ws_tests.erl @@ -100,6 +100,8 @@ ws_integration_test_() -> fun() -> test_handshake_header_injection(Port) end}, {"GHSA-q8jg: oversized frame rejected", fun() -> test_max_frame_size(Port) end}, + {"ws_connect passes max_frame_size", + fun() -> test_ws_connect_max_frame_size(WsUrl) end}, {"Send and receive text message", fun() -> test_text_message(WsUrl) end}, {"Send and receive binary message", @@ -146,6 +148,11 @@ test_max_frame_size(Port) -> ok = hackney_ws:send(Ws, {text, <<"large:5000">>}), ?assertMatch({error, {frame_too_big, _}}, hackney_ws:recv(Ws, 5000)). +test_ws_connect_max_frame_size(WsUrl) -> + {ok, Ws} = hackney:ws_connect(WsUrl, [{max_frame_size, 100}]), + ok = hackney:ws_send(Ws, {text, <<"large:5000">>}), + ?assertMatch({error, {frame_too_big, _}}, hackney:ws_recv(Ws, 5000)). + test_connect_disconnect(WsUrl) -> {ok, Ws} = hackney:ws_connect(WsUrl), ?assert(is_pid(Ws)),