Skip to content
mentoor

Topic 3 of 5 · 4 min read

Keeping secrets out of code, logs and context

Goal: Find the leaked secrets in a diff and a log excerpt and list the rotation and clean-up steps in order.

A secret that has been in a commit, a log or a prompt must be treated as public: revoke it first, clean up second. Deleting the line does not help, because git history, log archives and chat transcripts keep copies.

Check yourself · 1 of 3

An API key was committed and pushed. What is the first step?

Choose one answer

Why it matters

Agents write code that works on the first run. The quickest way to make an API call work is to paste the key into the file. Developers also paste .env contents or error output into a chat to ask for help. Each of those places is a leak path: the repository, the log, the prompt history and any context file the agent loads.

Where secrets belong

  1. Environment variables or a secret manager, read at runtime. The repository holds a .env.example with empty values.
  2. A .gitignore entry for .env and key files, added before the first commit.
  3. Least privilege. A key that can only read one bucket does less damage when it leaks.
  4. Secret scanning in a pre-commit hook and in CI, so a leak is blocked before it is pushed.

What to look for in review

  • String literals that look like keys: long random strings, prefixes such as sk_live_ or AKIA.
  • Config files and test fixtures with real values.
  • Logging of whole request objects, headers or environment, which prints tokens.
  • Secrets inside prompts, system messages or agent context files.

If one leaks: the order

  1. Revoke or rotate the key at the provider. This is the step that ends the exposure.
  2. Check the provider's usage logs for use you did not make.
  3. Remove it from the code and move it to the proper store.
  4. Clean history only if useful, knowing the old key is already compromised.
  5. Add scanning so it cannot happen the same way again.

Before and after

-const client = new Payments({ apiKey: 'sk_live_...redacted' });
+const client = new Payments({ apiKey: process.env.PAYMENTS_API_KEY });

The fix is incomplete: the old key is still in history, so it must also be rotated.

What goes wrong

  • Cleaning before rotating. Rewriting history takes hours while the key stays valid.
  • Logging too much. console.log(req.headers) writes bearer tokens to a file many people can read.
  • Pasting real values into a prompt. The text may be stored and seen under the vendor's retention rules.

Examples

A key in a log line

2026-11-30 09:14:02 INFO outbound call headers={'Authorization': 'Bearer sk_live_...redacted'}

The agent added debug logging for a failing call and left it in. Rotate the key, remove the log line, and log only the status code and request id.

Key points

  • Treat any secret that appeared in code, a log or a prompt as compromised.
  • Revoke or rotate first; removing it from the code and history comes after.
  • Keep secrets in environment variables or a secret manager, with an empty .env.example in the repository.
  • Run secret scanning before commit and in CI so leaks are blocked early.
  • Review logging calls: whole headers, request objects and environment dumps often contain tokens.

Common mistakes

  • Deleting the secret from the file and calling it fixed while the old key still works.
  • Pasting a real .env file into a chat to get help with an error.
  • Committing a test fixture that contains a live key.
  • Logging complete headers or request bodies.

Check yourself · 2 of 3

Which line is the safest way for code to get an API key?

Choose one answer

Check yourself · 3 of 3

You want help debugging a failing database connection. What should you paste into an AI chat?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic