Supabase security · 8 minute read
Supabase Realtime security: table RLS does not protect your channels
A team finishes its RLS work, tests every table, and ships a chat feature 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.
This is not a bug in Supabase; it is a boundary that is easy to miss because both features are configured in the same dashboard. Below are the two systems, the read-only SQL that shows whether each one is actually closing, and the timing detail that makes a revocation slower than expected.
Two systems, one product name
Postgres Changes streams row inserts, updates and deletes from your tables. Each subscriber receives a row only if a table policy would let that user read it, so Postgres Changes inherits the RLS work you already did. It also inherits 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 simply fails, and the failure looks like a broken feature rather than a security control. Turning the setting on quietly accepts public joins again. The setting is not visible from SQL, so it belongs on the deploy checklist, not only in the dashboard history.
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(). A membership table turns that into a policy:
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 — a read policy 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. First, 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. Second, granting select to authenticated on a topic that is derived from a table key means the topic string itself is the access-control input. If the client can choose the topic, it can choose a topic it is a member of only if the membership row exists — so the check must be against data, not 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 authenticated (or, worse, any anon) 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. With the anon key and the 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 is refreshed 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 for apps where membership changes matter, and call setAuth() after a permission change so the client pushes a fresh token.
If a token is never refreshed, the connection is dropped when it expires — so the window is bounded by token lifetime, not by the revocation.
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 no matter how good the policies are. - Edge Functions — a function with
service_rolein its environment bypasses every policy; the auth check inside the function is the only control. - Realtime — private channels, policies on
realtime.messages, and a publication list that contains only tables you meant to stream.
The 13-point checklist for AI-built Supabase apps walks the first three in order, with the SQL for each.
Honest limits
These are catalog reads and documented semantics, not a live channel test. They cannot prove that your client actually 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. The queries were checked 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 returned here 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. The free starter is here:
Get the free RLS Drift Monitor (13 checks, n8n + SQL) →
If you want a person to read the output and separate real exposure from noise, that is a fixed-scope review: Supabase RLS Spot Check. Work starts only after payment is secured.
Related: seven Supabase RLS leak surfaces · what an anon-key check can and cannot prove · the first 15 minutes of a Supabase security audit.