keepcalculator

06 Numbers and Powers

Prevent an AI Coding Assistant From Exposing API Keys

Card No. 06·57020 · Updated · Standard formula, shown below

AI coding assistants can accidentally reveal API keys when they read project files, include secrets in generated code, or send context to an external service. Use the steps below to keep credentials out of prompts and repositories, limit the damage if a key leaks, and check your project before sharing or deploying it.

What You’ll Need

  • Access to your coding assistant’s settings, especially options for project context, file exclusions, and data handling.
  • A way to store secrets outside source code, such as environment variables or your platform’s secret manager.
  • Version control and, ideally, a secret-scanning tool or repository hosting alerts.
  • Access to the account or service that issued your API keys, so you can revoke and replace them if necessary.

Step-by-Step

  1. Find and classify existing secrets. Search the project for likely credentials, including strings named API_KEY, TOKEN, SECRET, or PASSWORD, and inspect configuration files, scripts, notebooks, logs, and sample data. A text search can find obvious cases, but it cannot reliably identify every credential. Treat any real key found in tracked files or chat history as exposed until you have checked its history and access.
  2. Move credentials out of source files. Read keys from environment variables or a dedicated secrets manager instead of writing them into code. For local development, put values in a file such as .env only if your framework supports it, and add that file to .gitignore. Commit a safe .env.example containing variable names and placeholder values, never working credentials. On deployment, set secrets through the hosting platform’s protected configuration rather than copying a local file.
  3. Check what the assistant can read. Review its workspace, repository, indexing, and context settings. Exclude secret files and sensitive directories using the assistant’s supported ignore mechanism, if available. Do not assume that .gitignore automatically prevents an assistant from reading a file; confirm the product’s behavior. Keep credentials outside the workspace where practical, and avoid opening secret files in an assistant-enabled editor session.
  4. Limit what you send in prompts. Share only the code and context needed for the task. Before pasting logs, configuration, screenshots, or error output, remove keys, authorization headers, account identifiers, and private URLs. Ask the assistant to use a placeholder such as YOUR_API_KEY or an environment variable name. Do not ask it to inspect a real credential or paste a credential to help it debug authentication.
  5. Review generated changes before accepting them. Inspect every changed file and diff. Reject code that embeds a key, prints environment variables, logs authorization headers, or copies secrets into tests, documentation, examples, or client-side code. A key included in browser-delivered code is visible to users; use a server-side design where the provider requires a secret credential.
  6. Scan before committing and sharing. Run a secret scanner on the working tree and, where possible, enable repository scanning and push protection. Check staged files before committing, then inspect the commit diff. Scanning helps catch mistakes but can miss unfamiliar formats and can flag harmless examples, so review findings rather than relying on a clean result as proof.
  7. Use least privilege and separate credentials. Create keys limited to the permissions and services the application needs. Use separate credentials for development, testing, and production, and set provider-supported usage limits or restrictions where appropriate. Avoid giving a coding assistant access to a production key or account when a restricted test credential will do.
  8. Respond promptly if a key is exposed. Revoke or rotate the key at the issuing service; removing it from the latest file is not enough. Replace it in the approved secret store and update the application. Check provider activity and repository history. If a key was committed, treat it as compromised even after deleting the commit or rewriting history, since copies may remain in clones, logs, or caches. Follow your organization’s incident process.

Common Mistakes

  • Trusting ignore files without checking. Git ignore rules control what Git tracks; they do not necessarily control an assistant’s indexing or prompt context.
  • Deleting a leaked key instead of revoking it. Anyone who copied it may still be able to use it. Revoke first, then clean up files and history.
  • Putting a secret in a prompt “just once.” A prompt may be retained or processed according to the service’s settings. Do not send credentials unless your organization’s approved workflow explicitly permits it.
  • Keeping real keys in examples or tests. Use placeholders and mock credentials. Test fixtures are often shared, committed, or copied into public documentation.
  • Assuming scanning catches everything. Encoded, split, or unfamiliar secrets may escape detection. Combine scanning with careful review and restricted credentials.

Tips

  • Use clear variable names such as PAYMENTS_API_KEY, but never put the value in the name, a comment, or a command-line argument that may be saved in shell history.
  • Ask the assistant to change code to read a named environment variable, not to create or populate a secret file.
  • Keep a short checklist for pull requests: check diffs, inspect logs and examples, scan for secrets, and verify that local secret files are untracked.
  • Review assistant data controls and organizational rules before enabling repository-wide indexing or sharing code with an external service.

Troubleshooting

The assistant still mentions a key. Stop the session and do not copy or reuse the value. Revoke it if it was real, then remove it from files, logs, and any prompt content you control. Restart with a clean context after confirming the secret file is excluded; if exclusion is unavailable or unclear, work from a sanitized copy outside the assistant’s access.

A scanner reports a key in an example. Confirm whether it is a live credential. Replace real values with unmistakable placeholders, then rerun the scan. If it is a false positive, use the scanner’s documented suppression method narrowly; do not disable scanning for the whole project.

A key was committed but is no longer visible. Revoke or rotate it immediately. Inspect repository history and any forks, CI logs, artifacts, and shared clones. History cleanup can reduce further exposure, but it cannot make an already disclosed key safe again.

The application cannot find its key after you move it. Check that the variable is set in the process that runs the app, that the variable name matches exactly, and that local environment-file loading is configured for your framework. Never print the value to diagnose the issue; check only whether it is present, or use a redacted status message.

Frequently Asked Questions

Does adding a secret file to .gitignore protect it from an AI coding assistant?
No. It prevents Git from tracking an untracked file, but assistant context and indexing rules are product-specific. Configure and verify those separately.

Can I paste an API key into a private coding assistant?
A private or paid account does not automatically make sharing appropriate. Follow your organization’s policy and the service’s data controls; the safer practice is to use placeholders and keep real keys out of prompts.

What should I do if a key appears in a chat or generated file?
Revoke or rotate it if it is real, replace it in your secret store, and remove it from files and logs you control. Check the service’s deletion and incident procedures, but do not rely on deletion alone.

Is an environment variable always secure?
No storage method is perfect. Environment variables are generally safer than hard-coding keys, but can be exposed by process inspection, debugging, logs, or misconfigured deployment. Use a managed secret store when available and restrict access.

Should I give the assistant a test API key?
Only if necessary and permitted. Prefer a restricted, revocable test credential with no production access, and keep it out of prompts and source files.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →