Guide

Five ways a Postgres RLS policy passes review and still leaks

Row-level security is the right default for a multi-tenant Postgres database. It is also the control that most often looks correct — enabled, policied, tested by hand once — while a second path around it stays open.

None of the five mechanisms below is a bug in Postgres. Each is documented behaviour, which is exactly why each one survives a code review: the reviewer is checking the policy, and the policy is fine. The leak is somewhere the policy is not consulted.

The question is not "is RLS enabled?"

PostgreSQL's documentation is precise about the default: once row security is enabled on a table, all normal access must be allowed by a policy, and if no policy exists a default-deny policy applies. That is a strong default, and it is why enabling RLS feels like the fix.

The same page then lists the exceptions in one paragraph:superusers and roles with BYPASSRLS always bypass row security, and table owners normally bypass it too. A test that runs as the owner, or as the migration role, or as a superuser, is not testing what your application does.

Measured here, on PostgreSQL 16. Same table, same owner, one row, a policy of USING (false), and an owner with rolsuper = false androlbypassrls = false. With ENABLE alone the owner read1 row. With ENABLE + FORCE the owner read0 rows. The only roles that keep their exemption after FORCEare a superuser and a role with BYPASSRLS.

The five traps

1. The owner exemption

The app role is also the table owner, so no policy ever applies to it.

Fix: ALTER TABLE … FORCE ROW LEVEL SECURITY, applied by a role that is exempt.

2. The helper owns the policy

A SECURITY DEFINER function used inside a policy, created by a role that is itself subject to the policy.

Fix: Create helper functions as a superuser or a BYPASSRLS role.

3. The setting that was never reset

A per-request session variable survives on a pooled connection and reaches the next tenant.

Fix: set_config(name, value, true) inside an explicit transaction.

4. The second permissive policy

One extra policy with USING (true) is OR-ed with the others and widens access to everything.

Fix: Use AS RESTRICTIVE for the narrow clause, and count your permissive policies.

5. The paths that never read a policy

BYPASSRLS, foreign-key checks and TRUNCATE are not gated by row security at all.

Fix: Model the credential, not just the policy.

1. The owner exemption, and the contradiction it creates

The obvious fix for the owner exemption is FORCE ROW LEVEL SECURITY. The trap is what happens next: FORCE removes the exemption from the role that owns the table, which is usually the same role your setup instructions told you to connect as.

We shipped exactly this mistake. An earlier release told buyers to apply the RLS file as the table owner — and the same file runs FORCE ROW LEVEL SECURITY. Following the instruction as written left the policy helpers owned by a role that was now subject to the policies those helpers were used inside, and every gated read failed withstack depth limit exceeded. The instruction now asks for a role withBYPASSRLS or a superuser, and a regression test fails if it drifts back.

-- enable, then force: FORCE is what makes the owner subject to the policy
ALTER TABLE organizations ENABLE ROW LEVEL SECURITY;
ALTER TABLE organizations FORCE ROW LEVEL SECURITY;

-- and apply that as a role the policies do not apply to:
-- CREATE ROLE rls_migrator BYPASSRLS;
psql "$ADMIN_DATABASE_URL" -f rls/0010_rls_policies.sql

Two roles, not one. The application connects as a low-privilege, non-owner role; the migration connects as the exempt one. If those are the same credential, FORCE and the setup step contradict each other and at least one of them is wrong.

2. The helper function owns the policy

Policies that need to look at another table are usually written with a helper function, and that helper is usually SECURITY DEFINER so it can read a membership table the caller cannot see. The Postgres documentation notes that policy expressions run with the privileges of the user running the query, and thatsecurity-definer functions can be used to access data not available to the calling user.

The consequence is ownership: a SECURITY DEFINER function created by a role that is itself subject to RLS does not terminate the recursion. The function reads the membership table, that read is policy-checked, the policy calls the function. On the versions we test, the symptom is not a wrong answer — it is stack depth limit exceeded, which at least fails loudly.

