Files
devvm/testdata/script/network_log.txtar
T
w3cjandClaude Opus 5.5 ceda0bf925 feat(cli): --network TIER, devvm network TIER and the tier question
The command line names the four network tiers. --network TIER (open,
internet, allowlist or none) chooses a new VM's tier, checked before
anything runs (arg_not_choice). devvm network open|internet|allowlist|none
gives the folder's VM a tier through Net.SetTier, and devvm network -g
takes the tiers and ask. config.toml's and settings.json's network is ask
or a tier (settings.NetworkChoices, checked against netpolicy.TierNames
by a test, since settings can't import netpolicy).

The question on creation is Proxy.Choose(vm): net_about says what each
tier reaches, net_ask asks for one (Answer, AnsweredBy("--network")),
Enter or a refusal gives allowlist, and an answer that isn't a tier is
warned of (net_tier_unknown) and asked again. devvm setup's picker
offers ask and the four tiers (network_default_open, _internet,
_allowlist, _none). On an existing VM, --network is ignored with the
devvm network TIER command that changes it.

devvm network --json: policy is the tier or unenforced, and each policy
file whose denies alone apply (internet and none) has denies_only: true,
as the text output's "(denies only)", so a script can tell a file's
denies from all of its rules without knowing the tier rules.

Messages reworded: net_tier_backend (--network open), net_open_policy
(devvm network allowlist), net_about, net_ask, network_default*,
net_default_failed, network_failed, setting_already's and
widen_parent_vm*'s descriptions. safety/network's "Choosing a tier"
replaces its on/off section; docs, the cli skill, testscripts and e2e
use the new forms, and e2e's internet and none checks go through devvm
network.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 15:54:43 -07:00

116 lines
6.0 KiB
Plaintext

# network log: what this folder's VM's proxy allowed and blocked, the last N, or
# the blocks by host with the command that allows each.
# No VM: nothing to show.
project net
! exec devvm network log
stderr '^devvm: no_vm dir=\S*/net$'
# A VM with network policy that logged nothing yet.
exec devvm --network allowlist --no-git-protect
vm_name NET net
env LOG=$NET_dir/logs/network.jsonl
exec devvm network log
stdout '^==> log_empty vm=devvm-net-[0-9a-f]{14}$'
! stderr .
exec devvm network log --json
stdout '\A{\n "version": 1,\n "events": \[\]\n}\n\z'
! stderr .
# Allowed and blocked connections and requests, oldest first, the last 50
# or the last N.
exec sh -c 'mkdir -p "$NET_dir/logs" && cp "$WORK/events.jsonl" "$LOG"'
exec devvm network log
cmp stdout $WORK/log.txt
! stderr .
exec devvm network log -n 2
stdout '\A2026-10-09T10:01:00Z BLOCKED a.test:443 not on the allowlist\n2026-10-09T10:02:00Z BLOCKED db.test:5432 not on the allowlist\n\z'
exec devvm network log --lines 0
! stdout .
! exec devvm network log -n x
stderr '^devvm: flag_bad_value flag=--lines form=N value=x$'
! exec devvm network log -n -1
stderr '^devvm: flag_bad_value flag=--lines form=N value=-1$'
# --json: the events as the proxy logged them, without the VM's name;
# never followed.
exec devvm network log --json -n 3
stdout '\A{\n "version": 1,\n "events": \[\n {\n "time": "2026-10-09T10:00:08Z",\n "event": "tunnel",\n "host": "fd00::1",\n "port": 22,\n "method": "CONNECT",\n "rule": "\[fd00::1\]:22"\n },\n'
! stdout '"vm"'
! exec devvm network log --json --follow
stderr '^devvm: flags_exclusive flag=--(json|follow) other=--(json|follow)$'
# The blocks by host and port, by when last blocked, with the command
# that allows each: only for a host name or address, and never for a
# request refused whatever the allowlist says.
exec devvm network log --blocked
cmp stdout $WORK/blocked.txt
exec devvm network log --blocked --json
stdout '\A{\n "version": 1,\n "blocked": \[\n {\n "last": "2026-10-09T10:00:07Z",\n "count": 1,\n "host": "api.github.com",\n "port": 443\n },\n'
stdout '^ "last": "2026-10-09T10:01:00Z",\n "count": 2,\n "host": "a.test",\n "port": 443,\n "allow": "a.test"\n'
! exec devvm network log --blocked --follow
stderr '^devvm: flags_exclusive flag=--blocked other=--follow$'
# -f is --force's, never --follow's.
! exec devvm network log -f
stderr '^devvm: flag_unknown command=devvm network log flag=-f$'
# A log its proxy rotated: the old file's events come first, so the
# history and the blocks are the same; one just rotated isn't empty.
exec sh -c 'head -n 5 "$WORK/events.jsonl" > "$LOG.1" && tail -n +6 "$WORK/events.jsonl" > "$LOG"'
exec devvm network log
cmp stdout $WORK/log.txt
exec devvm network log --blocked
cmp stdout $WORK/blocked.txt
exec sh -c 'cp "$WORK/events.jsonl" "$LOG.1" && : > "$LOG"'
exec devvm network log -n 1
stdout '\A2026-10-09T10:02:00Z BLOCKED db.test:5432 not on the allowlist\n\z'
rm $LOG.1
# A host and reason from the VM with control characters: dropped.
cp $WORK/evil.jsonl $LOG
exec devvm network log --blocked
cmp stdout $WORK/evil-blocked.txt
exec devvm network log
cmp stdout $WORK/evil.txt
# An open VM logs only what goes through its proxy, and devvm says so.
project off
exec devvm --network open --no-git-protect
exec devvm network log
stdout '^==> log_empty vm=devvm-off-[0-9a-f]{14}$'
stderr '^net_open_log vm=devvm-off-[0-9a-f]{14}$'
-- events.jsonl --
{"time":"2026-10-09T10:00:00Z","vm":"x","event":"blocked","host":"a.test","port":443,"method":"CONNECT","reason":"not on the allowlist"}
{"time":"2026-10-09T10:00:05Z","vm":"x","event":"tunnel","host":"ok.test","port":443,"method":"CONNECT","rule":"ok.test"}
{"time":"2026-10-09T10:00:06Z","vm":"x","event":"intercept","host":"api.github.com","port":443,"method":"CONNECT","rule":"*.github.com"}
{"time":"2026-10-09T10:00:06Z","vm":"x","event":"forward","host":"api.github.com","port":443,"method":"GET","path":"/user","secrets":["github"]}
{"time":"2026-10-09T10:00:07Z","vm":"x","event":"blocked","host":"api.github.com","port":443,"method":"POST","path":"/user/keys","reason":"makes an account key","refused":true}
not an event
{"time":"2026-10-09T10:00:08Z","vm":"x","event":"tunnel","host":"fd00::1","port":22,"method":"CONNECT","rule":"[fd00::1]:22"}
{"time":"2026-10-09T10:01:00Z","vm":"x","event":"blocked","host":"a.test","port":443,"method":"CONNECT","reason":"not on the allowlist"}
{"time":"2026-10-09T10:02:00Z","vm":"x","event":"blocked","host":"db.test","port":5432,"method":"CONNECT","reason":"not on the allowlist"}
-- log.txt --
2026-10-09T10:00:00Z BLOCKED a.test:443 not on the allowlist
2026-10-09T10:00:05Z allowed ok.test:443 rule ok.test
2026-10-09T10:00:06Z allowed api.github.com:443 rule *.github.com, decrypted
2026-10-09T10:00:06Z allowed api.github.com:443 GET /user, token: github
2026-10-09T10:00:07Z BLOCKED api.github.com:443 makes an account key
2026-10-09T10:00:08Z allowed [fd00::1]:22 rule [fd00::1]:22
2026-10-09T10:01:00Z BLOCKED a.test:443 not on the allowlist
2026-10-09T10:02:00Z BLOCKED db.test:5432 not on the allowlist
-- blocked.txt --
LAST COUNT HOST ALLOW WITH
2026-10-09T10:00:07Z 1 api.github.com:443 -
2026-10-09T10:01:00Z 2 a.test:443 devvm network allow a.test
2026-10-09T10:02:00Z 1 db.test:5432 devvm network allow db.test:5432
-- evil.jsonl --
{"time":"2026-10-09T10:00:00Z","vm":"x","event":"blocked","host":"x;sh*","port":443,"method":"CONNECT","reason":"not on the allowlist"}
{"time":"2026-10-09T10:00:01Z","vm":"x","event":"blocked","host":"e\u001b]0;x\u0007vil.test","port":443,"method":"CONNECT","reason":"r\u009bm\nfake"}
-- evil-blocked.txt --
LAST COUNT HOST ALLOW WITH
2026-10-09T10:00:00Z 1 x;sh*:443 -
2026-10-09T10:00:01Z 1 e]0;xvil.test:443 -
-- evil.txt --
2026-10-09T10:00:00Z BLOCKED x;sh*:443 not on the allowlist
2026-10-09T10:00:01Z BLOCKED e]0;xvil.test:443 rmfake