Supabase Row Level Security for Founders Who Built With AI
Founder & CTO, Buttercloud
If you built your product with Lovable, Bolt, Cursor or Claude Code, there is a good chance it stores its data in Supabase. There is also a good chance that one setting in that database is off, and that the setting is the only thing standing between your users’ data and anyone on the internet who cares to look.
The setting is row level security, usually shortened to RLS. This article explains what it is in plain words, why AI-built apps so often ship without it, how to check yours in five minutes, and how to write and test the handful of policies a normal SaaS needs. It expands the database section of our vibe-coded app launch checklist, and it sits inside our wider guide to vibe coding security. You do not need to be a developer to follow it. You do need to be the kind of founder who would rather check than hope.
Why one setting matters this much
Most web apps you have used in your life sit behind a server. The browser asks the server, the server checks who you are, and only then does it ask the database. Supabase apps are usually built the other way. The browser talks to the database directly, through Supabase’s API, using a key that is public by design.
That is a legitimate architecture and it is why these tools can build a working product so fast. But it moves the entire question of “who is allowed to see what” out of your application code and into the database itself. Supabase’s own documentation puts it bluntly: a table in an exposed schema without RLS is readable and writable by any role with a grant on it, and on existing projects new tables in the public schema start out granted to the anonymous and authenticated roles. In other words, if RLS is off, the public key is a master key.
This is not theoretical. In 2025, researchers found Lovable-built apps with RLS switched off across a large sample, which was serious enough to be assigned CVE-2025-48757. We go through that story, and what it means if you use Lovable, in Is Lovable secure? The short version is that the vulnerability was not in the generated code. It was in the one setting nobody turned on.
What row level security is, in plain words
Think of a table of invoices with ten thousand rows belonging to five hundred customers. Without RLS, a query that says “give me all invoices” gives you all ten thousand, whoever you are. With RLS on, the database adds a silent filter to every query based on who is asking, and the same query gives each customer only their own twenty.
The “who is asking” part comes from the login token your app sends with every request. Supabase exposes it to the database through a helper called auth.uid(), which returns the signed-in user’s ID. A policy is a short rule that compares that ID to a column in the row. If they match, the row is visible. If not, the row simply does not exist for that user.
Two details worth knowing before you write anything:
- RLS on with no policies means nobody gets anything. Postgres applies a default deny. Supabase’s docs say the same: once RLS is enabled, no data is accessible through the API with a publishable key until you create policies. This is why enabling it mid-build looks like you broke the app.
- Policies have two halves. A
usingclause decides which existing rows a user can see or touch. Awith checkclause decides which new or changed rows they are allowed to write. Reads need the first. Inserts need the second. Updates need both.
Why AI builders leave it off
Not out of malice and not, usually, out of ignorance. The model is optimising for an app that works in the next thirty seconds. The generated SQL creates the tables. The generated frontend queries them with the publishable key. Everything runs. If the model does enable RLS, every query comes back empty until policies exist, the preview looks broken, and the next prompt from a frustrated founder is “fix it”, which the model does by turning it back off.
The result is an app that behaves perfectly for you, its only user, during the entire build. Nothing in the normal development loop ever exercises the case of a second person reading the database. That case arrives on launch day.
The five-minute check
You have two ways to find out where you stand, and you should do both.
1. The dashboard. In your Supabase project, open Advisors, then Security Advisor. Supabase runs a set of deterministic checks here. The one you are looking for is called rls_disabled_in_public, shown as “Table publicly accessible”, and it is rated an error. Each table it lists can be read, edited and deleted by anyone with your project URL and public key. Two related findings matter too: policy_exists_rls_disabled means someone wrote policies but never turned RLS on, so they do nothing, and security_definer_view means a view in your public schema ignores RLS entirely.
2. The SQL editor. Open the SQL editor and run this. It asks the Postgres system catalogue for every table in the public schema where row security is false.
select schemaname, tablename
from pg_tables
where schemaname = 'public'
and rowsecurity = false;
An empty result is what you want. Anything that comes back is a table your users can currently read in full. Write the list down. You are going to fix them one at a time.
Turning it on
For each table in that list, enabling RLS is one line, in the form the Supabase documentation uses:
alter table public.invoices enable row level security;
Do this for every table that holds anything a user would not want a stranger to see, which in practice is every table except reference data like a list of countries. Then, before you refresh your app and panic, write the policies. The next section gives you the four that cover almost every SaaS.
The four policies every SaaS needs
The examples below follow the patterns in Supabase’s row level security guide. They assume a table with a user_id column that stores the owner’s ID, which is how almost every AI-generated schema is laid out. Two conventions in them are deliberate. Policies are written to authenticated, so the rule is never evaluated for anonymous visitors at all. And auth.uid() is wrapped as (select auth.uid()), which Supabase recommends so Postgres evaluates it once per query instead of once per row. On a big table that is the difference between fast and unusable.
1. Users can read their own rows
create policy "Users can view their own invoices"
on public.invoices for select
to authenticated
using ( (select auth.uid()) = user_id );
2. Users can create rows that belong to them
The with check clause stops a user from inserting a row with someone else’s ID in the owner column.
create policy "Users can create their own invoices"
on public.invoices for insert
to authenticated
with check ( (select auth.uid()) = user_id );
3. Users can update and delete their own rows
Update needs both halves: using so they can only reach their own rows, with check so they cannot reassign a row to another user.
create policy "Users can update their own invoices"
on public.invoices for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );
create policy "Users can delete their own invoices"
on public.invoices for delete
to authenticated
using ( (select auth.uid()) = user_id );
4. Public read, where you actually mean it
Some tables are supposed to be readable by everyone: published blog posts, a public pricing table, a directory. For those, grant read access explicitly and narrowly, with a condition that limits it to the rows you intend to publish.
create policy "Anyone can read published posts"
on public.posts for select
to anon, authenticated
using ( published = true );
Notice there is no insert, update or delete policy here. Reading is public; writing is not. That is the point.
A fifth, for the admin you will need soon
Almost every founder needs an admin view that sees everything. The wrong way is to hand the admin dashboard the secret key. The documented way is a claim in the user’s token that regular users cannot edit. Supabase keeps a field called app_metadata on each user that only the server can write, and exposes it in policies through auth.jwt(). Set role to admin there for your own account, then:
create policy "Admins can view all invoices"
on public.invoices for select
to authenticated
using ( (select auth.jwt() -> 'app_metadata' ->> 'role') = 'admin' );
Two cautions from the docs. Do not use user_metadata for this, because users can change their own user_metadata and would be able to make themselves admin. And a token is only refreshed periodically, so removing someone’s admin role takes effect when their session renews, not instantly. If you need finer-grained roles and permissions later, Supabase documents a fuller pattern using custom claims and role-based access control.
One more performance note from the same guide: put an index on every column your policies filter on, which usually means user_id. A policy without an index becomes a full table scan on every request.
Test it as somebody else
A policy you have not tested is a guess. You, the founder, have a special problem here: you are the only user, your account owns every row, and every policy passes for you by accident. You need a second identity.
The simple way. Sign up for your own app with a second email address in a private browser window. Try to open things that belong to the first account by changing IDs in the URL, or by calling the API from the browser console. You should see nothing, or an error. Then try the reverse. Do this for every table you enabled RLS on, not just the first one.
The precise way. In the SQL editor, you can impersonate a user for the length of one transaction, using the mechanism Supabase’s own test tooling uses. Replace the ID with a real user ID from your Authentication page. Everything inside the block runs as that user, and the rollback means nothing you do there sticks.
begin;
set local role authenticated;
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select count(*) from public.invoices;
rollback;
The count should match what that one user owns, not the whole table. Run it again with set local role anon; and no claim, and the count should be zero for anything you did not deliberately make public.
Supabase also supports writing these checks as repeatable database tests with pgTAP, which is what we set up for clients, so the policies get re-checked every time the schema changes. For launch week, the two manual methods above are enough.
The two keys, and where each may live
Supabase gives every project two kinds of API key, and the API keys documentation is unambiguous about them.
The publishable key, which older projects call the anon key, is designed to be public. It goes in browser code, mobile apps and scripts, and everything it can do is limited by your RLS policies. This is the key your AI tool put in your frontend, and that is fine.
The secret key, which older projects call service_role, has the bypassrls attribute. It ignores every policy you just wrote. It belongs on servers, in edge functions, in cron jobs and in admin tools that do their own authorisation. The docs say it must never reach a browser, even on localhost.
Here is the practical check. Legacy keys are long tokens starting with eyJ, and the newer ones start with sb_publishable_ or sb_secret_. Search your frontend source and your built JavaScript bundle for service_role, sb_secret_ and any eyJ string, and look at what each one is. If a secret key is anywhere a browser can download it, every policy in this article is already bypassed. Rotate the key in Settings, then API Keys, fix the code that leaked it first so the new one does not follow the same path, and remember that a key which was ever public should be treated as copied.
Two habits from the docs are worth adopting on day one. Use a separate secret key for each backend component so a leak only forces one rotation. And name your environment variables honestly: a public prefix like NEXT_PUBLIC_ on a secret key is an instruction to the bundler to ship it.
What RLS does not protect
Turning on RLS and writing policies covers your tables. It does not automatically cover everything attached to them, and the gaps are where the next incident lives.
Storage. Files in Supabase Storage are governed by their own policies on a table called storage.objects. By default, Storage does not allow any uploads to a bucket until you write a policy. A typical rule lets a signed-in user write only into a folder named after their own ID. Public buckets are public to everyone; if your “private” uploads live in a public bucket, no policy will save them. Supabase documents the patterns in its storage access control guide, and notes that the secret key bypasses storage policies entirely, just as it does for tables.
Edge functions. A function that runs with the caller’s token respects RLS. A function that runs with the secret key does not, and Supabase’s functions auth guide warns that such a function querying a shared table without filtering by the caller’s ID will happily return every user’s rows. If your AI tool generated edge functions, read each one and ask which key it uses and whether it filters.
Views. A database view bypasses RLS by default because it runs with the privileges of whoever created it, usually the project owner. On Postgres 15 and later the fix is to create the view with (security_invoker = true) so it obeys the policies of the tables underneath. The Security Advisor flags offenders as security_definer_view.
Security definer functions. Any function marked security definer runs with its creator’s privileges and ignores the caller’s identity. The docs’ rule is simple: never create one in an exposed schema, and if you need one, set an empty search_path and schema-qualify every name inside it.
Your own server. If part of your app runs on a server with the secret key, that part has no RLS and has to do its own checks. Every route that reads or writes data on a user’s behalf needs to verify the user and filter by their ID. This is the part of a review where we spend the most time, because it is the part the model wrote with the master key in hand.
If you are on Firebase instead
Bolt and several other builders default to Firebase rather than Supabase. The concept is the same and so is the failure mode. Firestore and the Realtime Database are governed by security rules, and a project created in test mode lets anyone read and write. Firebase’s own guidance is to update the rules before deploying, and it points out that a deployed app is publicly accessible whether or not you have announced it.
The rule that matches our “own rows only” policy keys the data path on the user’s ID:
service cloud.firestore {
match /databases/{database}/documents {
match /some_collection/{userId}/{document} {
allow read, write: if request.auth != null && request.auth.uid == userId
}
}
}
And the equivalent of RLS with no policies is the locked-mode default, allow read, write: if false; on every document. If your rules file contains if true anywhere, treat it exactly like a Supabase table with RLS off.
What to do this week
- Run the Security Advisor and the
pg_tablesquery. Write down every table that comes back. - Enable RLS on each one, then add the own-rows read, insert, update and delete policies. Add public-read only where you genuinely mean public.
- Add an index on
user_idfor every table you just touched. - Sign up as a second user and try to read the first user’s data. Then impersonate in the SQL editor and check the counts.
- Search your frontend and built bundle for a secret key. Rotate it if you find one.
- Open every edge function and every view and ask: which key, and does it filter?
- Write down what you did. The next engineer who touches this schema needs to know the policies are deliberate.
Most founders get through the first five in an afternoon. The last two take someone who reads code. If you would rather have the whole list run and handed back to you with the gaps closed, that is what our launch review does, and keeping it true as the schema grows is part of DevOps as a service. Not sure your policies are right? Send us the project and we will check them.
Supabase Row Level Security FAQs
What is row level security in Supabase? Row level security is a Postgres feature that lets the database decide, row by row, which user may read or change what. Supabase relies on it because your app talks to the database directly from the browser with a public key. With RLS on and a policy in place, a signed-in user only sees their own rows. With it off, anyone holding that public key can read and edit every row in the table.
How do I check if RLS is enabled on my Supabase tables?
Open Advisors, then Security Advisor, in your project dashboard. Any table in the public schema without RLS shows up as “Table publicly accessible”. Or run a query in the SQL editor against pg_tables filtering on schemaname = 'public' and rowsecurity = false. An empty result means every public table has RLS on.
Why do AI tools leave Supabase RLS off? Because the app works without it. The generated SQL creates the tables, the generated frontend queries them with the publishable key, and everything runs. Enabling RLS with no policies makes every query return nothing, which looks like a bug to the model, so the path of least resistance is to leave it off. Nothing breaks until someone other than you reads the database.
Is the Supabase anon key safe to expose?
Yes, by design, as long as RLS is on. The publishable key, formerly the anon key, is meant to live in browser and mobile code and is limited by your policies. The secret key, formerly service_role, bypasses every policy and must only ever run on a server. If a secret key has reached a browser bundle, rotate it today.
Does RLS protect Supabase storage and edge functions too?
Partly. Storage has its own policies on the storage.objects table, and uploads are blocked until you write them. Edge functions respect RLS only when they use the caller’s token; a function that uses the secret key bypasses RLS and must filter by user itself. Views also bypass RLS unless created with security_invoker, and the Security Advisor flags that case.
Have a product idea to talk through?
We'll show you how we'd approach it — no pressure, just a real conversation.
Book a Discovery Call