Skip to content

Commit 3ba3301

Browse files
committed
dont crash if to field is missing
1 parent 7bcdebc commit 3ba3301

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/webrtc_ws_handler.erl

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,19 @@ websocket_handle({text, Text}, State = #{authenticated := true,
5151
peer_id := ThisPeer}) ->
5252
lager:debug("Received text frame ~p", [Text]),
5353

54-
%% TODO reply error if `to` is missing
55-
Message = #{to := OtherPeer} = json_decode(Text),
56-
%% crash if room doesn't match
57-
{Pid, {_Username, _PeerId, Room}} = syn:find_by_key(OtherPeer, with_meta),
58-
59-
%% extend message with this peer id before sending
60-
Message2 = Message#{from => ThisPeer},
61-
Pid ! {text, json_encode(Message2)},
62-
63-
{ok, State};
54+
case json_decode(Text) of
55+
#{to := OtherPeer} = Message ->
56+
%% crash if room doesn't match
57+
{Pid, {_Username, _PeerId, Room}} = syn:find_by_key(OtherPeer, with_meta),
58+
59+
%% extend message with this peer id before sending
60+
Message2 = Message#{from => ThisPeer},
61+
Pid ! {text, json_encode(Message2)},
62+
63+
{ok, State};
64+
_ ->
65+
{reply, reply_text(invalid_message), State}
66+
end;
6467

6568
websocket_handle(Frame, State) ->
6669
lager:warning("Received non text frame ~p~p", [Frame, State]),

test/webrtc_ws_handler_SUITE.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ join_and_send_message(Config) ->
9595
{ok, #{<<"event">> := <<"left">>}} = ws_client:recv(Conn1),
9696
{ok, #{<<"event">> := <<"left">>}} = ws_client:recv(Conn3),
9797

98+
%% fails without `to` field
99+
{ok, #{<<"event">> := <<"invalid_message">>}} = ws_client:send(Conn1, #{<<"event">> => <<"bye!">>}),
100+
98101
ok.
99102

100103
auth_failure(Config) ->

0 commit comments

Comments
 (0)