3. The setting that was never reset

RLS policies that key on a session variable — current_setting('app.current_org'), for example — isolate correctly only if that variable is set per request and cleared per request. A pooled connection gets reused. A variable that is set and never reset hands one tenant's rows to the next request that lands on the same connection.

The version that leaks is not the one that fails to set the variable. It is the one that sets it with a session-scoped set_config outside a transaction, so the value outlives the request. Setting it with the local flag inside an explicit transaction means the value is discarded when the transaction ends:

BEGIN;
SELECT set_config('app.current_user_id', 'USER_ID_HERE', true); -- true = local to this transaction
-- ... the request's queries ...
COMMIT;

If your queries can run outside that transaction — a background job, a health check, a strayawait that escapes the wrapper — the setting is absent and the policy is evaluating against nothing. Decide deliberately what "no setting" means, because the default depends on how you wrote the policy.

4. The second permissive policy

Policies combine with boolean logic, not with "the narrowest one wins". The documentation is explicit: permissive policies — the default kind — are combined with OR, and restrictive policies are combined with AND. So adding a permissive policy never narrows anything. A migration that addsCREATE POLICY read_all ON invoices FOR SELECT USING (true) to a table that already has a correct tenant policy has not extended access to one case; it has granted every row to everyone the table is reachable by.

-- these two are OR-ed. The result is every row, for every caller.
CREATE POLICY tenant_isolation ON invoices FOR SELECT
  USING (org_id = current_setting('app.current_org')::uuid);

CREATE POLICY read_all ON invoices FOR SELECT USING (true);

-- if you meant a narrow exception that must also hold, say so:
CREATE POLICY admin_local_only ON invoices AS RESTRICTIVE TO admin
  USING (pg_catalog.inet_client_addr() IS NULL);

Practical test: count the permissive policies per table and per command. More than one on the same table and command is not automatically wrong, but each one is an independent grant, and the union is what applies.

5. The paths that never read a policy

Some operations do not consult row security at all, no matter how well written the policies are. Three are documented on the same page:

  • BYPASSRLS and superusers. Always exempt. If an application credential has either, every policy in the database is decorative for that connection.
  • Referential integrity checks. Unique, primary-key and foreign-key checks bypass row security to preserve data integrity — which makes them a covert channel: a conflicting insert can tell a caller that a row exists in another tenant's data.
  • Whole-table operations. TRUNCATE and REFERENCES are not subject to row security.

On a hosted platform there is a fourth, and it is the one that shows up in incident reports: the server-side key that is designed to bypass RLS. If that key ships in a client bundle, or is the only credential the app ever uses, the database policies are not the control in force — the key is.

The probe that catches all five

Reading policies is not a test. A test is two connections, two tenants, and one assertion: that the second tenant sees a set of rows disjoint from the first.

-- run as the APPLICATION role, not the owner, not a superuser
-- session A
SET ROLE app_rw;
BEGIN;
SELECT set_config('app.current_org', 'ORG_A_UUID', true);
SELECT count(*) FROM invoices;          -- expect only org A's rows
COMMIT;

-- session B: same query, ORG_B_UUID.
-- The two counts must sum to the total, and the two row sets must not overlap.
-- If they do overlap, one of the five mechanisms above is open.

A negative control matters as much as the probe: the same query with the setting absent should return zero rows, not the whole table. If it returns the whole table, the policy is not consulting the setting at all, and every green result above it is meaningless.

Where this leaves you

RLS is worth adopting even with all five traps, because the failure mode it produces is a loud error rather than a silent cross-tenant leak. But "we have RLS" is a claim about a policy, and the leak is a claim about a credential, a connection and a code path. Those are three different artefacts and they are tested three different ways.

The implementation side — switching RLS on for the Postgres kit, and the per-request GUC pattern — is inthe Postgres starter's RLS guide. If you want the failure side checked against a database you already run, that is what theRLS audit is.

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