Check your Lovable or Supabase database rules with 6 read-only queries
In the last post I read 85 security fixes in Lovable apps and wrote down what was open before each one. 43 of the 85 were in the rules of the database itself. Most of those shapes can be looked for from the SQL editor o
In the last post I read 85 security fixes in Lovable apps and wrote down what was open before each one. 43 of the 85 were in the rules of the database itself.
Most of those shapes can be looked for from the SQL editor of your own project. Here are six queries for that. All six only read the catalog, the tables where Postgres keeps its own description of your database. They change nothing and read none of your rows. Paste each one into the SQL editor in the Supabase dashboard and run it.
A row in a result is a place to look, not a verdict. For each query I say what a row means and what the real fixes did about it. I ran every query and every fix below on a made-up database that has each hole once.
1. Tables with row level security off
select c.relname as table_name
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'r' and not c.relrowsecurity
order by 1;
A table on this list has no rules of its own. Wherever the browser's roles hold rights on it, anyone who holds the public key of your project can read and change every row, and that key is in the code your site sends to every browser. Until this year every new table was handed to those roles. Supabase changed that in 2026: a new table is no longer reachable until it is granted, and tables made before the change keep their rights. The fix is one line a table:
alter table public.backups enable row level security;
With row level security on and no policy, the browser reaches nothing in the table. Your server code, which works with the service key, still does.
2. Policies that let every caller through
select tablename, policyname, cmd, roles
from pg_policies
where schemaname = 'public' and (qual = 'true' or with_check = 'true')
order by 1, 2;
These are policies whose whole condition is true. On a SELECT that can be right: a table of blog posts is meant to be read. Ask it of each row anyway, because 11 of the 85 fixes were tables that more people could read than was meant. On an INSERT, UPDATE or DELETE it is almost never right.
The usual fix names the owner of the row:
drop policy "Profiles are viewable by everyone" on public.profiles;
create policy "Users read own profile" on public.profiles for select using (auth.uid() = id);
One limit: a policy can ask nothing of the caller without being the word true. using (published = true) lets everyone read the published rows, and this query does not list it.
3. Functions with their owner's rights that the browser may run
select p.proname as function_name,
pg_get_function_identity_arguments(p.oid) as arguments,
has_function_privilege('anon', p.oid, 'execute') as anyone_can_run,
(p.prorettype = 'boolean'::regtype
and pg_get_function_identity_arguments(p.oid) ~ 'uuid') as answers_about_a_user
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prosecdef
and p.prorettype <> 'trigger'::regtype
and (has_function_privilege('anon', p.oid, 'execute')
or has_function_privilege('authenticated', p.oid, 'execute'))
order by 1;
A security definer function runs with the rights of whoever created it, so the policies on the tables do not apply inside it. A function in the public schema can be called from the browser by name. Each row here is a function that does both. Read each one and ask what a stranger could do by calling it with values of their own choosing.
For a function that only your server or a timer needs, take it away from the browser. Name the two roles: taking it away from public alone was not enough in one of the real projects.
revoke execute on function public.purge_backups() from public, anon, authenticated;
The column answers_about_a_user marks the shape that 9 of the 85 fixes were about: a helper such as has_role that is handed a user id and answers yes or no. Your policies call it with the caller's own id. A browser can call it with anybody's id and learn who the admins are. Do not take this one away from signed-in users, because your policies run as them and would stop working. Make it answer only about the caller:
create or replace function public.has_role(_user_id uuid, _role text)
returns boolean language sql stable security definer set search_path = public as $$
select _user_id = auth.uid()
and exists (select 1 from public.user_roles
where user_id = _user_id and role = _role)
$$;
The helper stays on the list after this, since the list is of functions to read and you have now read this one. If an admin page or your server code asks it about other users, that needs another way to ask.
4. Views that answer with their creator's rights
select c.relname as view_name
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public' and c.relkind = 'v'
and not coalesce((select o.option_value = 'true'
from pg_options_to_table(c.reloptions) o
where o.option_name = 'security_invoker'), false)
order by 1;
A reader of the last post pointed this one out, and it hides well. A view runs with the rights of its creator, so the policies on the tables under it do not apply to whoever reads the view. An app can have tight policies on a table of deals and still show everyone every total through a view over it. On Postgres 15 and later the fix is one line:
alter view public.deal_totals set (security_invoker = true);
5. Update policies on tables that hold rank or money
select p.tablename, p.policyname,
string_agg(c.column_name, ', ' order by c.column_name) as columns_a_user_may_change
from pg_policies p
join information_schema.columns c
on c.table_schema = p.schemaname and c.table_name = p.tablename
where p.schemaname = 'public' and p.cmd in ('UPDATE', 'ALL')
and c.column_name ~ '^(role|is_admin|is_staff|is_pro|is_premium|plan|tier|credits?|balance|subscription_.*|is_verified|is_approved)$'
and has_column_privilege('authenticated',
format('%I.%I', p.schemaname, p.tablename)::regclass, c.column_name, 'UPDATE')
group by 1, 2
order by 1, 2;
11 of the 85 fixes were a user giving themselves rank or money. A policy that lets a user update their own row covers every column of that row, the paid flag and the credits too. This lists update policies on tables that hold such a column, where the signed-in role still has the right to change it. The list of column names is mine, so add the names your app uses.
The fix that holds takes the right on the whole table away and gives it back for the columns a user may change:
revoke update on public.profiles from anon, authenticated;
grant update (full_name) on public.profiles to authenticated;
Taking the right away for one column does nothing while the right on the whole table stands. That is how Postgres works, and it is why the first line is needed. If a trigger guards the column in your app instead, this query cannot see it: read the trigger and check that it names every column that matters.
6. Insert policies that say nothing of the role
select p.tablename, p.policyname, p.with_check
from pg_policies p
join information_schema.columns c
on c.table_schema = p.schemaname and c.table_name = p.tablename and c.column_name = 'role'
where p.schemaname = 'public' and p.cmd in ('INSERT', 'ALL')
and coalesce(p.with_check, p.qual, '') !~* '\mrole\M'
order by 1, 2;
with check (user_id = auth.uid()) on a table of team members says whose row it is. It says nothing about which team the row joins or what role it holds, so a signed-in user can add themselves to any team as an admin. This lists insert policies on tables with a role column whose condition never mentions the role.
drop policy "Members can join" on public.team_members;
create policy "Members can join" on public.team_members for insert with check (user_id = auth.uid() and role = 'member');
If the one who adds the row is the owner of the team, picking the role is theirs to do, and that policy is fine.
What these six do not show
They cover the database half. The other half of the 85 was in server code and in pages: a server function that never checks who is calling, a file path taken from the request, text from the database put into HTML. No query sees those. Neither does a query know the rules that only your app knows, such as which students a teacher may see.
And the queries read the database as it is today. If you also keep your rules in migration files, the two can differ. The last post has a section on that.
If you want this checked on your app
I do this as a fixed-price check on my Upwork listing. It reads the repository as well as the rules: the server functions, the routes and the pages. You get a report with the file and line of each finding, and every row is read by hand before you see it. $149 is an opening price for the first five clients, then $249. Fixing what it found is a separate order: up to three findings for $300, each with proof that it shut what was open and opened nothing new. Nothing is run against your live app, and no key is ever tried. An empty report does not mean your app is secure.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.