vibe-audit.lab

guides · 2026-11-14

Why Your Supabase Service Role Key Must Never Appear in Frontend Code

Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team

The short answer

Supabase ships two keys: the anon key, designed to be public and policed by Row Level Security, and the service role key, which bypasses RLS completely. If the service role key is in your frontend bundle, anyone can read and write every table as if RLS did not exist. Move it to a server-only environment variable, rotate it, and audit your tables for changes you did not make.

Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.

What is Service role key?

Supabase's administrative API key. Requests made with it skip Row Level Security policies entirely and run with full access to the project's data — by design, for trusted server-side jobs only. In a browser it is not a key; it is the whole database, published.

Two keys, opposite jobs

Supabase's model confuses newcomers because both keys are long JWTs that look alike. The anon key is a public identifier: the database still enforces your Row Level Security policies on every request, so a visitor can only see rows your policies allow. The service role key exists for backend tasks — migrations, admin jobs, webhooks — that legitimately need to work across all rows, so Supabase exempts it from RLS entirely.

AI generators blur this distinction constantly. Asked to 'fix the permission error', a model will swap the anon key for the service role key because that makes the error disappear — the app starts working, and the database is now wide open. Vibe-Code Audit's secret scanner carries a dedicated Supabase service key pattern precisely because this swap is one of the most damaging single-line mistakes in vibe-coded apps.

How to check whether yours is exposed

Open your live site, view the page source and search the JavaScript bundles for 'eyJ' — both keys are JWTs, so decode the payload of any you find (it is base64, not encryption) and read the role claim: 'anon' or 'service_role'. Also grep your repo for the key value and for the string service_role near client initialisation code. If the service key appears in anything the browser downloads, it is compromised — not 'at risk', compromised — because bundles are cached, archived and scraped.

While you are there, test your RLS from the outside: call your API with the anon key and try to read another user's row by ID. Many apps that kept the correct key still never enabled RLS on new tables the AI added later, which produces the same open database through a different door.

Recovery: rotate, relocate, audit

Rotate the service role key in the Supabase dashboard first — that instantly devalues every copied bundle. Then relocate: the new key lives only in server-side environment variables (an API route, an edge function with server context, a backend job), and the frontend goes back to the anon key plus real RLS policies. Expect things to break at this point: every place that secretly depended on bypassing RLS now gets permission errors, and each one is a policy you need to write properly — that breakage is the audit you were owed.

Finally, audit for abuse: review recent changes in sensitive tables (roles, balances, credits), look for accounts you do not recognise, and check Supabase logs for service-role requests originating from browser user agents or residential IPs during the exposure window.

Prompting patterns that prevent the swap

Prevention is a sentence long. In every prompt that touches data access, state the constraint explicitly: 'Use the Supabase anon key in client code with Row Level Security; the service role key must never appear in frontend code.' When a generated fix involves keys at all, treat it as a security change and re-scan before merging. And schedule the check: a monthly grep of your shipped bundle for the current service key's fingerprint takes two minutes and catches regressions introduced by any tool, human or AI.

Worked example: Fernway Fitness, a class-booking app

Fernway Fitness (fictional), a booking app for 3 studios with about 1,900 members, pasted its frontend code into a scan and got a CRITICAL finding: 'Exposed secret: Supabase service key'. Decoding the bundle's JWT showed role: service_role. With that key, a tester (the owner) listed all 1,900 member emails and could flip any booking's paid flag. The team rotated the key within the hour, moved two admin operations into server routes, wrote RLS policies for the 6 tables the app used, and spent an evening fixing the permission errors that revealed where the bypass had been load-bearing. A follow-up scan of the bundle came back clean, and log review found no foreign service-role calls.

Five things to do before you ship

  1. Search your shipped JavaScript bundles and repo for JWTs; decode each one and confirm no payload carries role: service_role.
  2. If the service key is exposed anywhere client-side, rotate it in the Supabase dashboard immediately — before refactoring anything.
  3. Move every service-key use into server-only code with the key in an environment variable; return the frontend to the anon key.
  4. Enable and test Row Level Security on every table: with the anon key, confirm one user cannot read or modify another user's rows.
  5. Re-scan the pasted frontend code and the live bundle, and review database logs for service-role activity you cannot explain.

Sources

Frequently asked questions

How can I tell the anon key from the service role key?

Decode the JWT payload (base64) and read the role field. It literally says anon or service_role. Never guess from the variable name — AI-generated code mislabels them often.

The key is in a private repo but not the website. Still rotate?

If it never shipped to browsers and the repo stayed private, rotation is still the safe call — repos get cloned, shared and screenshotted. At minimum, move it out of the repo into environment variables and narrow who can read it.

My app breaks when I use the anon key. What now?

That breakage is information: the app was relying on bypassing RLS. Write the Row Level Security policies your data model actually needs, table by table, instead of restoring the bypass.

Does rotating the service key log users out?

No. User sessions are separate JWTs signed by the project's auth secret. Rotation changes only the administrative key — your server jobs using the old key are what break, so update their environment first.

Related guides

Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back