Files
devvm/internal/cli/network_transparent.go
w3cjandClaude Opus 5.5 647ad4bf2a feat(network): transparent mode for tools that ignore the proxy settings
On every confined tier, a tool that ignores the proxy settings (workerd
under wrangler dev, a static Go binary, a container made through Docker's
API) reaches the VM's proxy by the names it looks up. In the VM, dnsmasq
answers every name with the stand-in address 198.18.0.1 and has no
upstream; nftables sends that address's ports 80, 443 and those the allow
rules name to socat listeners on it, which make each an authenticated
CONNECT transparent.devvm.proxy:PORT to the VM's proxy. The proxy reads the
host from the connection's first bytes (TLS SNI, or each HTTP request's
Host) and judges it as CONNECT host:PORT, with the same order of checks,
log, secrets and floor. A blocked HTTPS host gets the 403 over a
certificate from the VM's CA. Nothing new runs on the host, and the
backends don't change.

Proxy (internal/proxy):
- transparent.go: TransparentHost, ProxyDomain, SentinelAddr,
  IsSentinelAddr, ViaDNS, transparent (serve, take, tls, handler,
  refusePage, ports), sniHost, replay, reservedRefusal, withVia, viaOf.
- routeFor and route: the one order of checks for CONNECT and the
  transparent path; carry, the one tunnel with its SNI check; block split
  into the record and blockPage; links.refuse split (isLinkDomain);
  caEndedRefusal's test moved to routeFor; handshake and serveOne shared
  by intercept and the 403 page.
- devvm.proxy reserved whole, CONNECT devvm.proxy:P included; the stand-in
  address refused with its own reason.
- Hosts and SNI in ASCII alone, never folded (the Kelvin sign), in
  splitHostPort, sniMatches and sniHost.
- A ClientHello record past 16 KiB + 256 is never read; once it starts,
  a ClientHello must arrive whole within 10 s, on every tunnel; a stream
  whose first byte is 0x16 is TLS from then on, so a record header that
  doesn't come whole, or isn't TLS's version, is refused, never passed on
  unchecked; and only a ClientHello that parsed (sniOf) counts, so one
  split across records is refused, an address's tunnel's included, which
  may have no name (a gap older than this change).
- routeFor carries the method and path rules: a host they narrow is
  refused where it can't be decrypted and intercepted where it can, on
  the transparent path too.
- On the transparent HTTP path, devvm.proxy answers /blocked alone.
- policy.transparentPorts and /transparent/ports: 80, 443, then the ports
  allow rules name for hosts by name, at most 32, never 22 or 53.
- Event.Via ("dns"), shown in devvm network log, /blocked and the prompt's
  note as "without proxy settings".

Guest (assets/): base.sh installs dnsmasq(-base), socat and nftables;
guest/transparent.sh is devvm-transparent: setup and teardown from
sync.sh (the proxy URL on stdin), the supervisor service (systemd or
OpenRC), the ruleset (render: two filter chains at -150 keep every
interface but lo and Docker's bridges, and every protocol but TCP, UDP
and ICMP, off the stand-in address; both NAT chains at -110 map every port of
the stand-in address, so Docker's published ports never take one; DNS to
public servers and 127.0.0.1:53 go to dnsmasq, which listens on the
stand-in address alone; private, link-local, CGNAT and loopback servers
are left alone, but the VM's own name servers, which Lima's DHCP adds
beside 127.0.0.1 and Docker gives containers, go to dnsmasq too), slots,
the port list's check, and status for devvm-why. Names without a dot and
names under the search domains (this machine's network's, through Lima)
don't resolve.

Setting: config.toml's transparent-proxy, and devvm network transparent
[on|off], the VM's own value in net.transparent (Settings.Transparent,
SetTransparent; netpolicy.TransparentOf, TransparentAddr); the setup gets
DEVVM_TRANSPARENT_ADDR while the VM runs confined and its mode is on.
devvm network --json has transparent; devvm network lock lists apart the
hosts only tools without proxy settings reached (Learned.NoProxy, via in
--json). vmssh.dialAddr refuses the stand-in address.

