Beamfar

Supabase anon vs service_role and Firebase API keys: what is safe in frontend code

Developers shipping apps on Supabase or Firebase run into the same confusing moment right after launch: someone opens browser DevTools, spots a NEXT_PUBLIC_SUPABASE_ANON_KEY or a Firebase apiKey in the Network tab or JavaScript bundle, and asks whether the site just leaked its database credentials. Sometimes it is a completely harmless public project identifier; other times a single misplaced service_role key or missing Row Level Security policy hands full read/write access to every customer row in the database. Knowing the exact line between safe publishable identifiers and critical server secrets saves both panicked false alarms and real breaches.

The core rule: client identifiers identify a project, server secrets bypass rules

Any key delivered to a browser — whether inside HTML, __NEXT_DATA__, a Vite bundle, or an HTTP request header from client JavaScript — must be treated as public knowledge. Both Supabase and Firebase explicitly design one tier of key to be public, while relying on server-enforced database policies to control what a browser caller may read or modify.

Key or credentialSafe in browser JS?What it actually allows
Supabase anon / publishable key (role: "anon")Yes, only when Row Level Security (RLS) is enabled and tested on every exposed tableIdentifies your Supabase project and authenticates requests as the restricted anon or signed-in authenticated Postgres role
Supabase service_role / secret key (role: "service_role")NeverBypasses every Row Level Security policy with full administrative read, write, and delete privileges across the entire database
Direct PostgreSQL URL (postgres://postgres:...@db...supabase.co)NeverDirect database superuser or role login bypassing API gateways and RLS completely
Firebase Web apiKey (AIza...) in firebaseConfigYes, only when Firestore / Realtime Database / Storage Security Rules enforce access and API key restrictions are configuredIdentifies your Firebase project to Google APIs; per Firebase API key documentation, it is not a secret by itself
Firebase Admin SDK service account JSON (private_key PEM)NeverFull administrative access to Firebase Auth, Firestore, Storage, and Google Cloud APIs, bypassing all client Security Rules

Check it now. The frontend secret leak checker scans public HTML, __NEXT_DATA__ and up to four first-party JavaScript bundles across 12 live credential families without storing any secret; the security headers checker reads a page’s HSTS, CSP, X-Content-Type-Options, frame protection, Referrer-Policy and Permissions-Policy. No account, and the address is not stored.

How to verify your Supabase key is really anon and not service_role

Both legacy Supabase keys (anon and service_role) look almost identical at a glance because both are signed JSON Web Tokens starting with eyJ.... That visual similarity is why developers and AI editors accidentally paste the service_role token into NEXT_PUBLIC_SUPABASE_ANON_KEY or VITE_SUPABASE_KEY.

You can check what role a JWT carries in seconds without sending it anywhere: decode the middle Base64URL segment of the token and inspect the "role" claim.

  • "role": "anon" means it is the publishable client token documented in Supabase API Keys.
  • "role": "service_role" (or "supabase_admin") in any client file, HTML response, or public repository is an immediate production compromise. Rotate the JWT secret or revoke the secret key inside the Supabase dashboard first, move privileged calls into a server route or Edge Function, and inspect database logs for unauthorized reads or writes.

Modern Supabase projects also support opaque sb_publishable_... and sb_secret_... keys so humans and scanners can distinguish safe browser keys from server-only secrets by prefix alone. If you are starting a new project or rotating legacy keys, prefer the distinct prefix format so a secret key can never be mistaken for a client token.

Why a safe anon key still leaks data when RLS is off

Having a legitimate anon key in your frontend does not protect your database if Row Level Security is turned off or misconfigured. By default, a newly created Postgres table in the public schema has RLS disabled unless you explicitly enable it. When RLS is off, anyone holding your public project URL and anon key can query GET /rest/v1/<table_name>?select=* and dump every row in seconds.

Follow these four concrete steps from the Supabase Row Level Security guide before going live:

  1. Audit every table in the public schema: verify rowsecurity = true on every user, billing, order, message, and settings table. Even tables intended only for backend access should either live in a non-exposed private schema or have RLS enabled with zero anon/authenticated policies.
  2. Test as an anonymous caller from outside: run a real curl request against your Supabase REST endpoint using only the apikey: <anon_key> header and confirm it returns an empty set or authorization error for every private table.
  3. Check UPDATE and DELETE policies separately from SELECT: an overly broad USING (true) or WITH CHECK (true) clause written while prototyping lets any logged-in user overwrite or delete another tenant’s rows — the exact Broken Access Control pattern tracked in OWASP Top 10 A01.
  4. Audit RPC functions and Storage buckets: check SECURITY DEFINER Postgres functions and storage bucket policies so anonymous users cannot list private folders or trigger admin routines.

How to harden Firebase web apps beyond the public apiKey

Firebase web applications embed apiKey, authDomain, and projectId inside client JavaScript by design. The real security boundary sits in three places:

  1. Firestore, Realtime Database, and Storage Security Rules: never ship test-mode rules such as allow read, write: if true; or allow read, write: if request.auth != null; on multi-tenant collections. Always scope access to request.auth.uid == resource.data.ownerId or verified custom tenant claims.
  2. Restrict the Google Cloud API key: in Google Cloud Console, restrict your browser apiKey to your production HTTP referrers and only the specific Firebase APIs your web client actually calls (such as Identity Toolkit and Token Service), so the key cannot be abused against unrelated paid Google Cloud APIs.
  3. Keep Firebase Admin credentials strictly server-side: never commit serviceAccountKey.json to Git, never drop -----BEGIN PRIVATE KEY----- PEM blocks into frontend environment variables, and verify that server-only Next.js or Express modules are never imported into client components.

What to do right now if a service_role key or Admin PEM leaked

Treat any privileged credential that appeared on a public URL, in a JS bundle, or in a public Git commit as already harvested by automated bots. Removing it from the code alone is not enough.

  1. Rotate the compromised secret immediately in the Supabase dashboard, Google Cloud IAM, or database provider before shipping the code fix.
  2. Move privileged work behind a server endpoint (Next.js Route Handler, Supabase Edge Function, Express/FastAPI backend) that validates the caller’s session and checks tenant ownership before invoking admin privileges.
  3. Audit recent activity: review auth logs, newly created admin accounts, billing webhooks, storage uploads, and unexpected database reads or exports from the exposure window.
  4. Re-scan the live production build to confirm neither the HTML hydration state nor any built JavaScript bundle still carries a privileged secret or public source map.

How Beamfar helps

Beamfar distinguishes safe publishable keys from real production leaks automatically: our passive secret scanner decodes exposed JWTs in memory to flag privileged Supabase service_role tokens, PEM private keys, database connection strings with passwords, and live Stripe, AWS, OpenAI, Anthropic, GitHub, and Resend credentials — while ignoring safe anon and pk_live_ publishable keys so teams get zero false alarms on normal frontend configs. Raw secret values are redacted immediately and never stored. Test your live site now with our free frontend secret leak checker, or enable daily external security monitoring from the Solo plan alongside two-region uptime and CISA KEV Exploit Watch.

Questions

Is it safe to commit NEXT_PUBLIC_SUPABASE_ANON_KEY or put it in frontend JavaScript?

Yes, provided it truly carries "role": "anon" (never "service_role") and Row Level Security is enabled and tested on every exposed Postgres table and storage bucket. The anon key is designed to be public, while RLS enforces who can access each row.

Why do security scanners flag my Firebase apiKey (AIza...)?

Generic regex scanners often treat every AIza... string as a secret Google Cloud key. Per Firebase documentation, the web apiKey identifies your project and is expected in client code — your real protection is strict Firestore and Storage Security Rules plus HTTP referrer and API restrictions in Google Cloud Console.

How can I check whether my deployed site accidentally bundled a service_role key or private PEM?

Enter your public URL in the free frontend secret leak checker. It inspects the live HTML, __NEXT_DATA__, and first-party JavaScript bundles, decodes JWT role claims in memory without storing any values, and tells you immediately if a privileged key is exposed.

Sources

Read on . Standards and vendor pages change; confirm the current text before you rely on it.

Terms used in this guide

  • HSTSHSTS is a response header telling browsers to use only HTTPS for a site. What max-age and includeSubDomains do, and why an expired certificate then hurts.
  • TLS certificateA TLS certificate proves a website holds the name in the address bar and enables HTTPS. What it contains, how long it lasts, what expiry breaks.
  • HTTP status codeAn HTTP status code is the three-digit number that opens a server’s response: 2xx success, 3xx redirect, 4xx client error, 5xx server error.

Try it on your own sites.

Add your sites: checking starts as soon as you do.

No card for the Free plan.