An interactive coding assistant can pause before every shell command and wait for you to inspect it. A background coding agent working through an issue backlog cannot.
To diagnose a real bug or repair a failing build, a background agent has to ingest text written outside your repository: issue descriptions, pull request comments, bug-reporter attachments, test fixtures, dependency output, and failed CI logs.
Once a model reads untrusted text while holding shell and file tools, prompt instructions alone stop being a security boundary.
Why prompt-only rules break under untrusted input
To a language model, external input arrives as plain text in the same context window as its system instructions. A pasted stack trace, a comment from an external reporter, or a string embedded in a test fixture can contain misleading or adversarial instructions that push the agent toward running an unintended command, reading environment variables, or calling an external endpoint.
Writing "do not push" or "do not call external APIs" in a system prompt is good developer hygiene — it keeps normal runs predictable and reduces accidental mistakes. It is not an enforcement boundary.
A real security boundary lives outside the model's context window and still holds if the process ignores its instructions completely.
Four enforcement layers outside the model
When running background coding agents on your own machine or dedicated virtual machine, practical defense-in-depth comes down to four architectural controls:
1. Fresh, unprivileged container per run
- Start every run in a brand-new Docker container and remove it as soon as the run finishes so nothing persists between runs.
- Execute the agent as an unprivileged user without access to Docker inside the container, scoped only to the files of the active project.
- Be honest about the host boundary: whatever runner service starts those containers talks to the Docker socket, which implies root-level access on that host. Put the runner on a dedicated machine or VM rather than a shared workstation holding unrelated production credentials.
2. Fail-closed outbound network allowlist
- Restrict outbound network traffic from the run container to only what the task genuinely needs: your code host and issue tracker, your AI provider, package registries, and explicitly allowed project hosts.
- Fail closed: if the firewall rules cannot be initialized, refuse to start the run rather than falling back to unrestricted internet access.
- Know the limit: IP-based filtering with open DNS is a strong barrier, not an absolute guarantee. Never treat network egress filtering as your only line of defense.
3. Keep write tokens out of the run container
- An agent inspecting code and running local checks needs read access to fetch branches and history, not repository write or admin permissions.
- Pass only a read-only repository token into the agent container.
- Keep write-scoped tokens encrypted outside the execution container, mask known secrets in run output, and keep AI provider credentials local to the runner rather than spraying them across external services.
4. Move writes and pushes out of the agent process
- Opening merge requests or pull requests and posting private review drafts should happen outside the agent container.
- Execute
git pushin a separate container without the agent, triggered only after fixed branch checks pass (commits build cleanly on remote history, commit authorship matches the developer) and explicit human approval before the first push.
A quick audit checklist for your own setup
Before trusting any background agent workflow on a production repository, four questions tell you almost everything:
- If a hostile string in a ticket convinces the agent to run
git push --force, what stops it — a sentence in a prompt, or the absence of a write token? - If the agent tries to curl an arbitrary external domain with environment variables, what stops it?
- If the run finishes or crashes, does a fresh container guarantee zero leftover state for the next ticket?
- Does human approval sit before the first push reaches colleagues, and does merging stay strictly with your team?
Prompts describe how a careful developer should behave. Containers, network limits, read-only tokens, and agent-free pushes enforce what the process can actually do.
How is your team isolating background coding agents from write credentials and untrusted ticket/CI input today?
Originally published on the Mergehand blog.
Top comments (2)
Layer 4 has one soft spot worth naming: the human approving the first push usually reads the agent's own summary of the change. If a hostile ticket can steer the commands, it can also steer the PR description into "formatting only" while the diff touches a workflow file or a lockfile.
I'd have the approval step list changed paths in sensitive areas (CI config, dependency files, scripts) first, generated from the diff itself rather than from anything the agent wrote. Then the approver checks the part a prompt injection would actually aim at.
Fair catch — trusting an agent-authored summary as the main approval artifact is definitely a real blind spot. If untrusted input can steer the edits, it can just as easily steer the summary into "minor cleanup" while slipping a line into a CI workflow, lockfile, or build script.
Showing the raw commit list and full diff directly from Git (rather than only prose) helps, and a separate review pass can catch obvious mismatches, but another model is still not a deterministic guarantee.
Surfacing a deterministic, diff-derived banner of touched sensitive paths (
.github/workflows/,.gitlab-ci.yml, lockfiles, Dockerfiles, scripts) right at the top of the approval screen — computed purely from Git without touching model output — is a genuinely solid defense against that exact disguise. Definitely adding that to the list of things to implement.