# Supabase service_role key exposed in the frontend: what to do now

Canonical: https://caelo.sh/guides/supabase-service-role-key-exposed
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** Treat it as leaked. The service_role key and the newer `sb_secret_` keys bypass row level security, so anyone who copies one from your JavaScript can read and change every table. Switch the browser client to the publishable or anon key, move privileged work into server code, then revoke the leaked key in Settings > API Keys.

## How do you tell which key shipped to the browser?

Supabase has two kinds of keys. The publishable key (`sb_publishable_...`) and the legacy anon key are meant for the browser. The secret key (`sb_secret_...`) and the legacy service_role key are not. Supabase's docs say they "provide full access to your project's data, bypassing Row Level Security."

The new keys say what they are in their prefix. The legacy keys are both JWTs that start with `eyJ`, so you have to decode one to see its role. Decoding is local and does not call Supabase:

Decode a legacy key (Node.js 18 or later)

```js
// paste the key between the quotes; this only decodes it
const key = "eyJ...";
const payload = JSON.parse(Buffer.from(key.split(".")[1], "base64url").toString());
console.log(payload.role); // "anon" is the public key, "service_role" is the leak
```

Find candidate keys in your built files

```bash
npm run build
grep -roE "sb_secret_[A-Za-z0-9_-]+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+" dist | sort -u
```

Change `dist` to `.next/static` for a Next.js app. Every string that comes back is in the JavaScript your visitors download.

## The vulnerable pattern and the fix

AI builders often create one Supabase client and reuse it everywhere. When that client gets the secret key so that an admin feature works, the key ships to every visitor.

Vulnerable: a browser client built with the secret key

```ts
// src/lib/supabase.ts, bundled for the browser
import { createClient } from "@supabase/supabase-js";

export const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_SERVICE_ROLE_KEY // every visitor gets this
);
```

Fixed: publishable key in the browser, secret key only on the server

```ts
// src/lib/supabase.ts, browser
export const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY // or the legacy anon key
);

// server only: an API route, server action or Edge Function
const admin = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_SECRET_KEY!);
```

Supabase's docs say a secret key does not work in a browser: Supabase matches on the `User-Agent` header and returns HTTP 401. That guard is documented for the new secret keys, so do not rely on it for a legacy service_role JWT, and do not rely on it at all for a key someone copies into a script.

## Fix it, step by step

1. Search your source for `service_role`, `SERVICE_ROLE`, `sb_secret_` and any `eyJ` string, and decode each JWT as shown above.
2. Point every browser client at the publishable key or the anon key.
3. Move each feature that needed the secret key (deleting users, admin dashboards, writes that skip a policy) into server code. Supabase Edge Functions get `SUPABASE_SECRET_KEYS` and the legacy `SUPABASE_SERVICE_ROLE_KEY` as default secrets, so you never need to paste the key into a file.
4. Revoke the leaked key. A new secret key can be deleted in Settings > API Keys and replaced; Supabase says the new keys are not JWTs, so revoking one does not touch the others. Supabase's troubleshooting docs say it is "no longer possible to rotate the legacy anon, service and JWT secrets", so for a leaked legacy service_role key, move to publishable and secret keys and then disable the legacy keys in Settings > API Keys.
5. Rebuild and redeploy so the live bundle no longer contains the key, then repeat the grep on the new build.
6. Assume the data was readable while the key was exposed. Check that row level security is on with narrow policies (see our RLS guide) and review your project's logs in the Supabase dashboard for activity you do not recognise.

## Check it automatically

Caelo decodes every JWT it finds in your project files on your machine and flags the ones whose role is service_role (rule `secrets.supabase-service-role`). It does not flag the anon key, or the fixed demo key that every local `supabase start` uses. It also flags `sb_secret_` keys (`secrets.supabase-secret`) and a secret-sounding name behind a browser prefix such as `NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY` (`browser.public-secret-name`). It reads files only and does not read git history, so a key you deleted last week will not show up.

Part of the real report from the demo app in the Caelo README

```text
- Supabase service-role key in source
    src/lib/admin.ts:3  (secrets.supabase-service-role)
    value: service_role JWT (ref xyzcompany)
    why: A JWT with role=service_role bypasses row-level security. The anon key is fine; this one is not.
```

- [Install Caelo: npx caelo . or the MCP server](/)
- [How Caelo compares with gitleaks, TruffleHog, Semgrep and others](/compare)

## Check your own app

1. Run the free local check: `npx caelo .` Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and `.env` files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## Questions

### Is the Supabase anon key also a leak?

No. The anon key and the publishable key are designed to be public, and row level security decides what they can reach. Our anon key guide explains how to check that your policies actually protect your data.

### My repository is private. Do I still need to revoke the key?

If the key was in browser code, yes: anyone who opened your site could read it, whatever the repository settings. If it was only in a private repository, revoking is still the safe choice, because everyone with access, every clone and every connected integration has a copy.

### Can I keep using the legacy service_role key?

For now it works until you disable it, but Supabase is moving projects from the legacy anon and service_role keys to publishable and secret keys. New secret keys can be revoked one at a time, which makes the next leak much cheaper to fix.

### Does Supabase block the service_role key in the browser?

Supabase documents a browser block for the new secret keys only: it checks the `User-Agent` header and returns 401. That stops an accidental browser call. It does not stop someone who copied the key into curl or a script.

## Sources

- [Supabase docs: Understanding API keys (publishable, secret, anon and service_role).](https://supabase.com/docs/guides/api/api-keys)
- [Supabase docs: Migrating to the new API keys.](https://supabase.com/docs/guides/getting-started/migrating-to-new-api-keys)
- [Supabase troubleshooting: Rotating anon, service and JWT secrets.](https://supabase.com/docs/guides/troubleshooting/rotating-anon-service-and-jwt-secrets-1Jq6yd)
- [Supabase docs: Edge Function secrets (default secrets).](https://supabase.com/docs/guides/functions/secrets)
- [Supabase GitHub discussion #29260: Upcoming changes to Supabase API keys.](https://github.com/orgs/supabase/discussions/29260)
- [Caelo source: every rule in lib/rules.js.](https://github.com/arthi-arumugam-git/caelo/blob/master/lib/rules.js)

## Related guides

- [Is it safe to expose the Supabase anon key in my frontend?](https://caelo.sh/guides/is-supabase-anon-key-safe-to-expose)
- [I leaked an API key or the Supabase service_role key. What do I do now?](https://caelo.sh/guides/leaked-api-key-what-to-do)
- [How to check your Supabase row level security is actually working](https://caelo.sh/guides/supabase-rls-check)
- [How do you secure a Supabase Edge Function?](https://caelo.sh/guides/supabase-edge-functions-security)
- [Are NEXT_PUBLIC_ environment variables secret? What leaks, and how to fix it](https://caelo.sh/guides/next-public-env-variables-secrets)
