I built a client portal with Next.js, Supabase and Stripe, mostly with AI help. Each client company sees only its own projects, payments and files.
I had RLS tests, and every one of them passed.
Then I reviewed it the way I would review a client's app. I found six problems. None of them showed up in the tests.
This post is what I checked, what I found, and the queries you can run on your own project.
Why passing tests is not enough
RLS tests check the cases you thought of. "Client A can't read client B's projects" is a good test. But RLS is only one layer. Three other things decide what a user can do:
- Grants: what each role is allowed to do at all, before RLS runs
- Functions: some run with the owner's rights and skip RLS
-
Server code: anything that uses the
service_rolekey skips RLS completely
My tests covered RLS well. The problems were in the other three.
Part 1: Look at the live database, not just the policies
1. Leftover privileges
The authenticated role had privileges the app never used: TRUNCATE, TRIGGER, REFERENCES and MAINTAIN on three tables, plus DELETE on a table where nothing deletes. Supabase adds these by default when you create a table.
RLS doesn't apply to TRUNCATE. Today, the REST API has no way to call it, so this was not an open hole. But a privilege nobody needs is one config change away from becoming a hole, so it shouldn't be there.
Check yours. This lists table-level and column-level grants for the two client roles:
select c.relname as table_name, null::name as column_name,
a.grantee::regrole as grantee, a.privilege_type
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
cross join lateral aclexplode(c.relacl) a
where n.nspname = 'public' and c.relkind = 'r'
and a.grantee in ('anon'::regrole, 'authenticated'::regrole)
union all
select c.relname, att.attname, a.grantee::regrole, a.privilege_type
from pg_attribute att
join pg_class c on c.oid = att.attrelid
join pg_namespace n on n.oid = c.relnamespace
cross join lateral aclexplode(att.attacl) a
where n.nspname = 'public' and c.relkind = 'r' and not att.attisdropped
and a.grantee in ('anon'::regrole, 'authenticated'::regrole)
order by 1, 2 nulls first, 3, 4;
Ask for each row: does the app actually need this?
One more thing: removing them once is not enough. The default settings add them again to every new table. You can see what new tables will get:
select defaclrole::regrole as owner,
defaclnamespace::regnamespace as schema,
defaclobjtype as object_type,
defaclacl as default_grants
from pg_default_acl
order by 1, 2, 3;
I now have a rule in the project: every new table starts with revoke all, then grants only what the app uses.
2. A function that wasn't in the migrations
A SECURITY DEFINER function runs with the rights of its owner, not the caller. If it reads or writes a table, RLS doesn't protect that table inside the function.
The live database had one that wasn't in my migrations: rls_auto_enable. A dashboard setting had created it to turn on RLS for new tables automatically. It couldn't be called through the API, but the anon and authenticated roles had the right to run it.
The function itself was harmless. The real problem was that the live database no longer matched my migrations, and I hadn't noticed. I removed the run rights for the client roles.
Check yours. By default, every role can run a new function, so look at the last two columns:
select p.proname as function,
has_function_privilege('anon', p.oid, 'execute') as anon_can_run,
has_function_privilege('authenticated', p.oid, 'execute') as authenticated_can_run
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosecdef
and n.nspname = 'public';
For each one: should these roles be able to run it, and what does it touch?
3. One layer guarding admin access
Admins and clients both sign in, so they share the same authenticated role. What makes someone an admin is a role column in the profiles table. If a client could change their own role, they would become an admin.
Only one RLS policy stood in the way. If someone edited that policy by mistake later, any client could make themselves an admin.
Now there are three layers. Clients can only SELECT from profiles, so the grants block any update. RLS still blocks it. And a trigger stops anyone signed in through the API from changing a role.
I fixed all three problems in one migration. Granted privileges (table-level plus column-level) went from 28 to 11, and I checked every user flow afterward.
The fix: https://github.com/kuros-works/client-portal-demo/pull/3
Part 2: Attack it, don't just read it
Reading policies tells you what should happen. I wanted proof of what does happen.
I wrote a script that signs in as four different users: an anonymous visitor, two client companies, and an admin. Then it calls the REST, RPC and Storage APIs directly, the way an attacker would, without going through the UI.
It also checks which columns each role can read. Hiding a column in the UI doesn't protect it. If the grants and RLS allow it, anyone can read it with one API call. So the script compares what each role can read against a fixed list, and a new column fails the test until someone decides who may see it.
To make sure the test can actually fail, I break things on purpose (a "positive control") and confirm the script catches it.
All 145 checks passed.
Later, as the portal grew, the checks grew to 467, and they now run on every pull request in GitHub Actions. Back when there were 422, a test PR that made every company's projects visible failed 12 of them and could not be merged.
Part 3: RLS doesn't protect server code
Any route that uses the service_role key skips RLS. So I listed every server route and action, and found three weaker spots.
4. Links built from request headers
The sign-in link and the Stripe success/cancel links were built from the request's host headers. With a forged host, a sign-in email could send the user to someone else's site. Now every link is built from a fixed SITE_URL, and I removed a wildcard preview URL from Supabase's redirect allow list.
5. "Check admin first" was only a comment
All the routes that used service_role did check for an admin. But the rule was just a comment. One new route that forgot the check would skip RLS entirely.
Now the service_role client can only be obtained through a function that checks for an admin first. The Stripe webhook is the one exception, because no user is signed in there. A lint rule stops its key from being used in any other file.
6. A download route guarded by RLS alone
It worked, but one policy mistake would have exposed other companies' files. The route now checks in code that the file belongs to the user's company (or that the user is an admin).
All three are fixed and checked in production. These were found by reading the code, not by the 467 automated checks, which go through the APIs. Route-level tests are the part I haven't automated yet.
A quick checklist for your own app
- Run the grants query above. Remove anything the app doesn't use.
- Check your default privileges. Start every new table with
revoke all. - List your
SECURITY DEFINERfunctions. Check who can run each one and what it touches. - If a column like
roledecides who is an admin, make sure users can't change it, with more than one layer. - Test as real users against the API, not only through the UI. Include a test that is supposed to fail.
- Search your server code for
service_role. Make sure every use checks who is calling, and that the check is in code, not in a comment.
Your tests can all be green while your database says something else. The only way to know is to check the database itself.
I do this as a fixed-price review on Upwork: a report ranked by severity, with the SQL to fix each issue. If you'd like a second pair of eyes on your Supabase app: https://www.upwork.com/services/product/development-it-a-supabase-rls-security-review-for-your-lovable-bolt-or-cursor-app-2126067179511646761

