How to Fix Supabase API Key Exposure (2026)

The Supabase service_role key gives whoever holds it complete, unrestricted access to your database: every row, every table, no authentication checks, no RLS policies. In our scans of apps built with Bolt, Lovable, and Cursor, finding the service_role key in a client-side JavaScript bundle is one of the most common critical findings on Supabase-connected apps.

Two things make it harder than it should be. Older projects have two legacy keys that look almost identical (both long JWTs starting with eyJhbGci), and newer projects have a different pair, sb_publishable_ and sb_secret_. And older guides, ours included until this update, describe a "Reset" button next to each key. Supabase's dashboard doesn't have one. Retiring a leaked legacy service_role key takes the anon key down with it, so doing it in the wrong order breaks your app for every user at once.

TL;DR

If the leaked key starts with sb_secret_, or is a JWT whose payload says "role": "service_role", it bypasses all Row Level Security: treat it as an incident. An sb_secret_ key you replace and delete on its own. A legacy service_role key can only be retired by moving clients to the new keys, deactivating both legacy keys, and revoking the legacy JWT secret. If the key is anon or sb_publishable_, don't rotate anything; check your RLS policies.

Which Key Leaked?

This is the only question that decides what you do next. Supabase has had four key types, and only two of them are an emergency:

What you foundWhat it isBypasses RLS?What to do
sb_secret_...Secret key (new)YesRotate now: Step 2, path A
eyJhbGci... with "role": "service_role"Legacy admin keyYesRotate now: Step 2, path B
sb_publishable_...Publishable key (new)NoDon't rotate. Verify RLS (Step 5)
eyJhbGci... with "role": "anon"Legacy public keyNoDon't rotate. Verify RLS (Step 5)

If you want the full model (why there are four, which replaces which, and when the legacy pair goes away), that lives in our Supabase API keys guide. This page is about the incident: a key is out, now what.

AI coding tools tend to leak the admin key the same way. You ask for a quick admin feature or a data seed script, the tool builds an admin client in a component file, and the key compiles into your JavaScript bundle.

Never pass a service_role or sb_secret_ key to createClient() in a file under src/, app/, components/, or any directory that gets bundled for the browser. If it's there, any visitor can pull it out of the bundle and query your database directly. Supabase does return a 401 when a secret key is sent from a browser, but its own docs warn that a leaked key still works from curl or a script.

Step 1: Confirm Which Key Is Exposed

1

Check the prefix, then decode if it's a JWT.

An sb_secret_ or sb_publishable_ prefix answers the question on sight. A legacy key is a JWT, and the word service_role never appears in it as plain text; it's base64-encoded in the second segment. Paste this into your browser console, replacing YOUR_KEY:

JSON.parse(atob('YOUR_KEY'.split('.')[1]))

You'll see output like:

// anon key: not an emergency
{ "role": "anon", "iss": "supabase", "iat": 1710000000 }

// service_role key: rotate immediately
{ "role": "service_role", "iss": "supabase", "iat": 1710000000 }
2

Check your deployed JavaScript bundle.

Open your live app, press F12, go to Sources, and search all files (Ctrl+Shift+F) for sb_secret_, then for eyJhbGci. Decode every JWT you find. A plain-text search for service_role only catches variable names, not the key itself.

3

Search your codebase for admin keys in client code.

# Admin key references in source files
grep -rn "service_role\|SERVICE_ROLE\|sb_secret_\|SUPABASE_SECRET" --include="*.ts" --include="*.tsx" --include="*.js" --include="*.jsx" --include="*.vue" .

# Admin keys behind a public prefix (bundled into the browser by design)
grep -rn "NEXT_PUBLIC_SUPABASE_SERVICE_ROLE\|VITE_SUPABASE_SERVICE_ROLE\|NEXT_PUBLIC_SUPABASE_SECRET\|VITE_SUPABASE_SECRET" .

Any hit under src/, app/, components/, or pages/ is a problem.

4

Check git history for committed keys.

git log --all --full-history -- .env .env.local .env.production
git log -S 'sb_secret_' --oneline --all

If these return commits, the key was in your repo. Anyone who cloned or forked it may have a copy, even if you've since removed the file.

CheckYourVibe scans your deployed app's JavaScript for sb_secret_ keys and for Supabase JWTs. It decodes each JWT and only flags the ones whose role is service_role, so a public anon key doesn't show up as a false alarm.

Step 2: Retire the Leaked Admin Key

Fix the cause first (Step 4), or at least know where the key is used, then rotate. Supabase's API keys doc says to rotate "as soon as you fix the cause of the leak", because a new key dropped into the same leaky spot is leaked again on the next deploy. Every key lives on one dashboard page: Settings > API Keys.

Path A: a leaked sb_secret_ key

This is the easy case, and the reason the new key format exists.

1

Create a replacement secret key in Settings > API Keys. Name it after where it will live (server-api, edge-functions, ci).

2

