Secrets in code, logs and commits
Catch hardcoded keys added 'to make it work', tokens written to logs or error messages, committed .env files and credentials pasted into test fixtures. Remember that any secret the agent reads enters its context and can resurface. Once a secret has been committed, rotate it: deleting it from the code is not enough.
Builds on: Agents optimise the signal they can see, What the agent sees: its context
Green in a minute, but where is the key?
Your billing integration test fails with `401 Unauthorized`. You ask the agent to get it passing. A minute later the test is green, and the summary says *"Fixed authentication config."* Earlier in the transcript, the agent ran `$ cat .env` to look around. Before you push, ask where the real key might be now: in the code, a test fixture, a log line, or the summary itself.
Why the key ends up in the code
The proxy signal is *the 401 is gone*. The shortest path is to paste in the key the agent found. Nothing in its context shows that the repo gets pushed, cloned and read by CI. Once it has read `.env`, the key is in its context. It can resurface in a fixture, an error message or the summary. **Later cost:** a live key in git history and log stores, usable by anyone who sees them.
Secret tells to scan for in every diff
- String literals shaped like keys: `sk_live_`, `AKIA`, `ghp_`, `eyJ…` (JWTs), long hex or base64
- A real key used as the **default** in an env lookup
- Logs or error messages that include headers, a whole config or request, or a connection URL with a password
- `.env`, `*.pem` or credential JSON files added, or removed from `.gitignore`
- Test fixtures or recorded HTTP responses that hold real tokens
**Later cost** of each: a working secret sits in history, logs or fixtures that many people and systems can read. It stays valid until rotated.
Two leaks in one small diff
The fallback in `os.getenv` puts the live key into source, and into git history for good. It also hides missing config, because every machine silently uses that key. The new `log.debug` writes the `Bearer` token to wherever logs go once debug is on. **Later cost:** a permanent key in history, config errors that never fail loudly, and tokens in log stores. BETTER: keep `os.environ["PAY_API_KEY"]`, which raises `KeyError` when the variable is unset. Log the amount, never the headers.
// Python
--- a/billing/client.py
+++ b/billing/client.py
-API_KEY = os.environ["PAY_API_KEY"]
+API_KEY = os.getenv(
+ "PAY_API_KEY", "sk_live_51Hq8xT2eZvKa9")
def charge(amount):
headers = {"Authorization":
f"Bearer {API_KEY}"}
+ log.debug("charge %s", headers)
return post(URL, headers=headers)
A fixture copied from what the agent read
Asked for webhook tests, the agent ran `$ cat .env`. It then wrote `token = "ghp_7Rk2…"` into `conftest.py` and called it *sample data*. The tell: a fixture value with a real prefix and a real length, written right after the agent read the real one. **Later cost:** a live token in every clone of the test suite. BETTER: an obviously fake value like `"test-token"`, set with `monkeypatch.setenv`. If that file was already committed, rotate the token first, then fix the code.
Rotate first, then delete
Anything the agent reads can come back out in code, logs, fixtures or its summary. Check for secrets in every diff, and keep real secrets out of its reach when you can. Once a secret has been committed, deleting it is not enough. The old commit still holds it, and once pushed, clones, forks and CI logs keep copies. Rotate it: issue a new one, switch over, revoke the old one.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.