My Supabase service_role key is in my frontend. What do I do?
Rotate the key in your Supabase dashboard right away, then remove it from your frontend and move any code that needs it to a server. The service_role key bypasses Row Level Security, so anyone who copied it can read and change every table in your project.
Why is the service_role key a problem?
Supabase gives every project two main keys. The anon key is meant for browsers. It is limited by your Row Level Security (RLS) policies. The service_role key skips RLS completely. It is meant for trusted server code only.
If that key is in your frontend, anyone can open DevTools, copy it, and call your database with full access. They can read every table, edit rows and delete data. No policy can stop them.
How do I know the key is exposed?
AI tools sometimes paste the service key into client code to make an error go away. Look for these signs:
- The key is read from a variable that reaches the browser, such as
NEXT_PUBLIC_*in Next.js orVITE_*in Vite. Those values are written into your public JavaScript. - You call
createClientin a client component with a key that is not the anon key. - The key is committed to your repo.
To check a deployed site, open the Sources tab in DevTools and search your bundles for service_role. Supabase keys are JWTs that start with eyJ. Paste one into a JWT decoder. If the payload says "role": "service_role", it is the dangerous one.
What should I do first?
- Rotate the key. In the Supabase dashboard, open your project settings and the API keys page, and generate a new secret key. If your project still uses the legacy keys, rotating the JWT secret replaces them. Follow the current Supabase docs for the exact steps, because the dashboard changes. Removing the key from your code does not help while the old key still works.
- Remove it from client code. Delete every use of the key in the browser bundle and any
NEXT_PUBLIC_orVITE_variable that holds it. - Move that work to a server. Use a route handler, server action or edge function that checks who the user is, then uses the secret key only on the server. Store it as a server-only environment variable.
- Clean your git history. If the key was ever committed, treat it as leaked even after you delete it. Rotating is what fixes it.
- Redeploy so the new bundle no longer contains the old key.
How do I check if someone used the key?
Look at your Supabase logs for unusual reads, writes or deletes from IP addresses you do not know. Compare row counts and recent changes in your most important tables. Logs may not show everything, so if the data is sensitive, assume it was read and plan for that. Depending on where your users are, you may have a duty to tell them about a breach.
Is the anon key also a problem?
No. The anon key is public by design. It is only safe when RLS is on for every table in your public schema. See the guide on checking Supabase RLS.
// Server only (route handler or server action)
import { createClient } from "@supabase/supabase-js";
const admin = createClient(
process.env.SUPABASE_URL!,
process.env.SUPABASE_SERVICE_ROLE_KEY! // no NEXT_PUBLIC_ prefix
);Check your app
The free scan reads only what a browser downloads and takes under a minute. For a person to review your code, there is a human audit.