Put the new key in every component that used the old one and redeploy. Server environments, Edge Function secrets, CI secrets, cron jobs. Supabase's rotation guide says to wait until every component uses the new key before you go further.

3

Delete the compromised key in Settings > API Keys. The guide warns this can't be undone, which is fine: that's the point. Nothing else stops working, because each secret key is independent.

Path B: a leaked legacy service_role key

There's no per-key reset here. The legacy anon and service_role keys are both JWTs signed by your project's legacy JWT secret, so the only way to kill the leaked one is to stop trusting that secret. In practice that means migrating to the new keys under pressure. The order below follows Supabase's rotation guide.

1

Create a secret key and swap it into every server. In Settings > API Keys, create an sb_secret_ key and replace SUPABASE_SERVICE_ROLE_KEY with it everywhere: Vercel, Railway, Render, Edge Function secrets, GitHub Actions, the .env on the laptop you run seed scripts from.

2

Move your browser and mobile clients to a publishable key. This is the step people miss. The next step deactivates anon too, so any frontend still shipping the eyJhbGci anon key will start failing.

3

Confirm every component is on the new keys. Log in as a real user, trigger your webhooks and scheduled jobs, and check Logs for 401s.

4

Deactivate the legacy keys in Settings > API Keys. Supabase's guide is explicit that retiring the compromised key "deactivates both legacy keys".

5

Revoke the legacy JWT secret. Until you do, anything that trusts that secret can still accept the leaked key. This happens on the JWT signing keys page: migrate the secret, rotate to the new signing key, then revoke the old one. Supabase's signing keys guide suggests waiting at least 1 hour 15 minutes (with the default 1-hour token expiry) so logged-in users aren't signed out, and says that in an active incident you can revoke immediately and accept the sign-outs.

Revoking the legacy JWT secret affects user sessions, not just API keys, because it's also what signed your users' access tokens. If your own code verifies JWTs against that secret, logged-in users can start failing RLS with auth.uid() returning null. Check for that before you click.

Then check what the key was used for

Whichever path you took, open Logs in the dashboard, filter the API logs to the window when the key was exposed, and look for requests you don't recognise: bulk selects on user tables, deletes, calls to the auth admin endpoints. Rotation stops future access. It doesn't tell you what already left.

Step 3: Remove from Git History (if committed)

Rotation stops the current key from working. But if the key was ever in a git commit, everyone who cloned the repo before the scrub may still have a copy.

# Install git-filter-repo
pip install git-filter-repo

# Remove .env files from all history
git filter-repo --path .env --invert-paths
git filter-repo --path .env.local --invert-paths
git filter-repo --path .env.production --invert-paths

# If you committed the key directly in a config file, use path-regex
git filter-repo --paths-glob "**/*supabase*key*" --invert-paths

After scrubbing, force-push to all remotes and ask collaborators to re-clone. Add the files to .gitignore:

echo ".env" >> .gitignore
echo ".env*.local" >> .gitignore
echo ".env.production" >> .gitignore
git add .gitignore && git commit -m "chore: gitignore .env files"

Step 4: Fix Client Code to Use the Public Key with RLS

Rotating the key stops the immediate problem. Fixing the code is what stops the new key leaking the same way on your next deploy.

The Pattern AI Tools Generate

Tools like Bolt and Lovable sometimes generate an "admin" Supabase client in a component when you ask for operations that need admin access:

// Wrong: service_role in a React component or Vue page
import { createClient } from '@supabase/supabase-js';

const supabaseAdmin = createClient(
  process.env.VITE_SUPABASE_URL!,
  process.env.VITE_SUPABASE_SERVICE_ROLE_KEY! // this ends up in the bundle
);

The fix has two parts: use the publishable key (or the legacy anon key) in the browser, and write RLS policies so that key can only access what it should.

// Right: public key in client-side code, controlled by RLS
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL!,
  import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY! // sb_publishable_..., governed by RLS policies
);

Keep Admin Keys in Server-Side Code Only

For operations that actually need admin access (seed scripts, webhook handlers, admin APIs), put the secret key in a server-side function:

// server/api/admin-action.post.ts (Nuxt server route)
// api/admin-action.ts (Vercel Route Handler)
// supabase/functions/admin-action/index.ts (Supabase Edge Function)

import { createClient } from '@supabase/supabase-js';

const supabaseAdmin = createClient(
  process.env.SUPABASE_URL!,
  process.env.SUPABASE_SECRET_KEY! // sb_secret_..., no VITE_ or NEXT_PUBLIC_ prefix
);

// This code never runs in a browser

If you're on Next.js, store it in SUPABASE_SECRET_KEY (no NEXT_PUBLIC_ prefix) and read it only in Route Handlers or Server Actions. If you're on Vite (Lovable, Bolt), VITE_-prefixed variables go into the browser bundle by design, and a plain Vite SPA has no server at all. Put admin work in a Supabase Edge Function or another backend and call it from the client. Edge Functions already get your keys injected as SUPABASE_SECRET_KEYS (and the legacy SUPABASE_SERVICE_ROLE_KEY), so there's nothing to paste.

