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.
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.
sb_secret_ and for service_role. Legacy keys are JWTs that start with eyJ; Supabase notes that "If a tool, tutorial, or AI assistant tells you to copy a long key that begins with eyJ, it was written for the legacy keys" (source). Two eyJ keys in the bundle usually means anon and service_role are both there.VITE_ will be exposed in client-side source code after Vite bundling" (Vite docs: Env variables and modes). Next.js: a NEXT_PUBLIC_ variable "will be inlined into any JavaScript sent to the browser" (Next.js docs: Environment variables). VITE_SUPABASE_SERVICE_ROLE_KEY is public whatever the name says..env files committed by accident. Deleting the line in a new commit doesn't remove it from history.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.
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:
anon) key and never writes plan, credits or is_paid.checkout.session.completed webhook (Stripe docs: Fulfill orders). Edge Functions get the key from SUPABASE_SECRET_KEYS (Supabase docs: Edge Function secrets).// 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.
Supabase's rotation steps (source):
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.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.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.
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.
sb_secret_ and service_role: zero hits.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).checkout.session.completed from the Stripe Dashboard (how) and confirm access is granted once, by the server.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 checkNo. 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.
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.
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.
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.