Sub topic 9
Disposable sandboxes and reviewing changes
This chapter assumes you can run the hardened container from the previous chapters and that you know basic Git (clone, diff, commit, branch). Here you will make the sandbox disposable and put a review step between the agent's work and your real repository.
Why disposable matters
A hardened container limits what the agent can reach, but a long-lived sandbox still accumulates state. Caches, edited config files, leftover processes and half-finished changes can all carry over to the next session. A disposable sandbox starts from a known state each time and is deleted when the session ends. That gives you two properties:
- Reproducibility. Every run begins from the same image and the same commit.
- Containment of persistence. If the agent plants something, such as a modified startup file or a rewritten Git hook, it disappears with the sandbox.
The second property only holds if you never mount your real repository writable. Give the agent a copy instead, and decide afterwards which changes to keep.
The workflow
A complete script
The script below clones the repository into a temporary directory, runs the container, and shows the result. It asks before importing anything.
#!/usr/bin/env bash
set -euo pipefail
SRC="$(git rev-parse --show-toplevel)"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
git clone --quiet "$SRC" "$WORK/project"
docker run --rm -it \
--user "$(id -u):$(id -g)" \
--read-only --tmpfs /tmp \
--cap-drop=ALL \
--security-opt no-new-privileges \
--pids-limit 256 --memory 2g --cpus 2 \
--network none \
-e HOME=/tmp \
-v "$WORK/project":/workspace \
-v "$WORK/project/.git":/workspace/.git:ro \
-w /workspace \
python:3.12-slim bash
git -C "$WORK/project" add -A
if git -C "$WORK/project" diff --cached --quiet; then
echo "No changes."
exit 0
fi
git -C "$WORK/project" diff --cached --stat
git -C "$WORK/project" --no-pager diff --cached
read -r -p "Import these changes into a review branch? [y/N] " answer
if [[ "$answer" == "y" ]]; then
branch="agent-$(date +%Y%m%d-%H%M%S)"
git -C "$WORK/project" commit --quiet -m "Agent changes"
git -C "$SRC" fetch --quiet "$WORK/project" "HEAD:refs/heads/$branch"
echo "Created branch $branch"
fiSome details in this script matter:
git clonecopies only committed files. Untracked files, such as a local.env, never enter the sandbox. Uncommitted edits are also left out, so commit or stash first.- The second
-vline mounts the clone's.gitdirectory read-only over the writable workspace. The agent can edit files but cannot rewrite hooks or Git config. The host runs Git on that directory afterwards, so this matters. Config options such ascore.fsmonitorcan execute programs. - The agent cannot commit from inside the container. The host does that after you have read the diff.
trap ... EXITremoves the temporary directory even if the script fails.
Common errors
- Permission denied inside the container. The
--uservalue does not match the owner of the temporary directory. Using$(id -u):$(id -g)avoids this. - Git warns about "dubious ownership". The container user differs from the directory owner. Fix the
--uservalue instead of disabling the check. - Changes vanish. The clone lives in a temporary directory, so deleting it discards everything. Import a branch before the script exits.
- Merging blindly. Fetching into a branch is not a review. Read the diff before you merge.
Exercise
The script mounts the clone's .git directory read-only inside the container. What attack does this prevent, and why does it matter that the host runs Git afterwards?
Show answer
It stops the agent from modifying Git hooks or repository config, such as setting core.fsmonitor to a command. When the host later runs git add, git diff or git commit in that clone, such a setting could execute the agent's code outside the sandbox. With .git read-only, the host-side Git commands read only configuration the agent could not change.