Files
devvm/testdata/script/network.txtar
T
w3cjandClaude Opus 5.5 329e31be1d refactor(secrets): the proxy looks secrets up in-process, from one versioned bindings file
The proxy no longer runs devvm secret get for a value: proxy.IO.Secret,
which devvm _proxy sets to secrets.Store.Get, looks a binding's value up
in the proxy's own process (Keychain, devvm's secrets folder, or the
agent's login file), so internal/proxy stays standard library only.
Config.SecretCmd (secret_cmd) and the hidden devvm secret get command go,
with secret_to_terminal and secret_read_failed; Main refuses a config
with secrets and no IO.Secret.

One binding type, proxy.Binding, with kind secret|login and a login's
agent, in proxy.BindingList ({"version": 1, "bindings": [...]}), written
by secrets.Store (Encode) and read by the proxy and RefreshFiles with
proxy.ReadBindings (private regular file, no link, 1 MiB, version and
kinds checked; an unreadable file has no bindings). It replaces
proxy.binding, secrets.binding and agentlogin.Binding; the kind replaces
the name-prefix classifiers in proxy.ttlOf, secrets.loginOf and
RefreshFiles' Lookup check; proxy.InterceptLines replaces
agentlogin.Hosts and RefreshFiles' host loop.

agentlogin's user-actionable errors are typed (ErrCodexNoKey,
ErrCodexNoChatGPT, RefreshError) and secrets maps them to messages:
codex_no_key, codex_no_chatgpt, codex_refresh_failed,
opencode_refresh_failed, and login_read_failed for the rest.

Tests: proxy TestSecretValueLooksUpInProcess, TestReadBindings,
TestBindingsUnreadableHaveNone, ttlOf by kind, Main refusing secrets
without a lookup and -secret-cmd, config refusing secret_cmd; secrets
TestGet by kind, TestLoginFailed; agentlogin TestCodexBindings (kind and
agent checked on every binding), TestGetErrorsSayWhatToDo; secret.txtar
and network.txtar on the new file and without secret get; e2e's network
section keeps the fake token off argv and checks no command prints it.

Docs: the proxy README (Secrets section), internals/network.md,
safety/network.md, using/coding-agents.md, contributing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 19:35:25 -07:00

270 lines
12 KiB
Plaintext