Step 5: Enable RLS on Every Table

Once your client code is using the publishable or anon key, RLS is what keeps that key safe. Without it, the public key can read and write every exposed table for anyone who visits.

Open your Supabase dashboard, go to Database > Tables, click each table, and check Row Level Security. Enable it if it's off. Then add policies that match your app's access model.

Minimum policy for user data (users can only read their own rows):

-- Enable RLS first
ALTER TABLE profiles ENABLE ROW LEVEL SECURITY;

-- Users can read their own profile
CREATE POLICY "profiles_select_own"
  ON profiles FOR SELECT
  USING (auth.uid() = user_id);

-- Users can update their own profile
CREATE POLICY "profiles_update_own"
  ON profiles FOR UPDATE
  USING (auth.uid() = user_id);

For public data (anyone can read, only authenticated users can write):

CREATE POLICY "posts_public_read"
  ON posts FOR SELECT
  USING (true);

CREATE POLICY "posts_auth_insert"
  ON posts FOR INSERT
  WITH CHECK (auth.uid() IS NOT NULL);

A table with RLS enabled but zero policies blocks all access, including from authenticated users. After enabling RLS, test that your app still functions correctly. If you see empty results where you expected data, you're missing a SELECT policy.

Step 6: Prevent Future Leaks

Add Gitleaks as a pre-commit hook:

brew install gitleaks  # or: go install github.com/zricethezav/gitleaks/v8@latest

# Test your repo now
gitleaks detect --source . --verbose

# Add pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
gitleaks protect --staged --verbose
if [ $? -ne 0 ]; then
  echo "Gitleaks found potential secrets. Commit blocked."
  exit 1
fi
EOF
chmod +x .git/hooks/pre-commit

Gitleaks' default config has no Supabase-specific rule (we checked its gitleaks.toml). Its generic jwt rule will flag a legacy key in a commit, anon or service_role alike, but nothing in the defaults matches sb_secret_. Add one in a .gitleaks.toml at the repo root:

[extend]
useDefault = true

[[rules]]
id = "supabase-secret-key"
description = "Supabase secret key"
regex = '''sb_secret_[A-Za-z0-9_-]{20,}'''
keywords = ["sb_secret_"]

Environment variable checklist:

VariableBrowser-safe?Storage
NEXT_PUBLIC_SUPABASE_URL or VITE_SUPABASE_URLYesFrontend env var
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY or VITE_SUPABASE_PUBLISHABLE_KEY (sb_publishable_)Yes (with RLS)Frontend env var
NEXT_PUBLIC_SUPABASE_ANON_KEY or VITE_SUPABASE_ANON_KEY (legacy)Yes (with RLS)Frontend env var
SUPABASE_SECRET_KEY (sb_secret_)NoServer-only env var
SUPABASE_SERVICE_ROLE_KEY (legacy)NoServer-only env var

Is the Supabase anon key safe to expose?

Yes. The anon key, and its replacement the sb_publishable_ key, are designed to sit in your frontend. What they can reach is decided entirely by your Row Level Security policies, so if one is exposed the thing to check is RLS, not the key. The danger is the service_role key and sb_secret_ keys, which bypass every RLS policy.

How do I tell which Supabase key is in my frontend?

Look at the prefix. sb_secret_ is an admin key and must be rotated. sb_publishable_ is meant to be public. A key starting with eyJhbGci is a legacy JWT: run JSON.parse(atob('YOUR_KEY'.split('.')[1])) in the browser console and read the role field. anon means check RLS; service_role means treat it as an incident.

What can someone do with my Supabase service_role key?

Supabase's API keys doc says a secret key gives full access to your project's data, bypassing Row Level Security, and the legacy service_role key works the same way. Whoever holds it can read, change, or delete any row in any table, and use the admin auth and storage APIs. Treat it like a database superuser password.

How do I rotate a leaked Supabase service_role key?

There's no per-key Reset button for legacy keys. Supabase's rotation guide has you create a new sb_secret_ key in Settings > API Keys and put it in every server, move browser and mobile clients to a publishable key, deactivate the legacy keys (anon goes down with service_role), and then revoke the legacy JWT secret. A leaked sb_secret_ key is simpler: create a replacement, swap it in, delete the old one.

Can I use the service_role key in Supabase Edge Functions?

Yes. Edge Functions run server-side, so their environment variables never reach the browser. Supabase injects the keys for you: SUPABASE_SECRET_KEYS and SUPABASE_PUBLISHABLE_KEYS, plus the legacy SUPABASE_SERVICE_ROLE_KEY and SUPABASE_ANON_KEY. Edge Functions are a good home for any operation that needs admin access.

Check your Supabase deployment for service_role and sb_secret_ keys in your JavaScript bundle, tables missing RLS, and open policies. Free scan, no signup required.

How-To Guides

How to Fix Supabase API Key Exposure (2026)