Sub topic 8
Keeping secrets and credentials out of reach
This chapter assumes you can run a hardened container, as shown earlier in this tutorial. You should also know how Docker bind mounts and environment variables work.
Why secrets are the main target
An agent that runs shell commands can read anything its process can read. If a web page, issue, or dependency contains a prompt injection, the agent may be steered to find credentials and send them out. Network limits reduce the damage, but the safest secret is one the sandbox never receives.
Credentials usually leak through four paths:
| Path | Example |
|---|---|
| Inherited environment | `AWS_SECRET_ACCESS_KEY` exported in your shell |
| Home directory files | `~/.ssh`, `~/.aws`, `~/.config/gh`, `~/.npmrc` |
| Project files | `.env`, `terraform.tfvars`, committed keys in git history |
| Host sockets | `/var/run/docker.sock`, SSH agent sockets |
Start with nothing
Docker passes no host environment variables to a container unless you ask for them with -e or --env-file. Mount only the project directory, never $HOME. Do not mount the Docker socket. Anyone who can reach it can start a privileged container on the host, which defeats every other control.
Your project directory may still hold a .env file. Mounting an empty file over it hides the real contents from the agent:
docker run --rm -it \
--user 1000:1000 \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD":/workspace \
-v /dev/null:/workspace/.env:ro \
-w /workspace \
python:3.12-slim bashInside, cat .env prints nothing. The mount target must exist, so create an empty .env first if the project has none. Repeat the mount for each sensitive file, or keep secret files outside the project tree.
Pass an allowlist, not a denylist
When the agent is launched from a script, build the child environment from scratch. Copying os.environ and removing known secrets will miss the one you forgot.
import subprocess
ALLOWED_ENV = {"LANG": "C.UTF-8", "TZ": "UTC"}
def run_clean(command: list[str], timeout: int = 60) -> subprocess.CompletedProcess:
return subprocess.run(
command,
env=ALLOWED_ENV,
capture_output=True,
text=True,
timeout=timeout,
)
result = run_clean(["env"])
print(result.stdout)The output lists only LANG and TZ, whatever your shell exported.
Keep the real credential outside
Some tasks need an authenticated call, such as pushing a branch or calling a model API. Give the sandbox a way to make the call without giving it the credential. The usual design is a small proxy on the host or in a separate container. It holds the real token, accepts requests from the sandbox, and forwards only the operations you allow.
If a proxy is too much work, use a credential that is limited in three ways:
- Short-lived: it expires in minutes or hours, for example an STS session token.
- Narrow: a deploy key for one repository, or a token with read-only scope.
- Revocable: one you can revoke without disturbing anything else.
Watch for leaks after the fact
Secrets can leak through places you did not think to restrict:
docker inspectshows environment variables to anyone with access to the Docker daemon.- Agent transcripts and logs may record command output that contains a token.
- Files the agent writes into the project can be committed and pushed.
Scan the diff for secrets before you accept changes, and rotate any credential the sandbox may have seen.
Exercise
A teammate runs the agent with -v "$HOME":/workspace and --network none, saying the missing network makes it safe. What is wrong with this setup, and what would you change?
Show answer
The agent can still read ~/.ssh, ~/.aws, and other credential files, and it can write their contents into files in the mounted directory, which then persist on the host and may be committed or uploaded later. Mount only the project directory. Hide project-level secret files such as .env, pass no environment variables unless they are on an allowlist, and use a short-lived scoped token or a credential proxy if the agent needs authenticated access.