Credential Leaks in AI Coding Tools: Causes and Protection Guide
AI coding assistants often require permission to execute shell commands, offering an "always allow" option for convenience. However, this can inadvertently save sensitive parameters like database passwords and API tokens into local settings files. This article breaks down the root causes of these credential leaks, explains why standard .gitignore rules might fail, and provides a comprehensive guide for developers and organizations to detect, clean, and prevent these exposures through proper secret management and defense-in-depth strategies.
1. What is the Problem?
AI coding assistants typically prompt a confirmation dialog before executing shell commands. To reduce repetitive confirmations, tools usually offer “Allow once” and “Always allow” options. When a user selects “Always allow,” the tool writes this authorization rule to a local settings file within the project directory. Some implementations match the exact command string, meaning the parameters in the command—including database connection strings, access tokens, and passwords—are also saved.
This type of issue usually does not rely on malicious intrusion techniques. The root cause lies in trust spillover: developers trust the tool to only save the “allowed operations,” while the tool saves the full command with sensitive parameters to disk as-is.
Below is a demonstration snippet of a settings file; all values are placeholders and not real credentials:
{"permissions": {"allow": ["Bash(npm run deploy --token=<REDACTED_TOKEN>)","Bash(psql -h db.internal -U admin -W '<REDACTED_DB_PASSWORD>')"]}}
The longer the “don’t ask again” option is used, the more sensitive commands accumulate in this file. It effectively becomes a historical list of sensitive commands.
2. Why Global Ignore Rules Are Unreliable
Many teams rely on the global .gitignore to exclude local settings files, but this layer of defense has two common failure scenarios:
- Cross-device clone failure: A global
.gitignoreonly applies to a specific user configuration on a specific machine. When a project is cloned to a new computer, if the new environment lacks the same global rule, the settings file will be staged and committed viagit add. - Late initialization failure: Some projects start as ordinary folders and are initialized with
git initlater. The global rule might already exist, but if no one checks the file list before the first commit, the sensitive file enters the version history.
Furthermore, some tools might automatically commit changes to the settings file without explicit user confirmation. This can amplify a single misoperation into an automated information leak.
3. Developer Self-Check Steps
If you have used such tools, follow these steps to check your repository.
Step 1: Verify if the settings file is currently tracked by Git:
git ls-files | grep -i "settings.local"
Step 2: Check if the file has appeared in the commit history:
git log --all --full-history -- "*settings.local*"
Step 3: Determine if it has been pushed to a remote repository. As long as the credentials appear in a pushed commit, they should be considered leaked, and you should proceed directly to the credential rotation process.
If you confirm sensitive content exists in the history, you must clean the commit history. Simply deleting the file in the latest commit is not enough, as older commits remain accessible. Before cleaning, complete the following preparations:
- Backup the repository: Run
git clone --mirror <repo-url> backup.gitto retain a complete mirror. - Install the tool: Use
pip install git-filter-repoto install git-filter-repo. - Execute the cleanup: Run
git filter-repo --path-glob '*settings.local*' --invert-pathsin the repository to remove matching files from all commits. - Notify collaborators: Rewriting history changes all commit hashes. Pause pushes and inform your team members.
- Force push: Once verified, run
git push --force --allandgit push --force --tags. - Collaborator re-sync: Other members need to re-clone the repository or reset their local branches per team agreements. Existing Pull Requests and branch references may become invalid.
Note that rewriting history cannot undo the fact that credentials were accessed. Forks, old clones, and caches may still retain the original history. Therefore, you must still rotate the relevant credentials after cleaning the history.
4. Best Practices for Secret Management
- Separate secrets from commands: Database passwords, API tokens, etc., should be injected via environment variables, secret management services, or dedicated configuration centers, not written into the command line as plaintext.
- Least privilege and short TTLs: Credentials issued for CI/CD and local development should be granted only the necessary permissions and have short time-to-live (TTL) periods to minimize the window between a leak and its invalidation.
- Record authorizations by pattern: For operations that require frequent confirmations, prioritize using command templates or whitelists rather than saving the complete command.
- Incorporate security reviews: When enterprises build internal AI coding platforms, “how authorization rules are stored” should be included as an item in security reviews.
5. Organizational Defense in Depth
- Pre-commit blocking: Enable pre-commit hooks for secret scanning in the repository and configure server-side push protection to block sensitive information from entering the repository with a dual-layer approach.
- Periodic scanning of existing data: Perform regular scans of repository history, shared configuration files, and logs or conversation records that might contain credentials.
- Standardize false positive handling: Scan results need triage. Confirmed real credentials should be prioritized for rotation; false positives should be added to a whitelist with recorded reasons to prevent alert fatigue among teams.
- Leak incident response process: Upon discovering a leak, the first step is to rotate credentials, the second step is to check access logs, and the third step is to conduct a post-mortem on responsibility and processes.
- Separate handling for security reports: Security bug reports should have independent response SLAs. Auto-closure rules should exclude security issues to prevent high-severity reports from being closed due to prolonged inactivity.
6. Three Principles for Tool Designers
- Minimize sensitive data on disk: Authorization rules should only save command patterns or hashes, not the full plaintext including parameters.
- Defensive defaults: During installation or project initialization, tools should write ignore rules for sensitive files into the repository-level
.gitignore, rather than relying on the user’s global configuration. - Explicit confirmation for high-risk operations: Automated behaviors involving commits and pushes must receive explicit user authorization, especially when the content may contain sensitive fields.
7. Conclusion
This type of risk stems from the combination of two habits: tools defaulting to remembering all authorizations, and developers habitually choosing “don’t ask again.” In environments without secret scanning and least privilege controls, the risk of credential exposure is significantly higher. Moving secrets out of the command line and baking checks into the commit and deployment processes are the only ways to keep these risks under control.
