supabase
How to stop leaking your Supabase keys in a vibe-coded app (anon vs service role, RLS)
Learn why the Supabase anon key belongs in the browser, why service_role never does, how RLS protects your tables, and exactly how to rotate after a leak.
Paste your production URL into the free Vibe Code Health grader for a Semrush-style score on secrets, headers, TLS, and common leaks—then come back to this guide for the fixes.
If you vibe-coded an app with Supabase inside Lovable, Bolt, Cursor, or a Next.js template, you almost certainly pasted an API key into the client. That is not automatically wrong—but the wrong key in the client is one of the fastest ways to lose the entire database.
This post explains, in beginner language:
- What the anon (public) key is for
- Why the service_role key must never ship to the browser
- How Row Level Security (RLS) makes the anon key safe
- Concrete steps to find a leak, rotate, and harden
When you are done reading, paste your public URL into the Vibe Code Health scanner. It flags service_role JWTs in HTML/JS as a live secret (grade F) and treats a normal anon key as an informational note—not a failure—because anon belongs in the client when RLS is correct.
The two keys people mix up
Supabase projects expose API credentials that look like long JWTs. The important difference is the role claim inside the token, not how scary the string looks.
| Key | Typical env name | role claim | Browser OK? |
|---|---|---|---|
| Anon / public | NEXT_PUBLIC_SUPABASE_ANON_KEY, VITE_SUPABASE_ANON_KEY | anon | Yes |
| Service role | SUPABASE_SERVICE_ROLE_KEY | service_role | No |
Official docs: Understanding API keys.
You can paste a JWT into jwt.io (payload is base64—not encrypted) and read "role": "anon" vs "role": "service_role". Do that on a key you already consider public; do not paste production service keys into random websites if you can decode locally.
What anon is allowed to do
The anon key is a label for “untrusted client.” It does not mean “anonymous users can do anything.” What anon can read or write is decided almost entirely by:
- Whether RLS is enabled on each table
- The policies you wrote (or forgot to write)
Supabase’s model is: put anon in the client, protect data with RLS. See Row Level Security.
What service_role can do
The service role key is designed for trusted servers. It bypasses RLS. That is useful for admin jobs, webhooks, and privileged server routes. It is catastrophic in a Vite/React bundle, a Lovable client component, or a public GitHub repo.
If service_role ever hit the browser or a public repo, assume an attacker already copied it.
How vibe-coding tools accidentally leak service_role
Patterns we see constantly:
- “Make auth work” prompts — the model dumps both keys into a
supabaseClient.tsused by the browser. - Copy-paste from .env examples — templates show
SERVICE_ROLEnext toANON, and both get prefixed withNEXT_PUBLIC_orVITE_. - Server code imported by client components — a Next.js server helper that imports
SERVICE_ROLEgets pulled into a Client Component bundle. - Debug screens — “config” pages that
console.logthe wholeimport.meta.envobject.
Rule of thumb:
- Names that start with
NEXT_PUBLIC_orVITE_are public. SERVICE_ROLEshould have no public prefix and should only be read in server-only files.
Next.js documents the NEXT_PUBLIC_ exposure model here: Environment variables.
Step-by-step: find out what you shipped
1. Search the built assets
On the deployed site, open DevTools → Sources or Network → open JS files → search for service_role and eyJ (JWT prefix). Also search your repo:
rg -n "service_role|SERVICE_ROLE" --glob '!node_modules'
2. Decode any JWT you find
If the payload contains "role":"service_role", treat it as a full compromise of data access for that project.
3. Check the Supabase dashboard
Project Settings → API: confirm which keys exist, and whether you can roll them. Review the Table Editor for RLS status on every table.
4. Run a passive grader
Scan your URL. Vibe Code Health looks for service_role JWTs in HTML and JS and caps the grade at F when found. An anon key alone does not fail the scan.
Step-by-step: if service_role leaked
Do this in order. Do not “just remove it from the code” without rotating.
- Rotate / revoke the service_role key in the Supabase dashboard (Settings → API). Generate a new secret for servers only.
- Rotate related secrets if they lived in the same
.envthat may have been exposed (/.envon the static host, screenshots, chat logs). - Audit data access — check for unexpected rows, auth users, or storage objects. If you store payments or personal data, follow your incident process.
- Remove the key from client bundles — redeploy after confirming no
NEXT_PUBLIC_/VITE_reference remains. - Enable RLS everywhere — a new service key does not help if anon can still
select *with empty policies. - Re-scan the production URL until the live-secret finding is gone.
Step-by-step: make anon + RLS actually safe
Enable RLS
For each table in Supabase SQL or the dashboard:
alter table public.profiles enable row level security;
With RLS enabled and no policies, the default for anon/authenticated clients is deny. That is a good starting point.
Add least-privilege policies
Example: users can read/update only their own profile:
create policy "Profiles are readable by owner"
on public.profiles
for select
to authenticated
using (auth.uid() = id);
create policy "Profiles are updatable by owner"
on public.profiles
for update
to authenticated
using (auth.uid() = id)
with check (auth.uid() = id);
Adjust table/column names to your schema. Prefer authenticated over wide-open anon writes. For public read-only marketing content, an explicit for select to anon using (true) on a safe table is OK—never on payments or admin_notes.
Supabase’s policy primer: RLS docs.
Keep service_role on the server
Next.js App Router (server-only):
// lib/supabase/admin.ts — import only from Server Components / Route Handlers
import { createClient } from "@supabase/supabase-js";
export function createAdminClient() {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const key = process.env.SUPABASE_SERVICE_ROLE_KEY!; // NOT NEXT_PUBLIC_
return createClient(url, key, {
auth: { persistSession: false, autoRefreshToken: false },
});
}
Browser client:
import { createClient } from "@supabase/supabase-js";
export const supabaseBrowser = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
);
Never import createAdminClient from a file marked "use client".
Lovable / Bolt / v0 habits
When you prompt the AI:
Use only the Supabase anon key in client code. Keep service_role in server-only env vars. Enable RLS on every table with default deny and owner-scoped policies using auth.uid().
Then verify the generated files yourself—models still mix the keys under deadline pressure.
Storage, Edge Functions, and “hidden” admin paths
- Storage buckets: public buckets are world-readable by design. Private buckets need policies too (Storage RLS).
- Edge Functions: may use the service role internally—that is fine if the function itself checks auth and is not an open proxy to
sql. - Database webhooks / cron: also privileged; do not expose their secrets in the client.
How this maps to Vibe Code Health grades
| Finding | Typical grade impact |
|---|---|
service_role JWT in JS/HTML | Forced F |
| Anon JWT in JS/HTML | Info only (0 deduction)—fix RLS |
Exposed /.env containing secrets | Cap at D or worse if live secrets inside |
Use the grader after each deploy. Pair it with the beginner security checklist.
Sources worth keeping open
- Supabase API keys
- Supabase Row Level Security
- Next.js environment variables
- JWT introduction (Auth0) — for understanding claims vs encryption
Anon in the browser is normal. Service role in the browser is an incident. RLS is the seatbelt that makes the normal path survivable—put it on before you share the link.