Files
devvm/testdata/script/network_narrow.txtar
w3cjandClaude Opus 5.5 01e5d2488a feat(network): rules by HTTP method and path, and secrets by method
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]>
2026-11-08 21:38:23 -07:00

136 lines
6.9 KiB
Plaintext

# Rules by method and path: devvm network allow, deny and rm take --method
# and --path for a host name's rule, write it in the proxy's canonical form
# and say that the proxy decrypts the host to check each request.
env GLOBAL=$XDG_CONFIG_HOME/devvm/policy
project net
exec devvm --network allowlist --no-git-protect
vm_name NET net
# An allow narrowed to methods and a path, a deny narrowed to methods, and
# one to a path; the host's note once for each.
exec devvm network allow api.example.com --method head,get --path /repos/
stdout '^==> rule_added rule=api\.example\.com GET,HEAD /repos/ vm=devvm-net-[0-9a-f]{14}$'
stderr '^rule_narrows host=api\.example\.com$'
exec devvm network deny api.github.com --method POST,PUT,PATCH,DELETE
stdout '^==> rule_added rule=!api\.github\.com POST,PUT,PATCH,DELETE vm=devvm-net-[0-9a-f]{14}$'
exec devvm network deny github.com '*.githubusercontent.com' --path '/*/*/git-receive-pack'
stdout '^==> rule_added rule=!github\.com /\*/\*/git-receive-pack vm='
stdout '^==> rule_added rule=!\*\.githubusercontent\.com /\*/\*/git-receive-pack vm='
stderr -count=1 '^rule_narrows host=github\.com$'
stderr -count=1 '^rule_narrows host=\*\.githubusercontent\.com$'
cmp $NET_dir/policy $WORK/narrowed.policy
# The same rule, however its methods are written, is there already; another
# path's case isn't the same rule.
exec devvm network allow Api.Example.com --method GET,HEAD --path /repos/
stdout '^==> rule_present file=\S*/vms/[0-9a-f]{14}/policy rule=api\.example\.com GET,HEAD /repos/$'
! stdout rule_added
# devvm network shows the rules as the file has them, and none of them is
# a line that isn't a rule.
exec devvm network
stdout '^ api\.example\.com GET,HEAD /repos/$'
stdout '^ !api\.github\.com POST,PUT,PATCH,DELETE$'
! stdout 'not a rule'
exec devvm network --json
stdout '"api\.example\.com GET,HEAD /repos/"'
# rm takes a narrowed rule out by its flags, a deny with its !, and none
# of --method or --path is every method or every path.
exec devvm network rm api.example.com --method HEAD,GET --path /repos/
stdout '^==> rule_removed rule=api\.example\.com GET,HEAD /repos/ vm='
exec devvm network rm '!api.github.com' --method POST,PUT,PATCH,DELETE
stdout '^==> rule_removed rule=!api\.github\.com POST,PUT,PATCH,DELETE vm='
exec devvm network allow ok.example.com --method none --path none
stdout '^==> rule_added rule=ok\.example\.com vm='
! stderr rule_narrows
exec devvm network rm ok.example.com
cmp $NET_dir/policy $WORK/left.policy
# -g narrows every VM's rules.
exec devvm network deny -g pastebin.example --method POST
stdout '^==> rule_added_global rule=!pastebin\.example POST$'
cmp $GLOBAL $WORK/global.policy
# An allow by method and path for a host that a rule already allows whole,
# as devvm's own rules allow api.github.com and -g's *.corp.test its
# subdomains, narrows nothing: a warning names the deny that would, and
# there's no note that the host is decrypted. The warning names the VMs.
exec devvm network allow api.github.com --method GET,HEAD
stderr '^devvm: rule_narrows_nothing host=api\.github\.com rule=api\.github\.com GET,HEAD vms=devvm-net-[0-9a-f]{14}$'
! stderr '^rule_narrows host='
exec devvm network rm api.github.com --method GET,HEAD
exec devvm network allow -g '*.corp.test'
exec devvm network allow -g dev.corp.test --method GET
stderr '^devvm: rule_narrows_nothing host=dev\.corp\.test rule=dev\.corp\.test GET vms=devvm-net-[0-9a-f]{14}$'
exec devvm network allow -g '*.dev.corp.test' --path /api/
stderr '^devvm: rule_narrows_nothing host=\*\.dev\.corp\.test rule=\*\.dev\.corp\.test /api/ vms='
exec devvm network allow -g '*.corp.test' --method GET
stderr '^devvm: rule_narrows_nothing host=\*\.corp\.test '
exec devvm network rm -g '*.corp.test'
exec devvm network rm -g dev.corp.test --method GET
exec devvm network rm -g '*.dev.corp.test' --path /api/
exec devvm network rm -g '*.corp.test' --method GET
cmp $GLOBAL $WORK/global.policy
# With -g, each VM's own rules count too: a whole allow in this VM's file
# makes an allow for every VM narrow nothing on it. One of port 443 alone
# leaves port 80's plain HTTP narrowed.
exec devvm network allow whole.example.com https.example.com:443
exec devvm network allow -g whole.example.com --method GET
stderr '^devvm: rule_narrows_nothing host=whole\.example\.com rule=whole\.example\.com GET vms=devvm-net-[0-9a-f]{14}$'
exec devvm network allow https.example.com --method GET
stderr '^devvm: rule_narrows_http_only host=https\.example\.com rule=https\.example\.com GET vms=devvm-net-[0-9a-f]{14}$'
! stderr '^rule_narrows host='
exec devvm network rm -g whole.example.com --method GET
exec devvm network rm whole.example.com https.example.com:443
exec devvm network rm https.example.com --method GET
cmp $GLOBAL $WORK/global.policy
# Refusals, before anything runs: methods devvm doesn't know, CONNECT, a
# path that isn't a pattern, and methods or a path on an address, a CIDR
# or **, each a usage error that changes nothing.
! exec devvm network allow api.example.com --method GTE
stderr '^devvm: bad_methods given=GTE methods=GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE, QUERY$'
stderr '^usage_help command=devvm network allow$'
! exec devvm network allow api.example.com --method CONNECT
stderr '^devvm: bad_methods given=CONNECT '
! exec devvm network allow api.example.com --method GET,
stderr '^devvm: bad_methods given=GET, '
! exec devvm network allow api.example.com --path repos
stderr '^devvm: bad_request_path given=repos$'
! exec devvm network deny api.example.com --path '/a/../b'
stderr '^devvm: bad_request_path given=/a/\.\./b$'
! exec devvm network deny api.example.com --path '/a%2Fb'
stderr '^devvm: bad_request_path given=/a%2Fb$'
! exec devvm network allow 10.0.0.5 --method GET
stderr '^devvm: rule_not_narrowable rule=10\.0\.0\.5$'
! exec devvm network deny 10.0.0.0/8 --path /x
stderr '^devvm: rule_not_narrowable rule=!10\.0\.0\.0/8$'
! exec devvm network allow '**' --method GET
stderr '^devvm: rule_not_narrowable rule=\*\*$'
! exec devvm network allow 'api.example.com GET'
stderr '^devvm: not_a_rule rule=api\.example\.com GET$'
! exec devvm network rm 10.0.0.5 --method GET
stderr '^devvm: rule_not_narrowable rule=10\.0\.0\.5$'
cmp $NET_dir/policy $WORK/left.policy
cmp $GLOBAL $WORK/global.policy
# A line written by hand with a method devvm doesn't know isn't a rule, so
# as a deny it's a broken one, which denies everything, and devvm says so.
exec sh -c 'echo "!api.github.com PSOT" >> "$NET_dir/policy"'
exec devvm network
stdout '^ !api\.github\.com PSOT # not a rule, skipped \(line 3\)$'
exec devvm network allow fine.example.com
stderr '^devvm: policy_broken_deny file=\S*/vms/[0-9a-f]{14}/policy line=3 rule=!api\.github\.com PSOT$'
-- narrowed.policy --
api.example.com GET,HEAD /repos/
!api.github.com POST,PUT,PATCH,DELETE
!github.com /*/*/git-receive-pack
!*.githubusercontent.com /*/*/git-receive-pack
-- left.policy --
!github.com /*/*/git-receive-pack
!*.githubusercontent.com /*/*/git-receive-pack
-- global.policy --
!pastebin.example POST