Nexus Void Research

Supabase, Row Level Security, PostgreSQL, RLS Policies, service_role key, Vibe Coding Security, How-to

How to Secure a Supabase Database: RLS, Keys and Storage

How to secure a Supabase database: enable RLS on every table, write per-operation policies, keep the service_role key server-side, and test with the anon key.

To secure a Supabase database, enable Row Level Security (RLS) on every table in an exposed schema and write policies that tie each row to auth.uid(), because the anon (publishable) key in your frontend can query any table without RLS. Keep the service_role (secret) key on the server only, make views security_invoker, lock down security definer functions and storage buckets, and test with the anon key as an outsider would.

Supabase gives every project an auto-generated REST and GraphQL API on top of PostgreSQL. That is what makes it fast to build with, and it is also why misconfigured Supabase projects leak data: the key that ships in your JavaScript bundle is meant to be public, so the only thing standing between a stranger and your tables is RLS. This guide walks through the settings that matter, with SQL you can paste into the SQL editor. The underlying principle is the one in the OWASP Authorization Cheat Sheet: deny by default and check access on every request. Missing checks of this kind are tracked by MITRE as CWE-862: Missing Authorization.

Why is Supabase security mostly about RLS?

Your frontend talks to the database directly, using the project URL and the anon key (shown as a publishable key, sb_publishable_..., in newer projects). Anyone can copy both from your site. With them they can call:

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

If profiles has RLS disabled, that returns every row. If RLS is enabled with no policies, it returns nothing. If RLS is enabled with good policies, it returns only what the caller is allowed to see. Supabase's own Row Level Security guide is built on the PostgreSQL feature of the same name, described in the PostgreSQL documentation.

Step 1: Enable RLS on every exposed table

Tables you create in the Table Editor have RLS on by default. Tables created with SQL, migrations or some tools may not. Turn it on explicitly:

alter table public.profiles enable row level security;
alter table public.profiles enable row level security;
alter table public.profiles enable row level security;

Find any table in the public schema that still has it off:

select tablename
from pg_tables
where schemaname = 'public' and rowsecurity = false;
select tablename
from pg_tables
where schemaname = 'public' and rowsecurity = false;
select tablename
from pg_tables
where schemaname = 'public' and rowsecurity = false;

The Security Advisor in the Supabase dashboard also flags tables without RLS. Make "zero results" part of your release checklist.

Step 2: Write policies for each operation

RLS with no policies blocks everything. Add a policy per operation, tied to the logged-in user:

-- Users can read their own profile
create policy "Read own profile"
on public.profiles for select
to authenticated
using ((select auth.uid()) = user_id);

-- Users can create a profile only for themselves
create policy "Insert own profile"
on public.profiles for insert
to authenticated
with check ((select auth.uid()) = user_id);

-- Users can update only their own profile, and cannot reassign it
create policy "Update own profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);
-- Users can read their own profile
create policy "Read own profile"
on public.profiles for select
to authenticated
using ((select auth.uid()) = user_id);

-- Users can create a profile only for themselves
create policy "Insert own profile"
on public.profiles for insert
to authenticated
with check ((select auth.uid()) = user_id);

-- Users can update only their own profile, and cannot reassign it
create policy "Update own profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);
-- Users can read their own profile
create policy "Read own profile"
on public.profiles for select
to authenticated
using ((select auth.uid()) = user_id);

-- Users can create a profile only for themselves
create policy "Insert own profile"
on public.profiles for insert
to authenticated
with check ((select auth.uid()) = user_id);

-- Users can update only their own profile, and cannot reassign it
create policy "Update own profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = user_id)
with check ((select auth.uid()) = user_id);

Three details that catch people out:

  • using filters which existing rows a user can see or change; with check validates the new values on insert and update. Without with check on update, a user could change user_id to hand the row to someone else.

  • Wrapping auth.uid() in (select ...) lets Postgres evaluate it once per query instead of once per row, which Supabase recommends for performance.

  • Specify the role (to authenticated) so anonymous visitors do not match policies meant for logged-in users.

For team or organisation data, check membership through a join table, the same object-level check described in how to prevent IDOR.

Step 3: Keep the service_role key on the server

The service_role key (a secret key, sb_secret_..., in newer projects) bypasses RLS completely. It belongs only in trusted server code: Edge Functions, your backend, scheduled jobs.

  • Never put it in frontend code, mobile apps or any NEXT_PUBLIC_ or VITE_ variable.

  • Store it in your platform's secrets or supabase secrets set for Edge Functions.

  • If it has ever been in a client bundle or a public repository, rotate it now.

See how to store API keys securely for the general pattern.

Step 4: Make views and functions respect RLS

Two PostgreSQL features can quietly bypass your policies:

Views run with the permissions of their owner by default, which usually bypasses RLS. On Postgres 15 and later, make them use the caller's permissions:

create view public.my_orders
with (security_invoker = true) as
select id, total, created_at from public.orders;
create view public.my_orders
with (security_invoker = true) as
select id, total, created_at from public.orders;
create view public.my_orders
with (security_invoker = true) as
select id, total, created_at from public.orders;

security definer functions run as their owner and bypass RLS. Use them only when you need to, keep them out of exposed schemas, set a fixed search path, and check the caller inside:

create or replace function private.close_account()
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
  delete from public.profiles where user_id = (select auth.uid());
end;
$$;
create or replace function private.close_account()
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
  delete from public.profiles where user_id = (select auth.uid());
end;
$$;
create or replace function private.close_account()
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
  delete from public.profiles where user_id = (select auth.uid());
end;
$$;

Revoke execute on sensitive functions from anon and authenticated unless they are meant to be called from the client.

