Security and shipping

Supabase RLS: the mistake that exposes your users' data

Published Updated 7 min read

Read in : Français

Contents
  1. Why doesn't the Supabase public key protect anything?
  2. What is the mistake that exposes the data?
  3. How can you tell in 2 minutes whether your database is exposed?
  4. Step 1: the Security Advisor
  5. Step 2: the public key test
  6. Which RLS policies should you write for a user table?
  7. Which RLS traps does the AI miss?
  8. Which prompt fixes RLS without breaking everything?
  9. What if the app needs to access all the data?
  10. Frequently asked questions
  11. Frequently asked questions
  12. Sources

Supabase RLS (Row Level Security) is the only thing standing between your users' data and the rest of the internet. Your app talks to the database straight from the browser, using a key anyone can read. If a table has no RLS policies, or policies that are too broad, a stranger can read it with a single request. Here is how to check, fix and test.

Why doesn't the Supabase public key protect anything?

The Supabase public key is not a password; it is a visitor badge. The Supabase docs describe the publishable key (sb_publishable_..., or the older anon key) as "safe to expose online", precisely because what it can do is limited by RLS.

In practice, an app generated by Lovable or Bolt ships your project URL and that key inside its JavaScript. Any visitor can copy both from the Network tab of their browser and query your database's REST API directly, without going through your interface.

What decides the response:

State of the tableWhat a stranger gets with the public key
RLS disabledRead and write access to every row, depending on the grants on the role
RLS enabled, no policiesNothing: everything is denied by default
RLS enabled, using (true) policy for anonEvery row, read-only
RLS enabled, policy based on auth.uid()Only their own rows, if signed in

The Supabase docs are explicit about the second case: once RLS is enabled, no data is accessible through the API with the public key until you create policies.

What is the mistake that exposes the data?

The most common mistake is not forgetting RLS; it is working around it to make the app run. The pattern repeats itself:

  1. You enable RLS (or the tool does).
  2. The app shows nothing, because everything is denied by default.
  3. You ask the AI to fix it. It writes a using (true) policy, disables RLS, or switches to the secret key.
  4. The app works again. The data is open to everyone.

That is the kind of flaw described in CVE-2025-48757, rated 9.3 out of 10: insufficient RLS policies in sites generated by Lovable up to 15 April 2025 let unauthenticated attackers read or write database tables. Lovable disputes it, pointing out that each customer is responsible for protecting their app's data. Either way, the takeaway for you is the same: checking is your job.

How can you tell in 2 minutes whether your database is exposed?

The fastest route is the Supabase Security Advisor, followed by a manual test to confirm.

Step 1: the Security Advisor

In the Supabase dashboard, open Advisors, then Security Advisor. The alerts to handle first:

Step 2: the public key test

Grab your project URL and public key from your site's Network tab (F12), then run this in a terminal:

curl "https://YOUR-PROJECT.supabase.co/rest/v1/profiles?select=*" \
  -H "apikey: YOUR_PUBLIC_KEY"

Swap profiles for each of your tables. A private table should return [] or an error. If you see emails, names or orders, the table is open.

Which RLS policies should you write for a user table?

The baseline rule: one policy per operation, each restricted to the authenticated role and to rows owned by the user. Here is a template for a notes table with a user_id column:

-- 1. Enable RLS
alter table public.notes enable row level security;

-- 2. Read your own notes
create policy "Read own notes"
on public.notes for select
to authenticated
using ( (select auth.uid()) = user_id );

-- 3. Create notes in your own name only
create policy "Create own notes"
on public.notes for insert
to authenticated
with check ( (select auth.uid()) = user_id );

-- 4. Update your own notes
create policy "Update own notes"
on public.notes for update
to authenticated
using ( (select auth.uid()) = user_id )
with check ( (select auth.uid()) = user_id );

-- 5. Delete your own notes
create policy "Delete own notes"
on public.notes for delete
to authenticated
using ( (select auth.uid()) = user_id );

-- 6. Index for performance
create index on public.notes (user_id);

Three details from the Supabase docs:

Which RLS traps does the AI miss?

Even with RLS enabled everywhere, five mistakes keep showing up in generated apps.

  1. **The catch-all for all policy.** Supabase advises against it: a single policy for every operation makes mistakes hard to spot. Write one per operation.
  2. **Roles stored in user_metadata.** A signed-in user can edit their own user_metadata. If your admin policy checks user_metadata.role = 'admin', anyone can promote themselves. Use app_metadata, which only the server can change.
  3. Views that bypass everything. By default a view runs with its creator's privileges. On Postgres 15 and later, create it with (security_invoker = true).
  4. **Exposed security definer functions.** These functions run with elevated privileges. They should set search_path = '' and live outside any schema exposed by the API.
  5. **The silent null.** For a signed-out visitor, auth.uid() returns null, and null = user_id is never true. That is safe, but if you write more complex conditions with or, add auth.uid() is not null explicitly.

One last technical point: grants and RLS policies are two separate layers. The Supabase docs note that on existing projects, new tables in the public schema grant all privileges to the anon and authenticated roles, and adding policies does not remove those grants. RLS remains your main protection; tightening grants is a second belt.

Which prompt fixes RLS without breaking everything?

A good prompt explicitly forbids the dangerous shortcuts. Paste this into Lovable, Bolt, Cursor or Claude Code:

Audit this project's Supabase security, table by table.
For each table in the public schema:
1. Say whether RLS is enabled and list existing policies.
2. Propose a SQL migration that enables RLS and creates one policy per
   operation (select, insert, update, delete), restricted to the
   authenticated role and to (select auth.uid()) = user_id, unless the
   table holds explicitly public data: ask me which ones.
3. Add an index on every column used in the policies.

Do not:
- ever disable RLS;
- use using (true) on any table holding user data;
- ever use the service_role key or any secret key on the client;
- store any role in user_metadata.

If a feature breaks after the migration, explain why and propose a
precise policy, not a workaround.

After the migration, rerun the curl test on every table, then the two-account test: sign in as user A, create a record, and check that user B cannot see it. Those are the two core checks in our pre-launch security checklist.

What if the app needs to access all the data?

Some operations genuinely need every row: an admin dashboard, an export, a scheduled job. The fix is not to loosen the policies but to move the operation to the server.

Server-side key handling is covered in API keys: why they must never live in your front end, and user accounts in authentication without security holes. If you are starting with Supabase from scratch, see Supabase for beginners; if you build with Lovable, our full Lovable review covers its Supabase integration. And to check the rest of the generated code without being a developer: testing AI-generated code.

Frequently asked questions

Frequently asked questions

Is Supabase RLS enabled by default?

Never assume it is. Depending on how a table was created (table editor, SQL query, AI-generated migration), RLS may or may not be on. Open the Supabase Security Advisor, which flags every table in the public schema without RLS with the rls_disabled_in_public alert, and check table by table. A table that holds user data and has RLS off is readable and writable through your public API.

Why does my app show nothing after I enabled RLS?

Because a table with RLS but no policies denies everything by default, which is the intended behaviour. You now need policies that allow exactly what is required, for example reading your own rows with auth.uid() = user_id. Do not just ask the AI to "unblock" the app: it may disable RLS or write a using (true) policy, which reopens the data to everyone.

Is the anon key in my code a vulnerability?

No. The public key (anon or publishable) is designed to be visible in the browser. It only allows what your RLS policies permit. The vulnerability is a table without RLS, or with a policy that is too broad. The key you must never expose is the secret key (service_role or sb_secret), which bypasses every policy. If it sits in your front end, rotate it now.

Does RLS slow down queries?

It can on large tables if policies are badly written. The Supabase docs recommend two habits: wrap functions like auth.uid() in (select ...) so they are evaluated once per statement, and add an index on every column used in your policies, such as user_id. For an app with a few thousand rows, the impact is negligible.

Do Lovable or Bolt set up RLS correctly?

They often create policies, and Lovable checks for tables without RLS in its pre-publish scan. But the tool does not know your intentions: it cannot tell which data should be public or private. CVE-2025-48757 shows that generated apps have shipped with insufficient policies. Always confirm with the Security Advisor and a curl test using the public key.

Sources