Guides · Free check · $9 fix pack

Guides

Supabase service_role key in the browser? Rotate it and move the paid write to your webhook

The app works: users pay, the plan flips to paid. But the code that flips it runs in the browser with the Supabase service_role (or new sb_secret_) key, often added by an AI builder to get past a Row Level Security error. Anyone who opens DevTools can copy that key and read or rewrite every table, including who is paid.

Why this key is different

Supabase's rule is "where the code runs": the publishable key is for "Anything you ship: browser, mobile app, CLI, script" and "only reaches what Row Level Security allows", while the secret key is for "Anything you control: server, Edge Function, cron job" because "It bypasses Row Level Security, so it must never leave your control" (Supabase docs: API keys). The legacy service_role key is the "Legacy version of secret keys", and policies never apply to either "because service_role has the BYPASSRLS attribute" (source).

So this isn't a "the key is visible" cosmetic issue. With it, a visitor can skip your checkout and write is_paid = true for themselves, or read other users' rows.

1. Find out whether it shipped

Note: new sb_secret_ keys have a browser guard ("A secret key doesn't work in a browser. Supabase matches on the User-Agent header and returns HTTP 401 Unauthorized"), but Supabase is clear that "an attacker can still use the key from other tools" (source). A leaked secret key still needs rotating. The legacy service_role JWT has no such guard.

2. Move the paid write to the server first

Supabase says to "Fix the root cause of the leak before you rotate anything. Otherwise the new key can leak the same way" (Supabase docs: Rotate a leaked or compromised API key). For a paid flag, the root cause is usually that the browser writes it. The fix:

  1. The browser uses only the publishable (or legacy anon) key and never writes plan, credits or is_paid.
  2. Your Stripe webhook verifies the signature on the raw body, then writes the paid status with an admin client built from the secret key, on the server (Edge Function, API route, worker). Stripe's own fulfillment guide runs fulfillment on the server from the checkout.session.completed webhook (Stripe docs: Fulfill orders). Edge Functions get the key from SUPABASE_SECRET_KEYS (Supabase docs: Edge Function secrets).
  3. RLS stays on, and signed-in users can't update the paid column themselves. That part is in users can set themselves to paid; if the server write then hits an RLS error, see webhook RLS error in live mode.
// supabase/functions/stripe-webhook/index.ts (sketch; names are placeholders)
import Stripe from "npm:stripe";
import { createClient } from "npm:@supabase/supabase-js@2";

const stripe = new Stripe(Deno.env.get("STRIPE_SECRET_KEY")!);
const secretKeys = JSON.parse(Deno.env.get("SUPABASE_SECRET_KEYS") ?? "{}");
const adminKey = secretKeys["default"];
if (!adminKey) throw new Error("Supabase secret key missing"); // never fall back to the publishable key

const admin = createClient(Deno.env.get("SUPABASE_URL")!, adminKey, {
  auth: { persistSession: false },
});

Deno.serve(async (req) => {
  const body = await req.text();                 // raw body, before any parsing
  let event;
  try {
    event = await stripe.webhooks.constructEventAsync(
      body, req.headers.get("stripe-signature") ?? "",
      Deno.env.get("STRIPE_WEBHOOK_SECRET")!, undefined,
      Stripe.createSubtleCryptoProvider());
  } catch {
    return new Response("bad signature", { status: 400 });
  }
  if (event.type === "checkout.session.completed") {
    const s = event.data.object;
    if (s.payment_status === "paid") {
      const { error } = await admin.from("entitlements")
        .upsert({ user_id: s.client_reference_id, plan: "pro", checkout_session_id: s.id },
                { onConflict: "checkout_session_id" });
      if (error) return new Response("db error", { status: 500 }); // Stripe retries
    }
  }
  return new Response("ok", { status: 200 });
});

Sketch only: adapt table and column names. The browser bundle should contain no secret or service_role key at all after this change.

3. Rotate the key

Supabase's rotation steps (source):

  1. Create a new secret key in Settings > API Keys and put it in your server-side env only (Edge Function secrets, your host's server env). Creating new keys "doesn't revoke your legacy keys. Both key systems work at the same time" (source).
  2. If the leaked key is the legacy service_role, "also replace the legacy anon key with a publishable key in your browser and mobile clients. Retiring the compromised key deactivates both legacy keys" (source). Migration steps: Supabase docs: Migrating to new API keys.
  3. Retire the old key. Delete a leaked sb_secret_ key, or deactivate the legacy keys. "Deleting a secret key can't be undone", and if you later find a missed client, move it to a new key "rather than re-enabling them, because re-enabling restores the compromised key" (source). If someone is actively using the leaked key, retire it right away even though things break until you swap.
  4. For a leaked legacy service_role key, also revoke the legacy JWT secret. "The legacy keys are JWTs signed by that secret, so anything that still trusts the secret can accept the leaked key" (source).

Then review what the key could have touched: check your paid/plan table for users who are paid without a matching Stripe payment (the daily check in reconcile against Stripe catches this both ways). If a Stripe secret key sat in the same file, rotate that too: Stripe secret key exposed.

Prompt for your AI builder

The Supabase secret or service_role key must never be in frontend code. 1) Find every use of the Supabase secret/service_role key and every VITE_ or NEXT_PUBLIC_ variable that holds it, and remove them from client code. 2) The browser client must use only the publishable (or anon) key and must not write plan, credits or paid status. 3) Write paid status only in the Stripe webhook on the server, after verifying the Stripe signature on the raw body, using an admin client made from the secret key read from server-side env (SUPABASE_SECRET_KEYS in Edge Functions). If the key is missing, throw instead of falling back to the publishable key. 4) Keep RLS on and do not add policies that let users update their own paid status. 5) List the env variables I need to set on the server, and tell me to rotate the old key in Supabase.

Re-test

  1. Reload the live site with DevTools open and search all JavaScript for sb_secret_ and service_role: zero hits.
  2. Confirm the old key is retired: a request to your project's REST API with it is rejected.
  3. Send an unsigned curl -i -X POST with an empty JSON body to the live webhook URL: 400 is right, 200 means it isn't verifying (fails-open guide).
  4. Resend one recent checkout.session.completed from the Stripe Dashboard (how) and confirm access is granted once, by the server.
Check yours in 10 seconds (free).

Paste your webhook URL. WebhookProof sends 3 harmless test requests and tells you whether Stripe can reach it and whether it rejects unsigned events. If it finds a problem, the $9 fix pack gives you the exact copy-paste fix for your stack (refundable for 14 days).

Run the free check

FAQ

Is it safe to have the Supabase service_role key in my frontend?

No. The service_role key (and the new sb_secret_ secret key) bypasses Row Level Security, so anyone who copies it from your JavaScript can read and write every table. Only the publishable or anon key belongs in the browser.

How do I rotate a leaked Supabase service_role key?

Create a new secret key, move server code to it, replace the anon key with a publishable key in clients, then deactivate the legacy keys and revoke the legacy JWT secret. Re-enabling the legacy keys later restores the leaked key.

Why did my AI builder put the service_role key in the browser?

Usually to get past a Row Level Security error when the client writes paid status. The fix is to make that write from the verified Stripe webhook on the server, not to ship the secret key.

Sources

More guides

Stripe webhook not working? Check these 7 things in order · A customer paid but didn't get access. What went wrong? · No signatures found matching the expected signature for payload · Stripe webhook failing with a 307 or 308 redirect

Last reviewed 2026-11-17. WebhookProof is an independent tool, not affiliated with Stripe, Inc.. Quotes are from the linked documentation.