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?
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
- Environment variables or a secret manager, read at runtime. The repository holds a
.env.examplewith empty values. - A
.gitignoreentry for.envand key files, added before the first commit. - Least privilege. A key that can only read one bucket does less damage when it leaks.
- 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_orAKIA. - 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
- Revoke or rotate the key at the provider. This is the step that ends the exposure.
- Check the provider's usage logs for use you did not make.
- Remove it from the code and move it to the proper store.
- Clean history only if useful, knowing the old key is already compromised.
- 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.examplein 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
.envfile 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?
Check yourself · 3 of 3
You want help debugging a failing database connection. What should you paste into an AI chat?
Practice items, not counted towards your mastery.
Start here
- OWASP Top 10:2025 (opens in a new tab)StandardFree
The ten risk categories, from A01 Broken Access Control to A10 Mishandling of Exceptional Conditions, with examples and prevention advice.
- OWASP Cheat Sheet Series (opens in a new tab)DocsFree
Short, practical prevention guides for each topic in this skill.
- OWASP Application Security Verification Standard (opens in a new tab)StandardFree
A numbered list of security requirements you can cite in review comments.
More for this topic
- Secrets Management Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Storage, rotation and leak-response advice for keys and tokens.