mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
A policy rule for a host name can narrow it to some of its requests: `api.github.com GET,HEAD /repos/` in a policy file, written by devvm network allow, deny and rm from --method and --path. Allows add up and a deny beats any allow, as for every rule, so narrowing a host that a rule allows whole, such as devvm's built-in GitHub, takes a deny: `devvm network deny api.github.com --method POST,PUT,PATCH,DELETE`, or `devvm network deny github.com --path '/*/*/git-receive-pack'`. An allow by method and path for a host already allowed whole warns that it narrows nothing and names the deny. The proxy decrypts such a host's HTTPS on :443 with the VM's CA, refuses its tunnels on other ports and without a CA, and judges every request on the connection, kept-alive HTTP/1.1 and HTTP/2 alike, every absolute-form one, and the shared Nix cache's own fetches. A connection's verdict and whether its requests are judged come from one read of the policy files. A blocked request gets 403 and a blocked event with its method and path; devvm network log shows it, and an allowed one's forward event names the rule that let it through. One matcher, internal/proxy/request.go, judges rules and secrets' bindings: methods from a closed list (RFC 9110's, PATCH, QUERY; never CONNECT); the methods a request names besides its own (every override-like header in any spelling, `_` for `-`, and the query's _method), each word judged, and while a rule narrows by method, a value that isn't exactly one method refused; path patterns with *, ** and a last / for everything below; and only a path in plain form. Allows and bindings match strictly, denies broadly: any case, every path that isn't plain, the prefix above a trailing /, and a last segment cut at its first dot (/admin.json, /admin.). A method devvm doesn't know isn't a rule, so a typo in a deny is a broken deny. Upgrades other than WebSocket and CONNECT inside a decrypted tunnel are refused for narrowed hosts. devvm secret set takes an optional --method (none for every method), and a secret's --path takes the same patterns, marked path_pattern in custom-secrets.json; a saved path with a * from before is refused rather than read as a wildcard. The bindings file stays version 1 while every binding reads alike to an older proxy, so devvm secret set and rm after an upgrade don't take a running older proxy's bindings away; a binding with methods or a pattern makes it version 2, which such a proxy refuses instead of putting a value in for every method, and writing one warns for each such VM. devvm network allow, deny and rm warn, naming the VM, when a rule by method and path changes while the VM's proxy runs from another devvm binary, or one devvm can't tell. A query with a ; or one Go can't read whole is refused while a rule narrows the host by method, and denies also match each segment with its trailing dots dropped. An allow that narrows nothing names the VMs, with -g by each VM's own allowlist, and says when it narrows only port 80's plain HTTP. - proxy: match, Methods, ParseMethods, PathOK, plainPath, matchPath, overrides, overrideLike, server.judgeRequest; parseRule/parseHostRule, CanonicalRule, NarrowRule, Narrowable, hostRules (rulesFor, verdict, byRequest, judge, unjudgeable), egress.checkHost, Allowlist.CoversWhole, trimDots; Binding.Methods, BindingListVersion 2 when needed (v1Binding); pathWithin and PathPrefixOK folded into match; the Nix cache judged - netpolicy: Rule(rule, deny, methods, path), NarrowedHost, sameRule, Proxy.Allowlist, Proxy.NarrowNotes, Proxy.OtherBinaries, BasePolicy, Proxy.SecretsWritten (after RefreshSecrets and devvm agent login --api-key's write); FormatEvent shows a blocked request's method and path and a forward's rule - secrets: Def/Secret/customJSON Methods, path_pattern, CheckMethods, CheckPath on proxy.PathOK - vm: RuleChange, PolicyChange(ctx, rules, RuleChange) - cli: --method and --path on network allow, deny and rm, --method on secret set, METHODS in devvm secret and "methods" in its --json - messages: bad_request_path (was bad_secret_path), bad_methods, rule_not_narrowable, rule_narrows, rule_narrows_nothing, rule_narrows_http_only, rule_narrows_unknown, proxy_other_binary, secrets_other_binary - tests: TestParseRuleNarrowed, TestMatchPath, TestDenyBypasses, TestAllowNarrowed, TestCoversWhole, TestForRequestMethods, TestInterceptRules and TestInterceptSecretMethods (HTTP/1.1 kept alive and HTTP/2), TestNarrowedNotDecrypted, TestNixCacheRulesByPath, FuzzDenyBypass (paths, methods, headers, _method), TestRule, TestNarrowedRules, TestOtherBinaries, TestRefreshSecretsOtherBinary, TestNarrowNotesUnreadable, TestDefine's path of /, TestReadBindings' versions, TestCustomsUnreadable's prefix path, network_narrow.txtar (narrows nothing), secret_custom.txtar's --method; on 0.1.0's frozen state: TestV010Bindings, TestV010ProxyConfig, TestV010Rules (0.1.0's TestParsePolicy and TestDecide, frozen), fixture_v010_network.txtar; e2e network: netRequestRules and the basic secret's --method GET,HEAD - docs: safety/network.md's Rules by method and path, the secrets' flags and an example, internals/network.md's Requests by method and path, the design note replacing the open-work entry, attack vectors, the commands page, internal/proxy/README.md, the CLI reference Co-Authored-By: Claude Opus 5.5 <[email protected]>
116 lines
6.1 KiB
Plaintext
116 lines
6.1 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","rule":"api.github.com GET /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, rule api.github.com GET /user, token: github
|
|
2026-10-09T10:00:07Z BLOCKED api.github.com:443 POST /user/keys: 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
|