# Supabase service_role key exposed in frontend code

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/supabase-service-role-key-exposed.html
> Category: Env vars & leaked keys | Tools: Lovable, Bolt, v0, Cursor | Published: 2026-10-13 | Updated: 2026-10-13
> Weakness type: CWE-200 Exposure of Sensitive Information to an Unauthorized Actor (https://cwe.mitre.org/data/definitions/200.html)

## The error

service_role key found in the JS bundle / NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY

## Why it happens

The service_role key bypasses Row Level Security entirely. If it is in client code or in any NEXT_PUBLIC_/VITE_ variable, anyone can open DevTools, copy it and read or delete your whole database.

## How to fix it

1. Rotate the key immediately in Supabase → Project Settings → API (rotate JWT secret / API keys).
2. Remove it from all client-side code and public env vars.
3. Use only the anon/publishable key in the browser and rely on RLS.
4. Move privileged operations into a server route or Supabase Edge Function that reads the secret from server-side env.

## Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

```text
Audit this project for any use of the Supabase service_role key in client-side code or in env vars prefixed NEXT_PUBLIC_ or VITE_. Remove it from the client, use only the anon key in the browser, and move any operation that needs elevated access into a server-side API route or Supabase Edge Function that reads SUPABASE_SERVICE_ROLE_KEY from server env. List every file you changed.
```

## FAQ

### Is the anon key safe to expose?

Yes, it is designed to be public, as long as RLS policies protect your tables.

### I already deployed it. What now?

Rotate it first, then fix the code. Assume it was copied.

## Sources (official documentation)

- [Supabase: Understanding API keys](https://supabase.com/docs/guides/api/api-keys)

## Related fixes

- [Supabase Edge Function callable by anyone (the anon key passes verify_jwt)](https://unbreakmyapp.vercel.app/fixes/edge-function-callable-by-anyone-anon-key-is-a-jwt.html)
- [Admin page is hidden but not protected (anyone can open /admin or call admin APIs)](https://unbreakmyapp.vercel.app/fixes/admin-page-hidden-but-not-protected-server-side-check.html)
- [Is my Lovable app leaking data without login? A Supabase RLS audit checklist after the Crissy Field study](https://unbreakmyapp.vercel.app/fixes/lovable-app-leaking-data-without-login-audit-checklist.html)
- [AI "fixed" my Supabase error by disabling RLS or using the service_role key: how to undo it safely](https://unbreakmyapp.vercel.app/fixes/ai-fix-disabled-rls-or-swapped-service-role.html)
- [Your API keys may have leaked: how to rotate every key without breaking your app](https://unbreakmyapp.vercel.app/fixes/rotate-api-keys-after-leak-or-breach.html)

---
Unbreak My App: free fixes for broken vibe-coded apps. https://unbreakmyapp.vercel.app/
