# Service role key: where it may live and where it must never appear

The service role key bypasses every RLS policy. Anyone holding it can read and write your whole database. Agents treat it like the anon key and scatter it across client code. This skill is the containment rule.

## Where it may live

1. Server runtimes you control: Edge Functions (as a secret), your API server, background workers, admin scripts run locally.
2. CI/CD as a protected secret, used only by migration and seed jobs.

## Where it must never appear

1. Any `NEXT_PUBLIC_` / `VITE_` / `EXPO_PUBLIC_` variable. Those prefixes mean "embed in the client bundle". Grep your repo for the key name next to those prefixes; every hit is a live leak.
2. Mobile app binaries, Flutter, Swift, or Kotlin code. Binaries can be decompiled.
3. Frontend source, even in `.env.local` files that get committed. `.env.local` belongs in `.gitignore`, and the service key belongs nowhere near the frontend directory.
4. Error messages, logs, or URLs. The key in a query string ends up in access logs.

## Checkable procedure

1. Rotate the service role key in the dashboard if it ever touched client-side code. Assume compromise, not maybe.
2. After rotation, update every server consumer at once: Edge Function secrets, server env, CI secrets. A missed consumer fails loudly, which is better than a leaked key failing silently.
3. Prefer the new publishable key (`sb_publishable_`) for client code and keep the legacy anon key only where needed. Both are safe in clients; neither bypasses RLS.

## Quick test

Search your built frontend bundle and your mobile binary for the service role key prefix. Zero hits is the only passing result.