Vibe Code Guardian
← All posts

Exposed API Keys in AI-Generated Code: Why Vibe Coders Leak Secrets and How to Stop It

AI coding tools love to hardcode API keys, commit .env files, and paste secrets into frontend code. Here's how to catch every leak before your app goes live, plus the one workflow that prevents it happening again.

Aaron Allen·

The problem isn't AI. It's what you do with what it gives you

You asked Cursor to "add Stripe payments." It did. It also committed your STRIPE_SECRET_KEY to the repo, exposed NEXT_PUBLIC_SUPABASE_ANON_KEY in a client bundle, and left your OpenAI key sitting in a commented-out console.log three files deep.

This isn't a hypothetical. It's the default behaviour of every major AI coding tool when you don't explicitly constrain it.

Vibe coders ship fast. They also leak fast. The average time from first commit to exposed secret in a public GitHub repo is under 4 hours. Attackers run automated scanners 24/7. They don't need to target you. They just need to find you.


Where AI tools leak secrets (the pattern is always the same)

1. Hardcoded keys in generated code

// What Cursor writes when you say "add Stripe"
const stripe = require('stripe')('sk_live_51H...your_real_key_here');

The model has seen millions of examples with placeholder keys. It doesn't know yours is real unless you tell it.

2. Committed .env files

You create .env.local. The AI suggests git add . because "that's how you commit." Now your Supabase service role key is in history. And git rm --cached doesn't purge it from the remote.

3. Client-side exposure via NEXT_PUBLIC_ / VITE_ / REACT_APP_ prefixes

AI tools treat any env var with a "public" prefix as safe for the browser. It's not. Your Supabase anon key is meant for the browser. But your service role key, Stripe secret, and OpenAI key are not. The AI doesn't know the difference.

4. Secrets in logs, comments, and debug output

// Debug helper the AI added
console.log('Initializing Stripe with:', process.env.STRIPE_SECRET_KEY);

That line ships to production. Your key is now in every user's devtools console.

5. Config files the AI "helpfully" creates

vercel.json, netlify.toml, wrangler.toml, docker-compose.yml. The AI populates them with real values because you pasted them in chat once, three prompts ago.


The 5-minute audit: catch every leak before launch

Run these in order. Each takes seconds.

1. Scan the repo history (not just the working tree)

# TruffleHog catches keys in *any* commit, not just HEAD
trufflehog git file://. --json | jq '.[] | {commit: .commit, file: .path, type: .detectorType}'

If this returns anything, rotate those keys immediately. They're already compromised.

2. Search for hardcoded patterns in the current codebase

# Common secret patterns (add your provider-specific prefixes)
grep -rEi "(sk_live_|rk_live_|sk_test_|api_key|secret_key|access_token|private_key)" \
  --include="*.ts" --include="*.js" --include="*.json" --include="*.py" .

3. Verify env var prefixes match their actual sensitivity

Prefix Actually public? Common mistake NEXT_PUBLIC_ Only if you intend it for the browser Stripe secret, Supabase service role, DB URL VITE_ Same as above Any backend-only key REACT_APP_ Same Same EXPO_PUBLIC_ Same Same

Rule: If the key grants write access, charges money, or reads PII, it never gets a public prefix.

4. Check build outputs, not just source

# Next.js example: search the compiled output
grep -r "sk_live_" .next/ 2>/dev/null || echo "Clean"

Vercel/Netlify/Cloudflare Pages env vars are injected at build time. If your build process reads a secret at compile time (e.g., process.env.STRIPE_SECRET_KEY in a getStaticProps), it ends up in the client bundle.

5. Verify .gitignore actually ignores what matters

git check-ignore -v .env .env.local .env.production .env.staging vercel.json netlify.toml wrangler.toml

Every file above should return a match. If one doesn't, it's tracked.


The workflow that prevents this forever

Stop pasting secrets into chat. Every time you paste a key into Cursor/Claude/Lovable/Bolt, you've expanded the blast radius.

Instead, use this loop:

  1. Local-only .env: never committed, never shared with AI

  2. AI writes code that reads from process.env: you provide the variable names only in chat

  3. CI/CD injects secrets at deploy time: GitHub Actions secrets, Vercel env vars, Cloudflare Workers secrets

  4. Pre-commit hook runs TruffleHog on every commit: blocks the push if it finds anything

# .github/workflows/secret-scan.yml
name: Secret Scan
on: [push, pull_request]
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for TruffleHog
      - uses: trufflesecurity/trufflehog@main
        with:
          path: ./
          base: ${{ github.event.repository.default_branch }}
          head: ${{ github.sha }}

What you tell the AI instead of pasting keys

"Add Stripe checkout. Read the secret key from process.env.STRIPE_SECRET_KEY. Don't hardcode anything. Create a .env.example with placeholder values."

The AI writes clean code. You keep the actual key in your password manager and CI/CD. Zero exposure.


What to do if you've already leaked a key

  1. Rotate immediately: generate a new key, revoke the old one. Don't "check if it was used." Assume it was.

  2. Purge from git history: git filter-repo or BFG Repo-Cleaner, then force-push. This is painful but necessary.

  3. Check provider logs: Stripe, Supabase, OpenAI, AWS all show recent API calls. Look for unfamiliar IPs or volumes.

  4. Add the pre-commit hook: so it doesn't happen on the next feature.


This is exactly what the free check catches

Our free surface check scans your repo for:

  • Hardcoded secrets in current and past commits

  • Client-side exposure of backend-only keys

  • Missing/weak .gitignore coverage

  • Build-time secret injection risks

  • CI/CD configuration that leaks env vars

It takes 60 seconds. No code leaves your machine. You get a prioritised fix list.

If you want the full picture: auth gaps, RLS mistakes, error handling, deployment risks. The launch-readiness audit covers every category that kills AI-built apps in production.


TL;DR checklist for your next PR

  • No hardcoded keys in any file (run TruffleHog)

  • No .env* files tracked by git

  • No backend-only keys prefixed with NEXT_PUBLIC_ / VITE_ / REACT_APP_ / EXPO_PUBLIC_

  • No secrets in logs, comments, or debug output

  • No secrets in config files (Vercel, Netlify, Cloudflare, Docker)

  • Pre-commit hook installed and passing

  • CI/CD injects secrets at deploy time only

  • .env.example exists with placeholders only

Ship fast. Don't leak fast. The audit is free. The fix is a few minutes. The alternative is a rotated key, a purged repo, and an uncomfortable conversation with your users.

Run the free check →

Want a developer to look at your app?

Built your app with AI tools? Run the free surface check yourself in a few minutes. Nothing to buy, and we never look at your app unless you run it.