Guide

Supabase RBAC: grants, policies and the role that goes stale

Role-based access control on Supabase is not one feature you switch on. It is three independent controls that have to agree, and the failure that matters is the one where all three look present and only two are doing anything.

This is the layered-access question for a hosted Postgres platform specifically. If you are looking for the database-level mechanisms instead — the owner exemption, security-definer helpers, pooled session settings — that isthe RLS bypass guide.

Three controls, tested three different ways

PostgreSQL separates two questions that are easy to conflate: whether a role may touch a table, and which rows it sees once it may. Row security answers the second. It does not answer the first — the documentation puts it plainly: policiesrestrict, on a per-user basis, which rows can be returned by normal queries, and they sit on top of the SQL privilege system rather than replacing it.

The GRANT — May this role touch this table at all?

Fails loudly: Row security is on, but the role holds no privilege — every query errors.

Fails silently: Privileges are granted, but row security was never enabled — every row is readable.

The policy — Which rows may this role see?

Fails loudly: No policy on the table. Postgres applies default-deny, so nothing is visible.

Fails silently: A second permissive policy exists, and permissive policies are OR-ed together.

The role claim — Which role is the request running as?

Fails loudly: Token expired — the request is anonymous and sees whatever the anonymous role sees.

Fails silently: The claim was minted before a role change, so the request carries the old role.

A review usually checks one of these. "We wrote RLS policies" checks the middle row. That is why the two silent failures are the ones that ship.

The silent failure we actually found

While auditing a demo database that this project runs — a hosted Supabase project with seeded data, publicly listed demo logins, and a daily re-seed — we foundfive of six tables in the public schema with row security disabled, andall six granting all seven table privileges to both the anonymous and the authenticated roles. The application code was correct. The policies that existed were correct. The controls were simply not the ones in force.

Nothing in the application logged an error, because nothing was wrong from the application's point of view: it was asking for rows and getting them. The exposure was not a bug in a query. It was the default posture of a project created without anyone deciding what the default should be.

The check that finds this is a catalogue query, not a code review — one statement per schema, asking each table for relrowsecurity and for what each role holds. It is two minutes of work and it is the only thing that distinguishes "we have RLS" from "RLS is enabled on the tables that hold tenant data".

Why the role claim goes stale

The third layer is the one no database can fix. A request arrives carrying a token; the platform maps that token to a database role and, inside the policies, an identity value. Both are fixed at the moment the token was minted.

That is fine for a role that changes yearly and wrong for a role that changes per tenant. If a user is demoted from admin to member, the token in their browser still says admin until it is refreshed or revoked. If the organization they belong to is encoded in a custom claim, they keep the old organization until the same moment.

The durable fix is to stop treating the token as the source of truth for authorization decisions that can change inside the token's lifetime. Read the membership row inside the policy instead, so the decision is evaluated against the database on every query:

-- eligibility is a row you can update, not a claim you have to wait out
CREATE POLICY member_reads_org_data ON invoices
  FOR SELECT
  USING (
    org_id IN (
      SELECT m.org_id FROM memberships m
      WHERE m.user_id = auth.uid()
    )
  );

-- demote someone:
UPDATE memberships SET role = 'member' WHERE user_id = 'USER_ID_HERE';
-- the next query they make already reflects it.

High-churn permissions belong in tables. Low-churn ones — "this account is an administrator of the platform at all" — are fine in a claim. Putting a per-tenant role in the token means the token is now your authorization cache and your revocation is token expiry.

The escalation test, and why the client is not the control

Every one of these layers fails the same way when it is tested from the wrong place: the client refuses to send the request. A UI that only renders rows for the current organization proves nothing about the server, because an attacker does not use your UI.

The test is a request that asks for something the caller should not have, sent with a valid credential for the wrong tenant:

  1. Authenticate as a member of organization A.
  2. Send a read for organization B's records — with B's identifier in the filter, in the body, or in the path.
  3. The expected answer is zero rows or an error, produced by the database.
  4. The failing answer is a redirect, a filtered client response, or a 200 with A's rows, because that means the server trusted the client's scope.
  5. Repeat as an unauthenticated caller, and as the wrong role on the same tenant.

If step 3 comes back from application code rather than from the policy, you have an application-level filter — which is a legitimate design with a different failure mode: a filter that gets missed once returns the wrong rows silently instead of erroring.

The key that is designed to bypass all of it

Supabase documents a server-side key whose purpose is to bypass row-level security so administrative code can do things a user cannot. It does what it says. That makes it the most important credential in the system, and the two questions that decide whether it is a control or a hole are: where is it allowed to live, and what happens if it is in a client bundle.

The practical rule is narrow: it may exist only in server-side execution context, never in a build artefact that ships to a browser, and never as the credential a normal request path uses. If the application's only database credential is that key, every policy above it is decoration.

A posture you can actually check

Four assertions, each testable from outside the codebase, is a better review than reading the policies:

  • Every table that holds tenant data has row security enabled — read from the catalogue, not from the migrations.
  • The anonymous role holds no privilege on those tables — a public site needs no table grants.
  • Exactly one permissive policy per table and command — more than one is a union, and the union is usually wider than intended.
  • A cross-tenant read, sent with a valid credential, returns nothing — and the refusal comes from the database.

The kit this site sells ships orgs, memberships, invites and a role hierarchy as tables rather than as token claims, for the reasons above — that layer is documented inthe Supabase starter's RBAC page. For the hosted-platform equivalent of the four assertions, theRLS audit is what runs them against a live project.

Related:the Supabase starterandSvelteKit + Postgres vs Supabasefor the same stack choice without the hosted platform.

Get in touch

Questions about the product, team licenses, or anything else? We'll aim to respond within 48 hours.

Max 2000 characters

Stored in our own database — no third party. Deleted on request.

Related reading