A team finishes its RLS work, tests every table, and ships chat on Realtime. The tables are locked down. The chat is not.
Realtime has two separate authorization systems and only one of them is the table policies everyone reviewed. That is not a bug — it is a boundary that is easy to miss because both features live in the same dashboard.
Two systems, one product name
Postgres Changes streams row inserts/updates/deletes. A subscriber receives a row only if a table policy would let that user read it, so it inherits your RLS work — and its gaps. A table in the realtime publication with RLS off, or with USING (true), streams rows to every subscriber that can reach the channel.
Broadcast and Presence are a message bus. Broadcast moves arbitrary JSON between clients; Presence tracks who is online. Neither reads your application tables, so table policies never see them. They are authorized by policies on realtime.messages — a table in the realtime schema that exists for this purpose and stores nothing.
If you only audited public, you audited half of the surface.
The default that opens the bus
A channel is public unless two things are true: the project setting Allow public access is off in Realtime settings, and the client joins with { config: { private: true } }. A public channel can be joined by anyone holding the anon key — the key that is, by design, in your browser bundle.
Both halves matter. Turning the dashboard setting off while the client still subscribes without private: true does not silently upgrade the channel; the join fails. Turning the setting back on quietly accepts public joins again. The setting is not visible from SQL, so it belongs on the deploy checklist.
What a Realtime policy actually says
Policies on realtime.messages map one-to-one to actions:
-
SELECT— may this user receive broadcasts and presence updates on this topic? -
INSERT— may this user send a broadcast or publish presence on this topic?
The topic is read with realtime.topic(); the message kind is the extension column (broadcast or presence); the user is auth.uid().
create policy "members can receive on topic"
on realtime.messages
for select
to authenticated
using (
exists (
select 1
from public.room_members m
where m.user_id = (select auth.uid())
and m.topic = (select realtime.topic())
)
and extension in ('broadcast', 'presence')
);
And the write side is a separate policy — read permission does not imply send permission:
create policy "members can send on topic"
on realtime.messages
for insert
to authenticated
with check (
exists (
select 1
from public.room_members m
where m.user_id = (select auth.uid())
and m.topic = (select realtime.topic())
)
and extension in ('broadcast', 'presence')
);
Two rules that catch people out. A user needs at least one read or write policy to join a private topic at all — a topic with no matching policy is closed, which is the correct default. And if the topic string is derived from a table key, the topic itself is the access-control input: the check must be against data, never against a client-supplied user_id.
Read-only SQL: audit both systems
1. Is RLS actually on for realtime.messages? Do not run ALTER TABLE ... ENABLE ROW LEVEL SECURITY to "make sure". The table is not yours: ownership is checked before the setting, the statement fails with 42501 must be owner of table messages, and the whole migration transaction aborts — including the create policy statements after it. Read the catalog instead:
select relrowsecurity
from pg_class
where oid = 'realtime.messages'::regclass;
2. Which Realtime policies exist?
select policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'realtime'
and tablename = 'messages'
order by cmd, policyname;
A row with qual = 'true' on SELECT is a public read bus. A row with with_check = 'true' on INSERT lets any client publish to any topic it names.
3. Which streamed tables have RLS off? Postgres Changes is only as safe as the table it streams:
select n.nspname as schema, c.relname as table
from pg_publication_tables pt
join pg_class c on c.relname = pt.tablename
join pg_namespace n on n.oid = c.relnamespace and n.nspname = pt.schemaname
where pt.pubname = 'supabase_realtime'
and c.relkind = 'r'
and not c.relrowsecurity
order by 1, 2;
Every row returned is a table whose changes are streamed to anyone who can subscribe — and with the anon key plus default grants, that is everyone with your project URL.
4. Which tables are in the publication at all? The list you review and the list the service uses should be the same list:
select schemaname, tablename
from pg_publication_tables
where pubname = 'supabase_realtime'
order by 1, 2;
The cache: revocation is not instant
Realtime computes a client's access when the channel is joined, then caches it for the life of the connection. The cache refreshes on reconnect or when the client sends a new JWT over the access_token message. It is not refreshed when you delete a membership row.
The practical consequence: removing a user from a room does not remove them from the channel. They keep receiving messages until their token expires or the client refreshes it. If your tokens live for a day, a removed member can read a day of messages. Two mitigations, both design decisions rather than SQL: keep the JWT lifetime short where membership changes matter, and call setAuth() after a permission change so the client pushes a fresh token.
Where this fits in a launch checklist
Realtime is the fifth boundary in a Supabase project and the one most often left at its default:
-
Tables — RLS on, policies per operation, no
USING (true)on tenant data. -
Storage — private buckets plus
storage.objectspolicies; a public bucket bypasses RLS entirely. -
API / PostgREST — a route that queries with the anon key and no user JWT runs as
anon; a route that trusts a body-supplieduser_idis an IDOR regardless of policy quality. -
Edge Functions — a function with
service_rolein its environment bypasses every policy. -
Realtime — private channels, policies on
realtime.messages, and a publication list containing only tables you meant to stream.
Honest limits
These are catalog reads and documented semantics, not a live channel test. They cannot prove that your client passes private: true, that the project setting is off, or that a specific anon key can join a specific topic — those require a client that attempts the join and reports the result. I checked the queries against a local PostgreSQL fixture with a realtime schema; hosted Supabase defaults and the exact revision your project runs are outside what SQL can show. Zero rows means "no catalog-level gap I know how to detect", not "your channels are safe".
The recurring failure is not a missing check — it is that the schema changed after the check. A nightly read-only diff over the same catalog queries reports only new fingerprints and fails closed when the credential breaks, instead of writing an empty baseline.
Full write-up, with the other four boundaries and the free nightly workflow: https://cenkkurtoglu.com/blog/supabase-realtime-authorization-rls
Top comments (0)