From 3cda7156c89ae3fd1957f8e69c7b4c7b3118722d Mon Sep 17 00:00:00 2001 From: Chase Granberry Date: Fri, 18 Aug 2023 10:04:13 -0700 Subject: [PATCH 1/3] fix: handle nil access token --- lib/realtime_web/channels/realtime_channel.ex | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/realtime_web/channels/realtime_channel.ex b/lib/realtime_web/channels/realtime_channel.ex index a34468556..3517aa67f 100644 --- a/lib/realtime_web/channels/realtime_channel.ex +++ b/lib/realtime_web/channels/realtime_channel.ex @@ -351,6 +351,15 @@ defmodule RealtimeWeb.RealtimeChannel do {:noreply, socket} end + def handle_in( + "access_token", + %{"access_token" => refresh_token}, + %{assigns: %{access_token: _access_token}} = socket + ) + when is_nil(refresh_token) do + {:noreply, socket} + end + def handle_in( "access_token", %{"access_token" => refresh_token}, From 31f5dade0efabc291da188e4233b0913c3601ff7 Mon Sep 17 00:00:00 2001 From: Chase Granberry Date: Fri, 18 Aug 2023 10:15:30 -0700 Subject: [PATCH 2/3] fix: info log unexpected messages from client an clean up --- lib/realtime_web/channels/realtime_channel.ex | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/realtime_web/channels/realtime_channel.ex b/lib/realtime_web/channels/realtime_channel.ex index 3517aa67f..504e3b873 100644 --- a/lib/realtime_web/channels/realtime_channel.ex +++ b/lib/realtime_web/channels/realtime_channel.ex @@ -159,13 +159,13 @@ defmodule RealtimeWeb.RealtimeChannel do other end - Logger.debug("Postgres change params: " <> inspect(pg_change_params, pretty: true)) + Logger.debug("Postgres change params: " <> inspect(pg_change_params)) if !Enum.empty?(pg_change_params) do send(self(), :postgres_subscribe) end - Logger.debug("Start channel: " <> inspect(pg_change_params, pretty: true)) + Logger.debug("Start channel: " <> inspect(pg_change_params)) presence_key = presence_key(params) @@ -190,28 +190,28 @@ defmodule RealtimeWeb.RealtimeChannel do })} else {:error, :too_many_channels} = error -> - error_msg = inspect(error, pretty: true) - Logger.warn("Start channel error: #{error_msg}") + error_msg = inspect(error) + Logger.warn("Start channel error: " <> error_msg) {:error, %{reason: error_msg}} {:error, :too_many_connections} = error -> - error_msg = inspect(error, pretty: true) - Logger.warn("Start channel error: #{error_msg}") + error_msg = inspect(error) + Logger.warn("Start channel error: " <> error_msg) {:error, %{reason: error_msg}} {:error, :too_many_joins} = error -> - error_msg = inspect(error, pretty: true) - Logger.warn("Start channel error: #{error_msg}") + error_msg = inspect(error) + Logger.warn("Start channel error: " <> error_msg) {:error, %{reason: error_msg}} {:error, [message: "Invalid token", claim: _claim, claim_val: _value]} = error -> - error_msg = inspect(error, pretty: true) - Logger.warn("Start channel error: #{error_msg}") + error_msg = inspect(error) + Logger.warn("Start channel error: " <> error_msg) {:error, %{reason: error_msg}} error -> - error_msg = inspect(error, pretty: true) - Logger.error("Start channel error: #{error_msg}") + error_msg = inspect(error) + Logger.error("Start channel error: " <> error_msg) {:error, %{reason: error_msg}} end end @@ -286,7 +286,7 @@ defmodule RealtimeWeb.RealtimeChannel do {:noreply, assign(socket, :pg_sub_ref, nil)} error -> - message = "Subscribing to PostgreSQL failed: #{inspect(error)}" + message = "Subscribing to PostgreSQL failed: " <> inspect(error) push_system_message("postgres_changes", socket, "error", message, channel_name) @@ -296,7 +296,7 @@ defmodule RealtimeWeb.RealtimeChannel do end nil -> - Logger.warning("Re-subscribed to PostgreSQL with params: #{inspect(pg_change_params)}") + Logger.warning("Re-subscribed to PostgreSQL with params: " <> inspect(pg_change_params)) {:noreply, assign(socket, :pg_sub_ref, postgres_subscribe())} end end @@ -314,14 +314,14 @@ defmodule RealtimeWeb.RealtimeChannel do })} {:error, error} -> - message = "access token has expired: " <> inspect(error, pretty: true) + message = "access token has expired: " <> inspect(error) shutdown_response(socket, message) end end def handle_info(msg, socket) do - Logger.error("HANDLE_INFO message not handled: #{inspect(msg, pretty: true)}") + Logger.error("HANDLE_INFO message not handled: " <> inspect(msg)) {:noreply, socket} end @@ -461,14 +461,21 @@ defmodule RealtimeWeb.RealtimeChannel do {:reply, result, socket} end - def handle_in(_, _, socket) do + def handle_in(type, payload, socket) do socket = count(socket) + + # Log info here so that bad messages from clients won't flood Logflare + # Can subscribe to a Channel with `log_level` `info` to see these messages + Logger.info( + "Unexpected message from client of type `#{type}` with payload: " <> inspect(payload) + ) + {:noreply, socket} end @impl true def terminate(reason, _state) do - Logger.debug(%{terminate: reason}) + Logger.debug("Channel terminated with reason: " <> inspect(reason)) :telemetry.execute([:prom_ex, :plugin, :realtime, :disconnected], %{}) :ok end @@ -511,7 +518,7 @@ defmodule RealtimeWeb.RealtimeChannel do end other -> - Logger.error("Unexpected error for #{tenant} #{inspect(other)}") + Logger.error("Unexpected error for #{tenant}: " <> inspect(other)) {:error, other} end end @@ -645,7 +652,7 @@ defmodule RealtimeWeb.RealtimeChannel do when is_binary(message) do push_system_message("system", socket, "error", message, channel_name) - Logger.error(message) + Logger.error("Channel shutting down with message: " <> message) {:stop, :shutdown, socket} end From dd545cb4c0b37f35b4f37cac88147e718d232fe5 Mon Sep 17 00:00:00 2001 From: Chase Granberry Date: Fri, 18 Aug 2023 10:34:18 -0700 Subject: [PATCH 3/3] fix: bump version --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 477c48909..cc1f1a657 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Realtime.MixProject do def project do [ app: :realtime, - version: "2.20.1", + version: "2.20.2", elixir: "~> 1.14.0", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod,