Why Your Row Level Security Passes Local Tests But Leaks Data in Production

The Superuser Delusion

PostgreSQL policies fail in production for one structural reason: developers test them in environments that operate under completely different permissions than the client application. When setting up row level security postgresql supabase architectures, the default developer experience is deceptive.

Inside the Supabase SQL Editor, every query you run executes under the privileges of the database owner or superuser role. Postgres explicitly bypasses policy checks for table owners unless forced otherwise via table settings. You execute a query, see the expected results, and assume the policy works. It does not. The REST API, PostgREST, and client SDKs execute under restricted roles like anon or authenticated. If your policy syntax has a flaw, you will never see it inside the dashboard editor. It surfaces only when an actual client sends a request with an HTTP JWT payload attached.

The SECURITY DEFINER Function Leak

Functions in Postgres run as SECURITY INVOKER by default, adopting the permissions of the user calling them. Developers frequently switch helper functions to SECURITY DEFINER to bypass permission bottlenecks or query internal metadata tables.

This creates an immediate, silent vulnerability. A SECURITY DEFINER function executes with the rights of the role that created it—typically postgres. If an RLS policy calls a custom PL/pgSQL function marked as SECURITY DEFINER, any subquery inside that function ignores the row-level security of the target tables. The policy intended to lock down access ends up evaluating an unconstrained query with admin privileges, returning data across tenant boundaries.

USING vs WITH CHECK Asymmetry

Another persistent flaw is treating read access rules and write access rules as interchangeable. They are not.

Consider a typical row level security example applied to an orders table:

CREATE POLICY "Users can manage their orders"
ON orders
FOR ALL
TO authenticated
USING (user_id = auth.uid());

This policy looks complete because it specifies FOR ALL. But USING and WITH CHECK perform distinct roles during query execution.

The USING clause dictates which existing rows are visible to the query engine. The WITH CHECK clause dictates which new or modified rows are permitted to be written to the disk. When an UPDATE statement runs against a policy that lacks a WITH CHECK clause, Postgres re-uses the USING expression to filter which row can be updated. It does not validate the new payload content.

A user can update their own row and change the user_id column to belong to a target user. The calling user loses visibility of the row immediately after the update completes, but the row was successfully reassigned to another tenant. The write succeeded, data was corrupted, and no error was thrown to the application layer.

Subquery Recursion and Unprotected Helper Tables

Complex authorization models rely on subqueries inside policy definitions. A policy on a documents table often checks if the requesting user exists in an organization_members table.

If the referenced table does not have row-level security explicitly enabled, or if its policy is configured with permissive defaults, the primary table policy collapses. Users can construct API parameters that exploit the loose subquery target. Postgres joins across the unrestricted relationship, exposing primary table rows that should remain invisible. Every table referenced inside a policy expression must be audited with the same strictness as the primary entity.

How to Audit Execution Context Correctly

Stop testing policies using raw SELECT statements in the database dashboard interface. You must mimic the exact execution context of the client driver.

Before running test queries in your database console, explicitly switch the session role and inject the request context to mirror an authenticated application call:

SET LOCAL ROLE authenticated;
SET LOCAL "request.jwt.claims" = '{"sub": "00000000-0000-0000-0000-000000000000", "role": "authenticated"}';

Execute your SELECT, INSERT, UPDATE, and DELETE queries immediately after setting these session variables. If a query returns rows belonging to a different UUID, your policy contains a read leak. If an INSERT or UPDATE succeeds when targeting a foreign identifier, your write guards are broken.

Professional Security Audit

Eliminating silent data leaks across complex schemas requires testing execution context at every boundary. If you need a complete technical review of your database policies, performance bottlenecks, and access rules, GuardLabs provides dedicated engineering support. We offer Аудит и настройка Row Level Security в Supabase/Postgres to locate hidden leaks and harden your production backend.