Files
devvm/testdata/script/secret_custom.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

247 lines
12 KiB
Plaintext

# Your own secrets: devvm secret set NAME with the hosts it goes to and
# the variable VMs find its placeholder in, the value on stdin or at a
# prompt, never in a process's arguments; the VMs you use it in alone get
# its binding, its hosts on 443 and its placeholder. Every value here is
# fake.
# Each flag's value, refused before anything runs, as a usage error, and
# nothing saved.
! exec devvm secret set stripe --host '*.stripe.com' --env STRIPE_API_KEY
stderr '^devvm: bad_secret_host given=\*\.stripe\.com$'
stderr '^usage_help command=devvm secret set$'
! exec devvm secret set stripe --host api.stripe.com, --env STRIPE_API_KEY
stderr '^devvm: flag_blank flag=host$'
! exec devvm secret set stripe --host api.stripe.com,10.0.0.1 --env STRIPE_API_KEY
stderr '^devvm: bad_secret_host given=10\.0\.0\.1$'
! exec devvm secret set stripe --host api.stripe.com:8443 --env STRIPE_API_KEY
stderr '^devvm: bad_secret_host given=api\.stripe\.com:8443$'
! exec devvm secret set stripe --host printer.local --env STRIPE_API_KEY
stderr '^devvm: bad_secret_host given=printer\.local$'
! exec devvm secret set stripe --host 10.0.0.1.nip.io --env STRIPE_API_KEY
stderr '^devvm: bad_secret_host given=10\.0\.0\.1\.nip\.io$'
! exec devvm secret set stripe --host api.stripe.com --env PATH
stderr '^devvm: bad_secret_env given=PATH$'
! exec devvm secret set stripe --host api.stripe.com --env GH_TOKEN
stderr '^devvm: bad_secret_env given=GH_TOKEN$'
! exec devvm secret set stripe --host api.stripe.com --env GIT_SSH_COMMAND
stderr '^devvm: bad_secret_env given=GIT_SSH_COMMAND$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --header Cookie
stderr '^devvm: bad_secret_header given=Cookie$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --header Proxy-Authorization
stderr '^devvm: bad_secret_header given=Proxy-Authorization$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --header User-Agent
stderr '^devvm: bad_secret_header given=User-Agent$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --prefix 'sk '
stderr '^devvm: bad_secret_prefix given=sk most=32$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --scheme Basic
stderr '^devvm: bad_secret_scheme given=Basic$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --path v1/
stderr '^devvm: bad_request_path given=v1/$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --path /v1/%2e%2e/
stderr '^devvm: bad_request_path given=/v1/%2e%2e/$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --method GET,CONNECT
stderr '^devvm: bad_methods given=GET,CONNECT methods=GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE, QUERY$'
! exec devvm secret set github --host evil.example.com
stderr '^devvm: secret_fixed secret=github$'
! exec devvm secret set github --basic-user
stderr '^devvm: secret_fixed secret=github$'
! exec devvm secret set codex-key --host api.openai.com --env OPENAI_KEY
stderr '^devvm: secret_name_taken given=codex-key$'
! exec devvm secret set Stripe --host api.stripe.com --env STRIPE_API_KEY
stderr '^devvm: bad_secret_name given=Stripe most=40$'
# A new secret without its hosts or its variable, a scheme outside
# Authorization, or a name close to a secret's: a failure, before it asks
# or reads a value.
stdin stripe.token
! exec devvm secret set stripe --stdin
stderr '^devvm: secret_needs_host secret=stripe$'
! stderr usage_help
! exec devvm secret set stripe --env STRIPE_API_KEY
stderr '^devvm: secret_needs_host secret=stripe$'
! exec devvm secret set stripe --host api.stripe.com
stderr '^devvm: secret_needs_env secret=stripe$'
! exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --header x-api-key --scheme ApiKey
stderr '^devvm: secret_scheme_header secret=stripe$'
! exec devvm secret set githb
stderr '^devvm: secret_new_like given=githb like=github$'
! exists $DATA/custom-secrets.json
stub_calls
! stdout security
# Saved on stdin: the value where devvm's other secrets are, under a key
# of its own (custom.NAME), its definition, with an ID of its own, in
# devvm's list, neither in a process's arguments.
stdin stripe.token
exec devvm secret set stripe --host api.stripe.com --host FILES.stripe.com,api.stripe.com --header x-api-key --env STRIPE_API_KEY --prefix sk_ --path /v1/ --stdin
[darwin] stdout '^==> secret_saved_keychain secret=stripe$'
[!darwin] stdout '^==> secret_saved file=\S*/data/devvm/secrets/custom\.stripe secret=stripe$'
[darwin] exec stub keychain custom.stripe
[darwin] stdout '\Ask_test_e2efake\n\z'
[darwin] ! exec stub keychain stripe
[!darwin] grep '^sk_test_e2efake$' $DATA/secrets/custom.stripe
[!darwin] ! exists $DATA/secrets/stripe
stub_calls
! stdout e2efake
exec cat $DATA/custom-secrets.json
stdout '^ "id": "[0-9a-f]{32}",\n "hosts": \[\n "api\.stripe\.com",\n "files\.stripe\.com"\n \],\n "header": "X-Api-Key",\n "env": "STRIPE_API_KEY",\n "prefix": "sk_",\n "path": "/v1/",\n "path_pattern": true\n'
! stdout e2efake
exec sh -c 'stat -f %Lp "$DATA/custom-secrets.json" 2>/dev/null || stat -c %a "$DATA/custom-secrets.json"'
stdout '^600$'
exec devvm secret
stdout '^stripe +yes +STRIPE_API_KEY +X-Api-Key +api\.stripe\.com, files\.stripe\.com +/v1/ +- +-$'
! stdout e2efake
exec devvm secret --json
stdout '\n "name": "stripe",\n "saved": true,\n "used_by": \[\],\n "custom": true,\n "hosts": \[\n "api\.stripe\.com",\n "files\.stripe\.com"\n \],\n "header": "X-Api-Key",\n "env": "STRIPE_API_KEY",\n "prefix": "sk_",\n "path": "/v1/"\n }\n'
! stdout e2efake
# Each variable is one secret's.
stdin stripe.token
! exec devvm secret set other --host api.example.com --env STRIPE_API_KEY --stdin
stderr '^devvm: secret_env_taken env=STRIPE_API_KEY secret=stripe$'
# Without a terminal its question is refused, naming --stdin, after
# saying where the secret goes; in one, it asks without showing it, and
# a new value keeps the definition.
stdin stripe2.token
! exec devvm --no-input secret set stripe
stderr '^token_prompt\ndevvm: no_input answer=--stdin$'
ttyin -stdin $WORK/stripe2.token
exec devvm secret set stripe
stderr '^custom_secret_howto hosts=api\.stripe\.com, files\.stripe\.com secret=stripe$'
! stdout e2efake2
! stderr e2efake2
[darwin] exec stub keychain custom.stripe
[darwin] stdout '\Ask_test_e2efake2\n\z'
[!darwin] grep '^sk_test_e2efake2$' $DATA/secrets/custom.stripe
exec devvm secret
stdout '^stripe +yes +STRIPE_API_KEY +X-Api-Key +api\.stripe\.com, files\.stripe\.com +/v1/ +- +-$'
# Used by the VMs you use it in alone: its binding, in its header with
# the VM's own placeholder, its path and its definition's ID, its hosts on
# 443, and from the next launch its variable; the other VM gets none of it.
project web
exec devvm --network allowlist --no-git-protect
vm_name WEB web
project other
exec devvm --network allowlist --no-git-protect
vm_name OTHER other
cd $WORK/web
exec devvm secret use stripe
stdout '^==> custom_secret_used hosts=api\.stripe\.com, files\.stripe\.com secret=stripe vm=devvm-web-[0-9a-f]{14}$'
! stderr .
vm_state $WEB_dir/vm.json
stdout '^secrets.names=stripe$'
stdout '^secrets.yours.0.hosts=api\.stripe\.com files\.stripe\.com\nsecrets.yours.0.id=[0-9a-f]{32}\nsecrets.yours.0.name=stripe$'
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\{"name":"stripe","kind":"secret","hosts":\["api\.stripe\.com","files\.stripe\.com"\],"path":"/v1/","header":"X-Api-Key","placeholder":"sk_devvm-proxy-managed-secret-stripe-[0-9a-f]{32}","id":"[0-9a-f]{32}"\}\]\}$'
! stdout e2efake
exec cat $WEB_dir/run/secrets.policy
stdout '^api\.stripe\.com:443\nfiles\.stripe\.com:443\n'
exec cat $OTHER_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\]\}$'
exec devvm stop
exec devvm
exec sh -c 'salt=$(sed -n "s/.*\"proxy_salt\": \"\([^\"]*\)\".*/\1/p" "$WEB_dir/vm.json"); tar -xOf "$STUB_DIR/sync.tar" env | grep -Fx "export STRIPE_API_KEY=''sk_devvm-proxy-managed-secret-stripe-$salt''"'
exec tar -xOf $STUB_DIR/sync.tar env
! stdout e2efake
exec devvm secret
stdout '^stripe +yes .* devvm-web-[0-9a-f]{14}$'
# A host gained reaches the VM only once devvm secret use binds it again,
# and a host lost goes from it at once.
stdin $WORK/stripe.token
exec devvm secret set stripe --host api.stripe.com,new.stripe.com --stdin
stdout '^==> secret_hosts_changed count=1 hosts=api\.stripe\.com, new\.stripe\.com secret=stripe vms=devvm-web-[0-9a-f]{14}$'
stdout '^==> secret_hosts_grown count=1 secret=stripe vms=devvm-web-[0-9a-f]{14}$'
exec cat $WEB_dir/run/secrets.policy
stdout '^api\.stripe\.com:443\n'
! stdout 'new\.stripe\.com|files\.stripe\.com'
exec devvm secret use stripe
stdout '^==> custom_secret_used hosts=api\.stripe\.com, new\.stripe\.com secret=stripe vm=devvm-web-[0-9a-f]{14}$'
exec cat $WEB_dir/run/secrets.policy
stdout '^api\.stripe\.com:443\nnew\.stripe\.com:443\n'
# A scheme of its own and Basic's username: in its binding.
stdin $WORK/stripe.token
exec devvm secret set stripe --header Authorization --scheme ApiKey --basic-user --path none --stdin
exec cat $WEB_dir/run/secrets.json
stdout '"header":"Authorization","placeholder":"sk_devvm-proxy-managed-secret-stripe-[0-9a-f]{32}","scheme":"ApiKey","basic_user":true,"id":"[0-9a-f]{32}"'
! stdout '"path"'
exec devvm secret --json
stdout '"scheme": "ApiKey",\n "basic_user": true\n'
# Its methods: in its definition and its binding in their canonical form,
# kept when another flag changes, and none for every method again.
stdin $WORK/stripe.token
exec devvm secret set stripe --method head,get --stdin
grep '"methods": "GET,HEAD"' $DATA/custom-secrets.json
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":2,"bindings":.*"methods":"GET,HEAD"'
exec devvm secret
stdout '^stripe +yes +STRIPE_API_KEY +Authorization +api\.stripe\.com, new\.stripe\.com +- +GET,HEAD +devvm-web-[0-9a-f]{14}$'
exec devvm secret --json
stdout '"methods": "GET,HEAD",\n "basic_user": true\n'
stdin $WORK/stripe.token
exec devvm secret set stripe --prefix sk_ --stdin
grep '"methods": "GET,HEAD"' $DATA/custom-secrets.json
stdin $WORK/stripe.token
exec devvm secret set stripe --method none --stdin
! grep '"methods"' $DATA/custom-secrets.json
# Without methods or a pattern, the bindings file is of version 1, which a
# proxy from before them reads alike.
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":1,'
! stdout '"methods"'
exec devvm secret unuse stripe
stdout '^==> secret_unused secret=stripe vm=devvm-web-[0-9a-f]{14}$'
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\]\}$'
exec devvm secret use stripe
# Deleted, after a question that names its VMs: its value, its definition
# and its VMs' use of it go, and their proxies stop putting it in.
! exec devvm secret rm stripe
stderr '^secret_rm_confirm name=stripe\ndevvm: no_input answer=--force$'
ttyin -stdin $WORK/no.txt
exec devvm secret rm stripe
stderr '^secret_rm_users count=1 secret=stripe vms=devvm-web-[0-9a-f]{14}$'
stdout 'nothing_deleted'
exec cat $DATA/custom-secrets.json
stdout '"name": "stripe"'
exec devvm secret rm --force stripe
stdout '^==> secret_unused secret=stripe vm=devvm-web-[0-9a-f]{14}$'
stdout '^==> secret_removed secret=stripe$'
[darwin] ! exec stub keychain custom.stripe
[!darwin] ! exists $DATA/secrets/custom.stripe
exec cat $DATA/custom-secrets.json
! stdout stripe
vm_state $WEB_dir/vm.json
! stdout '^secrets.names=.'
! stdout '^secrets.yours'
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\]\}$'
! exec devvm secret use stripe
stderr '^devvm: no_secret secret=stripe$'
! exec devvm secret rm --force stripe
stderr '^devvm: no_secret secret=stripe$'
# A VM still bound to an earlier secret of the name (as if its state was
# written before the secret went) gets nothing of one saved again, and
# its launch says so; devvm secret unuse takes the name away.
stdin $WORK/stripe.token
exec devvm secret set stripe --host api.stripe.com --env STRIPE_API_KEY --stdin
json_set $WEB_dir/vm.json secrets.names:=["stripe"] secrets.yours:=[{"name":"stripe","id":"00000000000000000000000000000000","hosts":["api.stripe.com"]}]
exec devvm stop
exec devvm
stderr '^devvm: secret_stale secret=stripe vm=devvm-web-[0-9a-f]{14}$'
exec cat $WEB_dir/run/secrets.json
stdout '^\{"version":1,"bindings":\[\]\}$'
exec devvm secret unuse stripe
stdout '^==> secret_unused secret=stripe vm=devvm-web-[0-9a-f]{14}$'
exec devvm secret rm --force stripe
-- stripe.token --
sk_test_e2efake
-- stripe2.token --
sk_test_e2efake2
-- no.txt --
n