Dev.to Security 🔐 Cybersecurity 👁 0 📖 1 min read

Supabase anon key vs service_role key: what is safe to ship in your frontend

Short answer: the anon key is designed to be public. The service_role key must never leave your server. If a service_role key is in your browser bundle, rotate it today. Anon key. It identifies your project and acts as

Short answer: the anon key is designed to be public. The service_role key must never leave your server. If a service_role key is in your browser bundle, rotate it today.

Anon key. It identifies your project and acts as the "anonymous" or "authenticated" role. What it can read or write is decided by Row Level Security policies. So the anon key is only as safe as your RLS.

Service role key. It bypasses RLS completely. Anyone holding it can read and change every row.

How it leaks in AI-built apps. Builders often put the key in a VITE_ or NEXT_PUBLIC_ variable to make a call work from the browser. Those prefixes mean "send this to every visitor".

How to check. Search your deployed JavaScript for service_role or decode any eyJ token and look at the role claim. Do not test a found key by calling the API with it.

Fix. 1) Revoke and rotate the key in the Supabase dashboard. Removing it from the code is not enough. 2) Move privileged calls to a server route. 3) Review project logs for suspicious use. 4) Turn on RLS for every table and write policies per operation. 5) Rebuild and purge cached bundles, then rescan.

Test your RLS. Make two test users. With user B's session, try to read user A's rows. With no session, try again. Both should fail.

ProofForge's free scan checks the bundle and counts rows an anonymous visitor can read, without reading row contents. Free first look: https://proofforge.live

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.