Sub topic 8
Keeping secrets and credentials out of reach
This chapter assumes you can run the hardened container from the earlier chapters. It also assumes you know basic Docker usage, environment variables, and Linux file permissions. The goal is to make sure a sandboxed agent cannot read, copy, or send away credentials it does not need.
Why secrets are the main prize
A sandbox that stops the agent from damaging the host does not protect secrets that you hand to the agent. A coding agent reads files and command output and sends them to a model provider. It may also run commands that reach the network. If a secret is visible to the agent, assume it can end up in:
- The model provider's request logs, because file contents and command output are part of the prompt.
- A file the agent writes into the project, which you may later commit.
- A network request made by a command the agent runs, including one triggered by a malicious instruction hidden in a README, an issue, or a dependency.
- The transcript or log files you keep for auditing.
The rule is least privilege applied to credentials. Give the agent only the secrets the current task needs, with the narrowest scope and the shortest lifetime. Anything else stays outside the sandbox.
Where secrets leak from
Most leaks come from ordinary places. Check each of these before you start an agent.
| Source | How it leaks | Mitigation |
|---|---|---|
| Home directory mounted into the container | `~/.ssh`, `~/.aws`, `~/.config/gh`, `~/.npmrc`, `~/.docker/config.json` become readable | Mount only the project directory, never `$HOME` |
| Environment variables | `env` or `printenv` prints every value. Child processes inherit them | Pass only the variables the task needs |
| `.env` files in the project | The agent reads them as normal files | Exclude them from the mount or keep them out of the tree |
| Git history and config | Old commits may contain keys. `.git/config` may hold a token in a remote URL | Use a clean checkout and check remotes |
| Docker socket | `/var/run/docker.sock` gives control of the host's Docker daemon | Never mount it |
| Cloud metadata endpoint | `169.254.169.254` returns instance role credentials on cloud hosts | Block it with network rules, or require session tokens |
| Shell history and logs | Old commands may include tokens | Do not mount them. Redact logs |
The Docker socket deserves emphasis. A container that can talk to it can start a new privileged container with the host's root filesystem mounted. That undoes every hardening flag from the earlier chapters.
Start with nothing
The hardened command from the container chapter already avoids most of these leaks. It mounts only the project directory, starts with no extra environment variables, and uses --network none. Docker does not copy your shell environment into a container unless you ask for it with -e or --env-file.
Check this instead of trusting it. Run the container and inspect what the agent would see:
docker run --rm \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim \
sh -c 'env | sort; echo "---"; ls -la /workspace; echo "---"; ls -la /root /home 2>&1'The output should show only a small set of default variables such as PATH and HOSTNAME. If you see a token there, something in your launch script is passing it in.
Note that /workspace is your project. Whatever lives in it is visible to the agent, including hidden files. List them with ls -la, and look for .env, .npmrc, .pypirc, credentials.json, and private keys.
Keep secret files out of the mount
Mounting a directory exposes everything inside it. You have three practical options.
- Move secrets out of the project tree. Keep them in a directory that you never mount. This is the simplest and most reliable option.
- Work from a clean copy. Copy or clone the project into a temporary directory, delete secret files from the copy, and mount that. The ephemeral workflow chapter covers this in more detail.
- Mask individual files. Bind-mount an empty file over the secret path so the agent sees an empty file in its place.
The third option looks like this:
touch /tmp/empty
docker run --rm -it \
--user 1000:1000 \
--read-only \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD":/workspace \
-v /tmp/empty:/workspace/.env:ro \
-w /workspace \
python:3.12-slim bashMasking works, but it is easy to miss a file. Treat it as a patch for a known path. It does not replace a clean mount.
Passing a secret the task really needs
Sometimes the agent must call an API or install a private package. Choose the safest delivery method that works.
Environment variables
Environment variables are the easiest option and the weakest. Any process in the container can read them, and docker inspect shows them to anyone with Docker access on the host. Use them only for low-value, short-lived tokens.
docker run --rm -it \
-e API_TOKEN \
--network none \
python:3.12-slim \
sh -c 'test -n "$API_TOKEN" && echo "token is set"'Passing -e API_TOKEN with no value copies the variable from your shell. This keeps the secret off your command line and out of shell history.
Mounted secret files
A read-only file mounted from outside the project is better. It does not appear in docker inspect output as an environment value, and you can restrict who may read it.
docker run --rm -it \
--user 1000:1000 \
--read-only \
--cap-drop=ALL \
-v "$HOME/.secrets/api_token":/run/secrets/api_token:ro \
python:3.12-slim \
sh -c 'wc -c < /run/secrets/api_token'The agent can still read the file, so the token is still exposed to the model. File delivery limits accidental exposure through env and inspect output. It does not hide the secret from the agent.
Credentials the agent never sees
The strongest pattern keeps the secret outside the sandbox and lets something else use it on the agent's behalf. A proxy on the host or a sidecar container holds the real key. The agent sends requests to the proxy, and the proxy adds the authorization header and forwards the request to an allowed destination.
services:
agent:
image: python:3.12-slim
user: "1000:1000"
working_dir: /workspace
command: ["sleep", "infinity"]
read_only: true
cap_drop:
- ALL
volumes:
- ./project:/workspace
environment:
API_BASE_URL: http://api-proxy:8080
networks:
- sandbox
api-proxy:
image: my-org/api-proxy:latest
environment:
UPSTREAM_TOKEN_FILE: /run/secrets/upstream_token
secrets:
- upstream_token
networks:
- sandbox
- egress
secrets:
upstream_token:
file: ./secrets/upstream_token
networks:
sandbox:
internal: true
egress: {}In this layout, the agent is on an internal network and can reach only the proxy. The proxy is on both networks. The agent never holds the token, so a prompt injection cannot make it print the token. The proxy image here is a placeholder. You must supply one that forwards only the endpoints and methods you allow.
Use scoped, short-lived credentials
When a secret has to be inside the sandbox, limit the damage if it leaks.
- Scope. Use a token with only the permissions the task needs. A read-only token for one repository is better than a personal access token with full account rights.
- Lifetime. Prefer tokens that expire in minutes or hours. Cloud providers can issue temporary credentials through role assumption.
- Separate identity. Create a dedicated account or service identity for the agent. Its activity then shows up separately in audit logs, and you can revoke it without affecting yourself.
- Revocation plan. Know how to revoke the token before you hand it over.
Git credentials
Agents often need to commit. You do not need to give them push rights to do that. A common pattern is:
- The agent commits to a local branch inside the sandbox.
- You review the diff outside the sandbox.
- You push from the host, using your own credentials.
This keeps the push credential out of the sandbox completely. If the agent must push, use a deploy key or token limited to a single repository and a non-protected branch.
Secrets in output and logs
Even if the secret is correctly isolated, it can reappear in output. Test commands print configuration, error messages include URLs with tokens, and stack traces dump environment values. Handle this on the way out:
- Run a secret scanner over the diff and the logs before you share or commit them.
- Redact known token patterns from saved transcripts.
- Do not write secrets into command-line arguments. Arguments appear in process listings and logs.
Scanning cannot catch everything. Treat it as a second layer, not a replacement for keeping the secret away from the agent.
Network rules protect secrets too
A secret that the agent can read is only dangerous if it can leave the sandbox. The network egress chapter covers this in detail. For credentials, the two relevant points are:
- With
--network noneor an internal network, a leaked value cannot be sent to an attacker's server directly. - Allow only the destinations the task needs. A token for one API should not be usable from a container that can reach arbitrary hosts.
On cloud hosts, also block the metadata address 169.254.169.254 from the container, because it can return the host's role credentials.
How the pieces fit
Checklist before you start an agent
- Does the mount contain only the project, with no
.env, key, or credential files? - Does
envinside the container show only values you chose? - Is the Docker socket absent?
- Is every token scoped, short-lived, and revocable?
- Does the network limit where a leaked token could go?
- Will you scan the diff and logs before sharing them?
Common mistakes
- Mounting
$HOME"just for convenience". - Using
--env-file .envwith a file that holds production credentials. - Assuming a read-only mount hides a file. Read-only prevents writes, not reads.
- Believing that
.gitignorehides files from the agent. It affects Git, not the filesystem. - Putting a secret in the image with
ENVorCOPY. It stays in the image layers and anyone with the image can extract it.
Exercise
An agent needs to call a paid API during a task. You are choosing between passing the key as an environment variable and running a proxy that holds the key. Which is safer against a prompt injection that tells the agent to print its secrets, and why? Name one situation where the environment variable is still acceptable.
Show answer
The proxy is safer. The agent never holds the key, so a prompt injection has nothing to print or send. With an environment variable, any command the agent runs (such as env) can reveal the key, and it may end up in prompts, logs, or output. The environment variable is acceptable when the token is low-value, scoped to the task, and short-lived, so a leak costs little and the token expires soon, especially when the container has no outbound network access.
Exercise
Your project directory contains a .env file with a database password. You want to run the agent without a copy of the project. Name two ways to keep the agent from reading that file, and one reason .gitignore does not help.
Show answer
Move the file outside the mounted directory, or bind-mount an empty read-only file over /workspace/.env. A clean copy of the project without the file also works. .gitignore only tells Git which files to skip. It does not change file permissions or mounts, so the agent can still read the file from the filesystem.