Skip to content

Lesson 14 of 14 · 18m

Row-Level Security: Authorization in the Database

RLS makes authorization a property of the data itself — which is Supabase's entire model — and it fails open for table owners and closed for performance unless you write policies correctly.

Row-level security flips the usual architecture: instead of the application deciding who may see a row and Postgres trusting it, Postgres itself filters rows per policy per role. If you use Supabase, you are already inside this model — PostgREST connects as your JWT's role (anon, authenticated, service_role), and RLS policies on the tables are the authorization layer. There is no middleware checking anything. The database is the boundary.

The mechanics

ALTER TABLE documents ENABLE ROW LEVEL SECURITY;

CREATE POLICY own_docs ON documents
  FOR ALL TO authenticated
  USING      (owner_id = auth.uid())          -- read filter
  WITH CHECK (owner_id = auth.uid());         -- write filter

USING filters what existing rows a query sees; WITH CHECK validates what writes may produce — an INSERT that fails the check errors, a SELECT that fails the filter just sees nothing. FOR SELECT, FOR INSERT, FOR UPDATE, FOR DELETE scope policies per operation when read and write rules differ. With RLS enabled and no policy matching, the default is deny — zero rows, not an error. Policies are PERMISSIVE by default: multiple permissive policies are OR'd together. CREATE POLICY ... AS RESTRICTIVE AND's instead — the tool for "must also belong to a non-suspended tenant" layered over ownership rules.

The three ways it goes wrong

The table owner bypasses RLS. Postgres exempts the owner unless you say ALTER TABLE ... FORCE ROW LEVEL SECURITY. If your migration role owns the tables and also serves traffic, your policies are decorative. FORCE it, or serve through a non-owner role — Supabase's anon/authenticated roles exist precisely so the API never runs as owner.

The superuser and service_role bypass everything. BYPASSRLS roles and superusers skip policies entirely. Supabase's service_role key is BYPASSRLS — that's the point of it — which means a leaked service key is full database access with no policies in the way. It's the key that must never ship to a browser.

Policy cost is per-row evaluation. USING (owner_id = auth.uid()) calls a function per evaluated row — on a million-row scan it's a million function calls. The documented Supabase pattern is wrapping it: USING (owner_id = (select auth.uid())) makes it an initplan — evaluated once. At scale this is the difference between a query that finishes and one that pages someone. EXPLAIN your policies on real data; a policy joining to a permissions table per row is a nested loop you wrote in SQL that doesn't look like one.

What it actually closes

BOLA — broken object-level authorization, the "change id in the URL" bug — stops being an application discipline problem. GET /documents/123 can't leak row 124 because Postgres won't return it under your role, period, regardless of which endpoint or query path produced it. That's a genuinely stronger model than controller checks, which is why Supabase bets the platform on it.

It also has real CVE history — CVE-2024-10976, fixed in the November 2024 releases (17.1, 16.5, 15.9): row-security policies inside subqueries could be evaluated against the wrong user ID after SET ROLE/SET SESSION AUTHORIZATION changes, meaning a query could silently get another role's policies. Patch level matters when your authorization lives in the engine.

The practice

  • ENABLE ROW LEVEL SECURITY on every table reachable through the API, FORCE it on owner-served tables.
  • Deny by default — policies grant, absence denies. An empty policy set on an exposed table is a broken feature, not a vulnerability, which is the failure direction you want.
  • Wrap per-row functions as (select auth.uid()); EXPLAIN policies at production scale.
  • Test as the role that shouldn't see things — SET LOCAL ROLE anon or a real anon JWT — not as the superuser who sees everything. The test you run as postgres proves nothing about the policy.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.