Skip to content

Commit f3cf587

Browse files
authored
fix: Ensure token refresh checks write policy (#1288)
1 parent 6cc87f5 commit f3cf587

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule Realtime.MixProject do
44
def project do
55
[
66
app: :realtime,
7-
version: "2.34.13",
7+
version: "2.34.14",
88
elixir: "~> 1.17.3",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/integration/rt_channel_test.exs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,9 @@ defmodule Realtime.Integration.RtChannelTest do
565565
:authenticated_read_broadcast_and_presence,
566566
:authenticated_write_broadcast_and_presence
567567
]
568-
test "on new access_token and channel is private policies are reevaluated",
568+
test "on new access_token and channel is private policies are reevaluated for read policy",
569569
%{topic: topic} do
570570
{socket, access_token} = get_connection("authenticated")
571-
{:ok, new_token} = token_valid("anon")
572571

573572
realtime_topic = "realtime:#{topic}"
574573

@@ -580,6 +579,8 @@ defmodule Realtime.Integration.RtChannelTest do
580579
assert_receive %Message{event: "phx_reply"}, 500
581580
assert_receive %Message{event: "presence_state"}, 500
582581

582+
{:ok, new_token} = token_valid("anon")
583+
583584
WebsocketClient.send_event(socket, realtime_topic, "access_token", %{
584585
"access_token" => new_token
585586
})
@@ -601,6 +602,65 @@ defmodule Realtime.Integration.RtChannelTest do
601602
assert_receive %Message{event: "phx_close", topic: ^realtime_topic}
602603
end
603604

605+
@tag policies: [
606+
:authenticated_read_broadcast_and_presence,
607+
:authenticated_write_broadcast_and_presence
608+
]
609+
test "on new access_token and channel is private policies are reevaluated for write policy",
610+
%{topic: topic, tenant: tenant} do
611+
{socket, access_token} = get_connection("authenticated")
612+
realtime_topic = "realtime:#{topic}"
613+
614+
WebsocketClient.join(socket, realtime_topic, %{
615+
config: %{broadcast: %{self: true}, private: true},
616+
access_token: access_token
617+
})
618+
619+
assert_receive %Message{event: "phx_reply"}, 500
620+
assert_receive %Message{event: "presence_state"}, 500
621+
# Checks first send which will set write policy to true
622+
payload = %{"event" => "TEST", "payload" => %{"msg" => 1}, "type" => "broadcast"}
623+
WebsocketClient.send_event(socket, realtime_topic, "broadcast", payload)
624+
Process.sleep(1000)
625+
626+
assert_receive %Message{
627+
event: "broadcast",
628+
payload: ^payload,
629+
topic: ^realtime_topic
630+
},
631+
500
632+
633+
# RLS policies changed to only allow read
634+
{:ok, db_conn} = Database.connect(tenant, "realtime_test")
635+
clean_table(db_conn, "realtime", "messages")
636+
create_rls_policies(db_conn, [:authenticated_read_broadcast_and_presence], %{topic: topic})
637+
638+
# Set new token to recheck policies
639+
{:ok, new_token} =
640+
generate_token(%{
641+
exp: System.system_time(:second) + 1000,
642+
role: "authenticated",
643+
sub: random_string()
644+
})
645+
646+
WebsocketClient.send_event(socket, realtime_topic, "access_token", %{
647+
"access_token" => new_token
648+
})
649+
650+
# Send message to be ignored
651+
payload = %{"event" => "TEST", "payload" => %{"msg" => 1}, "type" => "broadcast"}
652+
WebsocketClient.send_event(socket, realtime_topic, "broadcast", payload)
653+
654+
Process.sleep(1000)
655+
656+
refute_receive %Message{
657+
event: "broadcast",
658+
payload: ^payload,
659+
topic: ^realtime_topic
660+
},
661+
500
662+
end
663+
604664
test "on new access_token and channel is public policies are not reevaluated",
605665
%{topic: topic} do
606666
{socket, access_token} = get_connection("authenticated")

0 commit comments

Comments
 (0)