Fixing a Leaked Stripe Secret Key (sk_live_)
A Stripe secret key (sk_live_*) grants full API control over your Stripe account: reading customer PII, issuing refunds, creating payouts, and modifying subscriptions. It must NEVER appear in browser JavaScript.
Founders paste both pk_live_ and sk_live_ into .env with NEXT_PUBLIC_ or VITE_ prefixes, or ask an AI to call Stripe directly from a React button component.
- Attackers can read every customer email, refund charges, or tamper with subscriptions
- Stripe's automated scanners may suspend your account if leaked keys are detected publicly
- Even restricted keys should never ship inside frontend bundles
Vulnerable Pattern vs. Production Fix
"use client";
// NEVER DO THIS: NEXT_PUBLIC_STRIPE_SECRET_KEY is compiled straight into browser JS!
const res = await fetch("https://api.stripe.com/v1/checkout/sessions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY}`,
},
});// app/api/checkout/route.ts — SERVER ONLY
import Stripe from "stripe";
import { requireSession } from "@/lib/auth";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); // No NEXT_PUBLIC_ prefix!
export async function POST() {
const session = await requireSession();
const checkout = await stripe.checkout.sessions.create({
mode: "subscription",
customer_email: session.user.email,
line_items: [{ price: process.env.STRIPE_PRO_PRICE_ID!, quantity: 1 }],
success_url: `${process.env.NEXT_PUBLIC_SITE_URL}/dashboard?upgraded=1`,
cancel_url: `${process.env.NEXT_PUBLIC_SITE_URL}/pricing`,
metadata: { userId: session.user.id },
});
return Response.json({ url: checkout.url });
}Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)
Audit our codebase for Stripe key exposure: 1. Search all files for sk_live_, sk_test_, rk_live_, NEXT_PUBLIC_STRIPE_SECRET, or VITE_STRIPE_SECRET. 2. Remove every secret Stripe reference from client components and client-prefixed env vars; keep ONLY publishable keys (pk_live_/pk_test_) in the frontend if Stripe.js is needed. 3. Move all Checkout Session, Billing Portal, and Subscription creation calls into authenticated server-side API routes or Supabase Edge Functions using server-only STRIPE_SECRET_KEY. 4. Confirm our Stripe webhook route verifies stripe-signature using STRIPE_WEBHOOK_SECRET.
How to Verify Your Fix Worked
- 1Immediately roll/revoke the leaked key in Stripe Dashboard -> Developers -> API Keys.
- 2Review Stripe Dashboard -> Logs for any unauthorized API calls during the exposure window.
- 3Redeploy and run VibeSecCheck to verify zero sk_live_ patterns appear in production bundles.
Frequently Asked Questions
Is it safe to expose pk_live_ (publishable key) in the frontend?
Yes. Stripe publishable keys (pk_live_ / pk_test_) are designed to be public for Stripe.js Elements. Only secret keys (sk_*) and restricted keys (rk_*) must stay strictly on the server.
How quickly should I roll a leaked Stripe key?
Immediately — within minutes. Stripe lets you roll a key with a short grace window or expire it immediately to cut off attackers right away.
Should I also check my Stripe price IDs from the client?
Never trust a price amount sent from the client. Map plan names ('pro', 'studio') to server-side allowlisted Stripe Price IDs in your API route.