Files
devvm/testdata/script/network.txtar
T
w3cjandClaude Opus 5.5 9cf45cc5e0 feat(net): network policy on by default; a warning where a VM can't have it
A new VM gets network policy unless it's turned off. In a terminal devvm
shows the tradeoff and asks yes by default ([Y/n]); without a terminal
it's on. A VM that can't have network policy gets no question, only a
warning, and is made without it: on a backend without egress-proxy (or
with it unreviewed), and a Docker VM made for the docker layer, whether
named or from a devcontainer.json's docker-in-docker. An explicit
--network-policy on such a VM is still refused (net_docker_layer, or
backend_lacks from Require). Existing VMs keep their state's choice.

- netpolicy.Proxy.Choose: no terminal gives on; in a terminal net_about,
  then Confirm (yes by default). Proxy's Ask field and netpolicy.UI's Ask
  removed (unused); vm.UI asks Ask itself.
- vm.creation.chooseNet: --no-network-policy first; the docker layer
  warns (net_off_docker_layer) or refuses --network-policy; a backend
  Registry.Allowed doesn't let egress-proxy through warns
  (net_off_backend); otherwise Choose.
- Messages: new net_off_docker_layer, net_off_backend (with doc links);
  net_about rewritten as the two-line tradeoff with a doc link
  (safety/network#turning-it-on-or-off); net_ask is "Turn it on?" (no
  fields); devcontainer_docker_net removed (no caller).
- --network-policy's help: secrets, default on, asked in a terminal; CLI
  reference regenerated.
