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

fix: Ensure token refresh checks write policy #1288

Merged
merged 1 commit into from
Jan 29, 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
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.34.13",
version: "2.34.14",
elixir: "~> 1.17.3",
elixirc_paths: elixirc_paths(Mix.env()),
start_permanent: Mix.env() == :prod,
Expand Down
64 changes: 62 additions & 2 deletions test/integration/rt_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,9 @@
:authenticated_read_broadcast_and_presence,
:authenticated_write_broadcast_and_presence
]
test "on new access_token and channel is private policies are reevaluated",
test "on new access_token and channel is private policies are reevaluated for read policy",
%{topic: topic} do
{socket, access_token} = get_connection("authenticated")
{:ok, new_token} = token_valid("anon")

realtime_topic = "realtime:#{topic}"

Expand All @@ -580,6 +579,8 @@
assert_receive %Message{event: "phx_reply"}, 500
assert_receive %Message{event: "presence_state"}, 500

{:ok, new_token} = token_valid("anon")

WebsocketClient.send_event(socket, realtime_topic, "access_token", %{
"access_token" => new_token
})
Expand All @@ -601,6 +602,65 @@
assert_receive %Message{event: "phx_close", topic: ^realtime_topic}
end

@tag policies: [
:authenticated_read_broadcast_and_presence,
:authenticated_write_broadcast_and_presence
]
test "on new access_token and channel is private policies are reevaluated for write policy",
%{topic: topic, tenant: tenant} do
{socket, access_token} = get_connection("authenticated")
realtime_topic = "realtime:#{topic}"

WebsocketClient.join(socket, realtime_topic, %{
config: %{broadcast: %{self: true}, private: true},
access_token: access_token
})

assert_receive %Message{event: "phx_reply"}, 500
assert_receive %Message{event: "presence_state"}, 500
# Checks first send which will set write policy to true
payload = %{"event" => "TEST", "payload" => %{"msg" => 1}, "type" => "broadcast"}
WebsocketClient.send_event(socket, realtime_topic, "broadcast", payload)
Process.sleep(1000)

assert_receive %Message{
event: "broadcast",
payload: ^payload,
topic: ^realtime_topic
},
500

# RLS policies changed to only allow read
{:ok, db_conn} = Database.connect(tenant, "realtime_test")
clean_table(db_conn, "realtime", "messages")
create_rls_policies(db_conn, [:authenticated_read_broadcast_and_presence], %{topic: topic})

# Set new token to recheck policies
{:ok, new_token} =
generate_token(%{
exp: System.system_time(:second) + 1000,
role: "authenticated",
sub: random_string()
})

WebsocketClient.send_event(socket, realtime_topic, "access_token", %{
"access_token" => new_token
})

# Send message to be ignored
payload = %{"event" => "TEST", "payload" => %{"msg" => 1}, "type" => "broadcast"}
WebsocketClient.send_event(socket, realtime_topic, "broadcast", payload)

Process.sleep(1000)

refute_receive %Message{
event: "broadcast",
payload: ^payload,
topic: ^realtime_topic
},
500
end

test "on new access_token and channel is public policies are not reevaluated",
%{topic: topic} do
{socket, access_token} = get_connection("authenticated")
Expand Down Expand Up @@ -1115,7 +1175,7 @@
} do
change_tenant_configuration(:private_only, true)

Realtime.Tenants.Cache.invalidate_tenant_cache(@external_id)

Check warning on line 1178 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.

Process.sleep(100)

Expand Down Expand Up @@ -1453,10 +1513,10 @@
end

def setup_trigger(%{tenant: tenant, topic: topic} = context) do
Realtime.Tenants.Connect.shutdown(@external_id)

Check warning on line 1516 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.
Process.sleep(500)

{:ok, db_conn} = Realtime.Tenants.Connect.connect(@external_id)

Check warning on line 1519 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.

random_name = String.downcase("test_#{random_string()}")
query = "CREATE TABLE #{random_name} (id serial primary key, details text)"
Expand Down Expand Up @@ -1493,7 +1553,7 @@
{:ok, db_conn} = Database.connect(tenant, "realtime_test", :stop)
query = "DROP TABLE #{random_name} CASCADE"
Postgrex.query!(db_conn, query, [])
Realtime.Tenants.Connect.shutdown(db_conn)

Check warning on line 1556 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.

Process.sleep(500)
end)
Expand All @@ -1506,9 +1566,9 @@
defp change_tenant_configuration(limit, value) do
@external_id
|> Realtime.Tenants.get_tenant_by_external_id()
|> Realtime.Api.Tenant.changeset(%{limit => value})

Check warning on line 1569 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.
|> Realtime.Repo.update!()

Realtime.Tenants.Cache.invalidate_tenant_cache(@external_id)

Check warning on line 1572 in test/integration/rt_channel_test.exs

View workflow job for this annotation

GitHub Actions / Tests

Nested modules could be aliased at the top of the invoking module.
end
end
Loading