Tests: the transparent path's unit tests and FuzzTransparent; the
setting, TransparentOf, FormatEvent's via and learned hosts; the ruleset,
slots and port list under bash 3.2; network_transparent.txtar and
sync.txtar; e2e: transparentRuns, the network section's checks, a docker
step, the from section's Debian 12 and Alpine on the allowlist tier, and
the guarantee checks run with transparent mode stopped and running.

Docs: safety/network.md (tools that ignore the proxy, what isn't
covered, the setting, logs, lock), internals/network.md (tools that skip
the proxy), internals/design.md (the design note and threat check),
using/settings.md, using/troubleshooting.md, using/limits.md,
using/images.md, safety/attack-vectors.md, internals/lima.md,
contributing/tests.md and testing.md, the proxy's README, the CLI
reference and the Commands page.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-09 07:18:53 -07:00

91 lines
3.0 KiB
Go

package cli
import (
"io"
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/netpolicy"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/ui"
"github.com/devvmhq/devvm/internal/vm"
)
// newNetworkTransparentCmd is devvm network transparent [on|off].
func newNetworkTransparentCmd(a *app) *cobra.Command {
var asJSON bool
cmd := &cobra.Command{
Use: "transparent [on|off]",
Annotations: about{docs: "safety/network#tools-that-ignore-the-proxy"}.annotations(),
Short: "Show or change transparent mode for this folder's VM",
Long: "Show whether this folder's VM sends tools that ignore the proxy settings to its proxy by the names they look up, on or off, and where that comes from: the VM's own setting, config.toml's transparent-proxy, or devvm's default, on. " +
"devvm network transparent on or off gives the VM its own setting, which applies from its next launch, while it's on a confined tier.",
Example: examples(
"devvm network transparent", "show the VM's transparent mode and where it comes from",
"devvm network transparent off", "turn it off for this VM, from its next launch",
"devvm network transparent on", "turn it on again, whatever config.toml says",
),
Args: showsOnly(maxArgs(1)),
ValidArgs: netpolicy.TransparentChoices(),
RunE: func(cmd *cobra.Command, args []string) error {
var (
t netpolicy.Transparent
dirs paths.Dirs
)
err := a.env(func(env *vm.Env) error {
n := vm.Settings{Env: env}
dirs = env.Dirs
if len(args) == 1 {
return n.SetTransparent(cmd.Context(), args[0], "devvm network transparent "+args[0])
}
var err error
t, err = n.Transparent(cmd.Context())
return err
}, msg.NetTransparentFailed)
if err != nil || len(args) == 1 {
return err
}
return printTransparent(a.u, asJSON, t, dirs)
},
}
jsonFlag(cmd, &asJSON)
return addFolderFlags(cmd, a)
}
// transparentJSON is a VM's transparent mode, in devvm network transparent
// --json and devvm network --json: whether it's on, and where that comes
// from, vm, config or default.
type transparentJSON struct {
On bool `json:"on"`
From string `json:"from"`
}
// printTransparent writes a VM's transparent mode, on or off, then a #
// and where it comes from (transparentFrom), or with asJSON as
// transparentJSON.
func printTransparent(u *ui.UI, asJSON bool, t netpolicy.Transparent, d paths.Dirs) error {
if asJSON {
return printJSON(u, transparentJSON(t))
}
v := netpolicy.TransparentOff
if t.On {
v = netpolicy.TransparentOn
}
_, err := io.WriteString(u.Stdout(), v+" # "+transparentFrom(t.From, d)+"\n")
return err
}
// transparentFrom is where a VM's transparent mode comes from, as devvm
// network transparent shows it: the command that set it, config.toml's
// path, or devvm's default.
func transparentFrom(from string, d paths.Dirs) string {
switch from {
case netpolicy.FromVM:
return "devvm network transparent"
case netpolicy.FromConfig:
return d.ConfigFile()
}
return "devvm: default"
}