Skip to content
mentoor

Topic 4 of 6 · 4 min read

Hardcoded secrets and credential leaks

Goal: Locate every place an AI-written change exposes a credential and choose the right fix: environment, secret manager or rotation.

A secret that has been committed, logged or pasted into a chat must be treated as leaked, and the only real fix is to rotate it.

Check yourself · 1 of 3

An AI agent committed an API key, and you removed it in the next commit. What else is required?

Choose one answer

Why it matters

An assistant wants the code to run now. If you paste an API key into the prompt, or the key sits in a .env file the agent can read, it may copy the value into source code, a test fixture, a Dockerfile, a README example or a log line. Git history keeps it forever, and public repositories are scanned for keys by bots within minutes. GitHub runs secret scanning for exactly this reason, and many providers revoke leaked keys they detect.

Where AI changes leak secrets

  1. Source code. const client = new Stripe("sk_live_...") written directly into a file.
  2. Config and fixtures. A .env file committed, a copied docker-compose.yml, a test that uses a real key.
  3. Logs and errors. Printing the full request, headers or config object while debugging.
  4. Transcripts and prompts. Keys pasted into a chat or a prompt file that is saved or shared.
  5. Client bundles. A server key placed in front-end code, which every visitor can read.

The fix, in order

  1. Rotate the secret first. Removing it from the latest commit does not undo exposure.
  2. Load it from the environment or a secret manager, and fail at startup when it is missing.
  3. Keep .env out of git with .gitignore, and commit a .env.example with empty values.
  4. Turn on secret scanning and a pre-commit check so the next leak is caught before it is pushed.
  5. Scope the key. Give it the least permissions and a short life.

Before and after

- const stripe = new Stripe("sk_live_...redacted");
+ const key = process.env.STRIPE_SECRET_KEY;
+ if (!key) throw new Error("STRIPE_SECRET_KEY is not set");
+ const stripe = new Stripe(key);

The diff is only half the repair. The original key is in history, so it needs to be revoked and replaced.

What goes wrong

  • Deleting the line and calling it fixed. The key remains in earlier commits.
  • Moving the secret to a config file that is committed. Same leak, new filename.
  • Logging the config object at startup "for debugging".
  • Giving the agent production credentials when a sandbox key would do the job.

Examples

The fixture with a real key

def test_charge():
    client = PaymentClient(api_key="sk_live_...redacted")
    assert client.charge(100).ok

The agent used the key you pasted into the chat so the test would pass. It is now in the repository. Revoke it, use the provider's test-mode key from an environment variable, and mock the network call in unit tests.

The helpful debug log

console.log("Starting with config", config);

If config holds a database URL with a password, it now sits in your log system. Log only the non-secret fields, or redact known keys before printing.

Key points

  • A leaked secret is compromised until it is rotated; deleting it from code is not enough.
  • Secrets belong in environment variables or a secret manager, with startup failure when one is missing.
  • Check source, fixtures, config files, logs and client bundles, not just the main code file.
  • Commit a .env.example with empty values and keep the real .env in .gitignore.
  • Give agents scoped sandbox keys and enable secret scanning and pre-commit checks.

Common mistakes

  • Removing a committed secret in a new commit and skipping rotation.
  • Checking only source files and missing keys in test fixtures, Docker files and logs.
  • Pasting production keys into an agent prompt when a restricted test key would work.
  • Putting a server-side key into front-end code because "it is just a build variable".

Check yourself · 2 of 3

Where should an application get its database password at runtime?

Choose one answer

Check yourself · 3 of 3

Which file is safe to commit for configuration that needs an API key?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic