Skip to content

Commit

Permalink
fix: Prevent empty topics (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipecabaco authored Dec 10, 2024
1 parent 378b030 commit afb2a1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ This is the list of operational codes that can help you understand your deployme

| Code | Description |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| TopicNameRequired | You are trying to use Realtime without a topic name set |
| RealtimeDisabledForConfiguration | The configuration provided to Realtime on connect will not be able to provide you any Postgres Changes |
| TenantNotFound | The tenant you are trying to connect to does not exist |
| ErrorConnectingToWebsocket | Error when trying to connect to the WebSocket server |
Expand Down
4 changes: 4 additions & 0 deletions lib/realtime_web/channels/realtime_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ defmodule RealtimeWeb.RealtimeChannel do
@confirm_token_ms_interval :timer.minutes(5)

@impl true
def join("realtime:", _params, _socket) do
Logging.log_error_message(:error, "TopicNameRequired", "You must provide a topic name")
end

def join("realtime:" <> sub_topic = topic, params, socket) do
%{
assigns: %{tenant: tenant_id, log_level: log_level, postgres_cdc_module: module},
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
def project do
[
app: :realtime,
version: "2.33.66",
version: "2.33.67",
elixir: "~> 1.17.3",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
16 changes: 16 additions & 0 deletions test/integration/rt_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,22 @@ defmodule Realtime.Integration.RtChannelTest do
end
end

test "handle empty topic by closing the socket" do
{socket, _} = get_connection("authenticated")
config = %{broadcast: %{self: true}, private: false}
realtime_topic = "realtime:"

WebsocketClient.join(socket, realtime_topic, %{config: config})

assert_receive %Phoenix.Socket.Message{
event: "phx_reply",
payload: %{
"response" => %{"reason" => "You must provide a topic name"},
"status" => "error"
}
}
end

defp token_valid(role), do: generate_token(%{role: role})
defp token_no_role(), do: generate_token()

Expand Down

0 comments on commit afb2a1b

Please sign in to comment.