Leaked secrets: API keys and credentials in prompts, code, and logs

Last updated

A secret is any piece of information that grants access: an API key, a password, an access token, a private key, a database connection string, or a recovery code. Secrets leak far more often than most businesses realize, and AI has created several new ways for them to escape. This article explains the common leak paths, how to prevent them, and what to do when a leak happens.

Why leaked secrets matter

A leaked secret is effectively an open door. Depending on what it unlocks, an attacker could:

  • Run up large charges on your AI or cloud accounts.
  • Read, change, or delete your data.
  • Access customer information, creating legal and reputational problems.
  • Send emails or messages as your business.
  • Use your accounts as a launching point for further attacks.

Automated scanners continuously search public code repositories and other public sources for exposed keys, and exposed keys can be found and misused quickly after they appear. Some platforms and providers scan for leaked keys and may revoke or alert on them, but you should not rely on that.

Common leak paths

Pasting secrets into AI prompts

People paste configuration files, error messages, code snippets, or log excerpts into AI chats to get help, without noticing a key or password embedded in them. Those prompts may be stored in conversation history, logged by the provider, shared by link, or, depending on the plan and settings, used for training. See What you should not paste into an AI tool.

AI coding assistants and agents

Coding assistants and agents read project files to help. If secrets are stored in project files, they can end up in the model's context, in generated code, in suggested commands, or in logs. An AI-generated snippet may also hard-code a key "for simplicity" when you ask it to make something work quickly.

Code repositories

Committing a key to a repository is one of the most common leaks. Removing it in a later commit does not help, because the key remains in the repository history. Private repositories reduce exposure but are not a safe place for secrets either: they get cloned, shared with contractors, connected to third-party tools, and occasionally made public by mistake.

Front-end code and mobile apps

Putting an AI API key in website JavaScript or a mobile app exposes it to anyone who looks. This is a frequent mistake when building quick AI prototypes. Keys must stay on the server. See Keeping API keys and customer data safe.

Logs and error reports

Applications that log full requests, environment variables, or error details can write secrets into log files, monitoring tools, and error trackers, which are often accessible to more people and services than the original system.

AI conversation logs

Applications that store AI conversations for quality or support purposes may capture secrets that users or tools passed through the AI.

Chat, email, and tickets

Sharing a key with a colleague or contractor through chat, email, or a support ticket leaves a copy in systems that are searchable, backed up, and accessible to others.

Screenshots and screen sharing

Dashboards, terminals, and settings pages displayed during a screen share or captured in a screenshot can expose keys.

How to prevent leaks

Store secrets properly

  • Use a secrets manager or your hosting platform's secure environment settings.
  • If environment files are used, restrict their permissions and exclude them from version control.
  • Never hard-code secrets in source code, templates, or prompts.

Share secrets safely

  • Do not send secrets through chat, email, or tickets.
  • Use a password manager's secure sharing feature or your secrets manager's access controls.
  • Better still, give each person or system its own credentials rather than sharing one.

Use scanning tools

  • Enable secret scanning on code repositories; many platforms offer it.
  • Add pre-commit checks that block commits containing likely secrets.
  • Scan logs and AI conversation stores for credential patterns.

Configure AI tools carefully

  • Exclude secret files from what coding assistants and agents can read, where the tool supports it.
  • Review AI-generated code for hard-coded credentials before using it.
  • Strip secrets from logs, configs, and error messages before pasting them into any AI tool. Replace them with placeholders like [API_KEY].

Reduce the impact of any single leak

  • Use separate keys for each application and environment.
  • Limit each key's permissions and set spending limits.
  • Prefer short-lived tokens over long-lived keys where possible.
  • Rotate keys on a regular schedule.

Redact logs

Configure applications to mask secrets and sensitive fields before writing logs.

What to do when a secret leaks

Act immediately. Speed matters more than certainty about whether it was actually misused.

  1. Revoke or rotate the secret. Generate a new key, update the systems that use it, and disable the old one. This is the most important step. Deleting the message, commit, or chat that exposed the key is not enough, because copies may already exist.
  2. Check for misuse. Review usage dashboards, billing, access logs, and audit trails for activity you do not recognize since the time of exposure.
  3. Remove the exposure. Delete the chat, edit the ticket, or clean the repository history if appropriate, understanding this limits future exposure but does not undo past exposure.
  4. Assess the impact. Determine what the secret could access and whether any data was viewed or changed.
  5. Notify as required. If customer data may have been accessed, follow your incident procedures and any legal notification requirements. Notify the provider if fraudulent charges occurred.
  6. Fix the root cause. Figure out how the leak happened and change the process so it does not happen again.

A quick self-check

  • Are any secrets stored in code, front-end files, or repositories?
  • Is secret scanning enabled on your repositories?
  • Do people share keys over chat or email?
  • Do your logs or AI conversation stores capture secrets?
  • Does each application have its own limited key with spending caps?
  • Does everyone know the first step when a key leaks is to rotate it?

Key takeaways

  • Secrets leak through prompts, code, repositories, front-end apps, logs, chat, and screenshots.
  • AI tools add new leak paths, including pasted prompts, coding agents, and conversation logs.
  • Store secrets in a secrets manager, share them securely, and scan for leaks.
  • Limit each key's scope and spending so one leak cannot do unlimited damage.
  • When a secret leaks, rotate it first, then investigate, clean up, and fix the cause.