Use cases · Published 2026-11-05
Run an AI Coding Agent Against Infrastructure Safely
How to run an AI coding agent near real infrastructure: give it its own identity, keep credentials out, sandbox it, narrow scopes and gate risky actions.
TL;DR
- An agent that can run commands has the reach of the account it runs as. OWASP calls the failure "excessive agency" and names three root causes: too much functionality, too many permissions and too much autonomy. Shrink all three.
- Give the agent its own identity. We tested it: run as your own user it reads your SSH key; run as a separate user it gets
Permission denied, nosudo, and cannot read another user's process environment. - Keep credentials out of its environment. A child process inherits everything.
env -iwith an allowlist starts it with only what you name. - Sandbox the files and the network, not only the prompts. Claude Code's own documentation says its working-directory boundary is a permission prompt, and an approved shell command can still write anywhere your account can. A container with a read-only repository, no capabilities and no network enforced the limits when we tried it.
- Narrow the scopes, then put a human in front of high-impact actions. Approvals live in the tool, and the ones that matter live in the platform too: branch protection, IAM and the apply pipeline.
- A hosted runtime moves these controls to a provider. It does not remove the need for them. The last section covers DigitalOcean Managed Agents, which we have not used, and says what to ask before you trust any such service.
- None of this stops prompt injection. It limits what a manipulated agent can reach.
How this was checked
The statements about OWASP, the Model Context Protocol (MCP), Claude Code and DigitalOcean were read from the pages listed under Sources on 4 November 2026.
The commands marked tested were run on 4 November 2026 on a disposable Ubuntu 26.04.1 LTS test machine (the user accounts and process environments) and with Docker 29.1.3 and the ubuntu:24.04 image (the container). All tokens and keys in the tests are fake or freshly generated for the test.
Not run: any agent, any MCP server, DigitalOcean Managed Agents, an egress proxy, a block on the cloud metadata address, and Claude Code's permission modes and sandbox. Where a section relies on a vendor's documentation instead of our test, it says so.
What can go wrong
OWASP describes excessive agency as a flaw that lets damaging actions follow from unexpected, ambiguous or manipulated model output. It lists three root causes: excessive functionality (tools the task does not need), excessive permissions (more access than the task needs) and excessive autonomy (high-impact actions with no human check). The manipulation can come from a prompt injection in a web page, an issue comment or a file the agent reads. Claude Code's documentation says plainly that its safeguards "significantly reduce risk" but that "no system is completely immune to all attacks".
So design for the day the agent does the wrong thing, whatever the reason. The question is what "wrong" can reach.
The MCP security guidance makes the same point about tool servers that run locally: they run with the same privileges as the client, and it recommends running them sandboxed with restricted file system and network access.
1. Give the agent its own identity (tested)
If the agent runs as you, it can do what you can. We created an operator account with an SSH key and a separate account for the agent:
# The agent as the operator can read the operator's private key.
su -s /bin/bash ops -c 'head -c 40 /home/ops/.ssh/id_ed25519'That printed -----BEGIN OPENSSH PRIVATE KEY-----. The same read as the separate agent account failed:
# Output as the agent account (uid 1003), on the same machine.
cat: /home/ops/.ssh/id_ed25519: Permission denied
ls: cannot open file '/home/ops/.ssh': Permission denied
sudo: I'm sorry agent. I'm afraid I can't do that
cat: /root/.bashrc: Permission deniedThe agent account also could not read the environment of a process owned by the operator (cat /proc/<pid>/environ gave Permission denied), while the operator could. A dedicated account is the lowest-effort control on this list, and it works for anything that runs commands, on a laptop or on a server. Give it a home directory with mode 700, no sudo, and no membership in groups such as docker that amount to root.
2. Keep credentials out of its environment (tested)
A child process inherits the environment of its parent. We exported a fake cloud token and access key and started a shell:
# A child process sees every exported variable of its parent.
export CLOUD_API_TOKEN=tok_demo_0123456789; export AWS_ACCESS_KEY_ID=AKIADEMO
sh -c 'env | grep -E "CLOUD_API_TOKEN|AWS_ACCESS_KEY_ID"'Both variables were printed. Starting the process with an empty environment and an allowlist removed them:
# Start the agent with only the variables it needs; token present: 0.
env -i PATH=/usr/bin:/bin HOME=/home/agent LANG=C.UTF-8 sh -c 'env | sort'The output held only HOME, LANG and PATH, plus the PWD that the shell itself sets. The environment is not the only place a credential lives, so look at what the process can reach, not only what you pass in: files such as ~/.aws and ~/.config, an ssh-agent socket, a cloud instance metadata address, a Git credential helper.
The stronger pattern is to keep the secret out of the sandbox entirely and let a broker attach it outside. Claude Code's documentation describes this for its cloud sessions: GitHub credentials "never enter the session VM", which holds a short-lived credential scoped to the session, while a proxy attaches the real credential on the server side. DigitalOcean describes the same idea for its Action Gateway (see the last section). We did not test either.
For your own pipelines, the same rule appears in managing CI variables and secrets safely, and if a credential has already been exposed, rotating a leaked credential without an outage is the procedure.
3. Sandbox the files and the network (tested)
Claude Code's documentation is direct about the limit of permission prompts. In Manual mode it asks before its file tools read or write outside the folder where it started, but "the boundary is a permission prompt, so a Bash command you approve can still write anywhere your user account can". For OS-level restriction it points to its sandboxing and, for more isolation, to running the whole process in a sandbox runtime or a dev container. The MCP guidance recommends the same for local servers: containers, restricted file system and network access, and explicit grants for anything more.
Tested. This is the sandbox we used, first with the host's environment passed in, for comparison:
# Plain container with the token passed in: root, writable root filesystem, network open.
docker run --rm -e CLOUD_API_TOKEN ubuntu:24.04 sh -c 'id; env | grep CLOUD_API_TOKEN; touch /etc/probe && echo wrote /etc/probe'It printed uid=0(root), the token, and wrote /etc/probe, and a connection to 1.1.1.1:443 succeeded. Then the same image with the limits:
# One agent session: read-only repository, scratch space, no network, no capabilities, non-root.
docker run --rm --user 10001:10001 --cap-drop ALL --security-opt no-new-privileges \
--read-only --tmpfs /tmp:rw,size=16m --network none \
-v "$PWD/repo:/work:ro" -v "$PWD/scratch:/scratch:rw" \
ubuntu:24.04 sh -c 'id; cat /work/app.py'What happened inside it:
| Attempt | Result |
|---|---|
id | uid=10001 gid=10001 |
| Token variable present | 0 (it was never passed) |
Read /work/app.py | worked |
touch /work/new.txt | Read-only file system |
touch /etc/probe | Read-only file system |
Write /scratch/out.txt and /tmp/t | worked |
Connect to 1.1.1.1:443 | Network is unreachable |
CapEff and NoNewPrivs | 0000000000000000 and 1 |
The file written to the scratch directory belonged to uid 10001 on the host, which is the one place the agent could leave something behind.
Two cautions. First, --network none is the strict end: an agent usually needs to reach its model API, so the strictest sandbox suits the steps that do not, such as running tests or builds. For the rest, allow only the destinations it needs. The MCP guidance, written about MCP clients that fetch URLs, recommends blocking private address ranges and the link-local cloud metadata address 169.254.169.254 and sending outbound requests through an egress proxy. It also warns against writing your own IP checks, because encodings such as octal and hex slip past them. The same idea fits an agent's sandbox. We did not test a proxy. Second, a container is not a virtual machine: it shares the host's kernel. Claude Code's own advice for untrusted content is to use virtual machines, especially when interacting with external web services. See also non-root users in Docker images.
4. Narrow the scopes of what it can call
Give the agent the smallest role that does the job, and let the expensive step belong to someone else. A read-only role is enough to inspect infrastructure and to run terraform plan. The identity that applies the change should be one the agent does not hold. Running Terraform plan and apply safely and protecting Terraform state and secrets explain why that split is already good practice with no agent involved, and a plan file can hold secrets, so keep it away from the agent's output (keeping secrets out of plan artifacts).
The MCP guidance gives three rules worth copying for any tool the agent calls:
- Minimal scopes, raised on demand. Start with low-risk read operations and elevate when a privileged operation is first attempted. Avoid wildcard or all-access scopes, which turn a leaked token into a master key.
- No token passthrough. An MCP server "MUST NOT accept any tokens that were not explicitly issued for the MCP server". Passing a client's token to a downstream API breaks the audience boundary and the audit trail.
- Per-client consent for proxies. A proxy that fronts a third-party API must get consent for each client, not once for all.
OWASP adds a related rule: enforce authorization in the downstream system and never rely on the model to decide what it may do.
5. Put a human in front of high-impact actions
OWASP's recommendation is to "require a human to approve high-impact actions before they are taken". Define high-impact before you start: terraform apply and destroy, deleting data or resources, changing IAM or network rules, pushing to a protected branch, sending messages to customers, anything that spends money.
Claude Code's documentation describes two modes. In Manual mode it starts read-only, asks before edits and commands, runs a built-in set of read-only commands such as ls and git status without asking, and does not auto-approve commands that fetch from the web such as curl and wget. Unmatched commands require approval. In its auto mode, a separate classifier model reviews actions instead of you. That is a different control from a human decision, so do not count it as one for the actions above. Allowlists reduce prompt fatigue, but an allowlist rule for part of a command, such as Bash(git *), does not skip the prompt for a command Claude Code cannot fully analyze, and a deny rule matches the command as written, so network enforcement that does not depend on command text belongs in the sandbox.
Put the approvals that matter where the agent cannot switch them off. A prompt inside the agent's tool is a setting; branch protection, an IAM policy that the agent's role lacks and a pipeline that needs a reviewer are enforcement.
6. Keep a log the agent cannot edit
An incident is hard to investigate when the same credential appears in the logs under another identity, which is the audit problem the MCP guidance raises for token passthrough. Log the commands and tool calls, and ship the log somewhere the sandbox cannot write. Claude Code documents OpenTelemetry metrics for monitoring usage and says its cloud sessions log all operations for audit.
A hosted option: DigitalOcean Managed Agents
Everything above can be done on a laptop or a server you own, and that is where to start. If you would rather have a provider host the sandbox, the sessions and the credential handling, one option is DigitalOcean Managed Agents. We have an affiliate relationship with DigitalOcean, and the link at the end of this post is an affiliate link. We have not used the product, and nothing in this section was tested. The following is what DigitalOcean's product page says on 4 November 2026:
- Both components are labeled Public Preview, and access requires opting in to the preview terms.
- Harness Runtime: each agent session runs in its own Firecracker microVM with its own compute and filesystem. Sessions can be paused, resumed, checkpointed and forked. It supports Claude Code, Codex CLI, OpenCode, Hermes, LangGraph and CrewAI, or your own agent as an OCI image.
- Action Gateway: governed access to "16,000+ tools through a single managed MCP endpoint". Credentials are resolved at execution time and never reach the model, the prompt or the sandbox. Permissions are set per tool and action, with human approval required for sensitive operations.
That is the same set of controls as above (isolation, a credential broker, approvals), operated by someone else. It does not remove your part. You still decide which tools the agent may call, what each approval covers and who answers it. Before you trust any hosted agent service, ask:
- Where do credentials live, and can you revoke them yourself?
- What is logged, for how long, and can you export it?
- What can the sandbox reach on the network, and who controls that?
- Where does your code run, and what happens to session data when you delete a session?
- What changes when a preview ends: terms, limits, support, price?
Checklist
- [ ] The agent runs as its own account, with no
sudoand no access to your home directory. - [ ] It starts with an empty environment and an allowlist, and no credentials are reachable from its files or sockets.
- [ ] Its files are read-only except one scratch directory, its network is closed or limited to named destinations, and it runs without capabilities.
- [ ] Its cloud and tool access is a read-only or narrowly scoped role, with no wildcard scopes and no token passthrough.
- [ ] Applying, deleting, IAM changes and protected-branch pushes need a human, and the platform enforces it.
- [ ] Commands and tool calls are logged outside the sandbox.
- [ ] You have a way to rotate every credential the agent has ever touched.
What we have not verified
- Any agent product's behavior. We read Claude Code's documentation and did not run its permission modes, sandbox or classifier.
- DigitalOcean Managed Agents. Everything in that section is DigitalOcean's description of a public preview.
- An egress proxy and a metadata-address block. Both come from the MCP guidance and were not tested.
- A virtual machine instead of a container. We tested a container only.
- Prompt injection. These controls limit the damage of a manipulated agent. They do not stop the manipulation.
If something differs on your system, tell us at info@opsbreakfix.com and we will fix the page.
Sources
More from the blog
- SSH Port 22 Open on GCP vs Azure: 12 Hours of Logs · Use cases, 2026-11-16
- What Happens When You Expose SSH to the Internet · Use cases, 2026-11-13
- Backup Monitoring with Heartbeat Checks: Dead Man's Switch, Exit-Status Alerts and Restore Tests · Use cases, 2026-11-01
This article is educational information, not incident-response or legal advice. Verify package status with your operating system, hosting, or runtime vendor.