Skip to main content

Five Lovable security fixes, as before and after SQL

The diffs I apply most in Lovable and Supabase apps: policies, grants, views, roles and a NULL that skipped a check.

Updated 5 min readMarkdown

Most of what I fix in a Lovable app is a few lines of SQL. Here are five, each as a before and an after, so you can compare them with your own project.

I'm Denys, a Forward Deployed Engineer. This article was written with AI assistance and checked by me. The before snippets use made-up table names and show the pattern, not a client's schema. I mark each one that I did not run myself.

A Lovable app calls the Supabase Data API straight from the browser, with a public key. There is no backend of your own in between, so every rule that protects customer data has to live in the database. That is why these are SQL fixes.

1. A policy that only asks "are you signed in?"

-- before
create policy "read orders" on public.orders
  for select to authenticated
  using (auth.uid() is not null);

-- after
create policy "read own orders" on public.orders
  for select to authenticated
  using (user_id = auth.uid());

auth.uid() is not null means any signed-in user. On a public sign-up page that means anyone. The fix compares the row's owner or company to the caller. Status: both snippets are illustrative, I did not run them. In a multi-company app the second line compares the company instead, and a user who belongs to two companies needs the query to filter by the company of the request as well.

2. Grants that came with the table

-- before: on one project the supabase/postgres 17.6.1 image had given
-- anon and authenticated TRUNCATE, REFERENCES, TRIGGER and MAINTAIN
-- on every table in public

-- after
revoke all on public.orders from anon, authenticated;
grant select, insert, update on public.orders to authenticated;

No policy applies to TRUNCATE. The Data API has no TRUNCATE endpoint, so this is not a one-request wipe, but no client role needs it. Revoke everything first, then grant back what the policies need. From 30 October 2026 Supabase stops granting new tables in existing projects automatically, so every migration that creates a table now carries its own grant next to the policies. Status: the after block is from my checklist.

3. A view that runs as its owner

-- before
create view public.customer_summary as
  select id, email, plan from public.customers;

-- after
alter view public.customer_summary set (security_invoker = true);

A view runs with its owner's rights, and in Supabase the owner is usually postgres. So a view over a protected table hands out every row the policies hold back. security_invoker needs Postgres 15 or later. Status: the after line is from my checklist, the view body is made up.

Findings 1, 2 and 3 are checks 2, 6 and 7 in my Lovable app security checklist. It has twelve checks, each with the query or test that proves it, a prompt you can paste into Lovable or Claude, and eight more I added after one client's app.

4. A role taken from sign-up metadata

-- before: the new-user trigger reads what the client sent to signUp
v_role := new.raw_user_meta_data ->> 'role';

-- after: the role comes from a table only the server writes
select role into v_role
from public.invites
where email = new.email;

raw_user_meta_data is whatever the client sent to signUp, and users can change it later. On one app the trigger read the invited role from it, so anyone who registered could pass the owner role and become Owner. The other safe place is app_metadata. Status: the before pattern is what I found. The after block is my sketch, with an invented invites table, and it is untested.

5. A NULL that skipped the check

-- before: assignee_id is null, so the whole condition is null
if not (has_permission or assignee_id = auth.uid()) then
  raise exception 'not allowed';
end if;

-- after
if not coalesce(has_permission or assignee_id = auth.uid(), false) then
  raise exception 'not allowed';
end if;

In PL/pgSQL, if skips a null condition, so the exception never fires. The same expression is safe in a policy, where null means deny. On one app that let a role with every permission switched off act on all 3683 tasks, because none of them had an assignee. The coalesce turns the null into false, and not false raises. Status: the before pattern is the one from that app. The after block is untested here.

How do you prove each fix?

Do not take "fixed" from the chat as proof. If you click Fix in Lovable and the next scan finds the same thing, compare select * from pg_policies where tablename = 'your_table'; with what the chat says it changed. A prompt that makes a warning go away can also do it by widening access.

Lovable's Quick and Deep scans read your code and your settings. They do not sign in as a second user. So after each fix run the two-user test: user A in company 1 tries to read, edit and delete user B's records by id, through search and through every export. Every attempt has to return nothing or an error.

If one of these diffs looks like something in your project and you are not sure how to read it, ask in the comments.

No comments yet. Be the first to comment.