Supabase · Lovable · Vite · Next.js
I exposed my Supabase service_role key. What do I do now?
The service_role (secret) key bypasses Row Level Security. How to rotate it, find out whether it was used, and move privileged work out of the browser, in the right order.
Why it's an emergency
The service_role key (called a secret key, sb_secret_…, in Supabase's newer key system) is meant for servers. It bypasses Row Level Security completely: every table, every row, every storage file, read and write. If it was in your JavaScript bundle, anyone who opened your site could copy it from the browser's developer tools.
It usually leaks one way: an environment variable with a public prefix. Anything named VITE_*, NEXT_PUBLIC_*, REACT_APP_* or EXPO_PUBLIC_* is compiled into the code every visitor downloads. That is the purpose of the prefix.
In the next hour
- Rotate the key. New API keys: in Project Settings → API Keys, create a new secret key and delete the exposed one. Legacy keys: rotate the JWT secret; both legacy keys change and existing sessions end, so redeploy your frontend with the new anon key.
- Remove the key from client code, from every VITE_/NEXT_PUBLIC_ variable, and from the hosting provider's build settings, then redeploy.
- Assume it was copied. Git history and old deployments still contain it; rotation is what makes those copies useless.
Find out whether it was used
Open the API logs in the Supabase dashboard for the period the key was public and look for requests made with the service role that your servers didn't make: bulk selects, deletes, changes to auth users or storage. Check for new admin users, changed roles and unfamiliar storage objects. If customer personal data could have been read, your obligations under privacy law may start now, not when you're sure.
Move privileged work to the server
import { createClient } from "npm:@supabase/supabase-js@2";
Deno.serve(async (req) => {
// A client that carries the caller's session, so Row Level Security applies.
const supabase = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_ANON_KEY")!,
{ global: { headers: { Authorization: req.headers.get("Authorization")! } } },
);
const { data: { user } } = await supabase.auth.getUser();
if (!user) return new Response("Unauthorized", { status: 401 });
// Use a service-role client only for the one step that needs it, after checking the user may do it.
return Response.json({ ok: true });
});Check it yourself
Run the free exposure check on your live site after redeploying. It downloads your page and scripts like a browser does and reports any service_role or secret key it finds, redacted.
Check what your app exposes (free, 30 seconds)
Questions
Is the anon (publishable) key a secret too?
No. It's designed to ship to browsers. Its safety depends entirely on your Row Level Security policies, which is why a permissive policy is as serious as a leaked key.
I deleted the key from my code. Am I safe?
Not until the key is rotated. Copies remain in Git history, old deployments, caches and anyone's notes.
Updated 18 November 2026 · by Psychevus