Leaked Supabase service_role key: what to do now
THE SHORT ANSWER
A leaked service_role key gives anyone full access to your database, because it bypasses Row Level Security. Act in this order: create a new secret key (sb_secret_) and move your server code to it, switch browser code to a publishable key, deactivate the legacy anon and service_role keys under Settings > API Keys, then revoke the legacy JWT secret, since that secret is what makes the old key valid. Read your logs before they expire (one day on the Free plan), turn on RLS for every exposed table, and move the code that needed the key to a server so it never reaches the bundle again. Removing it from git history comes last and doesn't undo the leak.
How bad is a leaked service_role key?
As bad as it gets for a Supabase project. Supabase's docs: “A secret key bypasses every Row Level Security policy you have.” The legacy service_role key and the new sb_secret_ keys both use the service_role Postgres role, which has bypassrls. Anyone holding one can read, change and delete every row in every table the API exposes.
First work out which kind you leaked:
| Looks like | What it is | Public? |
|---|---|---|
sb_publishable_... | Publishable key | Yes, by design |
sb_secret_... | Secret key | No |
eyJ... with "role": "anon" | Legacy anon key (a JWT) | Yes, by design |
eyJ... with "role": "service_role" | Legacy service_role key (a JWT) | No |
The legacy keys are JWTs, so the role is inside the token. The leaked API key scanner decodes Supabase JWTs it finds on a live site and reads that role claim: service_role is reported as an exposed secret, anon as public by design. It flags sb_secret_ keys too.
Rotate before you touch the code. Deleting the key from your repo or your bundle does nothing about copies people already have.
Step 1: create a new secret key and swap it in
Supabase's rotation guide has the same three steps for both key types: create a new secret key, replace the leaked key everywhere, then retire the leaked one. Fix whatever leaked the key first, or the new one leaks the same way.
- In the dashboard, open Settings > API Keys and create a new secret key. If you were on the legacy keys, create a publishable key as well.
- Put the secret key in every server-side component that used the old key: your backend, Edge Functions, cron jobs. Put the publishable key in browser and mobile code in place of the legacy
anonkey. - Check the two places that behave differently with the new keys. Database Webhooks and
pg_netcalls that sentservice_roleasAuthorization: Bearerneed the new key in theapikeyheader, because secret keys aren't JWTs. Edge Functions with the built-inverify_jwtcheck only understand the legacy keys.
Supabase recommends a separate secret key for each backend component: “If one component leaks its key, you only rotate that one.”
Step 2: delete the leaked key or turn off the legacy keys
Wait until everything uses the new key, unless someone is using the leaked one right now. Then the guide says to retire it immediately and accept that anything you missed breaks.
- A leaked
sb_secret_key: delete it under Settings > API Keys. Deleting a secret key can't be undone. - A leaked legacy
service_rolekey: you can't revoke it on its own. Deactivate the legacy keys (bothanonandservice_role) in the same section. Deactivation is reversible, but if a client breaks afterwards, move that client to a new key; turning the legacy keys back on restores the leaked one.
The same steps through the Management API, with a personal access token in an env var:
# turn off the legacy anon and service_role keys curl -X PUT "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/legacy?enabled=false" \ -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" # delete one secret key by id (list them with GET .../api-keys) curl -X DELETE "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/$KEY_ID" \ -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"
If your sb_secret_ key was pushed to a public GitHub repo, check the project before you start: Supabase wrote in January 2026 that it automatically revokes secret keys detected in public GitHub repositories and notifies the project owner. A backend that suddenly gets 401s may be the first sign.
Step 3: revoke the legacy JWT secret
This step applies only to a leaked legacy key. The legacy anon and service_role keys “are not only API keys, but are also valid JSON Web Tokens, signed by the legacy JWT secret”. Anything that still trusts that secret, such as your own code verifying Supabase JWTs, can still accept the leaked token. Revoking the secret ends that.
- On the JWT Signing Keys page, click Migrate JWT secret. Supabase says this “does not cause downtime” and creates a standby asymmetric key.
- Rotate to the new key. With signing keys, “no users will be forcefully signed out”: existing access tokens stay valid until they expire. Turn off
verify_jwton Edge Functions first if they use it. - Make sure the legacy
anonandservice_rolekeys are disabled (step 2); the docs require that before the next step. - Revoke the legacy JWT secret. Normally you wait about 1 hour 15 minutes so access tokens signed with it expire, but the docs allow revoking right away during an active security incident.
Supabase is retiring the legacy anon and service_role keys by the end of 2026, so this migration is coming anyway.
Step 4: check the logs for what someone did with it
Do this the same day. Supabase keeps logs for a short time:
| Plan | Log retention |
|---|---|
| Free | 1 day |
| Pro | 7 days |
| Team | 28 days |
| Enterprise | 90 days |
Open Logs and query the API Gateway source. The documented gateway fields don't record which key a request used, so look for traffic that isn't yours: unfamiliar IPs, writes and deletes you didn't make, tables your app never touches.
select
timestamp,
log_attributes['request.method'] as method,
log_attributes['request.path'] as path,
log_attributes['response.status_code'] as status,
log_attributes['request.headers.cf_connecting_ip'] as ip
from logs
where source = 'edge_logs'
and log_attributes['request.method'] in ('POST', 'PATCH', 'DELETE')
order by timestamp desc
limit 200;Group by ip and path to see who did the most. Check the Auth logs for users you didn't create and Storage for files you didn't upload. If data was read or changed, restore from a backup and decide whether you need to tell your users.
Step 5: check Row Level Security
RLS didn't protect you from the leaked key; the secret role skips it. It is what protects you from the publishable key that is meant to be in your browser code. Supabase's rule is short: “Enable RLS on every table in an exposed schema.” A table without it “is readable and writable by any role with a grant on it”.
-- tables in public without RLS select tablename from pg_tables where schemaname = 'public' and rowsecurity = false; alter table public.orders enable row level security;
Once RLS is on, the publishable key sees nothing until you add policies. The Security Advisor (under Advisors in the dashboard, or supabase db advisors) reports 0013_rls_disabled_in_public and 0023_sensitive_columns_exposed as errors.
This is also where many leaks start: a query fails because of RLS, and the quick fix is to swap in the service_role key. Write the policy instead, or move that query to the server.
Step 6: get the key out of the bundle and git history
A key reaches the browser when it's in a variable your build tool inlines (NEXT_PUBLIC_, VITE_, EXPO_PUBLIC_) or pasted into a component. Move every call that needs the secret key into server code: a Route Handler or Server Action, an Edge Function, or your API. Keep the key in an environment variable without a public prefix, rebuild, redeploy and scan the live site again. Supabase notes that a secret key used from a browser gets a 401, but that doesn't stop anyone from using a copied key with curl.
// app/api/admin/users/route.ts (server only)
import { createClient } from "@supabase/supabase-js";
const admin = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SECRET_KEY!);
export async function GET() {
// check the caller is an admin before this line
const { data, error } = await admin.auth.admin.listUsers();
if (error) return Response.json({ error: error.message }, { status: 500 });
return Response.json(data.users.map((u) => ({ id: u.id, email: u.email })));
}Git history comes last, after the key is dead. GitHub's docs say the first step is to “revoke and/or rotate that secret”, and that rewriting history doesn't reach other people's clones or forks. If you still want it gone:
# expressions.txt holds the leaked key, one per line git filter-repo --sensitive-data-removal --replace-text ../expressions.txt
Then force-push as GitHub's guide describes. GitHub Support can remove cached views and pull request references. For the related build-time problem, see NEXT_PUBLIC_ env variables and Vite env variables.
How do you keep it server-side on EthioDeploy?
Your backend and your front end can run on EthioDeploy, so the code that needs the secret key has a server to live on. Add the key under Environment Variables with a name that has no public prefix, such as SUPABASE_SECRET_KEY or SUPABASE_SERVICE_ROLE_KEY. The add form suggests “Runtime only” for names like these, and a runtime-only variable is kept out of the build, so it can't be inlined into a bundle. It never suggests that for a NEXT_PUBLIC_ or VITE_ name, because those exist to reach the browser.
After you redeploy, run the leaked API key scanner on the live URL. It reads the page's HTML and up to 25 JavaScript files from the same domain (15 MB in total), the same files a visitor downloads, and shows any key it finds masked; nothing is stored. It can't see pages behind a login or code that only loads after a click, so a clean result means no known key pattern was found, not that the app is secure.
Questions
Create a new secret key in Settings > API Keys, move your server code to it, then deactivate the legacy anon and service_role keys and revoke the legacy JWT secret. Change the code that leaked the key only after the old key stops working.
Not on its own. The legacy service_role key is a JWT signed by the project's legacy JWT secret. You replace it with a new secret key (sb_secret_), deactivate the legacy keys, and revoke the legacy JWT secret through the JWT signing keys migration.
Not if you migrate to JWT signing keys first. Supabase says rotating signing keys signs nobody out, because access tokens that haven't expired stay valid. Waiting about 1 hour 15 minutes before revoking the legacy secret lets one-hour tokens expire.
Yes, the anon key and the publishable key are meant for browser code. Your data is protected by Row Level Security, so every table in an exposed schema needs RLS enabled and policies for what logged-out and logged-in users may do.
No. Clones, forks and anyone who already copied the key keep it. Revoke or rotate the key first; rewriting history with git filter-repo is cleanup afterwards.
Sources
Checked on November 24, 2026:
- Supabase: Understanding API keys
- Supabase: Rotate a leaked or compromised API key
- Supabase: Migrating to new API keys
- Supabase: JWT signing keys
- Supabase: Logs
- Supabase: Advanced log filtering
- Supabase: Log field reference
- Supabase: Row Level Security
- Supabase: Database advisors
- Supabase blog: Security retro 2025
- PostgreSQL: pg_tables
- GitHub: Removing sensitive data from a repository
Push a repo. Get a URL.
Start freeJoin the EthioDeploy Telegram
Release notes as they ship, plus deploy help from the team and other developers.