LatchDog

← Guides

Your Supabase service_role or secret key leaked: what to do

Short answer: treat it as a full database compromise and act in this order. Move any privileged code out of the browser first, then create a new secret key (sb_secret_...) in Settings > API Keys, replace the leaked key everywhere on the server, confirm the app still works, and only then retire the old key. A leaked sb_secret_ key is deleted (that cannot be undone); a leaked legacy service_role JWT is deactivated in the same section, and you should also revoke the legacy JWT secret so that JWT stops being accepted. LatchDog can flag the key in your live JavaScript and source maps, but it cannot retire the key or tell you whether it was already used.

Why this key bypasses Row Level Security

Supabase now has two pairs of keys. The ones meant for anything you ship (browser, mobile app, CLI) are the publishable key (sb_publishable_...) and, on older projects, the legacy anon JWT. The ones meant only for code you control (a server, an Edge Function, a cron job) are the secret key (sb_secret_...) and, on older projects, the legacy service_role JWT. Supabase is deprecating the anon and service_role keys by the end of 2026 (Supabase: API keys, read Nov 11, 2026).

On the secret side, the docs are direct: "Secret key: It bypasses Row Level Security, so it must never leave your control." And: "Secret keys authorize access to your project's data through the built-in service_role Postgres role. By design, this role has full access to your project's data. It also has the BYPASSRLS attribute, so it skips every Row Level Security policy you attach." Anyone who copied the key can read and change every table that role can reach, regardless of your policies. For what the public keys can and cannot do, see is the Supabase anon key safe to expose?.

How to confirm the leak

Look in the built app, not only in the editor:

  1. The live JavaScript and source maps. Open the published site, then DevTools → Sources, and search the loaded scripts for sb_secret_ and for long JWTs that start with eyJ. If you find a JWT, decode the middle part (the payload) and look for "role":"service_role". Also request the .map file next to a script; secrets often survive in source maps after you remove them from source. For the general search, see exposed API keys in JavaScript bundles.
  2. Public env prefixes. A variable named VITE_, NEXT_PUBLIC_ or REACT_APP_ is compiled into the JavaScript every visitor downloads. Expo is the same for EXPO_PUBLIC_: "Do not store sensitive info, such as private keys, in EXPO_PUBLIC_ variables. These variables will be visible in plain-text in your compiled application" (Expo: Environment variables, read Nov 11, 2026).
  3. Git history and downloadable files. A key that was committed and later deleted is still in the history. Search the repo and git log -p for the same patterns. Try /.env, /.env.production and /.git/config on the live domain; if they download, assume those values were public too.

If you only find a publishable or legacy anon key, that is not this incident. If you find sb_secret_ or a JWT with "role":"service_role", keep going.

What to do, in order

This follows Supabase's own rotation guide: "Fix the root cause of the leak before you rotate anything. Otherwise the new key can leak the same way." If someone is already using the leaked key, they say to retire it right away, even though components that still use it will stop working until you replace it (Supabase: Rotate a leaked or compromised API key, read Nov 11, 2026).

  1. Move privileged code off the client. Remove the secret or service_role key from frontend code and from any VITE_, NEXT_PUBLIC_, REACT_APP_ or EXPO_PUBLIC_ variable. Anything that truly needs elevated access belongs in a server-side function (a Supabase Edge Function, an API route, or similar) that reads a secret key from a server-only secret. The browser should use only the publishable key, or the legacy anon key on a project that has not migrated yet.
  2. Create a new secret key in the Dashboard, Settings > API Keys: "Create a new secret key in the Settings > API Keys section of the Dashboard." If the leak was the legacy service_role key, this new sb_secret_ key is what replaces it.
  3. Replace the leaked key everywhere it is used on the server: Edge Functions, API routes, hosting env vars, CI, cron jobs, and any other service that holds it. Two details from the same guide: an Edge Function that checks the key with withSupabase({ auth: 'secret' }) "accepts only the secret key named default", so update it to accept the new key by name (for example auth: 'secret:rotated_secret_key'); and if a Database Webhook or pg_net call sends the legacy service_role key on the Authorization: Bearer header, send the new secret key on the apikey header instead. If the leak was a legacy service_role key, also replace the legacy anon key with a publishable key in the browser and mobile clients: "Retiring the compromised key deactivates both legacy keys."
  4. Confirm that every component now uses the new key and that the app still works.
  5. Retire the old key in Settings > API Keys. "Unless someone is actively using the leaked key, wait until every component uses the new key before you start. Deleting a secret key can't be undone." Then: "For a secret key, delete it." "For a legacy anon or service_role key, deactivate the legacy keys." If you later find a client you missed, move it to a new key rather than re-enabling the legacy keys, "because re-enabling restores the compromised key."
  6. If the leaked key was a legacy service_role JWT, also revoke the legacy JWT secret. Supabase: "If the compromised key is a legacy service_role key, also revoke the legacy JWT secret. The legacy keys are JWTs signed by that secret, so anything that still trusts the secret can accept the leaked key." The path is on the JWT signing keys page: migrate the JWT secret, rotate keys, then revoke the legacy JWT secret (Supabase: JWT Signing Keys, read Nov 11, 2026). Two cautions from that page: "Revoking the legacy JWT secret means that your application no longer trusts any JWT signed with it. Therefore before you revoke the legacy JWT secret, you must disable the anon and service_role" keys first; and if your access-token expiry is the default one hour, "wait at least 1 hour and 15 minutes before revoking the legacy JWT secret" so signed-in users are not forced out, unless this is an active incident, in which case "you may want to revoke the legacy JWT secret immediately." Also check that nothing (including Edge Functions with "Verify JWT") verifies tokens against the legacy secret. Deleting the legacy JWT secret is a different, currently disallowed action; the page says that is so you can still go back to it.
  7. Verify the old key is dead. From a machine you control, call your own project's Data API with only the old key, for example:
    curl -i "https://YOUR-PROJECT-REF.supabase.co/rest/v1/" -H "apikey: THE_OLD_KEY" -H "Authorization: Bearer THE_OLD_KEY"
    You want that request to fail (often a 401). If it still succeeds, the key is still live. We have not run this exact request against a live project after a deletion, so confirm it on yours. Do not test anyone else's project, and do not put the key in a ticket or a chat log.
  8. Review logs and data for misuse. In the Dashboard, open Logs and look at API Gateway, PostgREST, Auth and Edge Function events around the time the key was public (Supabase: Logging, read Nov 11, 2026). We did not verify that Logs will name the leaked key, so treat this as a search for unexpected traffic and writes, not as a complete audit. Assume anything the key could reach may have been read or changed, and check user records, billing and provider usage if those tables were exposed.

