NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY

The prefix is the whole problem. Any environment variable starting with NEXT_PUBLIC_ is inlined into the JavaScript bundle at build time, so the value is sitting in your deployed site, readable by anyone who opens developer tools.

The same applies to VITE_, REACT_APP_, EXPO_PUBLIC_, PUBLIC_, NUXT_PUBLIC_ and GATSBY_. The framework is doing exactly what it promises. The mistake is what got put behind the prefix.

Why this key in particular

Supabase issues two keys, and they are not two levels of the same thing.

So a leaked anon key is a normal Tuesday. A leaked service_role key means every row in every table can be read, changed or deleted by anyone who found it, whatever your policies say. There is no partial version of this.

What to do, in this order

1. Rotate the key. Not later, now.

In the Supabase dashboard: Settings, API, then reset the service key. Treat the old value as public from the moment it was deployed, because it was.

Rotate before you clean anything up. Cleaning first only signals which value mattered.

2. Look at what was done with it

Rotation stops future use. It tells you nothing about the past. In the Supabase logs, filter to the window between the deploy and the rotation, and look for bulk reads of tables that an ordinary user would never touch, and for deletes you cannot account for.

3. Move the key to the server

// Wrong: the value is in the bundle
const admin = createClient(url, process.env.NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY);

// Right: a server-only variable, used in server-only code
// app/api/things/route.ts
const admin = createClient(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_SERVICE_ROLE_KEY   // no public prefix, never reaches the browser
);

Worth knowing: with no public prefix, the variable is simply undefined in the browser. The framework will not leak it by accident. The prefix is the only thing that ever exposed it.

4. Ask whether you need it at all

Most uses of the service key are a workaround for Row Level Security getting in the way. That is worth resisting. A correct policy plus the ordinary client is safer than an admin client guarded by application code you might delete next month, because the policy applies to every path into the database and the code only applies to the path it sits on.

5. Clean the history, with realistic expectations

git ls-files | grep -E '^\.env'
git rm --cached .env.local
printf '.env\n.env*.local\n' >> .gitignore

Rewriting history on a public repository is rarely worth the disruption: assume forks already have the old commit. On a private repository with a small team, git filter-repo followed by a force push is reasonable, and everyone must re-clone.

Stopping it from happening again

The reliable fix is mechanical rather than disciplinary. A check that runs on every pull request and fails the build when a secret-shaped value appears behind a public prefix will catch this every time, including the times you are tired.

Related


Check your own project

One command. No install, no signup, and it never touches the network. It reads your repository and tells you which tables are reachable and which keys are exposed, with the fix beside each finding.

npx github:humora2504/vibeproof .

Free and MIT licensed. If you want the policies generated for your own schema, and a script that proves an anonymous visitor is refused, there is a $19 Fix Pack. The scanner stays free either way.