← All posts
v0API key leaksecurityvibe codingsecrets management

v0 API Key Leak Fix: Step-by-Step Guide for AI-Built Apps

O

OverMCP Team

Quick answer

If you've discovered a leaked API key in your v0 app, revoke it immediately, remove it from your codebase and environment variables, then rotate it. Use a secret scanner to find all instances, and adopt a secrets management workflow to prevent future leaks. This guide walks you through the exact steps for a v0 project, tailored to the way AI coding tools like v0 generate code.

What to check first

Before you panic, run through this checklist to assess the damage and stop the bleeding:

  • Identify the exposed key: Was it an OpenAI, Stripe, Supabase, or other service key? Knowing the provider helps you understand the blast radius.
  • Check if it's a public or secret key: Public keys (like Stripe publishable keys) are meant to be exposed, but secret keys (like sk- keys) must be kept private.
  • Search your entire codebase: Use grep -r "sk-" . or a tool like the secret leak scanner to find every occurrence.
  • Check git history: Even if you remove the key now, it might be in previous commits. Use git log -p to find when it was added.
  • Review your deployed app: If your app is live, the key might be in the client-side bundle. Check your browser's DevTools under Sources to see if it's exposed.
  • Assess usage: Check the provider's dashboard for unusual activity (e.g., unexpected API calls, new charges).
  • Step-by-step fix

    1. Revoke the leaked key immediately

    Go to your service provider's dashboard (e.g., OpenAI, Stripe, Supabase) and revoke or delete the leaked key. Most providers allow you to do this instantly. For example, in Stripe:

  • Go to Developers > API keys.
  • Find the leaked key and click "Revoke".
  • Create a new key with the same permissions, but note the new key's ID.
  • 2. Remove the key from your codebase

    Search your entire project for the key and remove it. Especially check:

  • .env files (and ensure they're in .gitignore)
  • config files
  • Any hardcoded strings in components or API routes
  • For a v0 app, you might have code like this:

    // Before (leaked key in client-side code)
    const openai = new OpenAI({ apiKey: 'sk-1234567890abcdef' });
    
    // After (use environment variable)
    const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

    3. Rotate the key and update environment variables

    If you use a hosting platform like Vercel, Netlify, or Replit, update the environment variables with the new key. For Vercel:

  • Go to your project settings > Environment Variables.
  • Update the variable with the new key.
  • Redeploy your app.
  • 4. Clean up git history

    If the key was committed in the past, you need to remove it from git history. Use git filter-branch or BFG Repo-Cleaner. For example:

    # Install BFG
    brew install bfg
    
    # Remove the key from history
    bfg --replace-text secrets.txt

    Then force-push to your remote repo.

    5. Scan your app again

    After cleaning up, run a security scan to ensure no other keys are exposed. Use the secret leak scanner to check your live app and your repo.

    Common mistakes

    AI-built apps often have these pitfalls that lead to API key leaks:

  • Hardcoding keys in client-side code: v0 and similar tools might generate code that includes API keys directly in React components, which are exposed in the browser.
  • Committing `.env` files: Many AI prompts include .env files in the generated project structure, and developers forget to exclude them.
  • Using the same key for development and production: This amplifies the impact of a leak.
  • Not using a secrets manager: Relying on environment variables is good, but a dedicated secrets manager (like AWS Secrets Manager or HashiCorp Vault) adds an extra layer.
  • Ignoring git history: Removing the key from the current code isn't enough; the key remains in history.
  • Preventing future leaks

  • Use environment variables: Never hardcode secrets. In your v0 app, ensure all API keys are accessed via process.env.
  • Add `.env` to `.gitignore`: Make sure your project's .gitignore includes .env and .env.local.
  • Implement a secret scanning pre-commit hook: Use tools like gitleaks or trufflehog to scan before every commit.
  • Rotate keys regularly: Set a reminder to rotate keys every 90 days.
  • Monitor your app with a [continuous security monitoring](https://www.overmcp.com/monitor) service to catch leaks in real-time.
  • FAQ

    How do I know if my v0 app has a leaked API key?

    Use a secret scanner like the secret leak scanner to scan your website's client-side code and your GitHub repo. You can also manually check by searching for patterns like sk- or api_key= in your browser's DevTools.

    Can I just delete the key and forget about it?

    No. You must revoke the key and rotate it. Simply deleting it from your codebase doesn't invalidate it, and the key could still be used by an attacker. Always revoke and create a new one.

    What if the leaked key is a public key (e.g., Stripe publishable key)?

    Public keys are meant to be exposed, so they are not a security risk. However, if you accidentally exposed a secret key, follow the steps above. For public keys, just ensure they are correctly configured.

    ---

    If you're building with v0 and want to ensure your app is secure before launch, run a free AI app security scanner to catch issues like leaked API keys, security headers, and SSL certificate problems.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free