Is it safe to expose the Supabase anon key?
Yes, and the word doing the work is conditionally. The anon key is designed to ship in your frontend. It is safe when your policies are right, and it is the whole attack surface when they are not.
That is not a hedge. It is the actual security model, and understanding it is the difference between a calm afternoon and a bad one.
The key is not the secret
Supabase issues two kinds of key and they are not two strengths of the same thing.
- The anon key, or
sb_publishable_in the newer format, is public by design. It identifies the anonymous role. Everything it can reach is decided by your table privileges and your Row Level Security policies. - The service role key, or
sb_secret_, exists specifically to bypass Row Level Security. Policies do not apply to it. It is a master key and belongs only in server code.
So the honest formula is: what a visitor can do equals your table grants intersected with your policies. The key contributes nothing to that calculation.
Check which key you actually have
If it is a JWT, decode the middle segment. No tool needed:
echo 'PASTE_THE_KEY' | cut -d. -f2 | base64 -d 2>/dev/null; echo
"role":"anon" means public by design. "role":"service_role"
means stop reading and rotate it now, in Settings, API, reset service key. Deleting
the line does not help: the old value remains in your git history.
Check whether yours is safe, from outside
The only answer that counts comes from making the request a stranger would make. Take your own project URL and anon key out of your deployed bundle, then:
curl "https://YOUR-PROJECT.supabase.co/rest/v1/YOUR_TABLE?select=*&limit=5" \ -H "apikey: YOUR_ANON_KEY"
An empty array means the policy is doing its job. Rows of real data mean that table is readable by anyone right now. Then try writing, which people skip:
curl -X POST "https://YOUR-PROJECT.supabase.co/rest/v1/YOUR_TABLE" \
-H "apikey: YOUR_ANON_KEY" -H "Content-Type: application/json" \
-d '{"id":"00000000-0000-0000-0000-000000000000"}'
Anything other than a refusal means a stranger can insert rows into your database.
Two things that quietly make it unsafe
A table with Row Level Security switched off. Supabase serves every table in the public schema over REST. Without policies, the anon key reaches all of it. List them:
select tablename, rowsecurity from pg_tables where schemaname = 'public' order by rowsecurity, tablename;
A policy that passes for everyone. The shield in the dashboard shows Row Level Security is on. It does not show whether the policy restricts anyone:
select tablename, policyname, roles, qual as using_clause from pg_policies where schemaname = 'public';
A using_clause of true granted to anon
is a shield with nothing behind it.
What to do if you leaked the wrong key
Rotate the secret key first, before cleaning anything up, because cleaning first only signals which value mattered. Then read the logs for the window between the deploy and the rotation, looking for bulk reads of tables an ordinary user would never touch. Then move the key into server-only code with no public prefix, where the browser simply cannot see it.
And leave the anon key alone. Rotating it breaks every deployed client and fixes nothing, because it was never the thing protecting you.
Related
- "new row violates row-level security policy"
- Is my Supabase database public?
- NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY
- RLS Disabled in Public
- Supabase returns an empty array but the rows are there
- Supabase security scanner
Check your own project
One command. No install, no signup, and it never touches the network. It reads your repository and tells you which tables are reachable and which keys are exposed, with the fix beside each finding.
npx github:humora2504/vibeproof .
Free and MIT licensed. If you want the policies generated for your own schema, and a script that proves an anonymous visitor is refused, there is a $19 Fix Pack. The scanner stays free either way.