internal/proxy keeps no package state. newServer makes a server from the config, as Main starts (it replaces Config.apply, loadPolicies and every global they set), and each part keeps what it changes under its own lock: events (the event log and recent blocks), egress (the policy, the host's addresses in hostAddrs, the company proxies in upstreams), secrets (the bindings file and the values cache, file lock taken before the values'), ca (devvm's CA and its leaves), links (the list and the sockets' folder) and nixCache (the cache's folder, checked narinfos, fetches and slots). The proxy's own log is a *log.Logger each part is given; Main's lifeline tick is a const, and run takes it for tests. What's left package-level is error sentinels and three fixed tables (notPublic, nat64, githubMinting), now in the structure test's allowedVars; the structure test no longer leaves internal/proxy out, and its rule case for the proxy fails a var. Tests make their own server with newServer (testServer, listen, logs, fakeSecret on a secrets, testCA) and all run in parallel. TestServersApart checks two servers in one process share no password, blocks, events, values or CA leaves. A tunnel to a host the policy allows passes the far end's bytes to the VM from the start (splice's before), so a server that speaks first gets its greeting through; the VM's bytes still pass only after the SNI check, which reads one byte first and the ClientHello only when it starts 0x16. TestTunnelServerSpeaksFirst failed on the code before. Docs: the proxy README (the server, files, testing), network.md's tunnel paragraph, AGENTS.md's test-seams line, Contributing's package-vars row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
internal/proxy
The network policy proxy: one process per VM with network policy, on the host, run as devvm _proxy. Go, standard library only, with internal/daemon, devvm's background-process helper, which is standard library only too (depguard enforces both).
The VM can reach nothing but this proxy. On macOS its Lima hostagent runs under a Seatbelt profile, and on Linux QEMU's network is restricted. Other programs on the host can reach the port too, so the port alone doesn't identify the VM. Every request must carry the VM's password in Proxy-Authorization, with the user devvm, and the VM's proxy URL has it. The VM's placeholders end in a random value of its own, which isn't the password, so no other VM's or program's placeholder gets a secret. The proxy runs with one config, Config, which holds the password, and it refuses to start without one (how devvm runs it). For each connection it:
- answers
407without the VM's password; - takes only a host name with no empty label, after it lowercases the name and drops one trailing dot, or an address, and answers
400to anything else; - with
linksin its config, sends a request for one of the VM's links (CONNECT db:5432,GET http://db:5432/) to that link's socket on the host before any rule (below); - checks
CONNECT host:portand absolute-form requests (GET http://host/…) against the VM's policy, which is devvm's built-in policies, read from the binary, and then the user's policy files, and tunnels or forwards allowed ones untouched; - resolves names itself, and refuses the host's own addresses and internal ones (loopback, private, link-local, multicast, unspecified, CGNAT
100.64.0.0/10) unless a rule names the address; through a company proxy (upstream), which resolves names itself, it refuses a name when the host's resolver answers with such an address, and a name the host can't resolve when only**allows it (unlessupstream_unresolved, which builds on Lima get), and it reads at most 64 KiB of the company proxy's answer toCONNECT; - requires a TLS ClientHello in a tunnel to name the CONNECT host (SNI), and passes nothing the VM sends to the far end before it has checked it, while the far end's bytes reach the VM at once, so a server that speaks first, such as SMTP's, sends its greeting before the VM sends anything;
- for hosts with a bound secret, swaps the placeholder in one header for the real value: it intercepts their
:443tunnels, and does the same for absolute-formhttps://requests (everything else passes unread); - with
nix_cache, answershttp://devvm.proxy/nix/itself, as a Nix binary cache that VMs share and only the host fills (below); - logs every decision as JSON lines.
For a VM whose image has the nix layer, devvm sets nix_cache, and the VM's Nix tries http://devvm.proxy/nix before https://cache.nixos.org. The proxy takes only GET and HEAD of nix-cache-info, <32 nix-base32>.narinfo and nar/<52 nix-base32>.nar (bare, .xz, .zst or .bz2), with no query, and answers 400 to anything else. It makes up nix-cache-info itself, at priority 30, and fetches the rest from https://cache.nixos.org alone, only when the VM's policy allows cache.nixos.org:443, through the floor or the company proxy, never following a redirect and sending nothing of the VM's request. Before keeping a narinfo, it checks its signature by the pinned cache.nixos.org-1 key over its fingerprint (1;StorePath;NarHash;NarSize;References), that its StorePath is the one asked for, and that its URL is named by its own NarHash or FileHash. It fetches a NAR only after a narinfo it checked names it, never for a HEAD, each one once however many ask and at most four at once, and keeps it only when its SHA-256 and size are that narinfo's FileHash and FileSize. Files are written whole and renamed into the folder, mode 600 in folders of 700. Anything that fails answers 404, and nothing of it is kept, so Nix goes to cache.nixos.org itself. Under a lock (flock on .lock), the proxy takes each file's room in whole blocks before it writes it (reserve), deleting the least recently used files (by modification time, which a hit updates) when the file would take the cache past nix_cache_max, down to nine tenths of it, and .size keeps the running total. A NAR being fetched is made its full size at once and counts against the cap, so a fetch that would pass it with them is refused. devvm gc and the retention settings trim it with TrimNixCache, under the same lock.
With links, the proxy is a second entrance to the VM's links between VMs, beside the ssh remote forward that carries every link. devvm _links writes every link the VM has, carried or not, to the list (LinkList, JSON, version 1) whenever they change, and leaves it when it ends, so a link with no socket is refused rather than asked of the rules, and the proxy reads it for every request: a regular file, not a symbolic link, that only its owner may write, at most 1 MiB. After the password and before any rule, it lowercases the host, drops one trailing dot, and looks for an entry that names that host (NAME or NAME.devvm.internal) and port exactly. It takes an entry only when its socket is LinkSocket of the sockets' folder (link_sockets, devvm's run folder; without it, no link is reached), the VM (vm), the entry's address and its port, which is how vmssh.Links names it, and its address is in 127.77.0.0/16, so the proxy never connects to another VM's link or to any other socket. Any other host under devvm.internal gets 403, logged as a refused block, before the rules. A CONNECT is tunneled to the socket as it is, with no SNI check, no interception and no secret, and an absolute-form http:// request goes there in origin form, without the proxy's headers, over a new connection for each request, which ends when the client gives it up. An https:// request in absolute form gets 400. The proxy logs nothing for a link it carried, because devvm _links logs it, and when the socket doesn't answer, after a few tries for a full backlog, it answers 502 and logs link_refused (not carried).
Once it listens, it says so with the address it listens on (daemon.Ready) in the status file that devvm names in its environment (DEVVM_DAEMON_STATUS), which is what devvm waits for as it starts the proxy. It returns when the VM's hostagent is gone.
Files
| File | What |
|---|---|
config.go |
Config, the one config a VM's proxy and a build's run with (Encode, Sum, ReadConfig, ConfigVersion), and the checked reads of the files devvm names to the proxy: ReadRegular, which policy files are read with here, in devvm network and in doctor, and readPrivate, for the config and the links' list (ErrNotRegular, ErrNotPrivate, ErrTooLong) |
proxy.go |
Main and its one flag, -config, the proxy's server, which newServer makes from the config (below), the listener (serve), CONNECT and absolute-form handling, the host names it takes (HostOK, which agentlogin checks MCP servers' hosts with too), the address floor (egress, hostAddrs), the log (Event, events, AllowRule: devvm network log reads it), the devvm.proxy endpoints |
policy.go |
the rule syntax and matching (ValidRule: devvm network allow, deny and rm take only these); ParsePolicy, the one strict parser of policy files, which devvm network and doctor use too: each line that holds more than a comment, with its number and whether it's a rule; the built-in policies are read once (readBuiltins), every line a rule, and come first in the union; the user's policy files are re-read when they change, each only as a regular file of at most 1 MiB (ReadRegular, PolicyMax; one that isn't keeps its last rules, logged once), and each line that isn't a rule is logged and skipped, except one that starts with ! (BrokenDeny), which makes the policy deny everything, as refused blocks naming the file and line, until it's fixed |
sni.go |
reads a tunnel's ClientHello to check its server name |
splice.go |
every tunnel the proxy carries itself: hijack takes a CONNECT's connection from the HTTP server and answers 200, and splice passes the bytes both ways as they are, the server's from the start and the client's once a check has passed them (the SNI check, for a tunnel the policy allows), passes on the end of what the client sent, and ends once the server's bytes end or the client has gone |
secrets.go |
interception for hosts with a bound secret: certificates from devvm's CA (ca), which devvm makes (trust.InitCA) and the proxy only loads (a day each, never past the CA's end; once the CA has ended, CONNECTs to be intercepted answered 403 and logged as refused with caEndedReason), header substitution; the VM's bindings and their values (secrets), Binding (KindSecret or KindLogin, with the login's Agent) in a BindingList (JSON, version 1), which internal/secrets writes and ReadBindings reads, and InterceptLines, the allowlist's lines for their hosts; values from IO.Secret, looked up in the proxy's own process (cached briefly, never logged); writeAtomic, the proxy's one writer of whole files |
upstream.go |
upstream and upstreams: a company proxy that allowed requests go through (CONNECT by name for tunnels and interception, absolute form for plain HTTP), NO_PROXY, and the floor for names it resolves (floorUpstream) |
rotate.go |
the logs' rotation past LogLimit (10 MiB): the event log renamed to <log>.1 under the file's lock, the proxy's own log, which is also its stderr, copied there and emptied; OpenEventLog, the event log's writer, which netpolicy's AppendEvent uses too, and which opens the log again before a line once the other writer has rotated it |
nixcache.go |
nix_cache: the shared Nix cache at http://devvm.proxy/nix/ (nixCache), its path pattern (nixPath), narinfo checks (parseNarinfo), NAR checks, the cap and its trim (NixCacheUsage, TrimNixCache) |
links.go |
links: the VM's links' list (LinkList, LinkEntry) and the proxy's way to them (links), their sockets' names (LinkSocket, LinkHash), their addresses and domain, which devvm's other packages use too (IsLinkAddr, LinkAddr, LinkDomain), matching (links.find), CONNECT and absolute-form requests to a link |
*_test.go |
unit tests, and fuzz targets for policy rules (FuzzParseRule), request targets (FuzzRequest), ClientHellos (FuzzClientHello) and the Nix cache's paths (FuzzNixPath); testdata/nix/ holds a narinfo and its NAR from cache.nixos.org |
How devvm runs it
internal/cli/proxy.go is the hidden devvm _proxy command: it hands its arguments to Main with the run UI's streams (IO: the event log and -help on its Stdout, the proxy's own log lines on its Stderr) and devvm's own files (IO.FS, which is assets.FS, so the package never imports assets) and the lookup of secrets' values (IO.Secret, which is secrets.Store.Get, so the package never imports internal/secrets or internal/agentlogin), and ends an error with its Die. The package itself prints nothing.
devvm _proxy -config ~/.local/share/devvm/vms/<hash>/run/proxy.json
Flags take one dash or two. The proxy has one, -config, and everything it runs with is in that file, Config as JSON, which netpolicy.Proxy writes for a VM's proxy and backend.ProxyStarter for a build's, each whole (Encode), mode 600, before it starts the proxy. The file holds the VM's password and the company proxy's credentials, which is why none of it is in the arguments: a process's arguments are visible to other users. ReadConfig reads it once, as the proxy starts: a regular file, not a symbolic link, owned by the proxy's user and readable and writable by nobody else, at most 1 MiB, of ConfigVersion ("version": 1), with no field Config lacks (DisallowUnknownFields), with a password, a loopback listen address, ca and ca_key together and a nix_cache_max of at least 0. Anything else is an error, and the proxy never listens. A running VM's proxy starts again when the config devvm would start it with has another Sum, the SHA-256 of its encoding.
| Field | What |
|---|---|
version |
ConfigVersion, 1: a file of another isn't read |
listen |
the VM's proxy port, on loopback; port 0 for any free one (a build's) |
vm |
the VM's name, for the log |
password |
the VM's key: requests need it as the proxy password, user devvm |
builtin_policies |
devvm's built-in policies, paths in IO.FS (layers/base.policy, shared-tools/node.policy), in order: read once as the proxy starts, ahead of policies, and never again; one that's missing, or holds a line that isn't a rule, is devvm's bug, so Main returns an error and the proxy never listens |
policies |
the user's policy files, in order, each re-read when it changes; a missing file has no rules |
log |
the JSON-lines event log, mode 600, rotated past 10 MiB; none: stdout |
proxy_log |
the file the proxy's stderr appends to (devvm opens it for the process): the proxy's own log lines, and its HTTP servers' errors, go through it and rotate past 10 MiB by copying, so the last error, written to stderr after Main returns, reaches the current file |
lifeline |
the hostagent's pid file (daemon.Lifeline): wait up to 15 minutes for it to name a process that runs, past what it held when the proxy started, and return once it no longer holds what it held then or that process is gone |
ca, ca_key |
devvm's CA, for interception |
secrets |
the VM's bindings file, BindingList (no values), re-read when it changes (secrets); Main refuses it without IO.Secret |
upstream_cas |
CA certificates (PEM) the proxy's own TLS connections trust besides the system's: the user's |
upstream |
the host's HTTPS_PROXY, HTTP_PROXY and NO_PROXY (https, http, no_proxy): allowed requests go through that company proxy, which gets host names, and its credentials only in its own Proxy-Authorization; logs name it by host:port |
upstream_unresolved |
with upstream: names the host can't resolve go there even when only ** allows them (a build's, which reaches internal addresses anyway) |
nix_cache |
the shared Nix cache's folder (~/.local/share/devvm/cache/nix-cache), made mode 700 if it isn't there: serve http://devvm.proxy/nix/ from it; devvm sets it for VMs whose image has the nix layer |
nix_cache_max |
with nix_cache: the cache's cap, in bytes (devvm sets nix-cache.size, 10 GiB when it's off; none: NixCacheDefault) |
links |
the VM's links' list, which devvm _links writes (~/.local/share/devvm/vms/<hash>/run/links.json): reach each link through its socket, before any rule; a missing list has no links |
link_sockets |
with links: the folder of the links' sockets, devvm's run folder (~/.local/share/devvm/run); without it, no link is reached |
devvm_sha256, ca_sha256 |
the SHA-256 of the devvm binary and of devvm's CA certificate: not read by the proxy, there so a new binary or CA changes the config's Sum and devvm starts the proxy again |
Running the proxy is Main's only mode. What only the host does for network policy runs in devvm itself, never through the proxy's arguments: trust.InitCA makes devvm's CA (ca.pem, ca.key, each written whole; valid 30 days; no host name in it) unless one with over a week left is there, and internal/agentlogin makes the VM's copies of agents' login and settings files, their bindings and their values.
The server
Everything a proxy runs with, and everything it keeps between connections, is in its server, which newServer makes from the config as the proxy starts. The package keeps no state of its own: its only package-level variables are error sentinels and fixed tables (notPublic, nat64, githubMinting), which the structure test allows. The server holds the VM's name and password, its own log, and these parts, each of which keeps what it changes under a lock of its own:
events: the event log and the last 50 blocks, for/blockedand/blocked/new;egress: the policy (policy, which reads the user's files again when they change), the host's own addresses (hostAddrs, read again every 30 seconds) and the company proxy (upstreams, set as the proxy starts and only read after);secrets: the bindings file, read again when it changes, and the values looked up for it, held through each lookup so lookups go one at a time;ca: devvm's CA and the certificates the proxy has made with it;links: the links' list and their sockets' folder, which are only read;nixCache: the Nix cache's folder, which every proxy shares under its.lock, and the narinfos this proxy has checked and the NARs it's fetching.
A test makes a server of its own with newServer, as Main does, and sets on it whatever else it needs, so no test changes another's proxy and the tests run in parallel.
Secrets
The bindings file, secrets.json in the VM's run folder, is a BindingList: {"version": 1, "bindings": [...]}, which secrets.Store writes whole, mode 600, at each launch and again whenever a secret is saved, deleted, bound or unbound. Each Binding has its name, its kind, its hosts, its header and its placeholder, and never a value. Its kind says where the value comes from: secret is one that devvm keeps (devvm secret set), in the Keychain or in devvm's secrets folder, and login is one of an agent's logins, read from the login file of its agent (codex, opencode or claude) on the host each time. Nothing about a binding is worked out from its name. The proxy reads the file with ReadBindings when its modification time changes: a regular file, not a symbolic link, owned by the proxy's user and readable and writable by nobody else, at most 1 MiB, of version 1, each binding of a kind it knows. A file it can't read that way has no bindings until devvm writes it again, and the proxy logs why once. A change also drops every value it has cached.
The proxy looks a value up in its own process, through IO.Secret, which devvm _proxy sets to secrets.Store.Get, so no other devvm runs for it, and no value is in any process's arguments. On macOS the Keychain is still asked through security find-generic-password, whose output is the value, as when you save one. Main refuses a config with secrets and no IO.Secret. The lookup gets the binding itself, so the kind decides whether it's the Keychain, devvm's secrets folder or the agent's file (agentlogin.File.Get, which refreshes an OAuth login on the host when it's due, under the file's lock). It runs for at most 20 seconds and one at a time, so two requests never refresh a login at once. The proxy keeps a value for 5 minutes, or for 30 seconds when its kind is login, and never logs it, and a lookup that fails or gives nothing puts nothing in and is logged, without a value, to the proxy's own log.
Inside the VM, http://devvm.proxy/blocked lists recent blocks, /blocked/new those since the last prompt, /ca.pem serves the CA certificate, and with nix_cache, /nix/ is the shared Nix cache.
Testing
go test ./internal/proxy runs the unit tests, in parallel, each with a server of its own (the server), and the fuzz targets' seeds; go test -fuzz FuzzRequest ./internal/proxy fuzzes one. testdata/script/proxy.txtar runs devvm _proxy itself and checks that it refuses any flag but -config. nix_cache.txtar runs it with a config of its own. internal/agentlogin and internal/trust have the tests of the logins, the redaction and the CA, and internal/secrets those of Store.Get, the lookup IO.Secret runs; the proxy's own tests give it a fake one.