Safeguard
Security Guides•

Hardcoded secrets in AI-generated code: a finding and fix checklist

How API keys and passwords end up in AI-written code, where to look for them, and what to do in the first hour if one reaches a repository today.

Safeguard Security Team
3 min read

Assistants write whatever makes the example run, and that sometimes includes a pasted key, a default password or a connection string with credentials. Search every AI-assisted diff for secrets, move them to a secret store or environment, and rotate anything that ever reached a repository.

How secrets get in

Three paths account for most cases.

  • You pasted it. A developer includes a real key in a prompt so the code works, and the assistant echoes it into a file.
  • The model invents a placeholder that looks real. A fake looking token in a config file gets copied around until someone swaps in the real value in place.
  • Example code with defaults. A sample admin password lands in a test fixture or setup script and is never removed.

MITRE's CWE-798 describes hard-coded credentials and notes that attackers who find one can bypass access controls, read data or run code, and that the same credential often exists in every installation.

Where to look in a diff

  1. Config and environment files, including .env files someone committed.
  2. Test fixtures and seed scripts.
  3. Docker files and CI workflow files.
  4. Comments that say "temporary" or "replace later".
  5. Any string longer than about 20 characters with mixed letters and digits.

Automated secret scanning in pre commit hooks and in CI catches most of these before review.

What to do when one is found

  1. Revoke or rotate first. Removing it from the file does not help; git history keeps it. Treat the secret as exposed.
  2. Check usage. Look at the provider's logs for activity you do not recognize.
  3. Remove from history only if needed, and understand that copies may already exist in forks or caches.
  4. Replace with a reference to an environment variable or secret manager.
  5. Add a scanner so the same class of mistake is blocked next time.

CWE-798's guidance for outbound credentials is to keep them outside the code, in protected configuration with tight access permissions. For expiring or generated credentials, rotation limits the damage window.

Prevent it at the prompt

Do not paste live secrets into an assistant. Use placeholders such as YOUR_API_KEY_HERE, and tell the assistant in its rules file to read values from environment variables.

Safeguard's policies and gates can block a pull request that fails a check like this, and the free scan at gold.safeguard.sh/scan is a quick way to see what a repository already contains.

Common false alarms

Scanners flag test tokens, example keys in documentation, and long hashes that are not secrets. Do not switch the scanner off. Mark known harmless values in an allow list with a comment saying why, and review that list occasionally. A noisy scanner that people trust is better than a quiet one nobody runs.

Quick check

If I deleted the secret in the next commit, is it safe?

No. Anyone with access to the history can still read it. Rotate the credential.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.