Is Your Secret Key Exposed in the Browser? How to Check and Rotate Without Downtime
I opened a client's production bundle and found a live Stripe sk_live key and Supabase service_role in 5 seconds. Here is why bundlers inline them and how to rotate safely.
01Run this check right now
Diagnostic TestThe 15-Second Bundle Secret Audit
Run this search across your compiled production output directory after running your build command. If any private tokens or service role keys appear in these files, every user who visits your web application downloads your administrative credentials.
grep -rnE "(sk_live_[0-9a-zA-Z]{24,}|service_role|AIza[0-9A-Za-z-_]{35}|ghp_[0-9a-zA-Z]{36})" dist/ .next/ 2>/dev/nullFindings from the Field
- /01I searched a production Vite bundle and found live Stripe secret keys and Supabase service_role credentials baked into static JS.
- /02AI models solve client-side permission errors by prefixing private environment variables with VITE_ or NEXT_PUBLIC_.
- /03Compilers do not execute process.env at runtime in browsers: they do literal string substitution during build time.
- /04A 15-second terminal grep command catches leaked tokens in dist/ before assets deploy to public CDNs.
- /05The 3-step zero-downtime key rotation protocol: deploy backend replacement, verify logs, revoke compromised keys.
The 5-second discovery in Chrome DevTools
A few weeks ago, I was doing a code audit on an AI-generated SaaS application that was already taking customer payments. Before opening their GitHub repository, I opened Chrome, went to their public marketing site, pressed `F12`, and opened the Sources tab.
I pulled down their compiled JavaScript chunk (about 412KB of minified code) and ran a regex search for `sk_live_`. Within five seconds, there it was: a live Stripe secret key, right next to their Supabase `service_role` superuser key, sitting in plain text inside a client-side React component.
Every single visitor who loaded their homepage was downloading the master administrative keys to their entire Stripe account and production PostgreSQL database.
How AI tools accidentally bake secrets into browsers
How does a master secret key end up in a browser bundle? It usually happens during a late-night debugging session.
The developer asks Cursor or Bolt to build a billing toggle. The model writes a client-side component that tries to create a customer subscription. The browser throws a CORS error or an authentication error because public keys are not allowed to create charges.
When the developer pastes the error back into the AI chat, the model takes the path of least resistance: 'Set your environment variable to `VITE_STRIPE_SECRET_KEY` or `NEXT_PUBLIC_STRIPE_SECRET_KEY` and import it into your component.'
The error disappears, the subscription works in testing, and the developer commits the code. But modern bundlers like Vite and Webpack treat any variable with that prefix as a public directive. During `vite build`, the compiler performs literal string replacement, inlining the secret key directly into the minified JavaScript code.
The bumper sticker analogy
Think of it like writing your Wi-Fi password and your master home safe combination on a bright neon sticker and pasting it onto the rear bumper of your car. It is convenient because you do not have to walk inside to check your notes, but anyone standing in traffic behind you can read the combination.
The 15-second local bundle audit
You do not need to wait for a security reviewer to catch this. Run your build locally, and search the generated output directory for known secret prefixes:
If grep outputs any matching lines with your live keys, that credential must be treated as fully compromised.
# For Vite / React projects:
bun run build && grep -rnE "(sk_live_|service_role|AIzaSy)" dist/
# For Next.js projects:
npm run build && grep -rnE "(sk_live_|service_role|AIzaSy)" .next/The zero-downtime rotation protocol
If you find an exposed key, do not panic and revoke it immediately in your dashboard. If you revoke the key before updating your code, every live customer transaction will fail with 401 errors.
We followed this exact 3-step sequence to rotate the credentials without dropping a single active customer:
Step 1: Move the secret to a server-side route
Extract the payment logic out of the React component and place it into a serverless API route. Remove the `VITE_` or `NEXT_PUBLIC_` prefix so the key remains strictly on the server runtime:
import { NextResponse } from "next/server";
import Stripe from "stripe";
// Loaded only on the server. Never exposed to browser bundles.
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2024-11-20.acacia",
});
export async function POST(req: Request) {
const { priceId, customerId } = await req.json();
const session = await stripe.checkout.sessions.create({
customer: customerId,
mode: "subscription",
line_items: [{ price: priceId, quantity: 1 }],
success_url: "https://yourdomain.com/dashboard?session_id={CHECKOUT_SESSION_ID}",
cancel_url: "https://yourdomain.com/pricing",
});
return NextResponse.json({ url: session.url });
}Step 2: Generate a replacement key and deploy
Generate a second secret key in your Stripe or Supabase dashboard. Update your server hosting environment variables (on Vercel, Railway, or AWS) with the new key, and redeploy your backend.
Watch your production telemetry for 15 minutes to confirm that incoming checkout requests succeed using the new key.
Step 3: Revoke the leaked key and audit logs
Once the new key is serving 100% of production traffic, click 'Revoke' on the old compromised key in your provider dashboard. This permanently closes the window for anyone who scraped your bundle.
Finally, check your Stripe API logs for any unusual exports or refunds during the window the key was exposed. In our audit, the leak was caught early and zero fraudulent calls had occurred.
Secrets belong in server processes, never in client assets. Now I can close my DevTools and get back to work, because yolo.
Production Hardening & Audits
Stuck on prompt regressions, leaked keys, or database scaling in an AI-built app? Explore our platform-specific teardowns:
Diagnostic & Rescue
1 business day replyThink your prototype is vulnerable or hitting scale limits?
Send us a note with your repository, Supabase link, or project details. A senior engineer will review your policies and reply within one business day.
