Topic 1 of 4 · 5 min read
How secrets reach the context window
Goal: Trace how a secret could enter an agent's context and block each path.
Anything an agent reads becomes part of its context window, and anything in the context window can be repeated, logged or leaked. A secret that reaches the context is no longer under your control.
Check yourself · 1 of 3
Why is a secret in an agent's context window a risk?
Why it matters
A coding agent such as Claude Code, Codex, Cursor or Copilot reads files, runs commands and sees their output. It does not know which text is a password. A model also has no private memory: the context is sent to the provider on every call, saved in transcripts, and may be copied into logs, tickets and pull requests. If a page the agent reads contains hidden instructions (prompt injection), the agent can be told to print or send what it has seen.
The four common paths in
- Files the agent opens.
.env,config/production.yml,~/.aws/credentials, aterraform.tfstate. An agent that "looks around the repo" reads them. - Pasted by a person. "Here is the error, and my key is sk-..." goes into the chat and into history.
- Tool output.
env,printenv,docker inspect,curl -vand verbose CI logs all echo tokens and headers. - Error messages and stack traces. A failed request prints the full URL with
?api_key=in it.
How to block each path
- Keep real secrets out of the project folder. Use a secret manager or the shell environment of a separate process, and give the agent a
.env.examplewith fake values. - Add deny rules for secret files in the agent's permission settings and in its ignore file.
- Run the agent in a sandbox or container whose environment holds only what the task needs.
- Redact in the tool layer: strip
Authorizationheaders and known key patterns from output before it goes back to the model. - Use short-lived credentials, so a leaked one expires soon.
Before and after
Weak: the agent debugs a failing API call by running printenv | grep KEY, and the live key lands in the transcript.
Better: the tool that makes the call reads the key itself and returns only the status code and a redacted error. The model never sees the value.
What goes wrong
- Hiding
.envfrom the editor but not from the shell tool, socat .envstill works. - Trusting "I only use a test key", then finding the test key has production rights.
- Forgetting that transcripts, traces and crash reports are copies of the context.
Examples
The helpful debug command
An agent is asked to fix a 401 error. It runs a command that prints the environment to compare variables. The output contains OPENAI_API_KEY=sk-...redacted and DATABASE_URL with a password. Both are now in the transcript and in any tool that stores it. Fix: the agent runs in a container where the key is not set, and the test uses a fake server.
A deny rule that actually works
{
"permissions": {
"deny": ["Read(./.env*)", "Read(~/.aws/**)", "Bash(printenv*)", "Bash(cat .env*)"]
}
}
The rule covers both the file tool and the shell. Rules like this reduce the risk but are not a sandbox: pair them with an environment that does not hold the secret.
Key points
- Everything an agent reads or runs becomes context, and context is sent to the provider and stored in transcripts.
- The four usual paths are files the agent opens, values people paste, tool output and error messages.
- Block the file path with a sandbox, deny rules and an example env file, and block the output path with redaction.
- Prompt injection turns a secret in context into a secret that can be sent out.
- Short-lived credentials limit the damage when a value does slip through.
Common mistakes
- Blocking the file-reading tool but leaving the shell open, so the same file is read with
cat. - Pasting a real key into a chat to "just get it working".
- Assuming a transcript is private, when it is stored and often shared in bug reports.
- Putting production credentials on a developer machine where the agent runs.
Check yourself · 2 of 3
Which command is the most likely to put a secret into an agent's context by accident?
Check yourself · 3 of 3
You want an agent to work on a project that needs an API key to run. What is the best setup?
Practice items, not counted towards your mastery.
Start here
- Secrets Management Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
Central storage, short-lived secrets, rotation and auditing: the baseline every agent setup should meet.
- MCP specification: Authorization (opens in a new tab)StandardFree
How MCP servers and clients must handle OAuth, audience-bound tokens and token passthrough.
- MCP security best practices (opens in a new tab)DocsFree
Names the confused deputy and token passthrough attacks and what servers must do instead.
More for this topic
- OWASP Top 10 for LLM Applications (opens in a new tab)DocsFree
Sensitive information disclosure, prompt injection and excessive agency, the risks behind agent secret leaks.