Supabase is retiring anon and service_role keys: check what your app ships
Supabase says it is deprecating its legacy anon and service_role keys by the end of 2026. It tells developers to move to publishable keys (sb_publishable_...) and secret keys (sb_secret_...). Many projects will be editing their keys over the coming months. That is when a powerful key is most likely to land in front-end code by mistake.
This article shows how to check which key your app sends to every visitor, and what to do if it is the wrong one.
Why the key type matters
One key is meant to be public. The other gives full access to your database.
Supabase's mapping is simple:
anonbecomes the publishable key. It is for browsers, mobile and desktop apps, CLIs and public source code.service_rolebecomes the secret key. It is for servers, Edge Functions, workers and other backend code.
A publishable or anon key in the browser is expected. It is only safe if Row Level Security (RLS) is on. RLS is Supabase's per-row access rules.
A secret key skips those rules entirely. service_role has the BYPASSRLS attribute, so policies never apply to it. If it reaches the browser, anyone who opens dev tools can read, change or delete your data.
How keys leak in AI-built apps
Vite says any variable starting with VITE_ is exposed in client-side code after bundling. Next.js does the same with NEXT_PUBLIC_. The prefix is the signal that says "send this to the browser."
A common pattern: a query fails because an RLS policy blocks it. Instead of fixing the policy, someone (or an AI assistant) swaps in the privileged key to make the error go away. Names like VITE_SUPABASE_SERVICE_ROLE_KEY or NEXT_PUBLIC_SUPABASE_SECRET_KEY end up in the bundle, even if the value was stored as a private setting on your host.
This is not rare. Small third-party scans of public repos and vibe-coded apps have found secret keys in public variables. The samples are small, but the mistake is easy to make.
The opposite failure also hurts. Moltbook is an example reported by the Cloud Security Alliance: a Supabase key in client-side JavaScript with RLS disabled. The report says that exposed 1.5 million authentication tokens and 35,000 email addresses. The public key was not the problem. Missing RLS was.
Step 1: See which keys your project has
In Supabase, open Settings, then API Keys. If you see a "Create new API keys" button, your project is still on legacy keys only.
Creating the new keys is safe. It adds a publishable key and a secret key next to your existing ones, and the legacy keys keep working. That also means an old leaked service_role key keeps working until you deactivate it.
Step 2: Search your code and your build
Search the repo for key names and long token strings:
grep -rIn --exclude-dir=node_modules --exclude-dir=.git -E "service_role|sb_secret_|eyJ[A-Za-z0-9_-]{20,}" .
Then build the app and search the output. For Vite that is dist/assets/. For Next.js, look in .next/static.
npm run build
grep -rIn -E "service_role|sb_secret_" dist/ .next/static/ 2>/dev/null
Also review your env files and hosting settings for any VITE_* or NEXT_PUBLIC_* name that sounds like a secret.
Check for source maps too. A production .map file can reveal source code and string literals even when the main bundle is minified. If you do not need them public, do not publish them.
Step 3: Work out what you found
sb_publishable_...in the browser: expected. Make sure RLS is in place.sb_secret_...in the browser: a leak.- A long
eyJ...string: could be either legacy key. Both are JWTs. Decode the middle section locally and check therolefield.anonis expected.service_roleis a leak.
Step 4: If a secret key leaked, rotate first
Treat it as compromised. Fix the key before you tidy the code.
- Create the new API keys in Supabase.
- Put the new secret key on the server only, replacing
service_role. - Deactivate the legacy keys.
- Check whether the legacy JWT secret needs to be revoked as a further step. Third-party guides describe this as coming after the legacy keys are disabled.
Rotation stops new access. It does not undo reads that already happened, so look at your logs and consider whether user data needs to be reported or users need to be told.
Step 5: Keep privileged keys on the server
Use Next.js route handlers, Supabase Edge Functions, or a small API for anything that needs full access. Never give a secret a VITE_ or NEXT_PUBLIC_ prefix.
In Next.js, mark your admin helper so the build fails if client code imports it:
import 'server-only'
import { createClient } from '@supabase/supabase-js'
export const admin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SECRET_KEY!
)
The variable names have no public prefix, so they stay on the server.
Step 6: Test RLS as a stranger
Call your tables using only the publishable key, with no logged-in user. Reads and writes should be denied unless a policy allows them. One anonymous test like this would have caught the missing-RLS problem behind CVE-2025-48757, according to one write-up.
If a query is blocked, fix the policy. Do not reach for a bigger key.
Step 7: Tell your AI assistant
Supabase's docs say that if a tool, tutorial or AI assistant tells you to copy a long key beginning with eyJ, it was written for the legacy keys. Tell your assistant to use sb_publishable_ in client code, sb_secret_ only on the server, and never to add public prefixes to secrets.
What to do today
- Open Settings, then API Keys in Supabase and note whether you are still on legacy keys.
- Run the search commands on your repo and on a fresh build output.
- Decode any
eyJstring you find and check its role. - If
service_roleorsb_secret_appears in browser code, create new keys, move the secret to the server, and deactivate the legacy keys. - Rename any secret that has a
VITE_orNEXT_PUBLIC_prefix, and addserver-onlyto admin helpers in Next.js. - Turn on RLS for every table and test with the publishable key alone.
- Switch client code to the
sb_publishable_key, confirm nothing uses a legacy key, then deactivate the legacy keys. - Disable public source maps unless you need them.
Sources
- Vibe Coding Boom Exposes Corporate Data as AI App Builders Face Security Gaps < IT·Gaming < 기사본문 - The Elec Inc.
- Vibe coding ships fast. Software engineering keeps it from falling apart. - DEV Community
- We checked 573 vibe-coded apps for leaked API keys. Here is what we found - DEV Community
- Four security bugs AI coding agents keep writing in Next.js + Supabase apps (and 4 false positives I fixed in my scanner) - DEV Community
- Supabase API Key Exposed? Publishable vs Secret Keys
- I scanned 8 newly shipped Next.js + Supabase AI repos on GitHub - 5 scored an F (288 security findings) - DEV Community
Check your own site
Attack Shield looks for these mistakes on your website and shows how to fix each one.
Scan your site