Who Can See What
Every Query Must Be Scoped, And Every Key Has A Blast Radius
Isolation fails on one unscoped query, so it needs a floor in the database as well as a filter in the application. Then there is the key that bypasses the floor entirely.
The failure mode is one query. Not a sophisticated attack, not a zero day: one SELECT written on a Thursday for a dashboard count, without the clause that restricts it to the current tenant. It will happen, because the number of places that touch your data grows every week and every one of them has to remember.
Two layers, and why the argument about which is wrong
There are two places to enforce scoping. Filtering in the application is necessary and insufficient, because it depends on every developer, script and tool remembering. Row level security is the floor underneath it: a policy attached to the table, applied by the engine to every query, including the one somebody forgot to scope. Supabase's own guidance is to keep the application filter in addition to the policy rather than instead of it. Each layer catches the mistake the other makes.
RLS mechanics that are not in the tutorial
- Index the columns your policies filter on, unless they are already a primary key or unique. A policy is a predicate added to every query, so an unindexed policy column means extra work on every request. Supabase report more than a hundredfold improvement on large tables from this alone.
- Wrap the auth function in a subquery. Writing (select auth.uid()) = user_id rather than auth.uid() = user_id makes the planner evaluate it once as an initPlan and cache the result instead of calling the function per row, and it is valid only where the value does not depend on the row.
- Name the role the policy applies to, with to authenticated, rather than leaving it open to every role. Otherwise anonymous requests are processed before being rejected.
- Shape joins so a row column is compared against a fixed set. Writing team_id in (select team_id from team_user where user_id = (select auth.uid())) outperforms the reversed form, because the inner set is computed once rather than per row.
- SECURITY DEFINER functions bypass RLS on the tables they touch. That is both the performance escape hatch and a serious footgun, and such a function must pin its search_path or it becomes a privilege escalation path.
- Policies are not enforced against the table owner unless FORCE ROW LEVEL SECURITY is set, and the owner is not an abstraction, it is whatever role your migrations run as.
Two keys, and the difference is total
In the Supabase model, which generalises across hosted databases, there are two kinds of key. The anon key is public by design: it ships to the browser, it sits in your JavaScript bundle, anybody can read it. That is fine, and only fine because your policies are correct, since its power is exactly what they permit. The service role key bypasses row level security entirely. It is not a more powerful user, it is the absence of the rules.
The ways it leaks are mundane. A variable given a public prefix so the bundler inlines it. A client component importing a module that imports the server client. A log line printing a whole configuration object. An error response echoing the request. A screenshot pasted into a support ticket. A repository that was private when the key went in and public later. A coding agent handed the repo and asked to write a config file.
The discipline is boring and it holds. Secrets live in your host's environment, read only in server code, never in a file the bundler can reach. Different jobs get different credentials, so the analytics tool is read-only and the worker cannot drop a table. No production credential enters a laptop's shell history or an agent's environment. And you know today how long rotating every secret takes and what breaks while you do it, because that is not a procedure to invent during an incident.
Isolation is a property you test, not a feature you switch on
The deliverable here is one test, and it is the most valuable in your suite. Create tenant A and tenant B, each with a user and rows in every table holding customer data. Then, authenticated as B, call every endpoint and attempt every table read that exists, and assert that nothing of A's comes back and nothing of A's can be written. Do it twice: once through your API, and once with the anon key against your database's REST interface directly, because that second surface is the one an attacker has and it bypasses all of your application code.
Then make it fail on purpose: comment out a policy and confirm the test goes red. A cross-tenant test that has never failed may be testing nothing, and you will not find out which until the day it matters.
The less obvious places tenancy leaks
- Caches and content delivery networks, where a key that omits the tenant serves one customer's page to another. The most catastrophic version of the bug, because it leaks with nobody attacking anything.
- Search indexes, analytics warehouses and scheduled exports, all of which are copies of your data living outside the database's policy engine.
- Email and notifications, where a template rendered with the wrong context sends one organisation's figures to another, and there is no un-sending it.
- Aggregates and counts, where the number is itself the leak: a total that reveals another customer's volume, or a search that helpfully reports how many results you are not allowed to see.
- Support and impersonation tooling, which crosses the boundary by design and therefore needs its own audit log recording who viewed what, when, and why.
Key takeaways
This is one lesson of 541.
The whole Foundation is free — 16 subjects, beginner to advanced. Sign in to track what you have read, take the end-of-module quizzes, and use the terminal.