Supabase Security: Row Level Security Mistakes and How to Test Them
Security TestingHetz Team

Supabase Security: Row Level Security Mistakes and How to Test Them

How to secure a Supabase app: row level security done right, the policy mistakes that leak data, service keys, storage and functions, and how to test it as an attacker.

Supabase gives you a Postgres database, authentication, storage and functions, with a client library that lets the browser query the database directly. That is why it is the backend behind so many SaaS products and AI-built apps. It is also why its security model is different from a classic backend: there is often no server code between the user and your data. The rules that protect your data are your row level security policies.

This guide explains how that model works, the mistakes that leak data most often, how to handle keys, storage and functions, and how to test everything the way an attacker would. If your app was generated with Lovable, Bolt or a similar builder, read AI app builder security alongside this one.

How Supabase Security Works

A Supabase frontend holds two values: your project URL and the anon (or publishable) key. Both are public. Anyone can open your site, copy them and talk to your database with the same rights as your app.

What stops them from reading everything is Postgres row level security. With RLS enabled on a table, every query is filtered by the policies you wrote. Without RLS, or with a policy that allows everything, the public key can read and often change the whole table.

ComponentPublic?What protects it
Project URLYesNothing needed
Anon or publishable keyYesRLS policies on every table
Service role or secret keyNeverKeep it on the server only
Tables in exposed schemasReachableRLS enabled plus correct policies
Storage bucketsReachableBucket privacy plus storage policies
Database functionsCallableSecurity definer choices, grants and checks inside

The Most Common RLS Mistakes

1. RLS is off on a table

New tables created through SQL do not always have RLS enabled. A single table without it, say a "profiles" or "orders" table added late, is readable by anyone with the public key. Supabase's security advisor flags this, and it is the first thing to fix.

2. Policies that check login, not ownership

A policy like "authenticated users can select" looks safe but means every signed-in user can read every row. On a public sign-up app, that is everyone. The policy should compare the row's owner with the current user, typically auth.uid() = user_id.

3. Update policies that allow changing ownership

A user may update their own rows, but can they set user_id to someone else, or set role to admin? Without a check on the new values, a user can move rows between accounts or promote themselves. Use both the condition on existing rows and a with check condition on the new values, and keep sensitive columns such as role or plan out of user-writable tables.

4. Multi-tenant tables without a tenant check

In a team-based app, rows belong to an organization, not a user. Policies need to check that the current user is a member of the row's organization. A policy that checks only that the organization id is present, or trusts an organization id from the client, lets users read other organizations' data.

5. Insert policies that trust client values

If users can insert rows with any user_id, they can create data in other people's accounts. Set ownership columns from auth.uid() with a default, and check it in the insert policy.

6. Views and functions that bypass RLS

Views in Postgres run with the rights of their owner unless configured otherwise, and functions declared security definer run with elevated rights. Both can quietly bypass the policies on the tables they read. Use security invoker views where possible, and inside any security definer function, check the caller's rights yourself.

7. Realtime and RPC left out of the review

Realtime subscriptions and RPC calls are easy to forget because they do not look like table queries. Realtime respects RLS on the tables it streams, so a loose select policy leaks live changes too, not just stored rows. Functions in the public schema are callable through the API by default. Review which roles hold execute rights on each one, revoke what the browser never needs, and treat every exposed function as a public endpoint that must check who is calling.

Keys and Secrets

The service role key bypasses RLS entirely. It belongs in server code, edge functions and scripts, never in the browser bundle, a mobile app or a public repository. If it has ever been exposed, rotate it.

Other secrets follow the same rule: payment provider secret keys, AI provider API keys and webhook signing secrets go in server-side environment variables. Search your deployed JavaScript for "service_role", "sk_live" and similar prefixes to be sure.

Storage

Supabase storage has buckets, which can be public or private, and policies on the storage objects table. Common mistakes:

  • A bucket for user documents set to public, so anyone with the URL can download files.
  • Predictable file paths, such as the user id plus "invoice.pdf", combined with a public bucket.
  • Storage policies that allow any signed-in user to read any object in the bucket.

Keep sensitive buckets private, put each user's files under a folder named by their id, write policies that check that folder, and share files through short-lived signed URLs.

Edge Functions and APIs

Edge functions often use the service role key to do privileged work. That makes them powerful and dangerous: the function itself must check who is calling and whether they are allowed to do what they ask. A function that takes a user id from the request body and acts on it, instead of reading the user from the verified token, is a classic access control bug. Our guide to IDOR and broken access control shows the pattern.

A Supabase Security Checklist

CheckHow to verify
RLS enabled on every table in exposed schemasSecurity advisor and table list
Every policy checks ownership or membershipRead each policy; test with two users
Update and insert policies use with checkTry to change user_id or role as a user
Sensitive columns not user-writableTry updating role, plan or credits
No service role key in the frontendSearch the deployed bundle
Views use security invokerInspect view options
Security definer functions check the callerCall them as another user
Sensitive buckets private, policies per userTry to download another user's file
Auth settings hardenedEmail confirmation, rate limits, MFA where needed

How to Test Supabase Security Like an Attacker

Reading policies is necessary but not enough, because the question is not "does a policy exist" but "can user B reach user A's data". Test it directly:

  1. Create two users, A and B, and give each some data in every feature.
  2. Sign in as B using the public URL and key, with the Supabase client or plain HTTP requests.
  3. For every table, try to select A's rows, insert rows as A, update A's rows and delete them.
  4. Try to update your own rows to change ownership, role or plan.
  5. Call every database function and edge function with A's identifiers.
  6. Try to list and download A's files from storage.
  7. Repeat as a logged-out visitor.

Doing this by hand for every table is slow, which is why it is often skipped. Hetz automates it: agents sign in as your test users, discover the tables, functions and buckets your app touches, and try every cross-user path, keeping only what they can prove. Each finding comes with the exact policy fix and a prompt you can paste into your editor or app builder.

Fixing Common Problems

For a typical user-owned table, the fix is: enable RLS, then add select, insert, update and delete policies that compare the owner column with auth.uid(), with a with check on insert and update. For team data, replace the owner check with a membership check against your members table. Move roles, plans and credits into tables users cannot write, updated only by server code.

After each fix, run the same two-user test again. A fix is real when the attack fails.

When to Test

Test before launch, whenever you add a table or feature, and before a customer's security review. Supabase projects grow quickly, and each new table is a new chance to forget RLS. A regular automated test catches that the same week. For the bigger picture, see SaaS security testing, and for budgeting, penetration testing cost.

Next Steps

Open the security advisor and fix every table without RLS today. Then test with two users. When you want the whole app tested end to end, see how a run works on our home page, check pricing, or start a scan.

FAQ

Frequently asked questions

Keep reading