HackOrBroke

Guide

Supabase service_role key in the frontend: what to do

Updated November 18, 2026

TL;DR

The anon key in your frontend is normal. It is safe only when Row Level Security is on and the policies actually limit rows. The service_role key is the opposite: it bypasses RLS, and it must never be in the browser.

That includes a NEXT_PUBLIC_ or VITE_ variable. Those prefixes publish the value into the JavaScript bundle at build time. If the key shipped, regenerate it in the Supabase dashboard's API settings, move privileged calls to the server, and check the logs.

Anon key versus service_role

Supabase apps built in Lovable, Bolt, Cursor, and similar tools talk to the database from the browser. They ship two public values: the project URL and the anon key. The anon key is a JWT whose role is anon. It is not a password. Newer projects may call the public key a publishable key. Same idea: it is meant to be in the browser.

What that public key can see is decided by grants and by Row Level Security. If RLS is off, or a policy is FOR ALL USING (true), anyone who copies the key out of your JavaScript can read and write the rows the grants allow. How to check that is in How to check if Supabase RLS is on (Lovable, Bolt, Cursor apps).

The service_role key bypasses RLS. So does a secret key that Supabase treats as the same role. Either one in the browser is full access for anyone who loads the page. It is not a second anon key, and it is not safe because the repo is private. The bundle is public once the site is deployed.

How it ends up in the bundle

Three patterns show up in generated apps:

  • NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY, or any NEXT_PUBLIC_ name holding that secret. Next.js inlines every NEXT_PUBLIC_ variable into the client bundle. The .env file can be gitignored. The built JavaScript is not.
  • A VITE_ variable, which Vite publishes the same way.
  • A string pasted into a client component, or a createClient call in a file marked "use client", that passes the service role key as the second argument.

A server-only name, SUPABASE_SERVICE_ROLE_KEY with no public prefix, stays on the server unless a client component imports the module that reads it. Importing it from use client pulls the value into the bundle.

What to do if it is already exposed

Editing the code does not un-publish a key that is already in a deployed bundle. Do these before you finish the patch. Dashboard labels move, so this is the action, not a click-path:

  1. Regenerate the service_role key in the Supabase dashboard's API settings. The old value has to stop working. If you create a replacement and the next deploy still ships it to the browser, regenerate that one too.
  2. Put the new value only in a server-side environment variable. No NEXT_PUBLIC_ prefix. No VITE_ prefix. Redeploy the server after you rotate, or the server will keep using the old value until you do.
  3. Search the new JavaScript bundle for the key, not only the source. A source map makes the original file easier to read, so check that too.

Do not paste the full key into chat, a ticket, or a screenshot while you look. Regenerate it first.

Move privileged calls server-side

The browser should keep using the anon key, or the publishable key, and RLS should decide what that key can see. Anything that truly must bypass RLS belongs in a server route, a server action, or an edge function that reads the secret from the server environment.

Check who is calling before you use that client. A service role client will happily update any row you ask it to. The request body is not a user id you should trust on its own.

Then confirm RLS on the tables the anon key can still reach. Rotating the service role key does not turn RLS on. How to check if Supabase RLS is on (Lovable, Bolt, Cursor apps) walks through the badges, the policies, and a logged-out request you can run against your own project.

Check the logs

After you rotate, look at the Supabase API logs for the window before the rotation. You are looking for requests you did not make, especially writes, against tables that should have been limited. The log will not always name the person. A burst of requests from the Data API, with the service role, is enough reason to assume the rows were exposed.

HackOrBroke cannot do this look-up for you. The free scan does not connect to your project.

A prompt you can paste into Lovable or Cursor

Paste this into Cursor or Lovable after you have started the rotation. It changes code. It cannot invalidate a key that is already public, and it should not print the new key.

The Supabase service_role key (or any secret key that bypasses Row Level Security) is reachable from the browser. Remove it from client components, from any NEXT_PUBLIC_ or VITE_ variable, and from any module a client component imports.

Keep the anon key, or the publishable key, in the browser. That public key is expected. Do not replace it with the service role key.

Move any call that needs to bypass RLS into a server route, server action, or edge function. Read the secret from a server-only environment variable. Check who the user is before that call.

Do not print the key, do not log it, and do not commit it. Do not query production tables to test this.

When you are done, list the files that used to contain the service role key so I can confirm the new bundle does not. I will regenerate the key in the Supabase API settings myself.

What the free scan does and does not do

The free scan fetches the public page you submit and a few of its JavaScript files. If those files contain a supabase.co URL, an anon key, or a service_role key, the report says so. An anon key is called out as normal, with a reminder to check RLS. A service_role key is the one that should not be there.

The scan does not connect to your database, list tables, read policies, or open storage. It does not log in. A project that only uses Supabase from a route the scan did not open will not show a Supabase finding at all. If you use Supabase, check the dashboard anyway.

Common questions

Is the Supabase anon key safe to expose?

It is meant to be public. Lovable, Bolt, and Cursor apps ship it in JavaScript on purpose. It is safe only with Row Level Security on and policies that do not allow every row. The anon key does not bypass RLS. A table with RLS off, or a policy of FOR ALL USING (true), is the leak.

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

The anon key identifies the project and is limited by RLS and grants. The service_role key bypasses RLS. Anyone who copies the service_role key out of your bundle can read and write rows the anon key should never see. Keep the anon key in the browser. Keep the service_role key on the server.

I put the service_role key in a NEXT_PUBLIC_ variable. Is deleting the line enough?

No. NEXT_PUBLIC_ and VITE_ variables are copied into the built JavaScript. Deleting the line in the next commit does not erase the copy already deployed, and it does not stop a key that was already downloaded. Regenerate the key in the Supabase API settings, store the new value only in a server-side variable, and redeploy.

Does HackOrBroke check whether RLS is on?

No. The free scan does not connect to your database and does not query your tables. It only looks at the public page and a few scripts. If it sees a Supabase URL or key, it reminds you to check RLS yourself. A reminder is not a test of your policies.

Run the free scan

Point it at a URL you own. If the page contains a Supabase URL or a service_role key, the report says so. It does not connect to your database, and it does not read your tables.

Run the free scan

Related: How to check if Supabase RLS is on (Lovable, Bolt, Cursor apps)