← All posts
API key leakvibe codingsecurityexposed secrets

Fix Exposed API Keys in Vibe Coded Apps: A Step-by-Step Guide

O

OverMCP Team

Quick answer

Exposed API keys in vibe-coded apps are a critical security risk that can lead to unauthorized access, data breaches, and financial loss. To fix them: identify all exposed keys using a secret scanner, revoke and rotate the leaked keys immediately, move them to environment variables or a secrets manager, and implement a monitoring strategy to prevent future leaks. This guide walks you through the entire process, from detection to prevention.

What to check first

Before you panic, run through this checklist to gauge the damage and prioritize your response:

  • Scan your codebase for any hardcoded secrets (API keys, tokens, passwords) using a tool like OverMCP's secret leak scanner.
  • Check your version control history for keys committed in the past—they might still be valid.
  • Look at your client-side code (JavaScript, HTML) for keys that should never be there, like database credentials or admin API keys.
  • Review your environment variables in your deployment platform (e.g., Vercel) to ensure they're set correctly and not exposed in build logs.
  • Check third-party services (like Stripe, AWS) for any unusual activity or charges that might indicate a key has been used maliciously.
  • Inspect your app's frontend code for any API keys that are meant to be secret but are embedded in the bundle.
  • Step-by-step fix

    1. Detect exposed keys

    Use a secret scanner to automatically find keys in your codebase. OverMCP's secret leak scanner can scan your GitHub repo or local files for patterns like sk-, AKIA, etc. Alternatively, you can use grep for a quick check:

    grep -r "sk-\|AKIA\|AIza" . --exclude-dir=node_modules --exclude-dir=.git

    If you find matches, note the file and line number.

    2. Rotate the keys immediately

    Once you've confirmed a leak, revoke the key from the service provider and generate a new one. For example, with Stripe:

  • Go to the Stripe Dashboard > Developers > API Keys.
  • Click "Roll key" for the leaked key.
  • Update your environment variables with the new key.
  • For AWS, you can deactivate the access key in IAM and create a new one.

    3. Move secrets to environment variables

    Never hardcode secrets in your source code. Instead, use environment variables. In a Next.js app, you'd create a .env.local file:

    STRIPE_SECRET_KEY=sk_live_...
    OPENAI_API_KEY=sk-...

    Then reference them in your code:

    const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

    In Vercel, you can add these via the Dashboard or CLI, and they'll be injected at runtime.

    4. Add a pre-commit hook

    To prevent future leaks, add a tool like git-secrets or a pre-commit hook that scans for known patterns. For example, using husky and lint-staged:

    {
      "husky": {
        "hooks": {
          "pre-commit": "lint-staged"
        }
      },
      "lint-staged": {
        "*.{js,ts,jsx,tsx}": ["secret-scan"]
      }
    }

    5. Monitor and rotate periodically

    Even after fixing, regularly scan your repo and use a continuous security monitoring service to alert you if new keys leak.

    Common mistakes

  • Assuming only the latest commit matters: Keys committed months ago are still valid if not rotated. Hackers scan historical commits.
  • Putting secrets in client-side code: Even if your app is a SPA, any key in the browser is exposed. Use a backend proxy or serverless functions to handle sensitive API calls.
  • Using the same key for multiple services: If one service leaks, all are compromised. Use separate keys per service and per environment.
  • Not checking build logs: Some CI/CD pipelines print environment variables in logs. Ensure your deployment platform doesn't expose them.
  • Ignoring third-party dependencies: Secrets can be accidentally committed in node_modules or other dependencies. Always add node_modules to .gitignore.
  • FAQ

    How do I know if my API key has been leaked?

    If you've committed it to a public repo, assume it's leaked. Check for unusual activity in the service dashboard, like unexpected API calls or charges. Use a secret scanner to detect patterns.

    Can I just delete the repo to fix a leaked key?

    No. Deleting the repo doesn't revoke the key. You must rotate the key at the provider. Even if the repo is private, it's best to rotate.

    How often should I rotate my API keys?

    It's good practice to rotate keys every 90 days, especially for production. Also rotate immediately after any suspected leak or when a developer with access leaves.

    ---

    For a comprehensive check of your app's security, use OverMCP's free AI app security scanner to catch other vulnerabilities like missing security headers or SSL issues. And remember, prevention is better than cure—integrate scanning into your workflow early.

    If you're deploying on Vercel, you can also use OverMCP's Vercel security scanner to automatically monitor your deployments for exposed secrets.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free