Fix Leaked API Keys in GitHub Repos: Vibe-Code Security Guide
OverMCP Team
Quick answer
Leaked API keys in GitHub repos are one of the most common security risks in vibe-coded apps. To fix them, immediately revoke the exposed key, remove it from your git history using git filter-branch or BFG Repo-Cleaner, and add a .gitignore file for .env and config files. Then scan your entire repo with a secret leak scanner to catch any other exposed secrets.
What to check first
Before you panic, run through this checklist to assess the damage:
git log --oneline to see commits. Look for any that might contain the key.git grep -i "sk-" (for OpenAI keys) or similar patterns to find all occurrences.Step-by-step fix
Follow these steps to permanently remove leaked API keys from your GitHub repo and prevent future leaks.
1. Revoke the exposed key immediately
Go to the service provider (e.g., OpenAI, Stripe, AWS) and revoke the compromised key. Generate a new one and store it securely in environment variables (e.g., in Vercel, Netlify, or your hosting platform). Do NOT commit the new key.
2. Remove the key from git history
You need to purge the key from all commits. The safest way is using BFG Repo-Cleaner:
# Clone your repo as a mirror
$ git clone --mirror https://github.com/yourusername/your-repo.git
# Download BFG jar from https://rtyley.github.io/bfg-repo-cleaner/
# Run BFG to remove the key (example: for OpenAI key starting with sk-)
$ java -jar bfg.jar --replace-text passwords.txt your-repo.git
# In passwords.txt, add the leaked key like: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Then clean and push
$ cd your-repo.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
$ git push --forceAlternatively, use git filter-branch:
$ git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
$ git push --force --all3. Add a .gitignore file
Create or update .gitignore to exclude sensitive files:
# .gitignore
.env
.env.local
.env.*.local
*.pem
*.key
config/credentials.yml4. Reset environment variables
Update your hosting environment with the new key. For Vercel, use the dashboard or CLI:
$ vercel env add OPENAI_API_KEY5. Force push and notify collaborators
After the history rewrite, force push to GitHub. All collaborators will need to reclone the repo. If others have forked your repo, ask them to delete their forks and refork.
6. Scan your repo again
Run a secret leak scanner to verify no other secrets remain.
Common mistakes
AI coding tools like Cursor, Bolt.new, and Lovable often generate code that includes hardcoded API keys. Here are the most common mistakes:
.env files or config files with placeholder keys that developers accidentally commit..env to .gitignore.config.js or settings.py..gitignore file at all.To avoid these, always run a free AI app security scanner before your first commit.
How to prevent future leaks
git-secrets or talisman can block commits containing patterns like sk- or AKIA.FAQ
How do I find leaked API keys in my GitHub repo?
You can manually search with git grep, or use automated tools like OverMCP's secret leak scanner that scan your entire repo for hundreds of patterns.
Can I remove a leaked API key without rewriting history?
No, once a key is pushed to a public repo, you must rewrite history to remove it. Even if you delete the file in a new commit, the key remains in older commits. Use BFG Repo-Cleaner or git filter-branch.
What should I do if my API key was already used by an attacker?
Revoke the key immediately, check your account for any unauthorized usage (e.g., excessive API calls), and contact the provider's support if needed. Also, consider rotating any other keys that might have been exposed in the same commit.