Secrets Committed by AI Coding Tools: Detection and Rotation Checklist
A containment-first protocol for detecting, revoking, and purging production secrets exposed during AI code generation sessions.

Developers building with AI pair programmers frequently commit sensitive credentials to git repositories when pasting live environment strings into prompts or committing unignored .env files. Remediating exposed credentials requires an immediate containment-first protocol: immediately revoking and rotating compromised API keys and database passwords before running repository forensics, using git filter-repo to cleanly purge secrets from git commit objects, coordinating force-pushes across collaborators, and establishing pre-commit hooks to prevent future credential leaks.
AI coding assistants require database schemas and API configurations to construct realistic integration code. During rapid prototyping, developers often copy entire live environment files into system prompts or create temporary scratch files that are accidentally staged and pushed to remote git repositories.
The short version
When an API secret or database connection string is committed to a git repository, execute containment actions in this exact sequence:
- Containment first: Treat any secret pushed to a remote repository as compromised immediately. Revoke or cycle the exposed token in the provider's dashboard before investigating commit history.
- Audit repository history: Scan all branches, tags, and commits using entropy and regex patterns for leaked payment keys, database URIs, and cloud tokens.
- Purge commit objects with
git filter-repo: Deleting the file in a new commit leaves the secret permanently accessible in git history. Rewrite repository tree objects to remove the file from all snapshots. - Coordinate upstream remotes: Force-push the sanitized mirror history, request cache invalidation for forks/views, and require all collaborators to re-clone.
- Install pre-commit verification gates: Configure local hooks and repository rules to block staging of high-entropy strings and unignored
.envfiles.
Review our Cursor-built codebase security review checklist and guide to production failures in Lovable apps for complementary launch verification steps.
1. Containment First: The Emergency Revocation Protocol
The most dangerous reaction to an exposed secret is pausing to rewrite git history while the compromised key remains active. Automated scrapers monitor public repositories and compromised developer machines, frequently abusing leaked credentials within minutes of push.
Prioritize containment based on provider capabilities:
Payment Gateways (e.g. Stripe)
Stripe allows developers to roll API keys with an expiration window (e.g. 12 hours) or revoke immediately. For production environments, create the new restricted secret key, update serverless environment variables, verify live checkout functionality, and then immediately revoke the old key.
Managed Databases and Backend Services (PostgreSQL, Supabase)
Differentiate direct database credentials from application API keys:
- Direct PostgreSQL connection strings: If a connection URI containing the master database password was exposed, rotate the database password immediately in your database provider console (e.g. Supabase Project Settings → Database). Be aware that rotating master database credentials terminates active connection pools, causing brief client disconnections while serverless workers reconnect.
- Supabase API keys (
service_roleandanon): Rotating the database password does not revoke Supabase API keys or JWT tokens. If aservice_rolekey was exposed, navigate to Supabase Project Settings → API Settings to roll the JWT secret and generate replacement API keys. Explicitly retire or deactivate the compromised legacy key, update server environment variables, and verify that HTTP requests using the old key receive HTTP 401 Unauthorized.
AI Model Providers & Cloud Providers (OpenAI, Anthropic, AWS)
Delete exposed API keys immediately from the provider's console. If an AWS access key (AKIA...) was pushed, check AWS CloudTrail logs for unauthorized IAM user creation or resource provisioning before creating replacement credentials.
2. Scanning Git History for Credential Signatures
Deleting a secret in a subsequent commit does not eliminate it from git storage. Anyone who clones the repository can inspect previous commits using git log or unpack git packfiles to retrieve raw strings.
To inspect your local commit history for common AI-committed credential patterns, run:
# Search commit diffs across all branches for common API key signatures
git log -p --all -S "sk_live_"
git log -p --all -S "supabase_service_role"
git log -p --all -G "(AKIA[0-9A-Z]{16})|(ghp_[0-9a-zA-Z]{36})"
To list every historical file path that ever contained environment configurations:
git log --all --pretty=format: --name-only --diff-filter=A | grep -E '(\.env|\.env\.local|secrets\.json)$' | sort -u
3. Why git rm Fails: The Git Object Model
In Git, every commit references an immutable tree object, and each file is stored as a compressed blob indexed by its SHA-1 or SHA-256 hash. Running git rm .env && git commit -m "remove secrets" creates a new commit where the file is absent from the current tree. However, the preceding commit object and its referenced blob still exist in the .git directory and in all remote forks.
To completely eradicate the blob, you must rewrite the repository's history so that the commit tree objects never included the file.
4. Purging Secrets with git filter-repo
The official tool recommended by Git documentation for purging files from repository history is git-filter-repo. Avoid the deprecated git filter-branch, which is slow and prone to leaving orphaned references.
Execute this structured runbook on a fresh, dedicated mirror clone:
# 1. Create a fresh mirror clone of the repository
git clone --mirror git@github.com:your-org/your-repo.git repo-cleanup
cd repo-cleanup
# 2. Run git-filter-repo to completely remove .env from all historical commits
# (Note: git-filter-repo automatically cleans references and repacks objects)
git filter-repo --invert-paths --path .env --path .env.local
# 3. Re-add the remote origin (git-filter-repo removes remotes to protect origin)
git remote add origin git@github.com:your-org/your-repo.git
# 4. Force-push the sanitized mirror history back to the remote origin
git push origin --mirror --force
Critical Collaboration Warnings
- Rewriting commit history alters every commit hash from the moment the secret was introduced onwards.
- All team members must re-clone the repository; merging an old local branch into the sanitized upstream will re-introduce the purged commit objects.
- If the repository was forked or published publicly on GitHub, GitHub maintains cached commit views. Contact GitHub Support to purge cached commit views.
5. Prevention: Pre-Commit Hooks and Sanitized Environments
To prevent future leaks during AI pair programming sessions:
- Never provide live production credentials to AI prompts: Provide sanitized
.env.examplestrings with mock identifiers (e.g.,sk_test_mock_12345). - Verify
.gitignorebefore initial commit: Ensure.env,.env.*, and.claude/are ignored in the repository root. - Configure local pre-commit hooks: Use tools like Gitleaks or git
pre-commithooks to evaluate staged diffs before committing:
#!/usr/bin/env bash
# .git/hooks/pre-commit: Reject commits containing suspected credentials
if git diff --cached | grep -E -q 'sk_live_[0-9a-zA-Z]{24}|AKIA[0-9A-Z]{16}'; then
echo "ERROR: Attempting to commit an active production secret. Commit aborted."
exit 1
fi
Next steps
Credential leakage is frequently paired with unverified database permissions and client-side authorization bypasses. Cross-reference your security controls with our 12 critical security flaws in vibe-coded apps and our hands-on Supabase RLS security audit walkthrough. To see how automated PR tools compare with whole-repository architectural audits, read our guide on AI code review vs AI-generated code audits.
If your project has experienced credential exposure or requires an end-to-end repository security audit before launch, consult Aatvi's AI Code Rescue engineering team. We audit repository history, guide key rotation, and install hardened verification pipelines.
Source notes
- The git-filter-repo documentation and tool repository details object rewriting safety and repository sanitization.
- GitHub's Best Practices for Removing Sensitive Data explains commit view caching and force-push risks.
- Stripe API Key Security & Rolling Procedures covers key expiration windows and live credential management.
- The OWASP Top 10 for Large Language Model Applications documents prompt credential leakage and sensitive information disclosure.
Start here: AI Code Rescue audit and stabilization.