Step 5: Secure Storage buckets

Storage uses RLS on storage.objects. Keep buckets private unless files are genuinely public, and scope access to a folder per user:

create policy "Users read own files"
on storage.objects for select
to authenticated
using (
  bucket_id = 'documents'
  and (storage.foldername(name))[1] = (select auth.uid())::text
);
create policy "Users read own files"
on storage.objects for select
to authenticated
using (
  bucket_id = 'documents'
  and (storage.foldername(name))[1] = (select auth.uid())::text
);
create policy "Users read own files"
on storage.objects for select
to authenticated
using (
  bucket_id = 'documents'
  and (storage.foldername(name))[1] = (select auth.uid())::text
);

Add matching insert, update and delete policies, and serve private files through short-lived signed URLs.

Step 6: Harden Auth settings

In the Auth settings:

  • Require email confirmation before an account can sign in.

  • Restrict redirect URLs to your real domains, so magic links and OAuth flows cannot be sent elsewhere.

  • Turn on MFA for your users where the data justifies it, and for every dashboard member.

  • Review rate limits for sign-ups, OTPs and password resets.

  • Disable providers you do not use.

Step 7: Lock down Edge Functions and the project

  • Verify the JWT in Edge Functions that act for a user, and check authorization inside, not just authentication.

  • Validate input in every function and RPC, exactly as you would in any API.

  • Enforce SSL for database connections and use network restrictions to limit direct Postgres access to known IPs.

  • Enable backups or point-in-time recovery so a bad policy or a breach is recoverable.

  • Restrict dashboard access and remove former team members.

Step 8: Test like an attacker

Using only the anon key and a normal user's session, try to:

  1. Read every table with select=*.

  2. Read another user's rows by changing filters.

  3. Insert a row with someone else's user_id.

  4. Update your row's user_id or role.

  5. Call every RPC function exposed in public.

  6. Download files from another user's storage folder.

Every one should fail. Automate the important ones as tests, and add RLS to your security code review checklist for every migration.

Supabase security checklist

Check

Done when

RLS everywhere

No public table returns rowsecurity = false

Policies per operation

select, insert, update and delete covered; with check on writes

service_role key server-only

Not in any client bundle, env var prefix or repository

Views use security_invoker

No view silently bypasses RLS

Definer functions locked

Not in exposed schemas; fixed search_path; execute revoked

Storage private

Buckets private; per-user folder policies; signed URLs

Auth hardened

Email confirmation, redirect allow-list, MFA for admins

Tested with the anon key

Cross-user reads and writes all fail

How does ARGUS help secure Supabase apps?

  1. Secret scanning catches a service_role or secret key committed to your repository, with vendor patterns, entropy and named-assignment checks, and marks it Live, Rotated or Test when AI scanning is on.

  2. AI business logic analysis looks for IDOR, privilege escalation and authentication bypass in your Python, JavaScript, TypeScript and Ruby code, including server code that queries Supabase with elevated keys.

  3. API inventory lists the routes your own backend defines and marks each Authenticated, Open or Not determined.

  4. Pull request review comments on risky changes before merge, and Assign to Agent opens a fix pull request for you to review and merge.

Limits: ARGUS analyses your repository's code. It does not log in to your Supabase project or read your live RLS policies, so run the anon-key tests above as well. See ARGUS secret scanning, or scan a repository free.

Frequently asked questions

How do I secure a Supabase database?

Enable Row Level Security on every table in exposed schemas, write policies for select, insert, update and delete that check auth.uid(), keep the service_role key on the server only, make views security_invoker, lock down security definer functions and storage, harden Auth settings, and test with the anon key.

Is the Supabase anon key safe to expose?

Yes, it is designed to be public, like a publishable key, but only if Row Level Security is enabled with correct policies on every exposed table. Without RLS, anyone with the anon key and your project URL can read and write those tables through the auto-generated API.

What happens if I expose the Supabase service_role key?

Anyone with it can bypass Row Level Security and read, change or delete any data in your project. Rotate it immediately in the dashboard, deploy the new key to your server-side code, check logs for unusual access, and remove it from your client code and repository history.

Is RLS enabled by default in Supabase?

Tables created in the dashboard's Table Editor have RLS enabled by default, but tables created with SQL, migrations or some tools may not. Check with a query on pg_tables for rowsecurity = false in the public schema, and use the dashboard's Security Advisor.

Do Supabase views bypass RLS?

By default, yes: views run with their owner's permissions, which usually bypasses the caller's policies. On Postgres 15 and later, create views with (security_invoker = true) so they apply the querying user's RLS policies, or keep views out of exposed schemas.

How do I test Supabase Row Level Security?

Use only the anon key and a normal user's session to try reading every table, reading another user's rows, inserting rows with someone else's ID, changing ownership fields, calling exposed RPC functions and downloading other users' files. Every attempt should fail. Automate these checks as tests.

How do I secure Supabase Storage?

Keep buckets private unless files are meant to be public, add RLS policies on storage.objects that restrict each user to their own folder, cover select, insert, update and delete, and give users time-limited access with signed URLs instead of public links.

Should I use security definer functions in Supabase?

Only when a function genuinely needs elevated rights. They bypass RLS, so keep them out of exposed schemas, set search_path = '', check the caller inside the function, and revoke execute from anon and authenticated unless the client must call them.

DATA SOURCES

Supabase RLS guide: https://supabase.com/docs/guides/database/postgres/row-level-security ; PostgreSQL row security: https://www.postgresql.org/docs/current/ddl-rowsecurity.html ; OWASP Authorization Cheat Sheet ; CWE-862

Liked this post? Share it: