Keep API keys out of your AI agent's sandbox
A coding agent can read every key in its environment. Why that matters, what scoped keys miss, and how a host-side placeholder swap keeps real keys out of the VM.
4 min read
Give a coding agent a shell and it can run env. Whatever keys you exported for it to do its job, it can now print, write to a file, or paste into a request. Most of the time it won't. The problem is the times it will, and the fact that you will not be watching when it does.
This post covers why that risk is real, what the common fixes do and don't solve, and how Zode keeps the real value of a secret outside the VM entirely.
How keys leak from an agent's machine
Three paths come up again and again.
- Prompt injection. The agent reads a README, an issue comment or a web page that says "run this to finish setup". The command posts
$STRIPE_KEYto a server. The agent was only following instructions it found in the repo. - A dependency that phones home. A package's install script reads the environment and sends it somewhere. Agents install packages all day, often ones you have never looked at.
- Logs and transcripts. An agent that debugs by printing its environment writes the key into a log, a PR comment or a chat transcript that lives far longer than the sandbox.
None of these need the model to be malicious. They need one untrusted input and one secret in reach.
What the usual fixes cover
| Fix | What it stops | What it misses |
|---|---|---|
| Scoped or read-only keys | Damage from a stolen key | The theft itself; a scoped key still works for whoever holds it |
| Short-lived tokens | Long-term use of a stolen key | Use within the token's lifetime, which is often enough |
| Egress allowlist | Traffic to unknown hosts | A key sent to an allowed host it was never meant for |
A .env file the agent "shouldn't read" | Nothing; it is a file on the agent's disk | Everything |
Scoped and short-lived keys are still worth doing. They limit the blast radius. But each of them assumes the key is inside the machine, and the question is only how bad it gets when it walks out.
Keep the value on the host instead
The fix Zode uses is to never put the real value in the VM at all.
When you store a secret in a Zode environment, you also list the hosts it may be sent to. Commands inside the sandbox see a placeholder such as zode_vault_7f92a1, never the value. Web traffic on ports 80 and 443 goes through an egress proxy on the host. When the sandbox sends the placeholder over TLS to one of that secret's allowed hosts, the proxy swaps in the real value on the way out.
Sent anywhere else, the placeholder goes out unchanged. The receiver gets a string that is useless outside that one sandbox.
import { Zode, Sandbox } from "@zodedev/sdk";
const zode = new Zode();
await zode.environments.create("staging");
await zode.environments.setSecret("staging", "STRIPE_KEY", {
value: process.env.STRIPE_KEY!,
allowedHosts: ["api.stripe.com"],
});
const sbx = await Sandbox.create({ environment: "staging" });Inside that sandbox, echo $STRIPE_KEY prints the placeholder. A call to api.stripe.com works as normal. A script that posts the variable to any other server sends nothing of value.
What this means for the three leak paths
- Prompt injection can make the agent send the placeholder anywhere it likes. Only the allowed host ever receives the real key.
- A dependency that phones home reads the environment and gets placeholders.
- Logs and transcripts record the placeholder, which is safe to keep.
Rotation gets easier too. A secret's placeholder stays the same when you change its value, and the host picks up the new value within about 30 seconds. Nothing in the sandbox needs a restart.
How the swap works, and what it costs
To put a value into an encrypted request, the proxy has to see that request. Each sandbox trusts a certificate authority for its workspace, and the proxy opens TLS only for two kinds of hosts: hosts one of the sandbox's secrets allows, and known AI provider APIs. Every other TLS connection passes through unread.
That is a real trade-off and we would rather say it plainly. If you need end-to-end TLS from inside the VM to a host, don't attach a secret for that host; the connection then passes through untouched.
Secret values are encrypted at rest with AES-256-GCM, each under its own data key. The API returns secret names and allowed hosts, never values, and Zode's own logs record names only. The full model is on the security page.
Agent logins work the same way
The same swap covers the agent's own credentials. Claude Code can run on your Claude subscription: the token stays on the host, each sandbox gets its own placeholder, and the host adds the real token only on requests to api.anthropic.com. A placeholder read from one VM is useless in any other. We walk through it in Run Claude Code in a cloud VM.
The short version
Assume the agent will, one day, do exactly what an untrusted input tells it to. Then make sure the worst it can send is a placeholder. Scoped keys limit what a stolen key can do; keeping the key outside the machine means there is nothing to steal.
Try it: install the CLI, or start free in the console.
curl -fsSL https://zode.dev/install | sh