Sample alert How it works Coverage PENTEST METHODOLOGY DOCS PRICING FAQ MCP CONTACT LOG IN SCAN MY APP →

Supabase service role key leaked: what to do now

FIX / LEAKED SERVICE ROLE KEY / 10 MIN READ

If your Supabase service_role key leaked, create a new sb_secret_ key and move every backend to it. Deactivate the legacy keys and revoke the legacy JWT secret. Do this today. This key skips every Row Level Security policy, so anyone who holds it can read, change, and delete all your data.

This guide shows how to confirm what leaked and how to rotate in the right order. It also shows how to check the logs and how to find the source of the leak.

What did the leak expose?

The service_role key gives full access to your database through the API. Supabase says that the secret key maps to the service_role Postgres role, and that this role has BYPASSRLS. It skips every RLS policy. The key does not need a login, and it does not expire.

A holder of the key can:

  • Read every row in every table, including users, orders, and messages.
  • Insert, update, and delete rows in any table.
  • Read and write files in Storage.
  • Call any database function that the API exposes.
  • Use the Auth admin API to list users, change emails, and create accounts.

RLS does not protect you in this case. Your policies still work for normal callers, but this key is not a normal caller. Treat all your data as copied.

The key that leaked can have one of two forms. Find which one you have, because the steps differ.

Key type Looks like Notes
Secret key sb_secret_... New format. You can create several and delete each one alone.
Legacy service_role key eyJ... (a JWT) Old format. Signed by the legacy JWT secret. You cannot rotate it alone.

The Supabase API keys guide says that the legacy keys stay valid until you deactivate them. A new secret key does not cancel them. This point is important. Many projects have both kinds. If an old key leaked, a new key does nothing until you deactivate the legacy keys.

How do I confirm that the key is the real one?

Test the key from your own computer. Do not paste it into a web page or a chat.

  1. Copy the key that you found. Do not share it again.
  2. Call the REST API with this key. Use a table that you know exists.
curl -s "https://YOUR-PROJECT-REF.supabase.co/rest/v1/YOUR_TABLE?select=*&limit=1" \
  -H "apikey: THE_LEAKED_KEY"
  1. Read the result. A row from a table with RLS enabled means that the key skips RLS. This is the service role.
  2. For a legacy key, decode the middle part of the JWT. Look at the role field. The value service_role confirms the type. The value anon means that the key is the public key, and this case is much less serious.
echo "THE_MIDDLE_PART_OF_THE_JWT" | base64 -d 2>/dev/null

An anon key is not secret. It is designed for the browser. The anon key guide explains the difference. Only RLS protects your data from an anon key.

Warning: Do not test a leaked key in an online JWT decoder. You send the key to a third party. Decode it on your own computer.

How do I fix it now?

Follow the Supabase guide on how to rotate a leaked API key. The steps below follow the same order and add detail.

First, read the rule from Supabase. If someone uses the key now, retire it at once. Parts of your app that still use the key then break until you replace it. If you see no active abuse, wait until every part of your app uses the new key. Then retire the old key.

For a leak that you just found, you do not know if someone used the key. Move fast, but do the steps in order.

Step 1: Fix the cause of the leak

Find where the key leaked, and close that place first. If you skip this step, the new key leaks in the same way. The section “Where did the key leak?” below shows the usual places. Do not wait for the full investigation. Remove the key from the public place now.

Step 2: Create a new secret key

  1. Open the dashboard. Select Settings, then API Keys.
  2. Create a new secret key. Give it a name that shows its use, for example backend_prod.
  3. Copy the value. Store it in your secret manager or in the environment variables of your host.

You can also use the Management API. The key value appears in the response, so run the request in a local terminal and not in CI:

curl -s -X POST "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys?reveal=true" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"type": "secret", "name": "backend_prod"}'

Supabase recommends one secret key for each backend part. Then you rotate only the part that leaked.

Step 3: Move your app to the new key

Replace the old key in every place that uses it. Use the apikey header for the new key. A secret key is not a JWT, so a tool that verifies it as a JWT fails.

  1. Server code. Change the environment variable, for example SUPABASE_SERVICE_ROLE_KEY. Deploy the app.
  2. Edge Functions. A function that uses withSupabase({ auth: 'secret' }) accepts only the secret key with the name default. Change it to the name of your new key, for example auth: 'secret:backend_prod'.
  3. Database Webhooks and pg_net. If they send the legacy service_role key in the Authorization: Bearer header, send the new secret key in the apikey header.
  4. Cron jobs and scripts. Search your repositories and your CI settings for the old value.
  5. Browser and mobile clients. If the leak was a legacy service_role key, replace the legacy anon key with the publishable key (sb_publishable_...). Supabase says that publishable keys are safe in browsers and apps.

Then confirm that every part uses the new key. Read your logs after the deploy. A part that still sends the old key shows up there.

Step 4: Retire the leaked key

Do this step only after step 3.

  • If the leaked key is an sb_secret_ key, delete it in Settings, then API Keys. Caution: You cannot undo this action.
  • If the leaked key is a legacy key, deactivate the legacy keys on the same page. This action deactivates both the anon key and the service_role key. Your clients must use the publishable key first.

You can use the Management API instead. List the keys, find the id, and delete the secret key. For the legacy keys, send PUT to the legacy path with enabled=false:

curl -s -X PUT "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/legacy?enabled=false" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"

curl -s "https://api.supabase.com/v1/projects/$PROJECT_REF/api-keys/legacy" \
  -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN"

The second request must show "enabled": false.

Warning: Do not enable the legacy keys again to fix a client that you missed. This action brings the leaked key back. Move the client to a new key.

Step 5: Revoke the legacy JWT secret

