Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate options passed to socket longpoll / websocket options #6072

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,43 @@ defmodule Phoenix.Endpoint do

defp socket_paths(endpoint, path, socket, opts) do
paths = []
websocket = Keyword.get(opts, :websocket, true)
longpoll = Keyword.get(opts, :longpoll, false)

common_config = [
:path,
:serializer,
:transport_log,
:check_origin,
:check_csrf,
:code_reloader,
:connect_info
]

websocket =
opts
|> Keyword.get(:websocket, true)
|> maybe_validate_keys(
common_config ++
[
:timeout,
:max_frame_size,
:fullsweep_after,
:compress,
:subprotocols,
:error_handler
]
)

longpoll =
opts
|> Keyword.get(:longpoll, true)
|> maybe_validate_keys(
common_config ++
[
:window_ms,
:pubsub_timeout_ms,
:crypto
]
)

paths =
if websocket do
Expand Down Expand Up @@ -745,6 +780,9 @@ defmodule Phoenix.Endpoint do
{conn_ast, path}
end

defp maybe_validate_keys(true, _), do: true
defp maybe_validate_keys(opts, keys) when is_list(opts), do: Keyword.validate!(opts, keys)

## API

@doc """
Expand Down
18 changes: 18 additions & 0 deletions test/phoenix/endpoint/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,22 @@ defmodule Phoenix.Endpoint.EndpointTest do
Endpoint.static_integrity("//invalid_path")
end
end

test "validates websocket and longpoll socket options" do
assert_raise ArgumentError, ~r/unknown keys \[:invalid\]/, fn ->
defmodule MyInvalidSocketEndpoint1 do
use Phoenix.Endpoint, otp_app: :phoenix

socket "/ws", UserSocket, websocket: [path: "/ws", check_origin: false, invalid: true]
end
end

assert_raise ArgumentError, ~r/unknown keys \[:drainer\]/, fn ->
defmodule MyInvalidSocketEndpoint2 do
use Phoenix.Endpoint, otp_app: :phoenix

socket "/ws", UserSocket, longpoll: [path: "/ws", check_origin: false, drainer: []]
end
end
end
end
2 changes: 1 addition & 1 deletion test/phoenix/integration/long_poll_socket_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Phoenix.Integration.LongPollSocketTest do
custom: :value

socket "/custom/:socket_var", UserSocket,
longpoll: [path: ":path_var/path", check_origin: ["//example.com"], timeout: 200],
longpoll: [path: ":path_var/path", check_origin: ["//example.com"], pubsub_timeout_ms: 200],
custom: :value
end

Expand Down