The Supabase service role key does not belong in the browser
The anon key is public and follows RLS. The service_role key and sb_secret keys bypass RLS. How they leak through Vite and Next, how to find one in the built JS, and how to rotate it.
Updated 27 October 2026 · LIM Agentic AI, Paris
Supabase gives a project two different kinds of credential. The anon key, and the newer publishable key (sb_publishable_...), identify the project and are meant to ship in the browser. Requests that use them run as anon or, with a user JWT, as authenticated. Row Level Security applies.
The legacy service_role key is a JWT whose payload role is service_role. A secret key (sb_secret_...) is the replacement. Both bypass RLS. A browser that holds one can read and write every row the key can reach, and it can call the Auth admin API. Treat a leaked copy as public. Rotating it is part of closing the leak, not a later chore.
How a builder puts the secret in the bundle
Vite inlines every variable whose name starts with VITE_ into client code. Lovable and Bolt projects are Vite apps, so a secret stored under VITE_SUPABASE_SERVICE_ROLE_KEY is in the JavaScript the browser downloads. Next.js does the same for NEXT_PUBLIC_. Create React App did it for REACT_APP_. The value is not fetched later from a server. It is a string in the file.
Next does not inline a variable that lacks the NEXT_PUBLIC_ prefix. Referencing process.env.SUPABASE_SECRET_KEY from a client component becomes empty in the browser build. The leak on Next is the public prefix, a secret rendered into HTML, or a shared module that a client component imports after someone pasted the key into a NEXT_PUBLIC_ name. The leak on Vite is any VITE_ name.
# These names are copied into the browser bundle.
VITE_SUPABASE_SERVICE_ROLE_KEY=...
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=...
REACT_APP_SUPABASE_SERVICE_ROLE_KEY=...
# Server-only. No public prefix. Never imported by client code.
SUPABASE_SECRET_KEY=...Source maps and a publicly served .env are the same class of mistake. A .map file next to the bundle contains the original source, including the key, if the key was in that source. Fetch /.env on the deployed host before you assume the host ignored it.
Find it in the built files
Search the built output, not only the repo. A key can be absent from git and still be injected at build time.
# Vite output (Lovable and Bolt ship a Vite app).
grep -R -n -E "service_role|sb_secret_" dist --include='*.js' --include='*.map'
# Next.js client chunks.
grep -R -n -E "service_role|sb_secret_" .next/static --include='*.js' --include='*.map'A legacy anon or service_role value is a JWT: three base64url segments, separated by dots. Decode the middle segment. Do not print the whole token into a ticket, a prompt, or a commit. The role is the only field you need.
const token = process.argv[2];
const parts = token?.split(".") ?? [];
if (parts.length < 2) {
console.error("Pass the JWT from the bundle. Do not commit it or paste it into a chat.");
process.exit(1);
}
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
console.log({ role: payload.role, ref: payload.ref });roleisanon. This is the public client key. It is fine in the bundle only when RLS is on and the policies are tight. Seeing it is not, by itself, an incident.roleisservice_role. The bundle bypasses RLS. Rotate. Remove it from client code before the new key is written anywhere the build can see.- The match is the text
sb_secret_. That is a secret key, not a JWT. Same response: get it out of the client, rotate, revoke the leaked copy. - The match is
sb_publishable_. That is the public key. Same rule as the anon key.
You can do the same check in the browser without saving the file. Open devtools, open a script under the deployed origin, and search for service_role and sb_secret_. If the site is behind auth, view the source of the login page. The client bundle is usually loaded there too.
Rotate the leaked key
Remove the key from the client first. If you rotate while the build still reads the secret from a VITE_ or NEXT_PUBLIC_ variable, the next deploy publishes the new key the same way.
- Delete the secret from every client env name and from source. Server code reads
SUPABASE_SECRET_KEY, or a name with no public prefix, from the server environment. - In the Supabase dashboard, open Settings, then API Keys. Create a secret key (
sb_secret_...). Creating one does not revoke the legacyservice_rolekey. Both work until you turn the old one off. - Put the new secret only in the server environment. Redeploy the server, not the client bundle.
- Confirm the server path you care about still works, and confirm the built JS no longer contains
service_roleorsb_secret_. - If a secret key leaked, delete that key after the server is on the new one. Deleting a secret key cannot be undone. If the legacy
service_roleJWT leaked, deactivate the legacy keys in the same API Keys section. Deactivate is reversible, which is what you want if a server you forgot is still using it.
Do not start by regenerating the JWT signing secret. The legacy anon key, the legacy service_role key, and existing user sessions are all signed by it. Supabase's current guidance is to switch servers onto a secret key and then deactivate the legacy key. That leaves user sessions and the anon key in place.
The server client should not keep a user session. It is not a browser.
import { createClient } from "@supabase/supabase-js";
// Import this module from server code only.
export const admin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SECRET_KEY!,
{ auth: { persistSession: false, autoRefreshToken: false } },
);Check it yourself
Run the grep against a fresh production build. Then open the deployed site and search the loaded scripts. Both have to be clean. A clean repo with a dirty host means the host is building from a different env.
A secret key also spends your quota. On the organization's billing page, set a spend cap if the plan has one, and turn on the billing alert emails. Supabase can pause the project when the cap is hit. The cap limits the bill while you rotate. It does not close the leak, and it does not protect rows that were already read.
After rotation, assume anything the leaked key could read was read. RLS policies do not help for the window where the secret was public. Change passwords for admin accounts that lived in that database if those passwords were stored there, and review Auth users created in that window. The Auth admin API is available to the service role.
Questions
Is the anon key a secret?
No. The anon key and the publishable key are public identifiers. They are safe to expose only because RLS applies to them. A service_role JWT or an sb_secret key is a secret even if the project URL is obscure. Obscurity of the URL is not a control.
I rotated the key but the old one still works. Why?
Creating a new secret key does not disable the legacy service_role key. Deactivate the legacy keys in Settings, API Keys, after every server is on the new secret. If you only generated a new key and left the old JWT in the bundle, both keys are public.
Should I regenerate the JWT secret?
Not as the way to kill one leaked service_role key. That secret also signs the anon key and user access tokens. Move the server to an sb_secret key and deactivate the legacy key instead.