Fix guides

Your Supabase service_role key is in the browser.

Updated 2026-10-13 · Plain English, written for founders who built with Lovable, Bolt, Cursor or similar.

Supabase gives you two kinds of key. The anon (or publishable) key is meant for the browser, and row level security limits what it can do. The service_role (or secret) key is meant for your server only. It skips row level security on every table. Whoever has it can read, change and delete everything in your database, including other users' data.

If that key is in your frontend JavaScript, every visitor already has it. AI coding tools sometimes put it there to make a "permission denied" error go away. The error stops because the key ignores your security rules.

Check if you're affected

Open your live app, open DevTools, and search all loaded JavaScript (Sources, then search across files) for:

To decode a JWT, take the middle section (between the two dots) and base64-decode it:

echo 'PASTE.THE.KEY' | cut -d. -f2 | base64 -d 2>/dev/null; echo

If the output contains "role":"service_role", the key is exposed. "role":"anon" is the normal public key.

Also check your code for env variables with a public prefix: NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY or VITE_SUPABASE_SERVICE_KEY. The NEXT_PUBLIC_ and VITE_ prefixes exist to copy a value into the browser bundle.

Rotate the key first

Removing the key from your code does not help on its own. Everyone who loaded your site has a copy, and it is still in your git history. The old value has to stop working.

Fix the code

The browser should only ever create its client with the anon key:

// browser code
const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_ANON_KEY   // never the service key
);

Anything that genuinely needs to bypass RLS, such as an admin action or a background job, moves to a server route or a Supabase Edge Function. There the key comes from a server-only secret and the code checks who is calling:

// supabase/functions/admin-action/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";

Deno.serve(async (req) => {
  // 1. Who is asking? Use their own token, not the service key.
  const asUser = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_ANON_KEY")!, {
    global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } },
  });
  const { data: { user } } = await asUser.auth.getUser();
  if (!user) return new Response("Unauthorized", { status: 401 });

  // 2. Are they allowed? Check a role you control, not one the user can edit.
  const admin = createClient(Deno.env.get("SUPABASE_URL")!, Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!);
  const { data: staff } = await admin.from("staff").select("id").eq("user_id", user.id).maybeSingle();
  if (!staff) return new Response("Forbidden", { status: 403 });

  // 3. Only now do the privileged work.
  return Response.json({ ok: true });
});

Most of the time you do not need the service key at all. If a query failed under RLS, the fix is a policy that allows exactly that query. See the RLS guide.

Work out what happened

Check your app

SecureIndie looks for this mistake. The free quick check covers the basics from outside (exposed files, keys in your site's code, headers) in about a minute. Database rules, payment webhooks and AI endpoints need the full audit, which also reads your code and proves what a visitor can actually reach.

Questions

What is the difference between the anon key and the service_role key?

Both are JWTs signed for your project. The anon key carries role "anon" and is limited by RLS. The service_role key carries role "service_role", which bypasses RLS on every table.

I deleted the key from my code. Is that enough?

No. Anyone who loaded your site already has it, and it is still in your git history. The key has to be rotated so the old value stops working.

Why did my AI tool put it there?

Usually because a query failed under RLS and the quickest way to make the error go away was a key that ignores RLS. The real fix is a correct policy, not a stronger key.

Related guides

Checks covered: SEC-007, SEC-013, SEC-HIST-003 · If you think data already leaked