Sub topic 12
Keeping secrets and credentials out of reach
This chapter assumes you know how allow, ask, and deny decisions work, and that you have read the chapters on sandboxing and prompt injection. It covers how secrets reach an agent and how to keep them away from it.
How secrets reach the agent
An agent can read anything your user account can read. Secrets usually get to it by one of four routes:
- Files:
.env,~/.aws/credentials,~/.ssh/id_*, and.npmrcfiles that hold tokens. - Environment variables: every command the agent runs inherits your shell's variables, such as
AWS_SECRET_ACCESS_KEYorGITHUB_TOKEN. - Command output:
env,printenv,git config --list, or a failing script that prints its configuration. - Tool arguments: the agent pastes a value into a command line or an external tool call.
Anything the agent reads goes into the model's context. From there it can be sent to the model provider, written into logs, or leaked by a prompt injection that tells the agent to post it somewhere.
Layer 1: deny reads of secret paths
Add deny rules for known secret locations. Deny wins over allow, so a broad "allow reads" rule cannot reopen them.
from fnmatch import fnmatch
from pathlib import Path
SECRET_PATHS = ["*/.env", "*/.env.*", "*/.ssh/*", "*/.aws/credentials", "*/.npmrc"]
def may_read(path: str) -> bool:
resolved = str(Path(path).expanduser().resolve())
return not any(fnmatch(resolved, p) for p in SECRET_PATHS)
print(may_read("src/app.py")) # True
print(may_read("~/.aws/credentials")) # FalseThe check resolves the path first, so ../.ssh/id_rsa and symlinks are judged by where they really point. Path rules still miss secrets stored under unusual names, so treat them as one layer among several.
Layer 2: scrub the environment
A file rule does not stop printenv. The stronger fix is to never pass secrets to the agent's child processes.
import os
import re
import subprocess
SECRET_NAME = re.compile(r"KEY|TOKEN|SECRET|PASSWORD|CREDENTIAL", re.IGNORECASE)
def clean_env() -> dict:
return {k: v for k, v in os.environ.items() if not SECRET_NAME.search(k)}
# On Linux or macOS: lists only the variables that survived the filter.
result = subprocess.run(["env"], env=clean_env(), capture_output=True, text=True)
print(result.stdout)A name-based filter is a heuristic. A variable called DB_URL can contain a password and would pass through. An allowlist of variables to keep, such as PATH and HOME, is safer than a list of names to remove.
Layer 3: sandbox the network and the filesystem
Rules inside the agent depend on the agent behaving. A sandbox does not. Run the agent in a container or a restricted user account that has no mounted home directory. Block outbound network access, or allow only the hosts the task needs. A secret that cannot leave the machine is much less dangerous when the agent reads it by mistake.
Prefer short-lived, narrow credentials
Assume that some secret will eventually leak, and limit the damage:
- Give the agent a separate token, never your personal one.
- Scope it to one repository or one action, such as read-only.
- Use credentials that expire, such as temporary cloud session tokens.
- Rotate any secret the agent printed, even if you think the log is private.
Quick checklist
| Exposure route | Control |
|---|---|
| Secret files | Deny rules on known paths, plus a workspace-only sandbox |
| Environment variables | Allowlisted environment for child processes |
| Command output | Ask before running `env`, `printenv`, and similar commands |
| Network exfiltration | Deny `curl *` or block egress in the sandbox |
| Long-term exposure | Scoped, expiring credentials |
Exercise
Your deny rules block reads of .env, yet the agent runs printenv and shows GITHUB_TOKEN in its output. Which control would have prevented this, and why did the file rule not help?
Show answer
Scrubbing or allowlisting the environment passed to the agent's commands would have prevented it. The token was never in a file the agent read. It was inherited as an environment variable, so a path rule never applied. Adding printenv and env to the ask or deny list, and using a narrowly scoped token, would reduce the risk further.