Skip to content

Commit fe7d359

Browse files
committed
validate options passed to socket longpoll / websocket options
1 parent 7252caa commit fe7d359

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

lib/phoenix/endpoint.ex

+40-2
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,43 @@ defmodule Phoenix.Endpoint do
690690

691691
defp socket_paths(endpoint, path, socket, opts) do
692692
paths = []
693-
websocket = Keyword.get(opts, :websocket, true)
694-
longpoll = Keyword.get(opts, :longpoll, false)
693+
694+
common_config = [
695+
:path,
696+
:serializer,
697+
:transport_log,
698+
:check_origin,
699+
:check_csrf,
700+
:code_reloader,
701+
:connect_info
702+
]
703+
704+
websocket =
705+
opts
706+
|> Keyword.get(:websocket, true)
707+
|> maybe_validate_keys(
708+
common_config ++
709+
[
710+
:timeout,
711+
:max_frame_size,
712+
:fullsweep_after,
713+
:compress,
714+
:subprotocols,
715+
:error_handler
716+
]
717+
)
718+
719+
longpoll =
720+
opts
721+
|> Keyword.get(:longpoll, true)
722+
|> maybe_validate_keys(
723+
common_config ++
724+
[
725+
:window_ms,
726+
:pubsub_timeout_ms,
727+
:crypto
728+
]
729+
)
695730

696731
paths =
697732
if websocket do
@@ -745,6 +780,9 @@ defmodule Phoenix.Endpoint do
745780
{conn_ast, path}
746781
end
747782

783+
defp maybe_validate_keys(true, _), do: true
784+
defp maybe_validate_keys(opts, keys) when is_list(opts), do: Keyword.validate!(opts, keys)
785+
748786
## API
749787

750788
@doc """

test/phoenix/endpoint/endpoint_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,22 @@ defmodule Phoenix.Endpoint.EndpointTest do
370370
Endpoint.static_integrity("//invalid_path")
371371
end
372372
end
373+
374+
test "validates websocket and longpoll socket options" do
375+
assert_raise ArgumentError, ~r/unknown keys \[:invalid\]/, fn ->
376+
defmodule MyInvalidSocketEndpoint1 do
377+
use Phoenix.Endpoint, otp_app: :phoenix
378+
379+
socket "/ws", UserSocket, websocket: [path: "/ws", check_origin: false, invalid: true]
380+
end
381+
end
382+
383+
assert_raise ArgumentError, ~r/unknown keys \[:drainer\]/, fn ->
384+
defmodule MyInvalidSocketEndpoint2 do
385+
use Phoenix.Endpoint, otp_app: :phoenix
386+
387+
socket "/ws", UserSocket, longpoll: [path: "/ws", check_origin: false, drainer: []]
388+
end
389+
end
390+
end
373391
end

test/phoenix/integration/long_poll_socket_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ defmodule Phoenix.Integration.LongPollSocketTest do
6767
custom: :value
6868

6969
socket "/custom/:socket_var", UserSocket,
70-
longpoll: [path: ":path_var/path", check_origin: ["//example.com"], timeout: 200],
70+
longpoll: [path: ":path_var/path", check_origin: ["//example.com"], pubsub_timeout_ms: 200],
7171
custom: :value
7272
end
7373

0 commit comments

Comments
 (0)