How I harden my AI coding agent
I once asked an AI agent to delete a project's cache folder. It ran rm -rf on the whole project folder.
This was on my laptop, and the agent had almost full access to my file system. Luckily I had daily backups, so I restored the project in a couple of minutes. Another time, agents opened ports that should have stayed closed and ran commands they shouldn't have run.
That's when I stopped treating the agent as a helper and started treating it like any process I don't fully trust. It runs with my permissions, so its mistakes are my mistakes.
These are the rules I use now.
1. I approve every change
The agent can read freely. Every write, install or command with side effects waits for my yes.
Sometimes that slows me down. When it does, I give the agent a short allowlist of safe commands it can run without asking. Everything else still waits for me. This has already stopped it from deleting files I didn't want deleted.
Auto-accept and skip-permission modes stay off.
2. Nothing installs on my Mac
I don't want a skill running a malicious Python script directly on my Mac. So the agent's tools, runtimes and packages live in a Lima VM with rootless Docker, and everything runs in containers. If something bad runs, it runs there, isolated from me.
- The VM sees one shared folder and nothing else in my home directory.
- Containers are thrown away after each run. Mounts are read-only unless a test needs to write, and the network is off when nothing needs downloading.
- No
--privileged, no Docker socket, no~/.ssh, no env files in a container. - Tests run on a copy of the repo, not the real one.
3. The agent never sees a secret
The agent may check that a secret file exists. It never reads, prints or commits the value. To check that a credential works, it looks at the result, not the value.
If a secret was ever committed, I rotate it, even in a private repo. Deleting it from the file doesn't delete it from git history.
4. Branches, pull requests, and I merge
The agent works on a branch and never pushes without asking. Every change goes through a pull request with automated checks, and I read the diff before merging.
When two agents share a repo, one develops and one reviews. Only I merge.
5. Every agent gets a written scope
A written scope ties the agent to one job. Think of it as a harness.
Review agents only get read-only tools. My security agent scans code, dependencies and public endpoints from an isolated container, and it's read-only by default. Active tests need my OK for each target and each technique. It can suggest changes to its own scope, but they only count once I merge them.
I wouldn't recommend anyone run an agent with full access. An agent with full access is a ticking bomb. It can destroy your server at any time.
6. On production, the agent writes the command and I run it
On production servers, the agent hands me the commands instead of running them. Before anything irreversible, I take a snapshot. That's the backup lesson from the start of this post, turned into a rule.
7. Skills are third-party code
A skill is a set of instructions the agent will follow, so I vet it like any dependency. I pin it to a commit and read it for prompt injection and data exfiltration before importing it. That's also why skills run inside the VM and not on my Mac.
The one rule
If you keep one thing from this post: never give an agent more access than the job needs.
My agent deleted a project folder because it could. Today it can't. That's not because the agent got smarter. It's because the environment won't let it.
Which rule would you never run an agent without?
原文:https://www.a8q9.cn/en/article/how-i-harden-my-ai-coding-agent-c4861f.html