Paste-in fix prompt for Lovable, Bolt or Cursor

My app's client-side JavaScript may contain a leaked Supabase secret key (a legacy service_role JWT or an sb_secret_ key). Both bypass Row Level Security. Please:
1. Search the repo, build output and env files for sb_secret_, service_role, and any JWT whose payload has "role":"service_role". Remove every one from frontend code and from env vars prefixed VITE_, NEXT_PUBLIC_, REACT_APP_ or EXPO_PUBLIC_. Do this before I replace the key, otherwise the new key can leak the same way.
2. Use only the publishable key (sb_publishable_...) in the browser. On a project that still uses the legacy keys, that means the legacy anon key.
3. Move any operation that truly needs elevated access into a server-side function (Supabase Edge Function or API route) that reads a secret key from a server-only secret.
4. Then walk me through replacing the leaked key, in this order, in the Supabase dashboard under Settings > API Keys:
   a. Create a new secret key.
   b. Replace the leaked key with the new one everywhere it is used on the server (Edge Functions, API routes, hosting env vars, CI, cron jobs). If the leaked key is the legacy service_role key, the new secret key replaces it. If an Edge Function uses withSupabase({ auth: 'secret' }), update it to accept the new key by name. If a Database Webhook or pg_net call sent the legacy key on Authorization: Bearer, send the new secret key on the apikey header instead.
   c. Confirm that every component now uses the new key and that the app still works.
   d. Retire the old key. A leaked sb_secret_ key is deleted, which can't be undone, so only after step c. For a leaked legacy service_role key, deactivate the legacy keys in the same section instead (that deactivates both legacy keys).
5. If the leaked key is a legacy service_role key, also walk me through migrating to JWT signing keys and revoking the legacy JWT secret, after the legacy keys are deactivated. Check that nothing in the app or Edge Functions verifies tokens against the legacy secret, and tell me how long to wait before revoking so signed-in users are not logged out, unless this is an active incident.
Do not disable RLS, do not put a secret key back in the browser, and do not change unrelated code.
Show me the changes first.

Review what the tool changes, then repeat the live-JavaScript search and the old-key request above. More on monitoring after a leak: how to monitor your Supabase app for leaked keys and open RLS.

What LatchDog does here (and doesn't)

LatchDog looks for this leak in the JavaScript and source maps your live site already serves. It flags an sb_secret_ string (20 or more characters after the prefix) and any JWT whose payload has role equal to service_role, both as a critical finding. Those checks run on the downloaded scripts and, when a public source map has original sources, on the app's sources in that map (library files under node_modules are skipped). Reports show a masked preview of the key, never the full value. The free check does this for up to 6 same-origin JavaScript files and up to 2 source maps, with no sign-up. It also requests /.env-style paths and /.git/HEAD / /.git/config; if a .env file is downloadable it reports only the variable names, not the values, and it does not search git history. It does not call your project with the leaked key, does not look at the Dashboard's key list, and does not test Storage.

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Check yours

Search your live JavaScript for sb_secret_ and for a JWT with "role":"service_role" now, then run the free LatchDog check on your published URL (no sign-up). If it finds a secret key, follow the order above before you create a new one.

Keep watching your app, not just checking it once

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Start with the free check (headers, HTTPS, leaked keys in your JavaScript, source maps, .env/.git). Open-table checks run after you verify your domain. See plans

Free, read-only public check: no sign-up, no card. Headers, HTTPS, leaked keys in your JavaScript (always masked), public source maps and exposed .env/.git files. Supabase table checks run after you verify you own the domain.

Want the open-table check too? Get my free deep scan: sign in with your email (no password) and verify you own the domain. Your first deep scan is free and adds CORS, more of your JavaScript and, if your app uses Supabase, whether anyone can read its tables and views with your public key.