Sub topic 5
Giving the agent only the files it needs
This chapter covers filesystem scoping: deciding which host files a sandboxed agent can see, and how it can touch them. It assumes you can use Docker volumes, know Linux users and file permissions, and have read the chapter on hardening a container. The earlier examples already mounted the project directory with -v "$PWD":/workspace. Here we look at what that mount exposes and how to narrow it.
Why the mount is the main exposure
A hardened container with no network and no capabilities still has one deliberate hole: the bind mount. Anything the agent can write there persists on the host after the container exits. The agent runs commands with your project as its working directory, so it can:
- Delete or corrupt source files and the
.gitdirectory. - Plant files that run later on the host, such as git hooks,
package.jsonscripts,Makefiletargets, or editor configuration. - Read secrets that sit inside the project, such as
.envfiles.
The goal is to give the agent the smallest set of files that lets it do the job, with the weakest access mode that works.
What a bind mount shares
A bind mount makes a host directory appear at a path inside the container. The container sees the real files, not a copy. Three things control what happens next:
- The mount mode. Read-write is the default. Add
:rofor read-only. - The user ID. The kernel checks file permissions using numeric IDs. If the container runs as
1000:1000and the host files belong to UID 1000, the agent has your permissions on them. - The mount scope. Mounting
$HOMEor/exposes far more than mounting one project folder.
Mount only the project
Mount the narrowest directory that works. If the agent works in a subfolder of a monorepo, mount that subfolder, not the repository root.
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD/services/billing":/workspace \
-w /workspace \
python:3.12-slim bashThe agent now sees only services/billing. Parent directories, sibling services, and the repository's .git directory are not visible. Some tasks need git history. In that case you must decide whether to expose .git or to give the agent a copy.
Read-only mounts with a writable overlay
Many tasks need to read a lot of code but write only a little. Mount the source read-only and give the agent a separate place to write output.
mkdir -p out
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$PWD":/workspace:ro \
-v "$PWD/out":/workspace/out \
-w /workspace \
python:3.12-slim bashHere /workspace is read-only, but /workspace/out is a second mount that is writable. Docker applies the more specific mount on top of the broader one. The agent can run tests and write reports to out, but it cannot edit source files. You then review the output and apply any changes yourself.
This works well for analysis, code review, and test runs. It does not suit tasks where the agent must edit source, because the edits would land in out and you would have to copy them back.
Work on a copy
When the agent must edit code, a copy gives you a clean review point. Copy the project to a temporary directory, mount the copy, and compare afterwards.
WORK=$(mktemp -d)
cp -a . "$WORK/project"
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--network none \
-v "$WORK/project":/workspace \
-w /workspace \
python:3.12-slim bash
diff -ru . "$WORK/project" | lessYour original files are untouched until you decide to merge. The next chapter on disposable sandboxes builds a full workflow on this idea. Note that cp -a copies everything, including .env files. Exclude secrets when you copy (see below).
Keep secrets out of the mounted tree
Files in the project directory are visible to the agent. Common leaks are:
.envand.env.localfiles.- Cloud credential files such as
.aws/credentialscopied into a repository. - Private keys,
*.pemfiles, andterraform.tfstate. - Git history that once contained a committed secret.
Two approaches work. The first is to copy without those files, using rsync with exclusions:
rsync -a \
--exclude '.env' \
--exclude '.env.*' \
--exclude '*.pem' \
--exclude '*.tfstate' \
--exclude '.git' \
./ "$WORK/project/"The second is to hide a file inside a live mount by mounting an empty file over it. Docker lets you bind a single file on top of a path from a broader mount:
touch /tmp/empty
docker run --rm -it \
--user 1000:1000 \
--network none \
-v "$PWD":/workspace \
-v /tmp/empty:/workspace/.env:ro \
-w /workspace \
python:3.12-slim bashInside the container, /workspace/.env exists but is empty. This is easy to forget for new secret files, so an allowlist copy is safer than a denylist. Copying only src/, tests/, and your build files cannot leak a file you never thought to list.
Protect files that run on the host
Some files are harmless to read but dangerous to let the agent write, because something on the host executes them later. If the agent can write these, it can turn a sandbox escape into code running as you.
| Path | Why it matters |
|---|---|
| `.git/hooks/*` | Git runs hooks on commit, merge, and checkout |
| `.git/config` | Can set `core.fsmonitor` or aliases that run commands |
| `Makefile`, `package.json` scripts | Run when you type `make` or `npm test` |
| `.vscode/tasks.json`, `.idea/` | Editors can run tasks automatically |
| `.github/workflows/` | Runs in CI with its own credentials |
You can make some of these read-only while the rest of the project stays writable, using the same overlay trick:
docker run --rm -it \
--user 1000:1000 \
--network none \
-v "$PWD":/workspace \
-v "$PWD/.git":/workspace/.git:ro \
-v "$PWD/.github":/workspace/.github:ro \
-w /workspace \
python:3.12-slim bashWhatever the agent produces, review changes to these paths first. A diff that touches .git/hooks or a workflow file deserves extra suspicion.
Permissions, ownership, and the user ID
Running with --user 1000:1000 means files the agent creates are owned by UID 1000 on the host. If your host user is not 1000, the agent either cannot write or creates files you cannot edit. Match the container user to your own:
docker run --rm -it \
--user "$(id -u):$(id -g)" \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashDo not fix permission errors by running as root (--user 0). Root inside the container owns root-created files on the host mount, and a root process has far more reach if anything else goes wrong. Fix the ownership or the user ID instead.
Symlinks and path tricks
A symlink inside the mounted directory can point to a path outside it. Inside the container the link resolves against the container's filesystem, so the agent cannot follow it to your host files. The risk appears later, when a host process follows a link the agent created. Suppose the agent creates report.txt as a symlink to ~/.ssh/config, and a host script copies or overwrites report.txt. The script then acts on the real file.
Defend against this on the host side:
- Review the output with a tool that does not follow links, such as
ls -landfind . -type l. - Use
rsyncwithout-Landcp -arather thancp -Lwhen moving results out. - Do not run host scripts over agent-written files without checking them.
find "$WORK/project" -type l -exec ls -l {} \;Temporary and cache paths
With --read-only, the container's root filesystem cannot be written, so tools that need scratch space fail. Provide tmpfs mounts for those paths. A tmpfs is memory-backed storage that disappears when the container stops.
docker run --rm -it \
--user 1000:1000 \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=256m \
--tmpfs /home/agent/.cache:rw,noexec,nosuid,size=512m \
-e HOME=/home/agent \
--network none \
-v "$PWD":/workspace \
-w /workspace \
python:3.12-slim bashThe options noexec and nosuid stop programs from running out of that space and ignore setuid bits. The size option caps memory use. Some tools, such as certain compilers and Python wheels with native code, need to execute from a temporary directory. If a build fails with "permission denied" on a file in /tmp, remove noexec for that mount only after checking you need it.
Diagram: what each mount exposes
Common mistakes
- Mounting
$HOME"for convenience". It exposes keys, tokens, and shell configuration. - Forgetting
:ro. Mounts are writable unless you say otherwise. - Using a denylist for secrets. New secret files appear over time. Prefer an allowlist.
- Running as root to avoid permission errors. Align UIDs instead.
- Trusting agent output. Files the agent wrote can contain hooks, scripts, or symlinks. Review before running anything on the host.
Exercise
You want an agent to refactor code in src/ but it must not change .git, .github, or .env, and it must not see ~/.ssh. Which mounts and options do you use, and why is working on a copy still worth considering?
Show answer
Mount only the project directory (not $HOME) read-write at /workspace. Add :ro bind mounts for .git and .github over their paths, and mount an empty file read-only over .env. Use --user "$(id -u):$(id -g)" so file ownership matches you, and avoid root. Working on a copy is still worth it because read-only mounts only cover paths you thought of. A copy lets you run diff -ru against the original and review every change, including new files, symlinks, or edited scripts, before merging anything back.
Exercise
A container runs with --read-only and --tmpfs /tmp:noexec. A build step fails with "permission denied" when it runs a binary it wrote to /tmp. What is the cause, and what is the safest fix?
Show answer
The noexec option on the tmpfs prevents executing files from /tmp. The safest fix is to point the tool at a different scratch directory, such as a dedicated tmpfs path mounted without noexec and with a small size limit. Removing noexec from /tmp is a fallback, and only after you confirm the build needs it.