# Network policy for a new VM: the choice, the VM's proxy (devvm _proxy,
# started before the VM), the confined start, the self-check, and the
# VM's secrets as the proxy sees them.
# --no-network-policy: off, with no proxy and no CA.
project off
exec devvm --no-network-policy --no-git-protect
stdout '^==> creating_from image=base vm=devvm-off-[0-9a-f]{14}$'
vm_name OFF off
vm_state $OFF_dir/vm.json
stdout '^net\.on=false$'
! stdout 'proxy_|net_check'
stub_calls
! stdout curl
! exists $DATA/ca
# --network-policy: devvm's CA (kept out of Time Machine on macOS), the
# VM's proxy, then the VM started confined, its environment pointing at
# the proxy with the VM's key, then the self-check, the setup and the
# shell.
project net
stub_reset
exec devvm --network-policy --no-git-protect
stdout '^==> starting vm=devvm-net-[0-9a-f]{14}$'
exists $DATA/ca/ca.pem $DATA/ca/ca.key
vm_name NET net
vm_state $NET_dir/vm.json
stdout '^net\.on=true$'
stdout '^net\.made\.check=ok$'
stdout '^net\.made\.proxy_key=[0-9a-f]{64}$'
stdout '^net\.made\.proxy_salt=[0-9a-f]{32}$'
stdout '^ports\.host\.ssh=[0-9]+$'
stdout '^ports\.host\.proxy=[0-9]+$'
stub_calls
[darwin] stdout '^tmutil addexclusion \S*/data/devvm/ca$'
[darwin] stdout '^limactl edit --tty=false devvm-net-[0-9a-f]{14} --set \.env = \{"HTTPS_PROXY":"http://devvm:devvm-proxy-password@192\.168\.5\.2:[0-9]+",.*"NODE_EXTRA_CA_CERTS":"/etc/ssl/devvm\.pem",.*"NO_PROXY":"localhost,127\.0\.0\.1,::1",.*\} --set \.propagateProxyEnv=false --set \.hostResolver\.enabled=false --set \.dns=\["127\.0\.0\.1"\]$'
[linux] stdout '^limactl edit --tty=false devvm-net-[0-9a-f]{14} --set \.env = \{"HTTPS_PROXY":"http://devvm:devvm-proxy-password@192\.168\.5\.4:3128",.*\} --set \.propagateProxyEnv=false --set \.hostResolver\.enabled=false --set \.dns=\["127\.0\.0\.1"\]$'
stdout '^limactl start --tty=false devvm-net-[0-9a-f]{14}\n(limactl list --json\n)*limactl shell --workdir / devvm-net-[0-9a-f]{14} sh -c .*/etc/ssh/ssh_host_.*\n(limactl list --json\n)*ssh -F none -M .*\nlimactl shell --workdir / devvm-net-[0-9a-f]{14} sh -c cd ''/'' && exec ''curl'' ''--noproxy'' ''\*'' ''-sS'' ''-m'' ''3'' ''-o'' ''/dev/null'' ''https://1\.1\.1\.1''\n'
stdout '^limactl shell --workdir /\S*/net devvm-net-[0-9a-f]{14}\n\z'
# The password is in no command's arguments: devvm wrote it into lima.yaml,
# the user's alone.
exec sh -c 'key=$(sed -n "s/^ *\"proxy_key\": \"\(.*\)\".*/\1/p" "$NET_dir/vm.json"); test -n "$key" && ! grep -qF "$key" "$STUB_LOG" && grep -qF "\"HTTPS_PROXY\":\"http://devvm:$key@" "$LIMA_HOME"/devvm-net-*/lima.yaml && ! grep -q devvm-proxy-password "$LIMA_HOME"/devvm-net-*/lima.yaml && ls -l "$LIMA_HOME"/devvm-net-*/lima.yaml'
stdout '^-rw-------\S* '
# macOS: the hostagent's profile lets it reach the proxy's port.
[darwin] exec sh -c 'grep -F "localhost:$(sed -n "s/^ *\"proxy\": \([0-9]*\).*/\1/p" "$NET_dir/vm.json")" "$NET_dir/run/sandbox.sb"'
# The proxy said it listens on that port, for that VM; its password is in
# its config, the user's alone, never in its arguments.
exec sh -c 'grep -F "listening on 127.0.0.1:$(sed -n "s/^ *\"proxy\": \([0-9]*\).*/\1/p" "$NET_dir/vm.json") for devvm-net-" "$NET_dir/logs/proxy.log"'
exec cat $NET_dir/run/proxy.json
stdout '^ "password": "[0-9a-f]{64}",$'
stdout '^ "version": 1,$'
exec ls -l $NET_dir/run/proxy.json
stdout '^-rw-------\S* '
exec sh -c 'ps -axww -o command= | grep "[_]proxy -config $NET_dir/run/proxy.json"'
stdout '_proxy -config \S*/run/proxy\.json$'
! stdout '[0-9a-f]{64}'
exec cat $NET_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\]\}$'
# The run log never holds the key.
exec sh -c 'cat "$DATA"/logs/runs/*.log'
stdout 'devvm:devvm-proxy-password@'
! stdout 'devvm:[0-9a-f]{64}@'
# A VM devvm started confined that still reaches the internet directly:
# stopped, no setup, no shell.
project leak
stub_reset
exec sh -c ': > "$STUB_DIR/net-direct"'
! exec devvm --network-policy --no-git-protect
stderr '^devvm: net_direct_route vm=devvm-leak-[0-9a-f]{14}$'
stub_calls
stdout '^limactl stop devvm-leak-[0-9a-f]{14}$'
! stdout 'tar -xf'
! stdout 'limactl shell --workdir /\S*/leak'
vm_name LEAK leak
vm_state $LEAK_dir/vm.json
stdout '^net\.made\.check=unenforced$'
rm $STUB_DIR/net-direct
# A running VM whose proxy is gone and can't start again (another program
# took its port) is stopped, never left running without it.
cd $WORK/net
exec sh -c 'pp=$(sed -n "s/^ *\"proxy\": \([0-9]*\).*/\1/p" "$NET_dir/vm.json"); pkill -f -- "_proxy -config $NET_dir/run/proxy.json"; sleep 0.5; printf "{\"version\": 1, \"listen\": \"127.0.0.1:%s\", \"password\": \"k\", \"lifeline\": \"%s\"}\n" "$pp" "$WORK/squat.none" > "$WORK/squat.json"; chmod 600 "$WORK/squat.json"; devvm _proxy -config "$WORK/squat.json" >/dev/null 2>&1 & echo $! > "$WORK/squat.pid"; sleep 1'
stub_reset
! exec devvm
stderr '^devvm: net_proxy_down_stopped vm=devvm-net-[0-9a-f]{14}$'
stderr '^devvm: proxy_port_taken port=[0-9]+ vm=devvm-net-[0-9a-f]{14}$'
stub_calls
stdout '^limactl stop devvm-net-[0-9a-f]{14}$'
! stdout 'limactl shell --workdir /\S*/net'
exec sh -c 'kill "$(cat "$WORK/squat.pid")"'
# Secrets: claude comes with Claude Code. The proxy gets its binding, with
# the VM's own placeholder and never the token, and its host on 443.
project agent
env CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-testfake
exec devvm --network-policy --no-git-protect --agents claude
vm_name AGENT agent
exec cat $AGENT_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\{"name":"claude","kind":"secret","hosts":\["api\.anthropic\.com"\],"header":"Authorization","placeholder":"sk-ant-oat01-devvm-proxy-managed-[0-9a-f]{32}"\}\]\}$'
! stdout testfake
exec sh -c 'salt=$(sed -n "s/^ *\"proxy_salt\": \"\(.*\)\".*/\1/p" "$AGENT_dir/vm.json"); test -n "$salt" && grep -F "$salt" "$AGENT_dir/run/secrets.json"'
exec cat $AGENT_dir/run/secrets.policy
stdout '^api\.anthropic\.com:443$'
# Its proxy's policies: base's and claude's built in, read from the
# binary, then the files: the user's, the VM's own and its secrets' hosts.
exec cat $AGENT_dir/run/proxy.json
stdout '"builtin_policies": \[\n "layers/base\.policy",\n "shared-tools/claude\.policy"\n \],\n "policies": \[\n "\S*/config/devvm/policy",\n "\S*/data/devvm/vms/[0-9a-f]{14}/policy",\n "\S*/data/devvm/vms/[0-9a-f]{14}/run/secrets\.policy"\n \],'
! exists $XDG_DATA_HOME/devvm/assets
env CLAUDE_CODE_OAUTH_TOKEN=
# A Codex login on the host: its bindings and hosts, with the VM's
# placeholders.
mkdir $HOME/.codex
cp $WORK/codex.json $HOME/.codex/auth.json
project codex
exec devvm --network-policy --no-git-protect --agents codex
vm_name CODEX codex
exec cat $CODEX_dir/run/secrets.json
stdout '"name":"codex-key","kind":"login","agent":"codex","hosts":\["api\.openai\.com"\]'
! stdout sk-testfake
exec cat $CODEX_dir/run/secrets.policy
stdout '^api\.openai\.com:443$'
# devvm network: on, off or unenforced, as devvm ls shows it. on and off
# change the VM's state, applied when it next starts.
project none
! exec devvm network
stderr '^devvm: no_vm dir=\S*/none$'
! exec devvm network sideways
stderr '^devvm: arg_not_choice choices=on, off command=devvm network given=sideways$'
cd $WORK/off
exec devvm network
stdout '^off$'
exec devvm network on
stdout '^==> net_set_running state=on vm=devvm-off-[0-9a-f]{14}$'
stderr '^net_on_old_tokens vm=devvm-off-[0-9a-f]{14}$'
vm_state $OFF_dir/vm.json
stdout '^net\.on=true$'
exec devvm network on
stdout '^==> net_already state=on vm=devvm-off-[0-9a-f]{14}$'
! stderr .
# Running, but not started confined: unenforced until devvm restart.
exec devvm network
stdout '^unenforced$'
exec devvm network off
stdout '^==> net_set_running state=off vm=devvm-off-[0-9a-f]{14}$'
! stderr .
# Stopped, it changes at its next start.
exec limactl stop $OFF
exec devvm network on
stdout '^==> net_set_stopped state=on vm=devvm-off-[0-9a-f]{14}$'
exec devvm network
stdout '^on$'
# Without a flag, and without a terminal to ask in: on, unasked.
project default
exec devvm --no-git-protect
stdout '^==> creating_from image=base vm=devvm-default-[0-9a-f]{14}$'
! stderr 'net_about|net_ask|net_off_'
vm_name DEFAULT default
vm_state $DEFAULT_dir/vm.json
stdout '^net\.on=true$'
stdout '^net\.made\.check=ok$'
# devvm network -g: what new VMs get, ask until it's saved; ask is only
# for new VMs.
exec devvm network -g
stdout '\Aask # devvm: default\n\z'
! grep network $DATA/settings.json
exec devvm network -g --json
stdout '\A{\n "version": 1,\n "policy": "ask",\n "source": "built-in"\n}\n\z'
! exec devvm network ask
stderr '^devvm: net_ask_global$'
! exec devvm network -g sideways
stderr '^devvm: arg_not_choice choices=ask, on, off command=devvm network given=sideways$'
! exec devvm network -g on off
stderr '^devvm: args_too_many args=\[on\|off\] command=devvm network given=on off$'
# A saved off: new VMs get none, unasked and unwarned; --network-policy
# beats it. The VMs that exist keep theirs.
exec devvm network -g off
stdout '^==> network_default mode=off$'
grep '^ "network": "off",?$' $DATA/settings.json
exec devvm network -g
stdout '\Aoff # devvm: settings\.json\n\z'
exec devvm network -g --json
stdout '\A{\n "version": 1,\n "policy": "off",\n "source": "settings.json"\n}\n\z'
project offsaved
exec devvm --no-git-protect
! stderr 'net_about|net_ask|net_off_'
vm_name OFFSAVED offsaved
vm_state $OFFSAVED_dir/vm.json
stdout '^net\.on=false$'
project offflag
exec devvm --network-policy --no-git-protect
vm_name OFFFLAG offflag
vm_state $OFFFLAG_dir/vm.json
stdout '^net\.on=true$'
cd $WORK/default
exec limactl stop $DEFAULT
exec devvm
vm_state $DEFAULT_dir/vm.json
stdout '^net\.on=true$'
stdout '^net\.made\.check=ok$'
# A saved on: new VMs get it; --no-network-policy beats it.
exec devvm network -g on
stdout '^==> network_default mode=on$'
project onsaved
exec devvm --no-git-protect
! stderr 'net_about|net_ask|net_off_'
vm_name ONSAVED onsaved
vm_state $ONSAVED_dir/vm.json
stdout '^net\.on=true$'
project onflag
exec devvm --no-network-policy --no-git-protect
vm_name ONFLAG onflag
vm_state $ONFLAG_dir/vm.json
stdout '^net\.on=false$'
# ask again. config.toml's network wins over what devvm saved, devvm
# network -g shows it came from there, and devvm network -g refuses to
# change it, naming the file and the key.
exec devvm network -g ask
stdout '^==> network_default mode=ask$'
grep '^ "network": "ask",?$' $DATA/settings.json
cp $WORK/config-on.toml $CFG/config.toml
exec devvm network -g
stdout '\Aon # \S*/config/devvm/config\.toml\n\z'
exec devvm network -g --json
stdout '"source": "config.toml"'
! exec devvm network -g off
stderr '^devvm: set_in_config file=\S*/config/devvm/config\.toml key=network$'
grep '^ "network": "ask",?$' $DATA/settings.json
project configon
exec devvm --no-git-protect
! stderr 'net_about|net_ask|net_off_'
vm_name CONFIGON configon
vm_state $CONFIGON_dir/vm.json
stdout '^net\.on=true$'
# A config.toml edited by hand with a typo: every command that reads it is
# refused, naming the file and the line, with a flag or without.
cp $WORK/config-typo.toml $CFG/config.toml
! exec devvm network -g
stderr '^devvm: net_default_failed reason=\S*/config/devvm/config\.toml:2: unknown key "netwrk"$'
project badsaved
! exec devvm --no-git-protect
stderr '\S*/config/devvm/config\.toml:2: '
! exec devvm --network-policy --no-git-protect
stderr '\S*/config/devvm/config\.toml:2: '
cp $WORK/config-maybe.toml $CFG/config.toml
! exec devvm network -g
stderr '^devvm: net_default_failed reason=\S*/config/devvm/config\.toml:1: "maybe" isn''t one of ask, on, off$'
rm $CFG/config.toml
exec devvm --network-policy --no-git-protect
-- config-on.toml --
# new VMs get network policy, whatever devvm network -g says
network = "on"
-- config-typo.toml --
# a typo
netwrk = "off"
-- config-maybe.toml --
network = "maybe"
-- codex.json --
{"OPENAI_API_KEY":"sk-testfake-codex","last_refresh":null}