← All posts
stripesecret key leakvibe codingnext.jssecurity

Leaked Stripe Secret Key Fix Vibe Coding: Next.js Step-by-Step

O

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:

  • [ ] Revoke the leaked key – Go to Stripe Dashboard > Developers > API Keys and click "Roll" or "Deactivate".
  • [ ] Check Stripe logs – Look for unusual API calls from unknown IPs or unexpected charges.
  • [ ] Scan your Git history – Use git log -p -S 'sk_live_' to find commits containing the key.
  • [ ] Check all branches – The key may be in feature branches or stashed changes.
  • [ ] Audit environment variables – Verify your .env.local, Vercel/Netlify env vars, and CI/CD secrets.
  • [ ] Review third-party services – If you committed to GitHub, assume the key is public. Check for any services that had access (e.g., deployment platforms, monitoring tools).
  • [ ] Rotate related keys – If you used the same key in multiple apps, rotate them all.
  • 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 push

    3. Add a `.env` file to `.gitignore`

    Ensure your .env and .env.local files are never committed. Add this to .gitignore:

    .env
    .env.local
    .env.*.local

    4. 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:

  • Hardcoding keys in components – AI tools like Cursor sometimes inline API keys in client components. Always move them to server-side environment variables.
  • Committing `.env` files – Many AI-generated projects miss the .gitignore entry. Double-check it.
  • Using `NEXT_PUBLIC_` for secrets – Next.js exposes any env variable prefixed with NEXT_PUBLIC_ to the browser. Never put secret keys there.
  • Not scanning Git history – Even if you remove the key now, it remains in history. You must purge it.
  • Ignoring staging/CI environments – Leaked keys in a staging .env are just as dangerous if that environment is publicly accessible.
  • Prevention

  • Use a free AI app security scanner to automatically detect secrets in your codebase.
  • Enable branch protection and require scans before merging.
  • Rotate keys regularly and use Stripe's restricted keys with least privilege.
  • 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.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free