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 filterUSING 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 SECURITYon every table reachable through the API,FORCEit 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 anonor a real anon JWT — not as the superuser who sees everything. The test you run aspostgresproves nothing about the policy.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
PostgreSQL Fixed 52 CVEs in One Release. The Upgrade Is the Easy Part.
PostgreSQL 18.7, 17.12, 16.16, 15.20 and 14.25 fix 52 security vulnerabilities, almost twice August's record of 28. The binary swap took under a second in my test. The post-update steps are where the work is, and one of them makes some indexes several times bigger.
What PostgreSQL 15 Changed
A new version of the popular open-source database PostgreSQL is now available, offering performance improvements over PostgreSQL 14 and new functions and capabilities for managing workloads in local and distributed deployments. Released on October 13 by the PostgreSQL Global Development Group,…
MySQL, PostgreSQL, Redis, and MongoDB End-of-Life Dates in 2026
Four major open source databases are hitting end-of-life milestones in 2026, and MySQL 8.0 has already crossed that line. Here's a practical, code-first look at how to audit what you're running, back it up properly, and migrate before the risk compounds.