This step applies only to a leaked legacy service_role key. The legacy keys are JWTs. The legacy JWT secret signs them. A service that still trusts this secret can accept the leaked key. So you must revoke the secret.

The signing keys guide gives the path. Here is the short version:

  1. Open Settings, then JWT Keys. Click Migrate JWT secret. This creates a new key in standby. Supabase says that this step does not cause downtime.
  2. Update the code that verifies JWTs directly with the legacy secret. Use supabase.auth.getClaims().
  3. Click Rotate keys. Auth then signs new tokens with the new key. This step does not sign out your users.
  4. Disable the anon and service_role keys. Supabase requires this before the revoke.
  5. Revoke the legacy secret. It moves from Previously used to revoked.

Why can the revoke sign users out?

The legacy secret also signed the access tokens of your users. After the rotation, Supabase still accepts the old tokens until they expire. After the revoke, it does not. A user whose token was signed by the old secret loses the session.

For a normal migration, wait. If the access token lifetime is one hour, wait at least one hour and 15 minutes before you revoke the secret. Users then stay signed in.

In an active incident, do not wait. Supabase says that you can revoke the secret at once. Active users can then lose their session. Ask them to sign in again. This cost is small compared to an open service role key.

How do I check if someone used the key?

Read the logs. The goal is to find requests that did not come from your own servers.

  1. Open the API logs. In the dashboard, select Logs, then API Gateway or Edge Logs.
  2. Filter for the old key. Look for requests with the role service_role. Group them by IP address and by user agent.
  3. Compare with your servers. Your own backend uses fixed addresses, or the address range of your host. Mark every other address.
  4. Look for bulk reads. Many GET requests with select=* and large limits, in a short time, mean a data copy.
  5. Look for writes. A POST, PATCH, or DELETE from an unknown address is the most serious sign.
  6. Check the Postgres logs. Look for new roles, new functions, new triggers, and changed policies. An attacker with the service role can create a function that gives later access.
  7. Check Auth. Look for new users that you do not know, changed emails, and users with a new role.
  8. Check Storage. Look for new files and deleted files.
  9. Check the retention. Logs have a limit that depends on your plan. If the logs are too short, assume that someone used the key.

If you find a bad request, or you cannot prove that there was none, read the Supabase app hacked guide. It covers the next steps. If personal data was in the tables, ask a lawyer about your duty to tell the users.

Where did the key leak?

Find the source. The key must not leak again after the rotation.

  • The JavaScript bundle. A variable with a NEXT_PUBLIC_, VITE_, or EXPO_PUBLIC_ prefix goes into the browser code. Search the built files of your live app for service_role, sb_secret_, and eyJ.
  • The Git history. A key in a commit stays in the history after you delete the file. Search the history. The GitHub secret alert guide shows what to do when GitHub tells you about a key.
  • A public repository. Lovable, Bolt, and other tools can sync a project to GitHub. Check if the repository is public. The Lovable public project guide covers this case.
  • A chat or a prompt. You can paste a key into an AI chat so that the tool fixes a bug. Search your chat history, and delete the key from any shared project or log.
  • Hosting settings. A host can expose an environment variable in a preview deployment, a build log, or a public setting. The Vercel environment variables guide shows how to check and rotate them.
  • A mobile app. A key in an iOS or Android build is readable by anyone who downloads the app. See the iOS API key guide.
  • Screenshots, videos, and support tickets. Check the places where you shared your screen.

The service role leak pattern shows how this leak looks in real apps.

A secret key has a protection against one case. Supabase checks the User-Agent header and returns 401 when a secret key comes from a browser. This does not protect a leaked key. An attacker can send the request from a script. Rotate the key in all cases.

How do I stop this problem in the future?

  1. Keep secret keys on the server. Use the secret key only in backend code, Edge Functions, and CI jobs. Use the publishable key in the browser.
  2. Use one secret key for each backend part. If one key leaks, you delete only that key.
  3. Never use a public prefix on a secret. Do not name the variable NEXT_PUBLIC_SUPABASE_SECRET_KEY.
  4. Block commits with secrets. Turn on GitHub push protection. Add a pre-commit scanner.
  5. Keep RLS on for every table. The anon and authenticated roles then have limited access. The service role key guide explains when you need the secret key at all.
  6. Rotate on a schedule. A fixed schedule shows that your rotation steps work before an incident.
  7. Scan the live app. A scan of the bundle and the API finds a key that a tool put into the browser code.

When should I get help?

Get help in these cases:

  • The logs show requests with the service role from unknown addresses.
  • You find new functions, new triggers, or changed policies that you did not make.
  • The tables hold personal data, payment data, or health data.
  • You cannot find all the places that use the old key.
  • The app stops after the rotation and you cannot find the cause.

After the rotation, test the old key with the curl command from the confirm section. The result must be an error, not data.

Common questions

Can I rotate only the service_role key?
No. Supabase does not rotate a legacy key alone. Create a new secret key, move the app to it, then deactivate the legacy keys and revoke the JWT secret.
Is a leaked sb_secret_ key as bad as a leaked service_role key?
The key has the same power, because it maps to the service_role role. One difference helps you. You can delete one secret key and keep the others.
Why does Supabase return 401 when I use a secret key in the browser?
Supabase checks the User-Agent header and blocks secret keys in browsers. An attacker can still use the key from other tools, so you must still rotate it.
Will revoking the JWT secret sign out my users?
It can. If you revoke it at once, users with active tokens can lose their session. In an incident, accept this cost. Tell the users to sign in again.
Is deleting the commit or the file enough?
No. Anyone who saw the key can still use it. Only a rotation makes the old key stop working.

Find leaked supabase keys

A VibeEval scan checks the JavaScript bundle and the responses of your live app for Supabase keys, and tests the API with the public key to find open tables.

14-day free trial · Cancel anytime

SCAN YOUR APP →