0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Add max_frame_size option for websocket handlers

Option allows to limit a frame by size before decoding its payload.

LH: I have added a test for when the limit is reached on a nofin
fragmented frame (the last commit addressed that case but it had
no test). I have fixed formatting and other, and changed the
default value to infinity since it might otherwise be incompatible
with existing code. I also added documentation and a bunch of other
minor changes.
This commit is contained in:
Kirill Kinduk 2017-08-25 12:08:26 +03:00 committed by Loïc Hoguin
parent 288deb5b88
commit 4c34774b7e
No known key found for this signature in database
GPG key ID: 8A9DF795F6FED764
5 changed files with 105 additions and 8 deletions

View file

@ -82,7 +82,8 @@ init_dispatch() ->
{"/ws_subprotocol", ws_subprotocol, []},
{"/terminate", ws_terminate_h, []},
{"/ws_timeout_hibernate", ws_timeout_hibernate, []},
{"/ws_timeout_cancel", ws_timeout_cancel, []}
{"/ws_timeout_cancel", ws_timeout_cancel, []},
{"/ws_max_frame_size", ws_max_frame_size, []}
]}
]).
@ -302,6 +303,44 @@ ws_init_shutdown_before_handshake(Config) ->
{ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []),
ok.
ws_max_frame_size_close(Config) ->
doc("Server closes connection when frame size exceeds max_frame_size option"),
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
Mask = 16#11223344,
MaskedHello = do_mask(<<"HelloHello">>, Mask, <<>>),
ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 10:7, Mask:32, MaskedHello/binary >>),
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
ws_max_frame_size_final_fragment_close(Config) ->
doc("Server closes connection when final fragmented frame "
"exceeds max_frame_size option"),
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
Mask = 16#11223344,
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
ws_max_frame_size_intermediate_fragment_close(Config) ->
doc("Server closes connection when intermediate fragmented frame "
"exceeds max_frame_size option"),
%% max_frame_size is set to 8 bytes in ws_max_frame_size.
{ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
Mask = 16#11223344,
MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
ok = gen_tcp:send(Socket, << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
{ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
ws_send_close(Config) ->
doc("Server-initiated close frame ends the connection."),
{ok, Socket, _} = do_handshake("/ws_send_close", Config),
@ -402,8 +441,8 @@ ws_text_fragments(Config) ->
%% Send three "Hello" over three fragments and one send.
ok = gen_tcp:send(Socket, [
<< 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
<< 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
<< 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
<< 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
<< 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
{ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
ok.