Supabase Row Level Security explained: why a public key can still be safe
The key in your app is public
Every Supabase app ships with a key. In the current docs it is called the publishable key (prefixed sb_publishable_); older projects call it the anon key. Supabase describes it as safe to expose online: in a web page, a mobile app or source code.
That description is accurate, but it leaves a question. If anyone can read the key, what can they do with it?
The answer is whatever Row Level Security (RLS) allows. Requests made with the publishable key are mapped to one of two Postgres roles: anon for requests without a signed-in user and authenticated for requests with one. Postgres then checks every row against the policies written for that role. The key is not the security boundary. The table is.
What happens when RLS is off
Supabase's documentation states it directly: a table in an exposed schema without RLS is readable and writable by any role with a grant on it. On some projects, depending on when they were created and their Data API settings, a new table in the public schema starts with privileges already granted to the anon and authenticated roles.
Put those together and an orders table without RLS can be read in full with a single request to the REST endpoint, using the key taken from the app. There is no exploit involved. The API does what it was configured to do.
Turning RLS on changes the default. PostgreSQL calls it default deny: when row security is enabled and no policy exists, no rows are visible and none can be modified. The same request returns an empty list.
Tables created in the Supabase Dashboard have RLS enabled by default. Tables created with plain SQL follow Postgres behaviour, where row security is off until ALTER TABLE ... ENABLE ROW LEVEL SECURITY is run.
A policy is a WHERE clause
Once RLS is on, access is granted with policies. Supabase's own framing is the clearest one: a policy works like a WHERE clause added to every query on the table.
create policy "Customers see their own orders"
on orders for select
to authenticated
using ( (select auth.uid()) = customer_id );With this policy, a query for all orders becomes, inside the database, a query for all orders where the customer is the person asking. The app code stays the same. The result depends on who sent the request.
Policies are permissive by default, and permissive policies on the same table are combined with OR. If any one of them allows a row, the row is visible. Restrictive policies exist as well and are combined with AND, which makes them useful for rules that must always hold.
Where auth.uid() comes from
auth.uid() returns the ID of the user making the request. When someone signs in, Supabase issues a JWT; the client sends it with each request, and auth.uid() reads the user ID from it.
For a request without a signed-in user, auth.uid() returns null. Supabase recommends checking for this explicitly in policies, for example auth.uid() is not null and auth.uid() = user_id. Adding to authenticated to the policy also limits it to signed-in requests.
Two performance notes from the same docs. Wrapping the call as (select auth.uid()) lets Postgres evaluate it once per statement instead of once per row. And every column a policy filters on should be indexed, because the policy runs against each candidate row.
USING and WITH CHECK
A policy can have two clauses, and they answer different questions.
USING applies to rows that already exist. It decides which rows a select can return and which rows an update or delete can target.
WITH CHECK applies to the new version of a row, on insert and update. It decides what a row is allowed to look like after the write.
An update policy without WITH CHECK lets a user find their own order (allowed by USING) and then change its customer_id to someone else's. Supabase notes that the WITH CHECK clause is what prevents a user reassigning user_id to another person. The docs also note that an update requires a matching select policy.
One policy per operation
A table usually ends up with a small set of policies, one for each operation that should be allowed. For an orders table owned by customers, that might be a select policy with USING (auth.uid() = customer_id), an insert policy with WITH CHECK (auth.uid() = customer_id), and an update policy with both clauses. If customers should never delete orders, there is simply no delete policy, and default deny covers it.
Inserts rely entirely on WITH CHECK, because there is no existing row for USING to examine. An insert policy without a check on ownership lets any signed-in user create rows with any customer ID.
The secret key skips all of it
The second Supabase key is the secret key (prefixed sb_secret_, formerly the service role key). It authorises requests as the service_role Postgres role, which has the BYPASSRLS attribute. Every policy is ignored.
That is intentional. Servers, scheduled jobs and admin tools often need to read everything. It also means the secret key cannot appear anywhere in a client app, including in Expo environment variables prefixed EXPO_PUBLIC_, which Expo's docs describe as visible in plain text in the compiled app.
Grants and policies are separate layers
Postgres checks two things. Table privileges, or grants, decide whether a role can select, insert, update or delete on a table at all. RLS policies then decide which rows. Supabase's documentation notes that adding policies does not remove grants already in place, so the two are managed independently.
When the rule belongs in server code
RLS is not the only place a permission rule can live. In my own setup, several apps share one Supabase database and a Node server. The customer side relies on RLS. On the rider side, the rules live in server code: the rider app calls the Node server, the server uses the service role, and the server decides which orders a rider can see.
The reason was that rider permissions depend on a lot of state, such as assignment and timing, and expressing that as SQL policies became hard to read. As a function on the server it is easier to read and test.
The cost is that the server now holds a key that bypasses RLS, and every route has to perform its own checks. If one route skips the check, there is no default deny underneath it. Both approaches work. What matters is knowing which one each table relies on.
Summary
The publishable key is public by design. RLS on means default deny. Policies behave like WHERE clauses, USING governs existing rows, WITH CHECK governs written rows, and the secret key bypasses all of it, so it stays on the server.