Leaked Stripe Secret Key Fix Vibe Coding: Next.js Step-by-Step
OverMCP Team
Quick answer
If you've leaked a Stripe secret key in a vibe-coded Next.js app, immediately revoke the key in the Stripe Dashboard, rotate it, and check for unauthorized charges. Then scan your entire codebase, Git history, and environment variables for any other exposed secrets. Use a secret leak scanner to automate detection and prevent future leaks.
What to check first
Before you panic, run through this checklist to contain the leak and assess the damage:
git log -p -S 'sk_live_' to find commits containing the key..env.local, Vercel/Netlify env vars, and CI/CD secrets.Step-by-step fix
1. Revoke and rotate the key
Go to the Stripe Dashboard and revoke the compromised key immediately. Create a new key and update your environment variables.
2. Remove the key from your codebase
Delete the key from all files, including .env, .env.local, config files, and any hardcoded strings. Use a tool like git filter-branch or bfg-repo-cleaner to purge it from Git history:
# Remove from Git history using BFG (recommended)
bfg --delete-text "sk_live_..." my-repo.git
# Then force push3. Add a `.env` file to `.gitignore`
Ensure your .env and .env.local files are never committed. Add this to .gitignore:
.env
.env.local
.env.*.local4. Use environment variables properly in Next.js
In your Next.js app, access secrets only server-side:
// pages/api/create-payment-intent.js
export default async function handler(req, res) {
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
// ...
}Never use NEXT_PUBLIC_ for secret keys, as they get exposed to the client.
5. Set up a pre-commit hook
Prevent future leaks with a Git hook or a tool like OverMCP's continuous security monitoring that scans every commit.
Common mistakes
Vibe-coded apps often make these errors:
.gitignore entry. Double-check it.NEXT_PUBLIC_ to the browser. Never put secret keys there..env are just as dangerous if that environment is publicly accessible.Prevention
FAQ
How do I know if my leaked Stripe secret key was used maliciously?
Check your Stripe Dashboard's API logs for any requests originating from unexpected IPs or with unusual parameters. Also review recent charges and refunds.
Can I just delete the key from my code without rotating?
No. If the key is already exposed, you must rotate it immediately. Deleting it from code does not invalidate the existing key.
What other secrets might be leaked in a vibe-coded app?
Common leaks include OpenAI API keys, AWS credentials, Supabase service role keys, and JWT secrets. Run a full scan with a tool like OverMCP's secret leak scanner to find them all.