- Tests: TestChoose (flags, terminal yes/no, no terminal on, what's said);
  TestChooseNet replaces TestChooseNetDockerLayer (docker layer named and
  from the file, no egress-proxy, unreviewed egress-proxy, flags, with and
  without a terminal); TestUpWithoutEgressProxy (backend_lacks for
  --network-policy, the warning and net=off by default).
  network.txtar: --no-network-policy for the off VM, and a VM made without
  a flag or terminal gets net=on. docker.txtar: another machine's engine
  (backend_lacks, then the warning and net=off), and the sysbox VM's
  warning. e2e's docker sysbox check expects the warning.
- Docs: first-vm, commands, safety/network (the prompt, the VMs that
  can't have it), security, attack-vectors, devcontainer, backends index
  and docker, design notes, README.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 08:45:55 -07:00

165 lines
7.7 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.
env DEVVM_MESSAGES=names
env DATA=$XDG_DATA_HOME/devvm
env VMS=$XDG_DATA_HOME/devvm/vms
env RUN=$XDG_DATA_HOME/devvm/run
# --no-network-policy: off, with no proxy and no CA.
mkdir $WORK/off
cd $WORK/off
exec devvm --no-network-policy --no-git-protect base
stdout '^==> creating_from image=base vm=devvm-off-[0-9a-f]{14}$'
exec sh -c 'cat "$VMS"/devvm-off-*'
stdout '^net=off$'
! 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.
mkdir $WORK/net
cd $WORK/net
exec sh -c ': > "$STUB_LOG"'
exec devvm --network-policy --no-git-protect base
stdout '^==> starting vm=devvm-net-[0-9a-f]{14}$'
exists $DATA/ca/ca.pem $DATA/ca/ca.key
exec sh -c 'cat "$VMS"/devvm-net-*'
stdout '^net=on$'
stdout '^net_check=ok$'
stdout '^proxy_key=[0-9a-f]{32}$'
stdout '^proxy_salt=[0-9a-f]{32}$'
stdout '^ssh_port=[0-9]+$'
stdout '^proxy_port=[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":"/usr/local/share/ca-certificates/devvm-proxy\.crt",.*"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}\nlimactl 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=//p" "$VMS"/devvm-net-*); ! 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_port=//p" "$VMS"/devvm-net-*)" "$RUN"/devvm-net-*.sb'
# The proxy said it listens on that port, for that VM; its password is in
# a file, never in its arguments.
exec sh -c 'grep -F "listening on 127.0.0.1:$(sed -n "s/^proxy_port=//p" "$VMS"/devvm-net-*) for devvm-net-" "$DATA"/logs/devvm-net-*.proxy.log'
exec sh -c 'cat "$RUN"/devvm-net-*.proxy.auth'
stdout '^[0-9a-f]{32}$'
exec sh -c 'cat "$RUN"/devvm-net-*.secrets.json'
stdout '^\[\]$'
# 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]{32}@'
# A VM devvm started confined that still reaches the internet directly:
# stopped, no setup, no shell.
mkdir $WORK/leak
cd $WORK/leak
exec sh -c ': > "$STUB_LOG" && : > "$STUB_DIR/net-direct"'
! exec devvm --network-policy --no-git-protect base
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'
exec sh -c 'cat "$VMS"/devvm-leak-*'
stdout '^net_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_port=//p" "$VMS"/devvm-net-*); pkill -f -- "_proxy -listen 127.0.0.1:$pp "; sleep 0.5; echo k > "$WORK/squat.key"; devvm _proxy -listen "127.0.0.1:$pp" -auth-file "$WORK/squat.key" -lifeline "$WORK/squat.none" >/dev/null 2>&1 & echo $! > "$WORK/squat.pid"; sleep 1'
exec sh -c ': > "$STUB_LOG"'
! 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 the image. The proxy gets its binding, with
# the VM's own placeholder and never the token, and its host on 443.
mkdir $WORK/agent
cd $WORK/agent
env CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-testfake
exec devvm --network-policy --no-git-protect claude
exec sh -c 'cat "$RUN"/devvm-agent-*.secrets.json'
stdout '^\[\{"name":"claude","hosts":\["api\.anthropic\.com"\],"header":"Authorization","placeholder":"sk-ant-oat01-devvm-proxy-managed-[0-9a-f]{32}"\}\]$'
! stdout testfake
exec sh -c 'grep -F "$(sed -n "s/^proxy_salt=//p" "$VMS"/devvm-agent-*)" "$RUN"/devvm-agent-*.secrets.json'
exec sh -c 'cat "$RUN"/devvm-agent-*.secrets.policy'
stdout '^api\.anthropic\.com:443$'
# Its proxy's policy files: base's, claude's, the user's, the VM's own and
# its secrets' hosts.
exec sh -c 'cat "$RUN"/devvm-agent-*.proxy.policy'
stdout '/data/devvm/assets/[0-9a-f]{16}/layers/base\.policy,\S*/data/devvm/assets/[0-9a-f]{16}/shared-tools/claude\.policy,\S*/config/devvm/policy,\S*/data/devvm/vms/devvm-agent-[0-9a-f]{14}\.policy,\S*/data/devvm/run/devvm-agent-[0-9a-f]{14}\.secrets\.policy$'
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
mkdir $WORK/codex
cd $WORK/codex
exec devvm --network-policy --no-git-protect codex
exec sh -c 'cat "$RUN"/devvm-codex-*.secrets.json'
stdout '"name":"codex-key","hosts":\["api\.openai\.com"\]'
! stdout sk-testfake
exec sh -c 'cat "$RUN"/devvm-codex-*.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.
mkdir $WORK/none
cd $WORK/none
! exec devvm network
stderr '^devvm: no_vm dir=\S*/none$'
! exec devvm network sideways
stderr '^devvm: usage_error reason='
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}$'
exec sh -c 'cat "$VMS"/devvm-off-*'
stdout '^net=on$'
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 sh -c 'limactl stop "$(cd "$VMS" && ls devvm-off-* | grep -v "\.")"'
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.
mkdir $WORK/default
cd $WORK/default
exec devvm --no-git-protect base
stdout '^==> creating_from image=base vm=devvm-default-[0-9a-f]{14}$'
! stderr 'net_about|net_ask|net_off_'
exec sh -c 'cat "$VMS"/devvm-default-*'
stdout '^net=on$'
stdout '^net_check=ok$'
-- codex.json --
{"OPENAI_API_KEY":"sk-testfake-codex","last_refresh":null}