Top comments (7)
Three more places to look in the same kind of review. Views run with the owner's rights by default, so a view over an RLS-protected table can return rows the caller should not see. On Postgres 15+, create view ... with (security_invoker = true) fixes that. For SECURITY DEFINER functions, pin set search_path = '' and schema-qualify everything inside, otherwise a caller can shadow an object the function relies on.
And for the default grants, alter default privileges in schema public revoke all on tables from anon, authenticated; (run it as the role that creates your tables) stops new tables from getting them in the first place.
Thanks, Hritik. Useful list, so I checked the demo repo against all three.
Thanks for the concrete SQL.
One detail on the default privileges step: ALTER DEFAULT PRIVILEGES only applies to objects created by the role that runs it, unless you add FOR ROLE. On Supabase the defaults usually exist for more than one creating role, so check SELECT defaclrole::regrole, defaclnamespace::regnamespace, defaclacl FROM pg_default_acl; and revoke for each role that creates tables (postgres, and supabase_admin if it appears). The verify check can then be: create a throwaway table as each of those roles in a transaction, read has_table_privilege('anon', 'it', 'select'), and roll back. That catches the case where one role was fixed and the other was not.
Good catch, thanks. I'll start by checking pg_default_acl on both the local stack and the hosted project, since they may not list the same creating roles, then revoke per role with FOR ROLE. The throwaway-table check is a nice way to verify it: one create, one has_table_privilege, one rollback per role, so it catches exactly the half-fixed case you describe.
Follow-up: done, and it's live in production now.
PR: github.com/kuros-works/client-port...
What I found, following your steps:
alter default privileges for role postgres in schema public revoke all ... from anon, authenticated, service_role. New tables and sequences now get nothing until a migration grants it.Your throwaway-table check is now in the verify script and in CI. Thanks for pushing on this.
Point 2 is the one that stuck with me: the live database had drifted from the migrations and nothing told you. The same drift happens in code. Your fix for #6 (the download route now checks the file belongs to the user's company) is exactly the kind of line a later AI edit can quietly drop while every RLS test stays green, because RLS still holds and the route test you haven't automated yet never runs.
A cheap guard for that: write the separation rules down in plain words ("a client user only reads files of their own company"), link each one to the line that enforces it, and on every change ask whether any of those lines moved or disappeared. It also covers the server code that uses service_role, which no RLS test sees.
Thanks, that's a fair point. The repo's CLAUDE.md already spells out who can see what in plain language, but none of those rules point to the line that enforces them. And the company check in the download route still isn't covered by an automated test, so an AI edit could drop it while CI stays green.
Linking each rule to its line, plus a test for that route, is going on my list. Good call on service_role too: that's exactly the code path the RLS tests never touch.
Some comments may only be visible to logged-in visitors. Sign in to view all comments.