Is the Supabase anon (publishable) key safe to expose?
#supabase#rls#secrets#next.js
Yes — the Supabase anon key is meant to be public and is safe to ship in your frontend, but only because Row Level Security is supposed to gate it. The moment one table has RLS off, the anon key reads that whole table. Safety comes from RLS, not from hiding the key.
This question comes up constantly because the anon key looks like a secret. It's a long eyJ... JWT, it sits in your .env, and every instinct says "don't commit that." But Supabase's own docs are blunt about it: the anon key is safe to expose in a browser if you have RLS enabled. It is designed to be embedded in client code. What actually protects your data is whether your policies are correct — not whether the key is visible.
So the honest answer is "yes, if." Let's make the if precise.
Is the Supabase anon key a secret?
No. The anon key is a public identifier that tells Supabase which project you're talking to and that the caller is unauthenticated (or carries a user's JWT). It grants exactly the access your RLS policies allow for the anon and authenticated roles — nothing more. That's why it's fine in your Next.js bundle, in a mobile app, or in a public repo.
Contrast it with the service_role key, which bypasses RLS entirely. That one is a secret, and shipping it to the browser is a full database compromise. The two keys look almost identical, which is exactly why they get confused — but their blast radius could not be more different.
| anon key | service_role key | |
|---|---|---|
| Meant to be public? | Yes | No — server only |
| Respects RLS? | Yes | No — bypasses every policy |
| Safe in the browser bundle? | Yes, with RLS on | Never |
| Blast radius if leaked | Limited by your policies | Your entire database |
When the anon key stops being safe
The anon key inherits every gap in your RLS. Here's the single failure that turns "public by design" into "public breach": a table with RLS turned off.
-- "Make the profiles table just work from the client"
create table public.profiles (
id uuid primary key references auth.users,
email text,
stripe_customer_id text
);
alter table public.profiles disable row level security;
With that one disable row level security, the profiles table — emails, Stripe customer IDs, everything — is readable and writable by anyone who opens devtools, copies your anon key, and hits the auto-generated REST endpoint. No login required. This is precisely the misconfiguration behind CVE-2025-48757, where a scan of AI-built (Lovable) apps found 303 endpoints across 170 projects leaking data through the public anon key. Here's what GuardLayer reports on exactly that migration — live engine output, not a mockup:
- Criticalsupabase/migrations/0001_profiles.sql:8
Row Level Security disabled
Re-enable RLS (ALTER TABLE <t> ENABLE ROW LEVEL SECURITY;) and add policies that scope access with auth.uid(). - Warningsupabase/migrations/0001_profiles.sql:2
Table created without enabling RLS
Add ALTER TABLE <table> ENABLE ROW LEVEL SECURITY; plus access policies right after the CREATE TABLE, and grant anon / authenticated only the privileges the app needs, per table, after RLS is on. A later migration in the same scan that enables RLS (or drops the table) clears this warning. It still shows when the enable is in a file whose run order is unknown (a schema.sql with no version number) or not part of this scan (a pull-request scan sees only the files the PR changes).
In May 2025, security researcher Matt Palmer scanned a batch of AI-generated Supabase apps and found 170 projects exposing data — 303 endpoints — to any caller holding the public anon key, all because RLS was never enabled. The key wasn't the problem. The missing policy was. (CVE-2025-48757)
The same is true of subtler mistakes: a table you forgot to enable RLS on, or a policy that's technically "on" but written as USING (true). In every case the anon key faithfully returns whatever your policies permit. If the policy is wide open, so is the key.
RLS on isn't enough: two policies that still leak
Turning RLS on is the start, not the finish. The anon key gets whatever your policies allow, and two common policies allow far more than they look like they do. Researchers who looked at Lovable apps in November 2026 found email addresses readable on 7,403 of them. The report blames access-control mistakes, and an open policy leaks exactly like a table with RLS off.
1. "Any signed-in user" policies with open sign-ups. A policy like this reads as protected:
create policy "Signed-in users can read"
on public.customers for select
to authenticated
using (true);
It only checks that the caller has a session. If anyone can sign up (the default), or anonymous sign-ins are on, getting a session takes seconds, and then every row is readable with your public key. Scope it to the row's owner instead:
using ( (select auth.uid()) = user_id )
2. Users updating their own row, including the columns that matter. RLS decides which rows a user can touch, not which columns. A policy that lets users update their own profile also lets them set role = 'admin', plan = 'pro' or credits = 999999 on it, straight from the browser. Restrict the columns with grants:
revoke update on public.profiles from authenticated, anon;
grant update (display_name, avatar_url) on public.profiles to authenticated;
Keep anything that grants access or money (roles, plans, credits) in columns only your server or a webhook can write, or in app_metadata, which users can't change (user_metadata vs app_metadata).
What about the new publishable and secret keys?
In mid-2025 Supabase introduced a new key format — a sb_publishable_... key that replaces the anon key, and sb_secret_... keys that replace service_role. Both key types work side by side today; Supabase has announced that the legacy anon and service_role JWTs will be phased out and removed in late 2026 (the exact date is still to be confirmed), with no action required before then. The security model doesn't change: the publishable key is still safe to expose and still gated entirely by RLS; the secret key is still server-only. The main win is rotation — a leaked secret key can now be revoked in seconds without invalidating every signed-in user's session, which was the painful part of rotating the old service_role key. If you're on a new project, you're already using these; on an older one, migrate before the deprecation deadline. The full breakdown of publishable vs secret keys covers the migration steps and the one mistake that turns a routine key swap into a breach.
How to check you're actually safe
The anon key being in your bundle is fine. What you need to verify is that RLS backs it up:
-- Any table in the public schema with RLS OFF is exposed to the anon key.
select tablename
from pg_tables
where schemaname = 'public'
and rowsecurity = false;
If that query returns any row, the anon key can reach that table unauthenticated. Enable RLS and add a policy scoped with auth.uid():
alter table public.profiles enable row level security;
create policy "Users read own profile"
on public.profiles for select
to authenticated
using ( (select auth.uid()) = id );
Then list the policies that only check for a session, the first leak above:
select tablename, policyname, cmd, roles, qual
from pg_policies
where schemaname = 'public'
and (qual = 'true' or qual ~* 'auth\.uid\(\).*is not null');
Then confirm no service_role key is anywhere near your client — that's the key that genuinely must stay hidden.
To check all of it at once on a live project, connect it read-only: GuardLayer reads your RLS settings, policies, grants and Auth settings together (never your rows), so it knows whether "any signed-in user" means anyone on your project, and it flags columns users can change on themselves. Each finding comes with the SQL fix. On every push, it also flags a disabled-RLS table, a missing policy, and an exposed service_role key in your code.
FAQ
Is it safe to expose the Supabase anon key in frontend code? Yes. The anon key is public by design and is meant to be embedded in client-side code. Its access is constrained entirely by your RLS policies, so it's only as safe as your policies are correct. Enable RLS on every public table before relying on it.
Is the anon key safe if RLS is enabled?
Only if the policies are right. RLS with a policy like using (true), or one that only requires a signed-in user while anyone can sign up, still lets the public key read every row. Scope policies to the row's owner with (select auth.uid()) = user_id, and limit which columns users can update.
Can I commit the anon key to a public repo?
Yes, it's not a secret. But never commit the service_role key — that one bypasses RLS and grants full database access. Confirm which key you're committing by checking the variable name and the JWT's role claim.
If the anon key is public, what actually protects my data?
Row Level Security. Every request made with the anon key runs as the anon or authenticated role, and RLS policies decide which rows those roles can see. No RLS means no protection.
Should I migrate to the new publishable key? Eventually, yes — Supabase has announced the legacy anon key will be removed in late 2026 (exact date to be confirmed). The publishable key behaves the same way (public, RLS-gated), so migration is low-risk, and the new secret keys are far easier to rotate if leaked. For the full side-by-side, see Supabase API keys: sb_publishable vs anon vs service_role.
Is the anon key the same as the JWT secret? No. The JWT secret signs user tokens and must stay private on the server. The anon key is a public API key. Leaking the JWT secret lets an attacker forge any user's session; leaking the anon key does nothing on its own if RLS is enabled.
See which of your Supabase tables anyone can read, free
Connect your Supabase project read-only. GuardLayer checks RLS, policies, grants and functions as they are right now, and gives you the SQL to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.