Files
devvm/tests/e2e/network_test.go
w3cjandClaude Opus 5.5 b625469ac6 fix(vm): devvm rm asks before opencode's worktrees' uncommitted changes go
An export leaves out opencode's worktree folder, as a git repository's config can name a command that git runs, so the uncommitted changes in the worktrees opencode makes there (~/.local/share/opencode/worktree/<project>/<name>, opencode v2.4.1's worktree/strategies.ts) went with the VM's disk on devvm rm, and on a remake with --clean-home, with nothing to say so.

The registry gets Worktrees (opencode's worktree/*/*, below its HistoryOwn, TestDefsComplete holding each to a plain pattern there). rm's unsaved-work check, cloneCheck, becomes workCheck: the clone (cloneLost, as before) and the agents' worktrees (agentTrees), one question for both. agentTreesState reads them in the running VM in one command, in its home folder, each * of a pattern sent as *, .[!.]* and ..?* (treeGlobs), so a name that starts with a dot is found: each folder with a .git, its git status and a detached HEAD's commit nothing else has; past 100 it says more. devvm reads at most 8 MiB of the answer, cancelling the command past it, and waits at most treesWait (2 minutes). parseAgentTrees takes the answer as untrusted: whole records of a glob asked for, each path clean, matched by its pattern and once, at most 100, each with one status. Dirty, detached, unreadable (wfail) and more are shown and asked about (delete_anyway or remake_anyway), which --force never answers; refused without a terminal (no_terminal_delete_trees, naming --no-worktree-check, or the clone's no_terminal_delete when it has work too). --no-worktree-check skips the check and warns (trees_check_skipped). A failed, late or hostile answer keeps the VM (trees_check_failed), and so does a VM that doesn't start for it (trees_check_not_started).

Rm and rmGone run it, and rmGone refuses a stopped VM with opencode, which starting would make its folder again (rm_gone_stopped_trees, naming --no-export --no-worktree-check); stillGone takes the refusal to give. remaking.checkWork runs it before a remake with --clean-home (remakeTo, Update, SetClone, whose clone check it joins), its messages naming the command as typed (remakeInVM.typed), with --clean-home, which --no-worktree-check needs (needs), so devvm clone off --clean-home's clone check messages name it too. A remake's flag has its own help. The stub guest runs the read in vmhome-<vm>.

Tests that fail without the check: TestRmAgentTrees, TestRmVMAgentTrees, TestRemakeAgentTrees, TestUpdateAgentTrees, TestSetCloneAgentTrees and the rm_agent_trees script; TestAgentTreesBounded (an endless answer cancelled, a silent VM given up on, in a synctest bubble); TestAgentTreesState runs the script under sh on real worktrees, a dot-named one included; TestParseAgentTrees holds the answer's checks, TestCloneGit the stub's. TestSetCloneOff names the command with --clean-home. e2e's network section makes a worktree in opencode's folder in the real VM, and devvm rm refuses, then --no-worktree-check deletes it.

Docs: coding agents' opencode's worktrees, How it works' opencode's worktrees, deleting a VM, a VM whose folder is gone, images, clone, the overview's remake, Commands, extending and rm's help; the changelog's What to do step 4 names --no-worktree-check for scripts and devvm rm VM of a stopped VM, and opencode's block gets the check; the release page has its section.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 00:03:05 -07:00

3432 lines
158 KiB
Go

//go:build e2e
package e2e
import (
"bytes"
"cmp"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"syscall"
"time"
"github.com/devvmhq/devvm/internal/proxy"
"github.com/devvmhq/devvm/internal/redact"
"github.com/devvmhq/devvm/internal/shell"
)
// netRun is the network section's: its project folder and the home of its
// own (a fake ~/.claude.json and Codex login, which devvm agent import
// copies into the sandbox's agent store, so no real token or login is
// read), its VM, the VM's proxy and the fake token endpoints.
type netRun struct {
s *S
n, nh string // the project folder, its HOME
logins string // the sandbox's agent store
vm string
vmd string // the VM's folder
pxc string // its proxy's config
pp string // its proxy's port on this machine
salt string // its placeholders' salt
events string // its proxy's events
tokLog string // what the fake token endpoints got
tokURL string // the fake endpoints, as a company proxy
tok *Proc
cap1 string // the VM's first captured login, as its curl got it
lan string // this machine's LAN address
before map[string]string // the home folder's login files
// lanServer answers on every address of this machine, for the tiers.
lanServer *Proc
// keys are the VMs' proxy passwords netNoLeaks has seen, so its last
// run checks those of VMs the section has deleted since.
keys []string
// corps are the passwords of the section's company proxies, which
// netNoLeaks looks for too.
corps []string
}
// The fake Claude Code token devvm's environment holds, which its proxy
// inherits: never a real one.
const fakeToken = "CLAUDE_CODE_OAUTH_TOKEN=sk-ant-oat01-e2efake" //nolint:gosec // G101: a fake token, which no service takes
// networkSection: a VM with Claude Code and Codex behind its proxy, its
// tokens bound to it as placeholders; direct connections, DNS and this
// machine's loopback refused; a company proxy upstream; each VM's CAs;
// logins made in the VM captured; devvm agent login, save and secret;
// the self-check; the hostagent's sandbox (macOS); and the tiers.
func networkSection(s *S) {
s.Section("network policy")
n := &netRun{s: s}
netSetup(n)
netLaunch(n)
netBlocks(n)
netTransparent(n)
netTransparentDocker(n)
netPiTools(n)
netLoopback(n)
netCorp(n)
netCAs(n)
netTokens(n)
netCapture(n)
netBroker(n)
netCodexLogin(n)
netOpencodeLogin(n)
netOpencodeHistory(n)
netOpencodeKeys(n)
netPi(n)
netCursor(n)
netProviderKeys(n)
netClaudeLogins(n)
netSave(n)
netGithub(n)
netNoLeaks(n, "once the section has used its secrets", false)
netMise(n)
netSelfChecks(n)
if runtime.GOOS == "darwin" {
netSandbox(n)
} else {
for _, t := range sandboxTitles() {
s.Skip(t, "Seatbelt is macOS's")
}
}
netUnenforced(n)
netOpen(n)
netTiers(n)
netLearn(n)
netNoLeaks(n, "at the section's end", true)
}
// leakValues are the fake values the network section saves, binds or has
// captured, which only the store, the Keychain and the VMs' proxies may
// hold: never a log.
func leakValues() []string {
return []string{
"ghp_E2EFAKE", "sk-ant-oat01-e2efake", customValue, basicValue, opencodeLocalKey, piKey, groqKey, piOpenAIKey,
googleKey, opencodeGroq, openRouterKey, "rt-e2efake-capture", "sk-e2efake-captured-key", "e2efake-oauth",
"e2efake-setup", "e2efake-refreshed",
}
}
// netNoLeaks, when: no VM's proxy password, no company proxy's password
// and none of the values the section used is in devvm's run logs, its
// other logs or any VM's proxy logs (proxy.log, the event log), as they
// are or in Basic credentials, as Proxy-Authorization carries a VM's
// password and a company proxy's, and git a token; and no VM's or company
// proxy's password, in any of its forms, is in Lima's logs of any VM
// (ha.stderr.log, the serial logs). A company proxy's password is looked
// for as it is, as the user:password of a URL, as Basic's base64 and as
// the whole Proxy-Authorization value. It runs while the section's VMs
// and their logs are still there, and again at its end (last), for the
// proxies started since, with every
// password it has seen, by when the section may have deleted every VM and
// its logs with it.
func netNoLeaks(n *netRun, when string, last bool) {
s := n.s
s.Check(when+", no VM's or company proxy's password, and no token, key or login the section used, is in devvm's run logs, any VM's proxy's logs or Lima's", func() error {
if k := n.key(); k != "" && !slices.Contains(n.keys, k) {
n.keys = append(n.keys, k)
}
states, _ := filepath.Glob(filepath.Join(s.Paths.Devvm, "vms", "*", "vm.json"))
for _, f := range states {
if k := readState(f)["net.made.proxy_key"]; k != "" && !slices.Contains(n.keys, k) {
n.keys = append(n.keys, k)
}
}
if len(n.keys) == 0 {
return errors.New("no VM's state holds a proxy password")
}
if len(n.corps) == 0 {
return errors.New("no company proxy's password: netCorp's company proxy didn't start")
}
basic := func(user, pass string) string { return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass)) }
// pws are the VMs' and company proxies' passwords in each of their forms, which Lima's
// logs are searched for too; the company proxies' users are ProxyUser, as their URLs and
// config.toml name them.
var pws []string
for _, k := range n.keys {
pws = append(pws, k, basic(proxy.ProxyUser, k))
}
for _, pw := range n.corps {
b := basic(proxy.ProxyUser, pw)
pws = append(pws, pw, proxy.ProxyUser+":"+pw, b, "Basic "+b)
}
values := slices.Clone(pws)
for _, v := range leakValues() {
values = append(values, v, basic("x-access-token", v))
}
logs, _ := filepath.Glob(filepath.Join(s.Paths.Devvm, "vms", "*", "logs"))
if len(s.Paths.RunLogs()) == 0 || len(logs) == 0 && !last {
return errors.New("no run log, or no VM's logs")
}
if err := NoLeak(values, append([]string{filepath.Join(s.Paths.Devvm, "logs")}, logs...)...); err != nil {
return err
}
lima, _ := filepath.Glob(filepath.Join(s.sb.lima, "*", "ha.stderr.log"))
serial, _ := filepath.Glob(filepath.Join(s.sb.lima, "*", "serial*.log"))
if len(lima) == 0 && !last {
return errors.New("no VM's ha.stderr.log")
}
return NoLeak(pws, append(lima, serial...)...)
})
}
// netLearn: a VM made with --network learn reaches two hosts off the
// allowlist on the internet tier; devvm network lock --all adds exactly
// the hosts it learned, at once, so an added one still works and a new
// one is blocked.
func netLearn(n *netRun) {
s := n.s
dir, _ := s.NewProject("net-learn")
vm, _, _ := launchWith(s, dir, n.home(), "--network", "learn", "--no-git-protect", "--tools", "none", "--agents", "none",
"--cpus", "2", "--memory", "2", "--ports", "none")
tier := func() string {
r, _ := s.Devvm(dir, "network")
return FirstLine(r.Stdout)
}
code := func(u string) string { return vmOut(s, vm, "curl -s -m 20 -o /dev/null -w %{http_code} "+u) }
s.Check("--network learn: the VM runs on the internet tier, learning, and reaches hosts off the allowlist", func() error {
st := s.Paths.State(vm)
switch {
case vm == "" || s.Status(vm) != "Running":
return errors.New("no VM, or it isn't running")
case st["net.tier"] != "internet" || st["net.learning.since"] == "" || tier() != "internet (learning)":
return fmt.Errorf("its tier is %q, learning since %q, shown as %q", st["net.tier"], st["net.learning.since"], tier())
case code("https://example.com/") != "200" || code("https://example.org/") != "200":
return errors.New("example.com or example.org wasn't reached")
}
return nil
})
var learned []string
s.Check("devvm network lock --json lists both hosts, and its launch counts them", func() error {
r, err := s.Devvm(dir, "network", "lock", "--json")
if err != nil {
return err
}
var out struct {
Hosts []struct{ Rule string } `json:"hosts"`
}
if err := json.Unmarshal([]byte(r.Stdout), &out); err != nil {
return err
}
for _, h := range out.Hosts {
learned = append(learned, h.Rule)
}
// Exact rules, each with its port: 443 doesn't allow 80 too.
if !slices.Contains(learned, "example.com:443") || !slices.Contains(learned, "example.org:443") || slices.Contains(learned, "github.com:443") {
return fmt.Errorf("learned %q", learned)
}
launch, err := s.Run(devvmCmd(s, dir, "exit\n", n.home("DEVVM_MESSAGES=names")))
if err != nil || !Matches(launch.Out, `net_learning count=`+strconv.Itoa(len(learned))+` since=`) {
return failed("the launch didn't remind it of the hosts it learned", err)
}
return nil
})
s.Check("devvm network lock --all adds exactly what it learned and gives it the allowlist tier at once: an added host works, a new one is blocked", func() error {
if _, err := s.Devvm(dir, "network", "lock", "--all"); err != nil {
return err
}
policy := strings.Fields(readText(filepath.Join(s.Paths.VMDir(vm), "policy")))
st := s.Paths.State(vm)
switch {
case !slices.Equal(policy, learned):
return fmt.Errorf("its own rules are %q, want %q", policy, learned)
case st["net.tier"] != "allowlist" || st["net.learning.since"] != "" || tier() != "allowlist" || s.Status(vm) != "Running":
return fmt.Errorf("its tier is %q, learning since %q, shown as %q", st["net.tier"], st["net.learning.since"], tier())
case code("https://example.com/") != "200":
return errors.New("example.com, which it learned, failed")
case code("http://example.com/") != "403":
return errors.New("example.com on port 80, which it didn't reach, wasn't refused with a 403")
case code("http://example.net/") != "403" || inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://example.net/") == nil:
return errors.New("example.net, which it didn't learn, wasn't refused with a 403")
}
return nil
})
s.Check("rm --force deletes the learning VM", func() error {
_, err := s.Run(devvmCmd(s, dir, "", n.home(), "rm", "--force", "--no-export"))
return err
})
}
// home is the VM's HOME, with env after it.
func (n *netRun) home(env ...string) []string { return append([]string{"HOME=" + n.nh}, env...) }
// dv runs devvm args in the project folder, input on its stdin.
func (n *netRun) dv(input string, env []string, args ...string) (Res, error) {
return n.s.Run(devvmCmd(n.s, n.n, input, env, args...))
}
// tier is devvm network's first line: the VM's tier.
func (n *netRun) tier() string {
r, _ := n.s.Devvm(n.n, "network")
return FirstLine(r.Stdout)
}
// key is the VM's proxy's password.
func (n *netRun) key() string { return n.s.Paths.State(n.vm)["net.made.proxy_key"] }
// codexWithheld is the warning, in names mode, for the fields of the
// imported Codex login that the VM's copy leaves out.
const codexWithheld = `login_fields_withheld agent=Codex file=auth\.json keys=personal_access_token, zz_e2e`
// claudeWithheld is the same warning for the imported ~/.claude.json's
// oauthAccount.
const claudeWithheld = `login_fields_withheld agent=Claude Code file=\.claude\.json keys=oauthAccount\.zz_e2e`
func netSetup(n *netRun) {
s := n.s
n.nh = filepath.Join(s.sb.work, "net-home")
_ = os.RemoveAll(n.nh)
madeFile(s.sb, n.nh)
_ = os.MkdirAll(filepath.Join(n.nh, ".codex"), 0o700)
n.before = map[string]string{
// An account field devvm doesn't know, which the VM's copy leaves out (claudeWithheld).
"claude": `{"mcpServers":{"e2e":{"type":"http","url":"https://api.github.com/devvm-e2e-mcp","headers":{"X-Devvm-E2e":"e2efake-mcp"}}},` +
`"oauthAccount":{"emailAddress":"[email protected]","zz_e2e":"e2efake-account"}}` + "\n",
// Fields devvm doesn't know, which the VM's copy leaves out (codexWithheld).
"codex": `{"OPENAI_API_KEY":"sk-e2efake-codex","last_refresh":null,"personal_access_token":"pat-e2efake-codex","zz_e2e":{"nested":["e2efake-codex"]}}` + "\n",
}
_ = os.WriteFile(filepath.Join(n.nh, ".claude.json"), []byte(n.before["claude"]), 0o600)
_ = os.WriteFile(filepath.Join(n.nh, ".codex", "auth.json"), []byte(n.before["codex"]), 0o600)
// macOS finds the Keychain through HOME, so the VM's home links the Keychains folder: devvm
// secret set and the proxies started with that HOME save and read secrets under the run's
// service, and without the link security waits on a Keychain that isn't there.
if runtime.GOOS == "darwin" {
_ = os.MkdirAll(filepath.Join(n.nh, "Library"), 0o700)
_ = os.Symlink(filepath.Join(os.Getenv("HOME"), "Library", "Keychains"), filepath.Join(n.nh, "Library", "Keychains"))
}
// The sandbox's store of logins, from no login: an earlier run's in the same slot, such as the
// opencode key netOpencodeKeys saves, would reach a VM at its first launch.
n.logins = filepath.Join(s.Paths.Devvm, "logins")
_ = os.RemoveAll(n.logins)
madeFile(s.sb, n.logins)
// The section's secrets, gone before it starts as well as at its end: one a killed run left,
// such as the proxy secret, would make netCorp's launch go ahead where it must be refused.
forget := func(ctx context.Context) {
forgetSecrets(ctx, s, "github", "claude", "custom."+customName, "custom."+basicName, proxy.UpstreamSecret)
}
forget(s.r.window().ctx)
s.Defer(forget)
}
func netLaunch(n *netRun) {
s := n.s
s.Check("devvm agent import copies the logins into the sandbox's agent store, only yours to read", func() error {
if _, err := s.Run(devvmCmd(s, "", "", n.home(), "agent", "import", "--force", "claude", "codex")); err != nil {
return err
}
a := filepath.Join(n.logins, "codex", ".codex", "auth.json")
switch {
case readText(a) != n.before["codex"]:
return errors.New("the store's Codex login isn't the home folder's")
case !strings.Contains(readText(filepath.Join(n.logins, "claude", ".claude.json")), "e2efake-mcp"):
return errors.New("the store's ~/.claude.json has no MCP header")
case modeOf(filepath.Join(n.logins, "codex")) != "700" || modeOf(a) != "600":
return errors.New("the store's Codex login isn't yours alone to read")
}
return nil
})
n.n, _ = s.NewProject("net")
w, werr := WatchProcs(s)
var lr Res
n.vm, lr, _ = launchWith(s, n.n, n.home(fakeToken, "DEVVM_MESSAGES=names"), "--network", "allowlist", "--no-git-protect", "--agents", "claude,codex,opencode,pi,cursor")
if werr == nil {
w.Stop()
}
s.Check("the launch names the fields of Codex's login and Claude Code's account that the VM's copies leave out", func() error {
if !Matches(lr.Out, `^devvm: `+codexWithheld+`$`) || !Matches(lr.Out, `^devvm: `+claudeWithheld+`$`) {
return errors.New("no warning names them:\n" + lr.Out)
}
return nil
}, Tail(40))
n.vmd = s.Paths.VMDir(n.vm)
st := s.Paths.State(n.vm)
n.pp, n.salt = st["ports.host.proxy"], st["net.made.proxy_salt"]
n.pxc, n.events = filepath.Join(n.vmd, "run", "proxy.json"), filepath.Join(n.vmd, "logs", "network.jsonl")
s.Check("--network allowlist: the VM runs behind its proxy, and the self-check passed", func() error {
st := s.Paths.State(n.vm)
switch {
case n.vm == "" || s.Status(n.vm) != "Running":
return errors.New("no VM, or it isn't running")
case st["net.tier"] != "allowlist" || st["net.made.check"] != "ok":
return fmt.Errorf("its tier is %q, its self-check %q", st["net.tier"], st["net.made.check"])
case !proxyRuns(s, n.pxc):
return errors.New("its proxy doesn't run")
case !strings.Contains(readText(n.pxc), `"listen": "127.0.0.1:`+n.pp+`"`):
return errors.New("its proxy's config doesn't listen on its port")
}
return nil
})
s.Check("the proxy's config is yours alone, holds its password, and is all its arguments name", func() error {
cfg, key := readText(n.pxc), n.key()
switch {
case modeOf(n.pxc) != "600" || !Matches(cfg, `^ "version": 1,$`):
return errors.New("the config isn't mode 600, or of version 1")
case key == "" || !strings.Contains(cfg, `"password": "`+key+`"`):
return errors.New("the config doesn't hold the VM's password")
}
p := pids(s, "[_]proxy -config "+n.pxc)
if len(p) == 0 {
return errors.New("the proxy doesn't run")
}
r, _ := s.Run(Cmd{Args: []string{"ps", "-ww", "-o", "command=", "-p", p[0]}})
if f := strings.Fields(r.Stdout); len(f) < 2 || strings.Join(f[1:], " ") != "_proxy -config "+n.pxc {
return errors.New("the proxy's arguments name more than its config")
}
return nil
})
// The control: the launch set the VM's environment (limactl edit, its
// password's stand-in written as <password> in the run log), and the
// guest has the password.
s.Check("the proxy's password was in no host process's arguments while devvm launched the VM, nor after; the guest has it", func() error {
key := n.key()
if len(key) != 64 || werr != nil {
return errors.New("no 64-character password, or no process watch")
}
if saw, err := w.Saw(key); err != nil || saw {
return failed("a host process's arguments held the password while devvm launched the VM", err)
}
if have, err := ProcsHave(s, key); err != nil || have {
return failed("a host process's arguments hold the password", err)
}
edited := false
for _, f := range s.Paths.RunLogs() {
edited = edited || Matches(readText(f), `limactl edit .*devvm:<password>@`)
}
if !edited {
return errors.New("no run log shows limactl edit setting the proxy, its password's stand-in in its place")
}
if has, err := vmHas(s, n.vm, true, "printenv HTTPS_PROXY", ":"+key+"@"); err != nil || !has {
return failed("the guest's HTTPS_PROXY doesn't hold the password", err)
}
return nil
}, ProcsFlaky())
s.Check("ssh devvm-net reaches it, confined, its host key checked", func() error {
cfg := filepath.Join(s.Paths.Devvm, "ssh_config")
if !Matches(readText(cfg), `^Host devvm-net `+regexp.QuoteMeta(n.vm)+`$`) {
return errors.New("ssh_config has no Host devvm-net " + n.vm)
}
if h := out1(s.Run(Cmd{Args: []string{"ssh", "-F", cfg, "devvm-net", "hostname"}})); h != "net" {
return fmt.Errorf("ssh devvm-net hostname: got %q", h)
}
return nil
})
s.Check("devvm host runs the self-check, which passes, and names it", func() error {
r, err := n.dv("", n.home(fakeToken), "host")
switch {
case err != nil:
return err
case s.Paths.State(n.vm)["net.made.check"] != "ok":
return errors.New("the self-check didn't pass")
case !grepF(r.Out, FirstLine(s.Msg("ssh_host_info", "dir="+n.n, "host=devvm-net"))):
return errors.New("devvm host didn't name devvm-net")
}
return nil
})
}
func netBlocks(n *netRun) {
s, vm := n.s, n.vm
// The host's confinement, without transparent mode's rules in the VM,
// which root there could remove: its service stopped for these.
s.Check("with transparent mode stopped, direct connections fail, even as root: an address, other ports", func() error {
return withoutTransparent(s, vm, func() error {
for _, args := range [][]string{
{"sudo", "curl", "--noproxy", "*", "-sS", "-m", "5", "-o", "/dev/null", "https://1.1.1.1"},
{"sudo", "curl", "--noproxy", "*", "-sS", "-m", "5", "-o", "/dev/null", "http://1.1.1.1"},
{"sudo", "timeout", "5", "bash", "-c", "exec 3<>/dev/tcp/1.1.1.1/53"},
{"sudo", "timeout", "5", "bash", "-c", "exec 3<>/dev/tcp/140.82.112.3/22"},
} {
if _, err := vmRun(s, vm, args...); err == nil {
return errors.New("connected: " + strings.Join(args, " "))
}
}
return nil
})
})
// With it running, a public server's DNS is the VM's own dnsmasq, which
// has no upstream, and so is the VM's own name servers' (Lima's DHCP
// gives it one): any answer is the stand-in address, and nothing else.
s.Check("with transparent mode running, a public DNS server and Lima's DNS addresses answer only with the stand-in address, TTL 0, or nothing", func() error {
return inVM(s, vm, dnsAnswers(true, "1.1.1.1", "8.8.8.8", "192.168.5.3", "192.168.5.2"))
})
s.Check("an allowed host works, through the proxy", func() error {
return inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://github.com")
})
// Before anything is blocked, with transparent mode stopped: a tool that
// skips the proxy fails for an allowed host, and devvm-why, with nothing
// to show, says why and that transparent mode isn't running; the agents'
// note says the same.
s.Check("with transparent mode stopped, a request that skips the proxy fails for an allowed host, and devvm-why points at the tool's proxy setting", func() error {
return withoutTransparent(s, vm, func() error {
if _, err := vmRun(s, vm, "curl", "--noproxy", "*", "-sS", "-m", "5", "-o", "/dev/null", "https://github.com"); err == nil {
return errors.New("github.com was reached without the proxy")
}
r, err := s.InVM(vm, "devvm-why")
switch {
case err != nil:
return err
case !r.Has("devvm: nothing blocked recently") || !strings.Contains(r.Stdout, "skipped the proxy on a port or in a way devvm can't send to it: turn on its own proxy setting") ||
!strings.Contains(r.Stdout, "#a-tool-that-skips-the-proxy-fails") || !r.Has("Transparent mode: not running."):
return errors.New("devvm-why doesn't point at the tool's proxy setting: " + r.Stdout)
}
if !vmGrep(s, vm, "cat ~/.claude/CLAUDE.md", "while `devvm-why` shows nothing blocked means the tool skipped the proxy") {
return errors.New("~/.claude/CLAUDE.md's note doesn't say a tool may skip the proxy")
}
return nil
})
})
// opencode 2 reads its model catalog from models.opencode.ai and its
// own service (Zen, the console, MCP's OAuth client) from opencode.ai.
s.Check("opencode's policy allows the hosts opencode 2 reaches at start", func() error {
for _, u := range []string{"https://models.opencode.ai/experimental/v2.0/api.json", "https://opencode.ai/v2/cli.json"} {
if err := inVM(s, vm, "curl -fsS -m 20 -o /dev/null "+u); err != nil {
return errors.New(u + " wasn't reached through the proxy")
}
}
return nil
})
// pi reads its model catalog from pi.dev, its policy's one host.
s.Check("pi's policy allows its model catalog", func() error {
if err := inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://pi.dev/api/models/providers/anthropic"); err != nil {
return errors.New("pi.dev's catalog wasn't reached through the proxy")
}
return nil
})
s.Check("another host is blocked by the proxy, and so is another port of an allowed one", func() error {
r, _ := s.InVM(vm, "curl -sS -m 20 -o /dev/null https://example.com")
switch {
case !strings.Contains(r.Out, "403"):
return errors.New("https://example.com wasn't refused with a 403")
case vmOut(s, vm, "curl -s -m 20 -o /dev/null -w %{http_code} http://example.com/") != "403":
return errors.New("http://example.com/ wasn't refused with a 403")
case inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://github.com:8443") == nil:
return errors.New("github.com:8443 was reached")
}
return nil
})
s.Check("devvm network log shows the block and the allowed connection", func() error {
r, err := s.Devvm(n.n, "network", "log")
if err != nil || !strings.Contains(r.Stdout, " BLOCKED example.com:443 ") || !strings.Contains(r.Stdout, " allowed github.com:443 rule ") {
return errors.New("devvm network log doesn't show example.com blocked and github.com allowed")
}
return nil
})
s.Check("devvm network log --blocked groups it, with the command that allows it", func() error {
r, _ := s.Devvm(n.n, "network", "log", "--blocked")
if !Matches(r.Stdout, `^[0-9TZ:-]+ +[0-9]+ +example\.com:443 +devvm network allow example\.com$`) {
return errors.New("devvm network log --blocked doesn't group example.com:443 with its command")
}
return nil
})
// The command devvm network log suggests, run as it is: the same proxy
// allows the host at once, and after devvm network rm blocks it again.
s.Check("devvm network allow applies at once to the running VM's proxy, devvm network shows it after on, and rm takes it back", n.allowAtOnce)
// A line that reads as a deny but isn't a rule: the running proxy
// denies everything, refused with no allow command, devvm network rm
// warns of it, and once it's gone the allowed host works again. The
// VM's own file is put back whatever happens, so the checks after
// this one see its rules.
s.Check("a deny that isn't a rule makes the running proxy refuse everything until it's gone", n.brokenDeny)
// With transparent mode stopped, as root could stop it: DNS fails at
// once, and Lima's network's DNS (192.168.5.3) and the host's address on
// it, asked directly, answer nothing: a refusal or no answer passes, an
// address for example.com fails.
// The VM's programs ask 127.0.0.1 themselves, past systemd-resolved's
// stub, which could keep retrying it without answering.
s.Check("with transparent mode stopped, DNS fails at once, not after a timeout, asking 127.0.0.1 alone, and Lima's network's DNS addresses, asked directly, answer nothing", func() error {
return withoutTransparent(s, vm, func() error {
if err := inVM(s, vm, `[ ! -L /etc/resolv.conf ] && [ "$(awk '$1 == "nameserver"' /etc/resolv.conf)" = "nameserver 127.0.0.1" ] &&
{ timeout 3 getent hosts example.com; [ $? -eq 2 ]; }`); err != nil {
return err
}
return inVM(s, vm, limaDNSAsked)
})
})
}
func (n *netRun) allowAtOnce() error {
s := n.s
p := pids(s, "[_]proxy -config "+n.pxc)
if len(p) == 0 {
return errors.New("the proxy doesn't run")
}
r, _ := s.Devvm(n.n, "network", "log", "--blocked")
cmd := ""
for l := range strings.Lines(r.Stdout) {
if f := strings.Fields(l); len(f) >= 3 && f[2] == "example.com:443" {
if i := strings.LastIndex(l, " devvm "); i >= 0 {
cmd = strings.TrimSpace(l[i+len(" devvm "):])
}
}
}
if cmd != "network allow example.com" {
return fmt.Errorf("devvm network log --blocked suggests %q", cmd)
}
if _, err := s.Devvm(n.n, strings.Fields(cmd)...); err != nil {
return err
}
r, err := s.Devvm(n.n, "network")
switch {
case err != nil || FirstLine(r.Stdout) != "allowlist" || !r.Has(" example.com"):
return errors.New("devvm network doesn't show allowlist and example.com")
case inVM(s, n.vm, "curl -fsS -m 20 -o /dev/null https://example.com") != nil:
return errors.New("example.com isn't allowed at once")
}
if _, err := s.Devvm(n.n, "network", "rm", "example.com"); err != nil {
return err
}
switch {
case vmOut(s, n.vm, "curl -s -m 20 -o /dev/null -w %{http_code} http://example.com/") != "403":
return errors.New("example.com is still allowed")
case strings.Join(pids(s, "[_]proxy -config "+n.pxc), " ") != p[0]:
return errors.New("the proxy was started again")
}
return nil
}
func (n *netRun) brokenDeny() error {
s := n.s
pol := filepath.Join(n.vmd, "policy")
keep, err := os.ReadFile(pol)
if err != nil {
return err
}
if err := AppendFile(pol, "! example.org\n"); err != nil {
return err
}
bad := func() error {
if vmOut(s, n.vm, "curl -s -m 20 -o /dev/null -w %{http_code} http://github.com/") != "403" {
return errors.New("github.com wasn't refused")
}
r, _ := s.Devvm(n.n, "network", "log")
if !Matches(r.Stdout, ` BLOCKED github\.com:80 .*reads as a deny`) {
return errors.New("devvm network log doesn't say the deny refused github.com:80")
}
r, _ = s.Devvm(n.n, "network", "log", "--blocked")
if !Matches(r.Stdout, ` github\.com:80 +-$`) {
return errors.New("devvm network log --blocked suggests a command for github.com:80")
}
r, _ = s.DevvmNames(n.n, "network", "rm", "nothere.test")
if !Matches(r.Out, `^devvm: policy_broken_deny `) {
return errors.New("devvm network rm didn't warn of the broken deny")
}
return nil
}()
if err := os.WriteFile(pol, keep, 0o600); err != nil {
return err
}
if bad != nil {
return bad
}
return inVM(s, n.vm, "curl -fsS -m 20 -o /dev/null https://github.com")
}
// A service on the host's own loopback (a devvm _proxy, which answers
// anything with 407): unreachable directly, and refused by the VM's proxy
// (internal addresses need a rule naming them).
func netLoopback(n *netRun) {
s := n.s
port := strconv.Itoa(s.Port(18765))
f := filepath.Join(s.sb.work, "listener.json")
var listener *Proc
err := WriteProxyConfig(s, f, proxy.Config{VM: "e2e-listener", Listen: "127.0.0.1:" + port, Password: redact.NewSecret("listener-key")})
if err == nil {
listener, err = s.Start(Cmd{Args: []string{s.sb.devvm, "_proxy", "-config", f}})
}
if err == nil {
err = listener.listening(s.r.window().ctx, "127.0.0.1:"+port, 15*time.Second)
}
s.Check("the host's loopback services are unreachable, directly or through the proxy", func() error {
if err != nil {
return fmt.Errorf("the listener: %w", err)
}
code, _, gerr := web{}.get(s.r.window().ctx, "http://127.0.0.1:"+port+"/")
switch {
case gerr != nil || code != 407:
return failed(fmt.Sprintf("the listener answered %d, not 407", code), gerr)
case func() bool {
_, e := vmRun(s, n.vm, "curl", "--noproxy", "*", "-sS", "-m", "5", "-o", "/dev/null", "http://192.168.5.2:"+port+"/")
return e == nil
}():
return errors.New("the VM reached it directly")
case vmOut(s, n.vm, "curl -s -m 10 -o /dev/null -w %{http_code} http://192.168.5.2:"+port+"/") != "403":
return errors.New("the VM's proxy didn't refuse 192.168.5.2")
case vmOut(s, n.vm, `curl -s -m 10 -o /dev/null -w %{http_code} --noproxy "" -x "$HTTP_PROXY" http://127.0.0.1:`+port+"/") != "403":
return errors.New("the VM's proxy didn't refuse 127.0.0.1")
}
return nil
})
if listener != nil {
listener.Stop()
}
}
// A company proxy: the VM's next launch with HTTPS_PROXY and HTTP_PROXY
// in devvm's environment starts its proxy again through it, which sends
// an allowed host there by name, and refuses one the VM's policy doesn't
// allow before it gets there. Then config.toml's [proxy] names the same
// company proxy, with its user alone, and the shell sets none: the launch
// is refused until the proxy secret holds the password, and once it does,
// starts the VM's proxy again through it (corpConfig). The launch after,
// with neither, starts it again going directly.
func netCorp(n *netRun) {
s := n.s
corp, cerr := StartCorp(s)
if cerr == nil {
n.corps = append(n.corps, corp.Password())
}
s.Check("behind a company proxy, an allowed host goes through it by name, and a blocked one never reaches it", func() error {
if cerr != nil {
return cerr
}
r, err := n.dv("exit\n", n.home(append([]string{fakeToken}, corp.Env()...)...))
return n.corpThrough(corp, r, err)
}, Tail(60))
s.Check("the company proxy's password is in no process's arguments, log or VM", func() error {
if cerr != nil {
return cerr
}
return corpNoLeak(n, corp, false)
})
s.Check("config.toml's [proxy] with its user alone: the launch is refused until devvm secret set proxy saves the password, then goes through the company proxy with it", func() error {
if cerr != nil {
return cerr
}
return corpConfig(n, corp)
}, Tail(60))
s.Check("... and that password, which the VM's proxy looked up, is in no process's arguments, log, proxy config or VM", func() error {
if cerr != nil {
return cerr
}
return corpNoLeak(n, corp, true)
})
s.Check("without either, the next launch starts the VM's proxy again, going directly", func() error {
if cerr != nil {
return cerr
}
return n.corpDirect(corp)
}, Tail(60))
if corp != nil {
corp.Stop()
}
s.Check("the proxy refuses anything on the host without the VM's password", func() error {
code, _, err := web{proxy: proxyURL(n.pp, "")}.get(s.r.window().ctx, "http://devvm.proxy/blocked")
if err != nil || code != 407 {
return failed(fmt.Sprintf("it answered %d, not 407", code), err)
}
return nil
})
s.Check("the token never reaches the VM", func() error {
if err := GuestHasNone(s, n.vm, []string{"e2efake"}, "/etc", "/home", "/root"); err != nil {
return err
}
if has, err := vmHas(s, n.vm, true, "env", "e2efake"); err != nil || has {
return failed("the guest's environment holds the token", err)
}
return nil
})
}
// corpUpstream is whether the VM's proxy runs, its config naming a
// company proxy.
func (n *netRun) corpUpstream() bool {
return proxyRuns(n.s, n.pxc) && Matches(readText(n.pxc), `^ "upstream": \{`)
}
// corpNoLeak: the company proxy's password is in no process's arguments,
// no log of devvm's or the VM's, and nowhere in the guest, its files or
// its environment; with fromConfig, config.toml's proxy having started the
// VM's proxy, not in that proxy's config either, which holds the
// environment's URL, password and all, otherwise.
func corpNoLeak(n *netRun, corp *Corp, fromConfig bool) error {
s := n.s
pw := []string{corp.Password()}
if have, err := ProcsHave(s, pw[0]); err != nil || have {
return failed("a process's arguments hold it", err)
}
files := []string{filepath.Join(s.Paths.Devvm, "logs"), filepath.Join(n.vmd, "logs")}
if fromConfig {
files = append(files, n.pxc)
}
if err := NoLeak(pw, files...); err != nil {
return err
}
if err := GuestHasNone(s, n.vm, pw, "/etc", "/home", "/root"); err != nil {
return err
}
if has, err := vmHas(s, n.vm, true, "env", pw...); err != nil || has {
return failed("the guest's environment holds it", err)
}
return nil
}
// corpConfig: config.toml's [proxy] names the company proxy as
// http://devvm@ADDR, the user alone, and devvm runs with no proxy
// settings. With no proxy secret saved, the launch is refused
// (proxy_password_missing), the VM left running if it ran; once devvm secret set proxy saves the
// password, read on its input, the launch starts the VM's proxy again
// through the company proxy, its config naming the secret and holding no
// password, and the company proxy, which refuses any request without that
// password, carries github.com. config.toml is put back and the secret
// removed after.
func corpConfig(n *netRun, corp *Corp) error {
s := n.s
cfg := filepath.Join(s.sb.config(), "devvm", "config.toml")
saved, err := os.ReadFile(cfg)
if err != nil {
return err
}
// Saved and deleted with the VM's HOME, as netCustomSecret's are: the
// proxy, which that HOME's launch starts, reads the Keychain with it.
defer func() {
_ = os.WriteFile(cfg, saved, 0o600)
_, _ = s.Run(devvmCmd(s, "", "", n.home(), "secret", "rm", "--force", proxy.UpstreamSecret))
}()
u := strconv.Quote("http://" + proxy.ProxyUser + "@" + corp.Addr())
if err := os.WriteFile(cfg, append(bytes.Clone(saved), []byte("\n[proxy]\nhttps = "+u+"\nhttp = "+u+"\n")...), 0o600); err != nil {
return err
}
launch := func() (Res, error) {
return s.Run(Cmd{Args: unproxied(s.sb.devvm), Dir: n.n, Stdin: "exit\n", Env: n.home(fakeToken, "DEVVM_MESSAGES=names")})
}
was := s.Status(n.vm)
if r, err := launch(); err == nil || !Matches(r.Out, `^devvm: proxy_password_missing`) {
return failed("with no proxy secret saved, the launch wasn't refused for it:\n"+tailLines(r.Out, 20), err)
}
// Refused before anything stops: a running VM runs on.
if now := s.Status(n.vm); now != was {
return fmt.Errorf("the refused launch left the VM %s, it was %s", now, was)
}
if _, err := s.Run(devvmCmd(s, "", corp.Password()+"\n", n.home(), "secret", "set", proxy.UpstreamSecret, "--stdin")); err != nil {
return fmt.Errorf("devvm secret set proxy: %w", err)
}
r, err := launch()
if err == nil && !Matches(readText(n.pxc), `"secret": "`+proxy.UpstreamSecret+`"`) {
return errors.New("the VM's proxy config doesn't name the proxy secret\n" + n.corpWhy(corp, r))
}
return n.corpThrough(corp, r, err)
}
// corpThrough: the launch r, err started the VM's proxy again through the
// company proxy, which carries an allowed host by name and never sees a
// blocked one.
func (n *netRun) corpThrough(corp *Corp, r Res, err error) error {
s := n.s
// Only the events from here on: an earlier launch's are there already.
corpSaw, vmSaw := eventsFrom(corp.Events), eventsFrom(n.events)
what := ""
switch {
case err != nil:
what = "the launch"
case !n.corpUpstream():
what = "the VM's proxy doesn't go upstream"
case inVM(s, n.vm, "curl -fsS -m 30 -o /dev/null https://github.com") != nil:
what = "github.com in the VM"
case !corpSaw(`"event":"tunnel","host":"github.com","port":443`):
what = "the company proxy didn't carry github.com"
case !vmSaw(`"host":"github.com".*"upstream":"` + regexp.QuoteMeta(corp.Addr()) + `"`):
what = "the VM's events show no upstream for github.com"
case !func() bool {
o, _ := s.InVM(n.vm, "curl -sS -m 20 -o /dev/null https://example.com")
return strings.Contains(o.Out, "403")
}():
what = "example.com wasn't refused"
case corpSaw(`"host":"example.com"`):
what = "example.com reached the company proxy"
default:
return nil
}
return errors.New(what + "\n" + n.corpWhy(corp, r))
}
// eventsFrom is whether the regular expression re matches an event that
// the event log f got after the call: the bytes past its size then, or
// all of it once it's smaller, rotated since.
func eventsFrom(f string) func(re string) bool {
n0 := len(readText(f))
return func(re string) bool {
t := readText(f)
if len(t) >= n0 {
t = t[n0:]
}
return Matches(t, re)
}
}
func (n *netRun) corpDirect(corp *Corp) error {
s := n.s
r, err := s.Run(Cmd{Args: unproxied(s.sb.devvm), Dir: n.n, Stdin: "exit\n", Env: n.home(fakeToken)})
what := func() string {
if err != nil {
return "the launch"
}
if n.corpUpstream() {
return "the VM's proxy still goes upstream"
}
n0 := strings.Count(readText(corp.Events), "\n")
if inVM(s, n.vm, "curl -fsS -m 30 -o /dev/null https://github.com") != nil {
return "github.com in the VM"
}
if strings.Count(readText(corp.Events), "\n") != n0 {
return "the company proxy saw it"
}
return ""
}()
if what == "" {
return nil
}
return errors.New(what + "\n" + n.corpWhy(corp, r))
}
// unproxied is args with no proxy settings in its environment: env -u
// each of them, then args.
func unproxied(args ...string) []string {
return append([]string{"env", "-u", "HTTPS_PROXY", "-u", "HTTP_PROXY", "-u", "https_proxy", "-u", "http_proxy"}, args...)
}
// corpWhy is what a company proxy check's failure shows: the VM's status
// and proxy, the launch's output, the end of the VM's proxy's log and
// events, and the company proxy's own.
func (n *netRun) corpWhy(corp *Corp, launch Res) string {
s := n.s
var b strings.Builder
args := ""
if p := pids(s, "[_]proxy -config "+n.pxc); len(p) > 0 {
r, _ := s.Run(Cmd{Args: []string{"ps", "-ww", "-o", "command=", "-p", strings.Join(p, ",")}})
for l := range strings.Lines(r.Stdout) {
args += " " + strings.TrimSpace(l)[:min(60, len(strings.TrimSpace(l)))]
}
}
fmt.Fprintf(&b, "the VM: %s; its proxy:%s\n", s.Status(n.vm), args)
fmt.Fprintf(&b, "the launch:\n%s\n", tailLines(launch.Out, 20))
fmt.Fprintf(&b, "the proxy log:\n%s\n", tailLines(readText(filepath.Join(n.vmd, "logs", "proxy.log")), 10))
fmt.Fprintf(&b, "its events:\n%s\n", tailLines(readText(n.events), 5))
fmt.Fprintf(&b, "the company proxy's log (it runs: %v):\n%s\n", corp.Running(), tailLines(corp.Log(), 5))
fmt.Fprintf(&b, "its events:\n%s", tailLines(readText(corp.Events), 5))
return b.String()
}
// A second VM with network policy, small and with no agents, in the same
// home: each VM has CAs of its own, the current one and the next, which
// its launch installs; its proxy presents only its own, and it trusts no
// other VM's. A certificate's second PEM line holds its serial, and a
// key's its secret, so grep finds that certificate or key in any file
// that holds it.
func netCAs(n *netRun) {
s := n.s
n2, _ := s.NewProject("net2")
vm2, _, _ := launchWith(s, n2, n.home(), "--network", "allowlist", "--no-git-protect", "--agents", "none", "--cpus", "2", "--memory", "2", "--ports", "none")
ca1, ca2 := filepath.Join(n.vmd, "run", "ca"), filepath.Join(s.Paths.VMDir(vm2), "run", "ca")
st2 := s.Paths.State(vm2)
at := func(d, f string) string { return filepath.Join(d, f) }
s.Check("each VM's launch installs its own CA and the next one, which only the host keeps the keys of", func() error {
if vm2 == "" || readText(at(ca1, "ca.pem")) == readText(at(ca2, "ca.pem")) || readText(at(ca1, "next.pem")) == readText(at(ca2, "next.pem")) {
return errors.New("no second VM, or the two VMs share a CA")
}
for _, c := range []struct{ vm, dir string }{{n.vm, ca1}, {vm2, ca2}} {
for crt, pem := range map[string]string{"devvm-proxy.crt": "ca.pem", "devvm-proxy-next.crt": "next.pem"} {
r, err := vmRun(s, c.vm, "cat", "/usr/local/share/ca-certificates/"+crt)
if err != nil || r.Stdout != readText(at(c.dir, pem)) {
return fmt.Errorf("%s's %s isn't its %s", c.vm, crt, pem)
}
}
}
if err := GuestHasNone(s, n.vm, []string{pemLine(at(ca1, "ca.key"), 2), pemLine(at(ca1, "next.key"), 2)}, "/etc", "/home", "/root", "/usr/local"); err != nil {
return err
}
if modeOf(at(ca1, "ca.key")) != "600" || modeOf(at(ca1, "next.key")) != "600" {
return errors.New("the CA keys aren't mode 600")
}
return nil
})
s.Check("a VM trusts no other VM's CA", func() error {
bundle := []string{"/etc/ssl", "/usr/local/share/ca-certificates"}
if err := GuestHasNone(s, vm2, []string{pemLine(at(ca1, "ca.pem"), 2), pemLine(at(ca1, "next.pem"), 2)}, bundle...); err != nil {
return err
}
if err := GuestHasNone(s, n.vm, []string{pemLine(at(ca2, "ca.pem"), 2), pemLine(at(ca2, "next.pem"), 2)}, bundle...); err != nil {
return err
}
_, err := s.Run(Cmd{
Args: []string{"limactl", "shell", "--workdir", "/", n.vm, "sudo", "grep", "-qsF", "-f", "/dev/stdin", "/etc/ssl/devvm.pem"},
Stdin: pemLine(at(ca1, "next.pem"), 2) + "\n",
})
if err != nil {
return errors.New("the VM's /etc/ssl/devvm.pem doesn't hold its next CA")
}
return nil
})
// The proxies' passwords stay in this process: in no command's
// arguments.
s.Check("each VM's proxy serves its own CAs, and presents its own CA's certificates, which the other VM's CAs don't verify", func() error {
ctx := s.r.window().ctx
p1, p2 := proxyURL(n.pp, n.key()), proxyURL(st2["ports.host.proxy"], st2["net.made.proxy_key"])
for _, c := range []struct {
p web
dir string
}{{web{proxy: p1}, ca1}, {web{proxy: p2}, ca2}} {
code, b, err := c.p.get(ctx, "http://devvm.proxy/ca.pem")
if err != nil || code != 200 || string(b) != readText(at(c.dir, "ca.pem"))+readText(at(c.dir, "next.pem")) {
return failed(fmt.Sprintf("a proxy's ca.pem isn't its VM's CAs (%d)", code), err)
}
}
if _, _, err := (web{proxy: p1, roots: rootsOf(at(ca1, "ca.pem"), at(ca1, "next.pem"))}).get(ctx, "https://api.anthropic.com/devvm-e2e-ca"); err != nil {
return fmt.Errorf("the VM's own CAs don't verify its proxy: %w", err)
}
if _, _, err := (web{proxy: p1, roots: rootsOf(at(ca2, "ca.pem"), at(ca2, "next.pem"))}).get(ctx, "https://api.anthropic.com/devvm-e2e-ca"); !unverified(err) {
return failed("the other VM's CAs verified its proxy, or something else failed", err)
}
return nil
})
s.Check("devvm rm deletes a VM's CA with its folder, and leaves the other VM's", func() error {
if _, err := s.Run(devvmCmd(s, n2, "", n.home(), "rm", "--force", "--no-export")); err != nil {
return err
}
if Exists(ca2) || readText(at(ca1, "ca.key")) == "" {
return errors.New("the removed VM's CA is still there, or the other VM's is gone")
}
return nil
})
}
func netTokens(n *netRun) {
s, vm := n.s, n.vm
s.Check("the VM gets its own placeholder, not the token", func() error {
return inVM(s, vm, "[[ $CLAUDE_CODE_OAUTH_TOKEN == sk-ant-oat01-devvm-proxy-managed-"+n.salt+" ]]")
})
s.Check("the VM's Codex and Claude Code (MCP) logins hold only placeholders", func() error {
r, _ := vmRun(s, vm, "bash", "-c", "cat ~/.codex/auth.json ~/.claude.json")
c, _ := vmRun(s, vm, "bash", "-c", "cat ~/.codex/auth.json")
if strings.Contains(r.Stdout, "e2efake") || !strings.Contains(c.Stdout, "devvm-proxy-managed") {
return errors.New("a login holds a token, or Codex's no placeholder")
}
return nil
})
s.Check("the VM's Codex reads its copy of the login, the fields devvm doesn't know left out", func() error {
// An API-key login whose key is devvm's placeholder: Codex 0.167.0
// shows the key's start ("Logged in using an API key - sk-devvm***…"),
// where earlier releases said "API key configured" alone.
r, err := s.InVM(vm, "codex login status")
if err != nil || !strings.Contains(r.Out, "API key configured") && !Matches(r.Out, `^Logged in using an API key - sk-devvm`) {
return failed("codex login status:\n"+r.Out, err)
}
return nil
})
// Requests verify the proxy's certificates: no -k.
s.Check("the proxy puts the token in for its host", func() error {
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.anthropic.com/v1/models -H "Authorization: Bearer $CLAUDE_CODE_OAUTH_TOKEN"`); err != nil {
return err
}
if !lineWith(readText(n.events), `"path":"/v1/models"`, `"secrets":["claude"]`) {
return errors.New("no event shows the claude secret put in")
}
return nil
})
s.Check("but not for another VM's placeholder", func() error {
other := "sk-ant-oat01-devvm-proxy-managed-" + strings.Repeat("0", 32)
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.anthropic.com/devvm-e2e-other -H "Authorization: Bearer `+other+`"`); err != nil {
return err
}
ev := readText(n.events)
if !strings.Contains(ev, "/devvm-e2e-other") || lineWith(ev, "/devvm-e2e-other", "secrets") {
return errors.New("no event for the request, or one with a secret put in")
}
return nil
})
s.Check("and an MCP server's header for its host, from the VM's copy of the login", func() error {
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.github.com/devvm-e2e-mcp -H "X-Devvm-E2e: $(jq -r '.mcpServers.e2e.headers["X-Devvm-E2e"]' ~/.claude.json)"`); err != nil {
return err
}
if !lineWith(readText(n.events), "/devvm-e2e-mcp", `"secrets":["claude-mcp-`) {
return errors.New("no event shows the MCP header put in")
}
return nil
})
s.Check("and Codex's API key, from the VM's copy of its login", func() error {
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.openai.com/v1/models -H "Authorization: Bearer $(jq -r .OPENAI_API_KEY ~/.codex/auth.json)"`); err != nil {
return err
}
if !lineWith(readText(n.events), `"path":"/v1/models"`, `"secrets":["codex-key"]`) {
return errors.New("no event shows Codex's key put in")
}
return nil
})
s.Check("the home folder's and the store's login files are unchanged", func() error {
if readText(filepath.Join(n.nh, ".codex", "auth.json")) != n.before["codex"] || readText(filepath.Join(n.nh, ".claude.json")) != n.before["claude"] ||
readText(filepath.Join(n.logins, "codex", ".codex", "auth.json")) != n.before["codex"] {
return errors.New("a login file changed")
}
return nil
})
}
// A login made in the VM, captured by its proxy. OpenAI's token endpoint
// is faked by a company proxy of this run's that answers a CONNECT to
// auth.openai.com itself, with a certificate from the run's CA, which the
// launch puts among your CAs, so the VM's proxy trusts it: fake tokens
// for a code (each holding e2efake, which no file in the VM may), and a
// fake API key for an exchange of its fake id_token alone. The VM's
// requests are curl's, as Codex sends them.
func netCapture(n *netRun) {
s := n.s
terr := netTokenServer(n)
s.Check("a login made in the VM is captured: the store gets the real tokens, the VM placeholders and no refresh token, and nothing is logged", func() error {
if terr != nil {
return fmt.Errorf("the fake token endpoints: %w", terr)
}
if what := n.captured(); what != "" {
return errors.New(what + "\n" + n.tokenWhy())
}
return nil
}, Tail(60))
}
// A secret of yours, its host faked by the fake token endpoints' company
// proxy (customHost), which logs the headers each request arrives with.
const (
// cursorAPIHost is Cursor's API, which the fake company proxy stands in for, answering its
// login poll as Cursor does.
cursorAPIHost = "api2.cursor.sh"
customName = "e2e-custom"
customHost = "api.devvm-e2e-custom.dev"
customValue = "sk_e2e_e2efake-custom-value"
// A second secret of yours, in Authorization below /v1/ alone, for GET
// and HEAD alone.
basicName = "e2e-basic"
basicValue = "e2efake-basic-value"
)
// netCustomSecret: two secrets of yours, saved on stdin with their hosts,
// headers, variables, prefix, path and methods, and used by the VM, which
// sees only its own placeholders in the variables. Their host gets the real
// values in their headers, in the shapes the proxy fills in, and below the
// path for those methods alone; another host, another path, another method
// and an echoed shape, Basic's username, get only the placeholder, and no
// log holds a value.
func netCustomSecret(n *netRun) {
s, vm := n.s, n.vm
s.Check("a secret of yours reaches its host alone, in its header, and the VM holds only its placeholder", func() error {
if n.tok == nil {
return errors.New("the fake endpoints didn't start")
}
// Saved and deleted with the VM's HOME, as netGithub's token is: the
// proxy, which that HOME's launch started, reads the Keychain with it.
defer func() {
for _, name := range []string{customName, basicName} {
_, _ = s.Run(devvmCmd(s, "", "", n.home(), "secret", "rm", "--force", name))
}
}()
for _, set := range [][]string{
{customValue, customName, "--header", "x-api-key", "--env", "E2E_CUSTOM_KEY", "--prefix", "sk_e2e_"},
{basicValue, basicName, "--env", "E2E_BASIC_KEY", "--path", "/v1/", "--method", "GET,HEAD"},
} {
args := append([]string{"secret", "set", set[1], "--host", customHost, "--stdin"}, set[2:]...)
if _, err := s.Run(devvmCmd(s, "", set[0]+"\n", n.home(), args...)); err != nil {
return fmt.Errorf("devvm secret set %s: %w", set[1], err)
}
if _, err := n.dv("", n.tokenEnv(), "secret", "use", set[1]); err != nil {
return fmt.Errorf("devvm secret use %s: %w", set[1], err)
}
}
// The variables come with the next launch.
if _, err := n.dv("exit\n", n.tokenEnv()); err != nil {
return fmt.Errorf("the launch: %w", err)
}
if err := n.customChecks(vm); err != nil {
return err
}
return basicChecks(n, vm)
}, Tail(40))
}
// rulesPath is what netRequestRules' rule narrows customHost to.
const rulesPath = "/devvm-e2e-rules/"
// netRequestRules: a rule by method and path for customHost, which the fake
// endpoints' company proxy fakes and no other rule allows once
// netCustomSecret's secrets are gone. The VM's proxy decrypts the host to
// check each request: a GET below the path reaches the host, while a POST
// to the same path, a GET elsewhere and a GET that names POST in an
// override header or a _method parameter get the proxy's 403, never reach
// the host, and are logged as blocked with their method and path, which devvm
// network log shows.
func netRequestRules(n *netRun) {
s, vm := n.s, n.vm
s.Check("a rule by method and path lets a GET through and blocks a POST to the same host, logged", func() error {
if n.tok == nil {
return errors.New("the fake endpoints didn't start")
}
rule := []string{customHost, "--method", "GET", "--path", rulesPath}
defer func() { _, _ = n.dv("", n.tokenEnv(), append([]string{"network", "rm"}, rule...)...) }()
added, err := n.dv("", n.tokenEnv(), append([]string{"network", "allow"}, rule...)...)
if err != nil {
return fmt.Errorf("devvm network allow: %w", err)
}
Note(s, "devvm network allow %s:\n%s", strings.Join(rule, " "), added.Out)
if _, err := n.dv("exit\n", n.tokenEnv()); err != nil {
return fmt.Errorf("the launch: %w", err)
}
for _, req := range []struct{ method, path, header, code string }{
{"GET", rulesPath + "get", "", "404"}, // the fake host's answer
{"POST", rulesPath + "post", "", "403"},
{"GET", "/devvm-e2e-other", "", "403"},
// A GET that names another method, as some servers read it, isn't the GET it allows.
{"GET", rulesPath + "override", "X_HTTP_METHOD_OVERRIDE: POST", "403"},
{"GET", rulesPath + "override?_method=POST", "", "403"},
{"GET", rulesPath + "get-again", "", "404"},
} {
h := ""
if req.header != "" {
h = ` -H '` + req.header + `'`
}
r, err := s.InVM(vm, `curl -sS -m 20 -o /dev/null -w '%{http_code}' -X `+req.method+h+` 'https://`+customHost+req.path+`'`)
if got := strings.TrimSpace(r.Stdout); err != nil || got != req.code {
return fmt.Errorf("%s %s: %q; want %s (%w)", req.method, req.path, got, req.code, err)
}
}
got, events := readText(n.tokLog), readText(n.events)
switch {
case !lineWith(got, "api "+rulesPath+"get ") || !lineWith(got, "api "+rulesPath+"get-again "):
return errors.New("the GETs didn't reach the host:\n" + got)
case strings.Contains(got, rulesPath+"post") || strings.Contains(got, rulesPath+"override") || strings.Contains(got, "/devvm-e2e-other"):
return errors.New("a blocked request reached the host:\n" + got)
case !lineWith(events, `"event":"blocked"`, `"method":"POST"`, `"path":"`+rulesPath+`post"`):
return errors.New("no blocked event for the POST")
case !lineWith(events, `"event":"forward"`, `"path":"`+rulesPath+`get"`, `"rule":"`+customHost+` GET `+rulesPath+`"`):
return errors.New("no event names the rule that let the GET through")
}
page, _ := s.InVM(vm, `curl -sS -m 20 -X POST https://`+customHost+rulesPath+`post`)
r, err := n.dv("", n.tokenEnv(), "network", "log", "-n", "20")
if err != nil || !lineWith(r.Stdout, "BLOCKED", customHost+":443", "POST "+rulesPath+"post: ") {
return fmt.Errorf("devvm network log doesn't show the POST blocked (%w):\n%s", err, r.Stdout)
}
Note(s, "the POST's answer in the VM:\n%s\ndevvm network log:\n%s", page.Stdout, r.Stdout)
return nil
}, Tail(40))
}
// basicChecks are netCustomSecret's checks of the secret in Authorization
// below /v1/: Basic's password and Bearer get the value there, and Basic's
// username, which servers echo, Bearer on another path, keep the
// placeholder.
func basicChecks(n *netRun, vm string) error {
s := n.s
ph := "devvm-proxy-managed-secret-" + basicName + "-" + n.salt
b64 := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }
for _, req := range []string{
`-u "x-access-token:$E2E_BASIC_KEY" https://` + customHost + `/v1/basic-pass`,
`-u "$E2E_BASIC_KEY:x" https://` + customHost + `/v1/basic-user`,
`-H "Authorization: Bearer $E2E_BASIC_KEY" https://` + customHost + `/v1/bearer`,
`-H "Authorization: Bearer $E2E_BASIC_KEY" https://` + customHost + `/other/bearer`,
`-H "Authorization: Bearer x$E2E_BASIC_KEY" https://` + customHost + `/v1/framed`,
`-X POST -H "Authorization: Bearer $E2E_BASIC_KEY" https://` + customHost + `/v1/post`,
} {
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null `+req); err != nil {
return fmt.Errorf("curl %s: %w", req, err)
}
}
got := readText(n.tokLog)
for path, want := range map[string]string{
"/v1/basic-pass": "Basic " + b64("x-access-token:"+basicValue),
"/v1/basic-user": "Basic " + b64(ph+":x"),
"/v1/bearer": "Bearer " + basicValue,
"/other/bearer": "Bearer " + ph,
"/v1/framed": "Bearer x" + ph,
"/v1/post": "Bearer " + ph, // a method the secret doesn't go in for
} {
if !lineWith(got, "api "+path+" "+want) {
return fmt.Errorf("%s didn't arrive with %q:\n%s", path, want, got)
}
}
return NoLeak([]string{basicValue}, filepath.Join(n.vmd, "logs"), filepath.Join(n.vmd, "run"),
filepath.Join(s.Paths.Devvm, "logs"), filepath.Join(s.Paths.Devvm, "custom-secrets.json"))
}
// customChecks are netCustomSecret's checks once the VM uses the secret:
// its first that failed, nil when none.
func (n *netRun) customChecks(vm string) error {
s := n.s
ph := "sk_e2e_devvm-proxy-managed-secret-" + customName + "-" + n.salt
if err := inVM(s, vm, `[[ $E2E_CUSTOM_KEY == `+ph+` ]] && ! grep -rqF e2efake-custom ~/.config/devvm /etc/environment`); err != nil {
return errors.New("the VM's variable isn't its placeholder alone, or the value is in the VM")
}
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://`+customHost+`/devvm-e2e-custom -H "X-Api-Key: $E2E_CUSTOM_KEY"`); err != nil {
return fmt.Errorf("the request to its host: %w", err)
}
if err := inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.anthropic.com/devvm-e2e-custom-other -H "X-Api-Key: $E2E_CUSTOM_KEY"`); err != nil {
return fmt.Errorf("the request to another host: %w", err)
}
got := readText(n.tokLog)
switch {
case !lineWith(got, "api /devvm-e2e-custom ", "x-api-key "+customValue):
return errors.New("its host didn't get the value in X-Api-Key:\n" + got)
case !lineWith(got, "api /devvm-e2e-custom-other ", "x-api-key "+ph) || lineWith(got, "/devvm-e2e-custom-other", customValue):
return errors.New("another host got more than the placeholder:\n" + got)
case !lineWith(readText(n.events), `"path":"/devvm-e2e-custom"`, `"secrets":["`+customName+`"]`):
return errors.New("no event shows the secret put in")
}
return NoLeak([]string{customValue}, filepath.Join(n.vmd, "logs"), filepath.Join(n.vmd, "run"),
filepath.Join(s.Paths.Devvm, "logs"), filepath.Join(s.Paths.Devvm, "custom-secrets.json"))
}
// netTokenServer starts the fake token endpoints (fakeTokenPy) on slot 0's
// port 47199 as the slot has it, with a certificate the run's CA signed,
// which devvm's CA folder holds until they stop.
func netTokenServer(n *netRun) error {
s := n.s
ca, err := NewCA(s)
if err != nil {
return err
}
cf, kf, err := ca.Sign("auth", "auth.openai.com", "platform.claude.com", "api.anthropic.com", "mcp-proxy.anthropic.com", "api.groq.com", "api.openai.com", geminiAPIHost, "openrouter.ai", customHost, cursorAPIHost)
if err != nil {
return err
}
if err := ca.Trust(); err != nil {
return err
}
n.tokLog = filepath.Join(s.sb.work, "token.log")
madeFile(s.sb, n.tokLog)
if err := os.WriteFile(n.tokLog, nil, 0o600); err != nil {
return err
}
port := strconv.Itoa(s.Port(47199))
n.tokURL = "http://127.0.0.1:" + port
if n.tok, err = s.Start(Cmd{Args: []string{"python3", "-I", "-c", fakeTokenPy, port, cf, kf, n.tokLog}}); err != nil {
return err
}
return n.tok.listening(s.r.window().ctx, "127.0.0.1:"+port, 15*time.Second)
}
// tokenEnv is the VM's HOME and the fake endpoints as devvm's company
// proxy, with env after them. Without a Claude Code token in devvm's
// environment (the sandbox's has none), which the VM's proxy inherits and
// which would win over the Claude Code logins it captures.
func (n *netRun) tokenEnv(env ...string) []string {
return n.home(append([]string{"HTTPS_PROXY=" + n.tokURL, "HTTP_PROXY=" + n.tokURL}, env...)...)
}
// tokenWhy is what the capture's checks look at, when one fails.
func (n *netRun) tokenWhy() string {
out := ""
if n.tok != nil {
out = tailLines(n.tok.Out(), 5)
}
var auth []string
for l := range strings.Lines(readText(n.events)) {
if strings.Contains(l, "auth.openai.com") {
auth = append(auth, strings.TrimRight(l, "\n"))
}
}
return "the fake endpoint got:\n" + readText(n.tokLog) + out +
"\nthe proxy log:\n" + tailLines(readText(filepath.Join(n.vmd, "logs", "proxy.log")), 10) +
"\nits events:\n" + strings.Join(auth[max(0, len(auth)-5):], "\n")
}
// codexCode is the code Codex sends OpenAI's token endpoint.
const codexCode = "grant_type=authorization_code&code=e2e-code&client_id=app_EMoamEEZ73f0CkXaXp7hrann&code_verifier=e2e&redirect_uri=http%3A%2F%2Flocalhost%3A1455%2Fauth%2Fcallback"
// tokenPost is the VM's curl of OpenAI's token endpoint with body.
func tokenPost(body string) string {
return "curl -fsS -m 30 https://auth.openai.com/oauth/token -d '" + body + "'"
}
// captured is the capture's first step that failed, nothing when none.
func (n *netRun) captured() string {
s := n.s
if _, err := n.dv("exit\n", n.tokenEnv()); err != nil {
return "the launch through the fake endpoint"
}
r, err := s.InVM(n.vm, tokenPost(codexCode))
if err != nil {
return "the code's reply"
}
n.cap1 = r.Stdout
c := []byte(n.cap1)
if strings.Contains(n.cap1, "e2efake") || jsonAt(c, "refresh_token") != "devvm-proxy-managed" ||
!strings.HasSuffix(jsonAt(c, "access_token"), ".devvm-placeholder") || !strings.HasSuffix(jsonAt(c, "id_token"), ".devvm-placeholder") {
return "the reply holds placeholders, and no refresh token"
}
a := filepath.Join(n.logins, "codex", ".codex", "auth.json")
if jsonFileAt(a, "tokens.refresh_token") != "rt-e2efake-capture" || jsonFileAt(a, "tokens.account_id") != "acct-e2e" ||
!strings.Contains(jsonFileAt(a, "tokens.access_token"), "e2efake") || modeOf(a) != "600" {
return "the store has the real tokens, only yours to read"
}
ex := "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&client_id=app_EMoamEEZ73f0CkXaXp7hrann&requested_token=openai-api-key&subject_token=" +
jsonAt(c, "id_token") + "&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aid_token"
if r, err = s.InVM(n.vm, tokenPost(ex)); err != nil {
return "the API-key exchange of the VM's id_token"
}
if jsonAt([]byte(r.Stdout), "access_token") != "sk-devvm-proxy-managed-codex-"+n.salt || jsonFileAt(a, "OPENAI_API_KEY") != "sk-e2efake-captured-key" {
return "the key is the VM's placeholder, the store's the real one"
}
if sec := readText(filepath.Join(n.vmd, "run", "secrets.json")); !strings.Contains(sec, `"name":"codex"`) || !strings.Contains(sec, `"name":"codex-key"`) {
return "the proxy binds the captured login at once"
}
login := `"event":"login","host":"auth.openai.com","port":443,"method":"POST","path":"/oauth/token","login":"codex"`
if countWith(readText(n.events), login) < 2 ||
NoLeak([]string{"rt-e2efake-capture", "sk-e2efake-captured-key", "e2efake-sig"}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs")) != nil {
return "the events name the logins, and no log holds a value"
}
return ""
}
// devvm agent login codex: the real Codex logs in in the VM, and the
// browser's callback comes back through the forward of this machine's
// localhost:1455, which a request of this process stands in for here,
// with the state Codex's address names; the fake endpoint answers its
// code and its API-key exchange. The forward lasts only while the login
// runs, interrupted too.
func netCodexLogin(n *netRun) {
s := n.s
s.Check("devvm agent login codex logs the VM's Codex in through the forward of localhost:1455, and the store saves the login", func() error {
p, err := s.Start(devvmCmd(s, n.n, "", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "codex"))
if err != nil {
return err
}
if what := n.codexLoggedIn(p); what != "" {
p.Stop()
return errors.New(what + "\n" + n.loginWhy(p))
}
return nil
}, Tail(60))
s.Check("an interrupted devvm agent login takes its forward away", func() error {
p, err := s.Start(devvmCmd(s, n.n, "", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "codex"))
if err != nil {
return err
}
ctx := s.r.window().ctx
what := func() string {
if loginState(s, p) == "" {
return "Codex shows its address"
}
if forwardGone(ctx) {
return "the forward listens"
}
if err := p.cmd.Process.Signal(os.Interrupt); err != nil {
return "the interrupt: " + err.Error()
}
if rc, ok := waitProc(p, time.Minute); !ok || rc != 130 {
return fmt.Sprintf("devvm agent login exited %d, not 130", rc)
}
if !forwardGone(ctx) {
return "the forward is gone"
}
return ""
}()
if what != "" {
p.Stop()
return errors.New(what + "\n" + n.loginWhy(p))
}
return nil
})
}
// devvm agent login opencode --device-code: the real opencode 2 logs in
// in the VM with its Codex device code login, the fake endpoint
// answering its code and the code's exchange, whose reply the VM's proxy
// captures, so opencode's database in the VM holds placeholders alone.
func netOpencodeLogin(n *netRun) {
s := n.s
s.Check("devvm agent login opencode --device-code logs opencode 2 in in the VM, and the store saves the login", func() error {
p, err := s.Start(devvmCmd(s, n.n, "", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "opencode", "--device-code"))
if err != nil {
return err
}
if what := n.opencodeLoggedIn(p); what != "" {
p.Stop()
return errors.New(what + "\n" + n.loginWhy(p))
}
return nil
}, Tail(60))
}
// devvm agent login opencode --api-key saves a key from the network VM;
// a second opencode 2 VM, whose database was made before, gets the key's
// placeholder at its next launch, beside a credential of its own that
// stays.
func netOpencodeKeys(n *netRun) {
s := n.s
oc, _ := s.NewProject("net-oc")
stored, planted := plantOpencodeService(s)
vm, lr, _ := launchWith(s, oc, n.home("DEVVM_MESSAGES=names"), "--network", "allowlist", "--no-git-protect", "--agents", "opencode", "--cpus", "2", "--memory", "2", "--ports", "none")
netOpencodeService(n, oc, vm, stored, planted, lr)
s.Check("opencode's self-update is off in an opencode VM's login shell", func() error {
if got := vmOut(s, vm, "printenv OPENCODE_DISABLE_AUTOUPDATE"); vm == "" || got != "1" {
return fmt.Errorf("OPENCODE_DISABLE_AUTOUPDATE is %q", got)
}
return nil
})
own := `[{"id":"cred_e2e_own","integrationID":"anthropic","label":"mine","active":true,"value":{"type":"key","key":"sk-ant-e2e-own"}}]`
s.Check("an opencode 2 VM whose database exists gets a key saved from another VM at its next launch", func() error {
switch {
case vm == "":
return errors.New("no second VM")
case inVM(s, vm, "opencode auth list >/dev/null && ls ~/.local/share/opencode/opencode*.db >/dev/null") != nil:
return errors.New("the second VM's opencode made no database")
case inVM(s, vm, "echo "+shell.Quote(own)+" | opencode auth import") != nil:
return errors.New("the second VM's opencode didn't import a credential of its own")
}
// The company proxy's environment, as the logins before: the save brings the VM up as it was.
if r, err := n.dv("sk-or-e2efake-saved\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "opencode", "--api-key", "--provider", "openrouter", "--stdin"); err != nil {
// The run log's end holds opencode.sh's stderr: the import's own, or the command that failed.
return failed("devvm agent login opencode --api-key failed:\n"+r.Out+"\n"+n.runLogEnd(r.Out), err)
}
switch has, err := vmHas(s, vm, true, "opencode auth export", "devvm-proxy-managed-opencode-openrouter"); {
case err != nil:
return failed("reading the second VM's opencode logins", err)
case has:
return errors.New("the second VM had the key before its launch")
}
if _, _, err := launchWith(s, oc, n.home()); err != nil {
return err
}
export := "opencode auth export openrouter"
if has, err := vmHas(s, vm, true, export, `"id": "cred_devvm_openrouter"`); err != nil || !has {
return errors.New("the second VM's opencode has no devvm entry for the key\n" + n.opencodeWhy(vm))
}
if has, err := vmHas(s, vm, true, export, "devvm-proxy-managed-opencode-openrouter"); err != nil || !has {
return errors.New("the second VM's opencode has no placeholder of the key")
}
if has, err := vmHas(s, vm, true, "opencode auth export", "e2efake"); err != nil || has {
return errors.New("the second VM's opencode holds the key itself")
}
if has, err := vmHas(s, vm, true, "opencode auth export anthropic", "sk-ant-e2e-own"); err != nil || !has {
return errors.New("the second VM's own credential is gone")
}
return nil
}, Tail(40))
s.Check("... and gets it again at its next launch once its database is made again", func() error {
// opencode's service keeps the old database open: it goes first, within 10 s.
remake := "pkill -x opencode; i=0; while pgrep -x opencode >/dev/null && [ $i -lt 20 ]; do sleep 0.5; i=$((i + 1)); done; " +
"! pgrep -x opencode >/dev/null && rm -f ~/.local/share/opencode/opencode.db* && opencode auth list >/dev/null"
if vm == "" || inVM(s, vm, remake) != nil {
return errors.New("the second VM's opencode didn't make its database again")
}
if has, err := vmHas(s, vm, true, "opencode auth export", "cred_devvm_openrouter"); err != nil || has {
return errors.New("the new database already holds devvm's entry")
}
if _, _, err := launchWith(s, oc, n.home()); err != nil {
return err
}
if has, err := vmHas(s, vm, true, "opencode auth export openrouter", "devvm-proxy-managed-opencode-openrouter"); err != nil || !has {
return errors.New("the new database has no placeholder of the key after the launch\n" + n.opencodeWhy(vm))
}
return nil
}, Tail(40))
netOpencodePassKeys(n, oc, vm)
netOpencodeWorktrees(n, oc, vm)
if vm != "" {
// Gone already, unless a check above failed.
_, _ = s.Run(devvmCmd(s, oc, "", n.home(), "rm", "--force", "--no-export", "--no-worktree-check"))
}
}
// opencodeLocalKey is the fake key of a local model server's in the
// store's opencode login, which devvm withholds unless config.toml's
// [pass-keys] passes it.
const opencodeLocalKey = "sk-local-e2efake-ollama"
// netOpencodePassKeys: a key of opencode's that the proxy can't put in, a
// local model server's, reaches the VM, vm in the folder oc, only once
// config.toml's [pass-keys] lists its provider, and then opencode 2's
// database gets it too; an OAuth login it doesn't bind never does, listed
// or not. The store's login and config.toml are put back after.
func netOpencodePassKeys(n *netRun, oc, vm string) {
s := n.s
store := filepath.Join(n.logins, "opencode", ".local", "share", "opencode", "auth.json")
cfg := filepath.Join(s.sb.config(), "devvm", "config.toml")
savedLogin, _ := os.ReadFile(store)
savedCfg, _ := os.ReadFile(cfg)
defer func() {
_ = os.WriteFile(store, savedLogin, 0o600)
_ = os.WriteFile(cfg, savedCfg, 0o600)
}()
launch := func() (Res, error) {
_, r, err := launchWith(s, oc, n.home("DEVVM_MESSAGES=names"))
return r, err
}
s.Check("opencode's key for a provider devvm doesn't bind is withheld from the VM, warned, and the OAuth login it doesn't bind holds no token", func() error {
var m map[string]any
if vm == "" || json.Unmarshal(savedLogin, &m) != nil {
return errors.New("no second VM, or no store login to add to")
}
m["ollama"] = map[string]any{"type": "api", "key": opencodeLocalKey}
m["copilot-ghe"] = map[string]any{"type": "oauth", "access": "at-e2efake-ghe", "refresh": "rt-e2efake-ghe", "expires": 1, "enterpriseUrl": "ghe.example.com"}
b, _ := json.Marshal(m)
if err := os.WriteFile(store, b, 0o600); err != nil {
return err
}
r, err := launch()
switch {
case err != nil:
return failed("the launch failed:\n"+r.Out, err)
case !Matches(r.Out, `^devvm: login_withheld agent=opencode name=opencode providers=(.*, )?copilot-ghe, (.*, )?ollama(, .*)?$`):
return errors.New("no warning names the withheld logins:\n" + r.Out)
}
if has, err := vmHas(s, vm, false, "cat ~/.local/share/opencode/auth.json", "e2efake-ghe", opencodeLocalKey); err != nil || has {
return failed("the VM's auth.json holds a withheld key or token", err)
}
if has, err := vmHas(s, vm, true, "opencode auth export", opencodeLocalKey); err != nil || has {
return failed("the VM's opencode holds the withheld key", err)
}
return nil
}, Tail(40))
s.Check("... and once config.toml's [pass-keys] lists ollama, the VM and its opencode 2 get that key, noted, but never the OAuth login", func() error {
if vm == "" {
return errNo
}
if err := os.WriteFile(cfg, append(bytes.Clone(savedCfg), []byte("\n[pass-keys]\nopencode = [\"ollama\", \"copilot-ghe\"]\n")...), 0o600); err != nil {
return err
}
r, err := launch()
switch {
case err != nil:
return failed("the launch failed:\n"+r.Out, err)
case !Matches(r.Out, `^login_passed agent=opencode providers=ollama$`):
return errors.New("no note names the passed key:\n" + r.Out)
case vmOut(s, vm, "jq -r .ollama.key ~/.local/share/opencode/auth.json") != opencodeLocalKey:
return errors.New("the VM's auth.json lacks the passed key")
}
if has, err := vmHas(s, vm, false, "cat ~/.local/share/opencode/auth.json", "e2efake-ghe"); err != nil || has {
return failed("the VM's auth.json holds the OAuth login's tokens", err)
}
// Every credential: auth export TARGET resolves the target among opencode's own
// integrations, which a local server's isn't.
if has, err := vmHas(s, vm, true, "opencode auth export", opencodeLocalKey); err != nil || !has {
return errors.New("the VM's opencode didn't import the passed key\n" + n.opencodeWhy(vm))
}
return nil
}, Tail(40))
}
// piKey is the fake Anthropic API key devvm agent login pi saves.
const piKey = "sk-ant-e2efake-pi"
// pi in the network VM: it runs from its read-only shared image with its
// version check and install report off, and its start finds base's fd
// (fdfind) and rg rather than downloading its own. devvm agent login pi --api-key saves a fake Anthropic key in
// the store; the VM's pi gets only its placeholder, and pi's own request
// to Anthropic's API, which the run's fake answers, carries the key the
// VM's proxy put in, logged nowhere; a fake Groq key the same, in pi's
// Authorization to Groq's API; and a fake OpenAI key, whose placeholder
// starts sk- as pi checks, in an API key's request to OpenAI's API.
func netPi(n *netRun) {
s, vm := n.s, n.vm
s.Check("pi runs in the VM from its read-only shared image, its version check and install report off", func() error {
switch {
case inVM(s, vm, "pi --version </dev/null") != nil:
return errors.New("pi --version failed")
case vmOut(s, vm, "printenv PI_SKIP_VERSION_CHECK") != "1" || vmOut(s, vm, "printenv PI_TELEMETRY") != "0":
return errors.New("PI_SKIP_VERSION_CHECK isn't 1, or PI_TELEMETRY isn't 0")
case nonLogin(s, vm, "printenv PI_SKIP_VERSION_CHECK PI_TELEMETRY") != "1\n0":
return errors.New("a non-login bash -c, as ssh VM pi runs pi, lacks PI_SKIP_VERSION_CHECK=1 or PI_TELEMETRY=0")
case inVM(s, vm, "fdfind --version && rg --version") != nil:
return errors.New("base has no fdfind or rg for pi")
case inVM(s, vm, `d=$(dirname "$(mise which pi)") && [ -d "$d" ] && ! touch "$d/devvm-e2e" 2>/dev/null`) != nil:
return errors.New("pi's install can be written to")
}
return nil
})
s.Check("devvm agent login pi --api-key: the store keeps the key, the VM's pi its placeholder, and the proxy puts the key in pi's request", func() error {
r, err := n.dv(piKey+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "pi", "--api-key", "--provider", "anthropic", "--stdin")
if err != nil || !Matches(r.Out, `^==> agent_key_saved agent=pi provider=anthropic vm=`) {
return failed("devvm agent login pi --api-key didn't save the key:\n"+r.Out, err)
}
store := filepath.Join(n.logins, "pi", ".pi", "agent", "auth.json")
ph := "devvm-proxy-managed-pi-anthropic-" + n.salt
switch {
case jsonFileAt(store, "anthropic.key") != piKey || modeOf(store) != "600":
return errors.New("the store's pi login isn't the key, only yours to read")
case vmOut(s, vm, "jq -r .anthropic.key ~/.pi/agent/auth.json") != ph:
return errors.New("the VM's pi login doesn't hold the key's placeholder")
}
if has, err := vmHas(s, vm, false, "cat ~/.pi/agent/auth.json", "e2efake"); err != nil || has {
return failed("the VM's pi login holds the key", err)
}
// The fake answers with a 404, so pi fails, after its request.
_, _ = s.InVM(vm, `timeout 120 pi --no-session --no-tools -p --provider anthropic --model haiku "e2e" </dev/null`)
switch {
case !lineWith(readText(n.tokLog), "api /v1/messages", "x-api-key "+piKey):
return errors.New("pi's request didn't reach Anthropic's API with the key the proxy put in\n" + n.tokenWhy())
case !lineWith(readText(n.events), `"host":"api.anthropic.com"`, `"secrets":["pi-anthropic"]`):
return errors.New("no event shows pi's key put in")
}
return NoLeak([]string{piKey}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}, Tail(40))
s.Check("devvm agent login pi --api-key --provider groq: the VM's pi holds the placeholder, and Groq's API gets the key in Authorization", func() error {
r, err := n.dv(groqKey+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "pi", "--api-key", "--provider", "groq", "--stdin")
if err != nil || !Matches(r.Out, `^==> agent_key_saved agent=pi provider=groq vm=`) {
return failed("devvm agent login pi --api-key --provider groq didn't save the key:\n"+r.Out, err)
}
ph := "devvm-proxy-managed-pi-groq-" + n.salt
if vmOut(s, vm, "jq -r .groq.key ~/.pi/agent/auth.json") != ph {
return errors.New("the VM's pi login doesn't hold Groq's key's placeholder")
}
if has, err := vmHas(s, vm, false, "cat ~/.pi/agent/auth.json", "e2efake"); err != nil || has {
return failed("the VM's pi login holds the key", err)
}
// The fake answers with a 404, so pi fails, after its request.
_, _ = s.InVM(vm, `timeout 120 pi --no-session --no-tools -p --provider groq --model llama-3.1-8b-instant "e2e" </dev/null`)
switch {
case !lineWith(readText(n.tokLog), "groq /openai/v1/chat/completions", "Bearer "+groqKey):
return errors.New("pi's request didn't reach Groq's API with the key the proxy put in\n" + n.tokenWhy())
case !lineWith(readText(n.events), `"host":"api.groq.com"`, `"secrets":["pi-groq"]`):
return errors.New("no event shows pi's Groq key put in")
}
return NoLeak([]string{groqKey}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}, Tail(40))
s.Check("devvm agent login pi --api-key --provider openai: the VM's pi holds a placeholder starting sk-, and sends OpenAI's API an API key's request with the key", func() error {
r, err := n.dv(piOpenAIKey+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "pi", "--api-key", "--provider", "openai", "--stdin")
if err != nil || !Matches(r.Out, `^==> agent_key_saved agent=pi provider=openai vm=`) {
return failed("devvm agent login pi --api-key --provider openai didn't save the key:\n"+r.Out, err)
}
ph := "sk-devvm-proxy-managed-pi-openai-" + n.salt
if vmOut(s, vm, "jq -r .openai.key ~/.pi/agent/auth.json") != ph {
return errors.New("the VM's pi login doesn't hold OpenAI's key's placeholder")
}
// The fake answers with a 404, so pi fails, after its request. A key that doesn't start
// with sk- would be a ChatGPT sign-in's token to pi, which leaves max_output_tokens out.
_, _ = s.InVM(vm, `timeout 120 pi --no-session --no-tools -p --provider openai --model gpt-4o "e2e" </dev/null`)
switch {
case !lineWith(readText(n.tokLog), "openai /v1/responses Bearer "+piOpenAIKey+" max_output_tokens"):
return errors.New("pi's request didn't reach OpenAI's API as an API key's, with the key the proxy put in\n" + n.tokenWhy())
case !lineWith(readText(n.events), `"host":"api.openai.com"`, `"secrets":["pi-openai"]`):
return errors.New("no event shows pi's OpenAI key put in")
}
return NoLeak([]string{piOpenAIKey}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}, Tail(40))
}
// groqKey is the fake Groq API key devvm agent login pi saves.
const groqKey = "gsk_e2efake-pi-groq"
// piOpenAIKey is the fake OpenAI API key devvm agent login pi saves.
const piOpenAIKey = "sk-e2efake-pi-openai"
// Fake keys of the providers whose header or agent differs: Gemini's
// for pi, Groq's for opencode.
const (
googleKey = "AIza-e2efake-pi-google"
opencodeGroq = "gsk_e2efake-opencode-groq"
geminiAPIHost = "generativelanguage.googleapis.com"
)
// netProviderKeys: the providers whose keys reach their API another way
// than pi's Anthropic and Groq keys. devvm agent login pi saves a fake
// Gemini key, and pi's request to Gemini's API, which the run's fake
// answers, carries it in x-goog-api-key and never in its URL; devvm agent
// login opencode saves a fake Groq key, and opencode's own request to
// Groq's API carries it in its Authorization. The VM holds only their
// placeholders, and no log the keys.
func netProviderKeys(n *netRun) {
s, vm := n.s, n.vm
s.Check("devvm agent login pi --api-key --provider google: the VM's pi holds the placeholder, and Gemini's API gets the key in x-goog-api-key, not in the URL", func() error {
r, err := n.dv(googleKey+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "pi", "--api-key", "--provider", "google", "--stdin")
if err != nil || !Matches(r.Out, `^==> agent_key_saved agent=pi provider=google vm=`) {
return failed("devvm agent login pi --api-key --provider google didn't save the key:\n"+r.Out, err)
}
if vmOut(s, vm, "jq -r .google.key ~/.pi/agent/auth.json") != "devvm-proxy-managed-pi-google-"+n.salt {
return errors.New("the VM's pi login doesn't hold Gemini's key's placeholder")
}
if has, err := vmHas(s, vm, false, "cat ~/.pi/agent/auth.json", "e2efake"); err != nil || has {
return failed("the VM's pi login holds the key", err)
}
// The fake answers with a 404, so pi fails, after its request.
_, _ = s.InVM(vm, `timeout 120 pi --no-session --no-tools -p --provider google --model gemini-2.5-flash "e2e" </dev/null`)
got := readText(n.tokLog)
switch {
case !lineWith(got, "google /v1", "x-goog-api-key "+googleKey):
return errors.New("pi's request didn't reach Gemini's API with the key the proxy put in\n" + n.tokenWhy())
case lineWith(got, "google /", "key="):
return errors.New("pi's request to Gemini's API has a key in its URL\n" + n.tokenWhy())
case !lineWith(readText(n.events), `"host":"`+geminiAPIHost+`"`, `"secrets":["pi-google"]`):
return errors.New("no event shows pi's Gemini key put in")
}
return NoLeak([]string{googleKey}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}, Tail(40))
s.Check("devvm agent login opencode --api-key --provider groq: the VM's opencode holds the placeholder, and its request to Groq's API gets the key", func() error {
r, err := n.dv(opencodeGroq+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "opencode", "--api-key", "--provider", "groq", "--stdin")
if err != nil {
return failed("devvm agent login opencode --api-key --provider groq failed:\n"+r.Out+"\n"+n.runLogEnd(r.Out), err)
}
if has, err := vmHas(s, vm, true, "opencode auth export groq", "devvm-proxy-managed-opencode-groq-"+n.salt); err != nil || !has {
return errors.New("the VM's opencode has no placeholder of Groq's key\n" + n.opencodeWhy(vm))
}
if has, err := vmHas(s, vm, true, "opencode auth export; cat ~/.local/share/opencode/auth.json", "e2efake-opencode"); err != nil || has {
return failed("the VM's opencode holds the key", err)
}
// Any of Groq's models in opencode's own catalog, which the company proxy keeps it from
// updating. The fake answers with a 404, so opencode fails, after its request.
out, _ := s.InVM(vm, `l=$(opencode models </dev/null 2>&1); m=$(grep -m1 '^groq/' <<<"$l") || { echo "no Groq model in: $(head -5 <<<"$l")"; exit 1; }; `+
`echo "model $m"; timeout 120 opencode run -m "$m" "e2e" </dev/null`)
switch {
case !lineWith(readText(n.tokLog), "groq /openai/v1/", "Bearer "+opencodeGroq):
return errors.New("opencode's request didn't reach Groq's API with the key the proxy put in\n" + tailLines(out.Out, 20) + "\n" + n.tokenWhy())
case !lineWith(readText(n.events), `"host":"api.groq.com"`, `"secrets":["opencode-groq"]`):
return errors.New("no event shows opencode's Groq key put in")
}
return NoLeak([]string{opencodeGroq}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}, Tail(40))
s.Check("devvm agent login pi --api-key --provider openrouter: OpenRouter's site gets the key below /api/ alone, in x-api-key or after Bearer, and the placeholder for anything else", func() error {
return netOpenRouter(n)
}, Tail(40))
}
// openRouterKey is the fake OpenRouter API key devvm agent login pi saves.
const openRouterKey = "sk-or-e2efake-pi-openrouter"
// netOpenRouter saves a fake OpenRouter key for pi, then sends OpenRouter's
// site the VM's placeholder from the VM, as pi does for its anthropic/*
// models (x-api-key, /api/v1/messages) and its others (Bearer), and in
// ways the proxy leaves alone: framed, outside /api/, through a dot
// segment, and another provider's placeholder in x-api-key.
func netOpenRouter(n *netRun) error {
s, vm := n.s, n.vm
r, err := n.dv(openRouterKey+"\n", n.tokenEnv("DEVVM_MESSAGES=names"), "agent", "login", "pi", "--api-key", "--provider", "openrouter", "--stdin")
if err != nil || !Matches(r.Out, `^==> agent_key_saved agent=pi provider=openrouter vm=`) {
return failed("devvm agent login pi --api-key --provider openrouter didn't save the key:\n"+r.Out, err)
}
ph := "devvm-proxy-managed-pi-openrouter-" + n.salt
if vmOut(s, vm, "jq -r .openrouter.key ~/.pi/agent/auth.json") != ph {
return errors.New("the VM's pi login doesn't hold OpenRouter's key's placeholder")
}
other := "devvm-proxy-managed-pi-groq-" + n.salt
c := `curl -sS -m 20 -o /dev/null --path-as-is https://openrouter.ai`
if err := inVM(s, vm, c+`/api/v1/messages -H "x-api-key: `+ph+`" && `+
c+`/api/v1/chat/completions -H "Authorization: Bearer `+ph+`" && `+
c+`/api/v1/e2e-framed -H "Authorization: xxxx `+ph+`" -H "x-api-key: xxxx `+ph+`" && `+
c+`/settings/e2e -H "Authorization: Bearer `+ph+`" -H "x-api-key: `+ph+`" && `+
c+`/api/../settings/e2e-dots -H "x-api-key: `+ph+`" && `+
c+`/api/v1/e2e-other -H "x-api-key: `+other+`"`); err != nil {
return failed("the VM's requests to OpenRouter's site", err)
}
got := readText(n.tokLog)
for _, want := range []string{
"openrouter /api/v1/messages authorization - x-api-key " + openRouterKey,
"openrouter /api/v1/chat/completions authorization Bearer " + openRouterKey + " x-api-key -",
"openrouter /api/v1/e2e-framed authorization xxxx " + ph + " x-api-key xxxx " + ph,
"openrouter /settings/e2e authorization Bearer " + ph + " x-api-key " + ph,
"openrouter /api/../settings/e2e-dots authorization - x-api-key " + ph,
"openrouter /api/v1/e2e-other authorization - x-api-key " + other,
} {
if !HasLine(got, want) {
return errors.New("OpenRouter's site didn't get " + want + "\n" + n.tokenWhy())
}
}
if !lineWith(readText(n.events), `"host":"openrouter.ai"`, `"/api/v1/messages"`, `"secrets":["pi-openrouter"]`) {
return errors.New("no event shows pi's OpenRouter key put in")
}
return NoLeak([]string{openRouterKey}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs"))
}
// pi's terminal UI looks for fd (as fd or fdfind) and rg as it starts,
// and downloads each it doesn't find into ~/.pi/agent/bin, unpinned, from
// GitHub, which the allowlist has. With base's on the PATH it downloads
// neither. The control: with a PATH that has no fdfind, the same start
// downloads fd, so the start did reach that check. It runs before the
// checks behind the run's company proxies, which refuse GitHub.
// netCursor checks Cursor's CLI in the VM: the shared release devvm pins, as cursor-agent and agent,
// with its own node and rg off the PATH and its updates off; and its login's poll, a GET at
// api2.cursor.sh that the proxy captures, which passes Cursor's answer for a login that isn't
// there as it is.
func netCursor(n *netRun) {
s, vm := n.s, n.vm
s.Check("Cursor's CLI is the shared release, as cursor-agent and agent, its node and rg off the PATH", func() error {
target := vmOut(s, vm, "readlink ~/.local/bin/agent")
version := strings.TrimSuffix(strings.TrimPrefix(target, "/opt/devvm/tools/cursor/"), "/cursor-agent")
switch {
case version == "" || version == target || strings.Contains(version, "/"):
return errors.New("~/.local/bin/agent links to " + target)
case vmOut(s, vm, "readlink ~/.local/bin/cursor-agent") != target:
return errors.New("cursor-agent isn't agent's link")
case vmOut(s, vm, "bash -lc 'cursor-agent --version'") != version:
return errors.New("cursor-agent --version isn't " + version)
}
for _, cmd := range []string{"node", "rg"} {
if p := vmOut(s, vm, "bash -lc 'command -v "+cmd+"'"); strings.Contains(p, "/tools/cursor/") {
return errors.New(cmd + " is Cursor's: " + p)
}
}
return nil
}, Tail(40))
s.Check("Cursor's CLI is on its static channel, which it never updates on", func() error {
if got := vmOut(s, vm, "jq -r .channel ~/.cursor/cli-config.json"); got != "static" {
return errors.New("its channel is " + got)
}
return nil
})
s.Check("the proxy captures Cursor's login poll, a GET, and passes Cursor's answer for a login that isn't there", func() error {
cfg, err := proxy.ReadConfig(n.pxc)
if err != nil {
return fmt.Errorf("proxy.json: %w", err)
}
if !slices.ContainsFunc(cfg.Captures, func(c proxy.Capture) bool {
return c.Agent == "cursor" && c.Method == http.MethodGet && c.Host == "api2.cursor.sh" && c.Path == "/auth/poll"
}) {
return fmt.Errorf("proxy.json captures no GET of api2.cursor.sh/auth/poll for cursor: %+v", cfg.Captures)
}
// Its proxy behind the fake company proxy, which stands in for Cursor's API.
if _, err := n.dv("exit\n", n.tokenEnv()); err != nil {
return failed("the launch behind the fake endpoints", err)
}
got := vmOut(s, vm, `curl -sS -m 20 -w '\n%{http_code}' 'https://`+cursorAPIHost+`/auth/poll?verifier=devvm-e2e&uuid=00000000-0000-4000-8000-000000000000'`)
if got != "Not found\n404" {
return errors.New("Cursor's poll answered, through the proxy: " + got + "\n" + n.tokenWhy())
}
// It went on with its query canonical, and the login in no header.
const want = "cursor GET /auth/poll?uuid=00000000-0000-4000-8000-000000000000&verifier=devvm-e2e HTTP/1.1 authorization -"
if !slices.Contains(strings.Split(readText(n.tokLog), "\n"), want) {
return errors.New("Cursor's stand-in didn't get " + want + ":\n" + tailLines(readText(n.tokLog), 5))
}
return nil
})
s.Check("Cursor's agent's Run streams through the proxy with its messages gzipped, and a token in one ends it there", func() error {
run := `curl -sS -m 20 --http1.1 -X POST -H 'Content-Type: application/connect+proto' -H 'Connect-Protocol-Version: 1' ` +
`--data-binary '' -o /tmp/devvm-e2e-run -w '%%{http_code}' %s 'https://` + cursorAPIHost + `/agent.v1.AgentService/Run'`
if code := vmOut(s, vm, fmt.Sprintf(run, "")); code != "200" {
return errors.New("Run answered " + code + " through the proxy\n" + n.tokenWhy())
}
// Its plain message and its end of stream came as Cursor sent them.
if got := vmOut(s, vm, `grep -c e2e-run-bye /tmp/devvm-e2e-run; tail -c 7 /tmp/devvm-e2e-run | od -An -tx1 | tr -d ' \n'`); got != "1\n02000000027b7d" {
return errors.New("the stream that reached the VM: " + got)
}
_ = vmOut(s, vm, fmt.Sprintf(run, "-H 'x-e2e-token: 1'"))
if got := vmOut(s, vm, `grep -c -e e2e-run-bye -e eyJ /tmp/devvm-e2e-run`); got != "0" {
return errors.New("the stream with a token went on past it: " + got)
}
return nil
})
// api3.cursor.sh, the CLI's metrics, is quiet in Cursor's policy: blocked, logged as quiet, and
// never in the VM's notices or with a command to allow it.
s.Check("Cursor's metrics host is blocked quietly: no notice in the VM, logged as quiet", func() error {
const count = "curl -sS -m 10 http://devvm.proxy/blocked/new | head -1"
before := vmOut(s, vm, count)
r, _ := s.InVM(vm, "curl -sS -m 20 -o /dev/null -w '%{http_code}' https://api3.cursor.sh/; curl -sS -m 20 http://api3.cursor.sh/")
switch {
case !strings.Contains(r.Out, "403") || strings.Contains(r.Out, "devvm network allow"):
return errors.New("api3.cursor.sh wasn't refused quietly: " + r.Out)
case before == "" || vmOut(s, vm, count) != before:
return errors.New("the VM's notices counted api3.cursor.sh's block")
case strings.Contains(vmOut(s, vm, "devvm-why"), "api3.cursor.sh"):
return errors.New("devvm-why shows api3.cursor.sh")
}
l, err := s.Devvm(n.n, "network", "log")
if err != nil || !Matches(l.Stdout, ` BLOCKED api3\.cursor\.sh:443 .*, quiet$`) {
return errors.New("devvm network log doesn't show api3.cursor.sh's block as quiet")
}
b, _ := s.Devvm(n.n, "network", "log", "--blocked")
if !Matches(b.Stdout, `api3\.cursor\.sh:443 +- \(quiet\)$`) {
return errors.New("devvm network log --blocked offers api3.cursor.sh: " + b.Stdout)
}
return nil
})
netCursorLogin(n)
netCursorEditor(n)
}
// cursorJWT is a fake Cursor token: a JWT with sub and a far exp, as Cursor's are, and mark in its
// signature so a check can find it anywhere.
func cursorJWT(mark string) string {
enc := func(v string) string { return base64.RawURLEncoding.EncodeToString([]byte(v)) }
return enc(`{"alg":"none","typ":"JWT"}`) + "." + enc(`{"sub":"auth0|e2efake","exp":4070908800}`) + "." + mark
}
// netCursorLogin: with a Cursor login in the store (a fake one), the CLI's own methods get it from
// the VM's proxy in place of the VM's placeholder, and the agent store's are refused before they
// leave the VM's proxy, its token's mint among them, so the stand-in never sees one.
func netCursorLogin(n *netRun) {
s, vm := n.s, n.vm
s.Check("with a Cursor login saved, the CLI's methods get it and the agent store's are refused at the proxy", func() error {
at := cursorJWT("e2efake-cursor-at")
login := filepath.Join(n.logins, "cursor", ".config", "cursor", "auth.json")
if err := os.MkdirAll(filepath.Dir(login), 0o700); err != nil {
return err
}
if err := os.WriteFile(login, []byte(`{"accessToken":"`+at+`","refreshToken":"`+cursorJWT("e2efake-cursor-rt")+`"}`), 0o600); err != nil {
return err
}
defer func() { _ = os.Remove(login) }()
if _, err := n.dv("exit\n", n.tokenEnv()); err != nil {
return failed("the launch with the Cursor login", err)
}
ph := vmOut(s, vm, "jq -r .accessToken ~/.config/cursor/auth.json")
switch {
case ph == "" || ph == "null":
return errors.New("the VM has no Cursor login")
case strings.Contains(ph, "e2efake-cursor"):
return errors.New("the VM holds the Cursor login itself")
}
post := func(method string) string {
return vmOut(s, vm, `curl -sS -m 20 -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/proto' `+
`-H `+shell.Quote("Authorization: Bearer "+ph)+` --data-binary '' 'https://`+cursorAPIHost+`/aiserver.v1.`+method+`'`)
}
if code := post("DashboardService/GetMe"); code != "404" {
return errors.New("the CLI's GetMe answered " + code + " through the proxy\n" + n.tokenWhy())
}
const got = "cursor POST /aiserver.v1.DashboardService/GetMe HTTP/1.1 authorization Bearer "
if !slices.Contains(strings.Split(readText(n.tokLog), "\n"), got+at) {
return errors.New("Cursor's stand-in didn't get the login in GetMe:\n" + tailLines(readText(n.tokLog), 5))
}
for _, m := range []string{"AgentStoreService/MintAgentStoreToken", "AgentStoreService/PresignAgentStoreWrites", "AgentStoreService/ListAgentStoreDirectory"} {
if code := post(m); code != "403" {
return errors.New("the agent store's " + m + " answered " + code + ", not the proxy's 403")
}
}
if strings.Contains(readText(n.tokLog), "AgentStoreService") {
return errors.New("an agent store request reached Cursor's stand-in:\n" + tailLines(readText(n.tokLog), 5))
}
return nil
}, Tail(40))
}
// netCursorEditor: what devvm can check of the Cursor editor without one. devvm cursor runs the
// editor's cursor command (a stand-in that records what it got) with the Remote-SSH host and the
// VM's folder alone, and a Cursor editor's sign-in on this machine (a fake one in its state file)
// reaches neither the command nor the VM: not its home, any process's environment or the VM's
// proxy's logs. Whether the real editor sends its token into the VM over Remote-SSH needs a real
// sign-in (cursor-manual-checks in the notes).
func netCursorEditor(n *netRun) {
s, vm := n.s, n.vm
s.Check("devvm cursor gives the editor only the host and folder, and the editor's sign-in on this machine reaches no VM", func() error {
const mark = "e2efake-cursor-editor"
state := filepath.Join(n.nh, ".config", "Cursor", "User", "globalStorage", "state.vscdb")
if err := os.MkdirAll(filepath.Dir(state), 0o700); err != nil {
return err
}
if err := os.WriteFile(state, []byte("cursorAuth/accessToken "+cursorJWT(mark)+"\n"), 0o600); err != nil {
return err
}
defer func() { _ = os.RemoveAll(filepath.Join(n.nh, ".config", "Cursor")) }()
bin := filepath.Join(s.sb.work, "fake-cursor")
madeFile(s.sb, bin)
if err := os.MkdirAll(bin, 0o700); err != nil {
return err
}
got := filepath.Join(bin, "got")
script := "#!/bin/sh\n{ printf '%s\\n' \"$@\"; env; } > " + shell.Quote(got) + "\n"
if err := os.WriteFile(filepath.Join(bin, "cursor"), []byte(script), 0o700); err != nil { //nolint:gosec // G306: a test's own script, run by devvm cursor
return err
}
if r, err := n.dv("", n.home("PATH="+bin+":"+os.Getenv("PATH")), "cursor"); err != nil {
return failed("devvm cursor:\n"+r.Out, err)
}
args := strings.SplitN(readText(got), "\n", 4)
switch {
case len(args) < 4 || args[0] != "--remote" || !strings.HasPrefix(args[1], "ssh-remote+devvm-") || !strings.HasPrefix(args[2], "/"):
return errors.New("the editor's command got:\n" + readText(got))
case strings.Contains(readText(got), mark):
return errors.New("the editor's command got the editor's sign-in")
}
if has, err := vmHas(s, vm, true, `grep -rIl --exclude-dir=.cache `+mark+` ~ 2>/dev/null; `+
`for e in /proc/[0-9]*/environ; do sudo tr '\0' '\n' < "$e" 2>/dev/null; done`, mark); err != nil || has {
return failed("the VM holds the editor's sign-in", err)
}
logs := readText(filepath.Join(n.vmd, "logs", "proxy.log")) + readText(n.events)
if strings.Contains(logs, mark) {
return errors.New("the VM's proxy logged the editor's sign-in")
}
return nil
}, Tail(40))
}
func netPiTools(n *netRun) {
s, vm := n.s, n.vm
s.Check("pi's start finds base's fdfind and rg, and downloads neither; without fdfind it downloads fd", func() error {
const bin = "~/.pi/agent/bin"
if err := inVM(s, vm, "rm -rf "+bin+" && "+piStart(`"$PATH"`)); err != nil {
return failed("pi's start", err)
}
if got := vmOut(s, vm, "ls -A "+bin+" 2>/dev/null"); got != "" {
return errors.New("pi downloaded " + got + " into " + bin + " with base's tools on the PATH")
}
hidden := vmOut(s, vm, `d=$(mktemp -d) && for b in /usr/local/bin/* /usr/bin/* /bin/*; do n=${b##*/}; `+
`case $n in fd|fdfind) continue ;; esac; [ -e "$d/$n" ] || ln -s "$b" "$d/$n"; done && echo "$HOME/.local/share/mise/shims:$d"`)
if hidden == "" {
return errors.New("no PATH without fdfind")
}
defer func() { _ = inVM(s, vm, "rm -rf "+bin+" "+shell.Quote(hidden[strings.LastIndex(hidden, ":")+1:])) }()
if err := inVM(s, vm, piStart(shell.Quote(hidden))); err != nil {
return failed("pi's start without fdfind", err)
}
if inVM(s, vm, "test -x "+bin+"/fd") != nil {
var gh []string
for l := range strings.Lines(readText(n.events)) {
if strings.Contains(l, "github") {
gh = append(gh, strings.TrimRight(l, "\n"))
}
}
return errors.New("the control failed: without fdfind, pi's start downloaded no fd, so it may never have looked\n" +
"the proxy's last GitHub events:\n" + strings.Join(gh[max(0, len(gh)-5):], "\n"))
}
return nil
}, Tail(40))
}
// nonLogin is what script printed in a non-login bash -c in the VM, as
// ssh VM COMMAND and editors run one, trimmed.
func nonLogin(s *S, vm, script string) string {
r, _ := vmRun(s, vm, "bash", "-c", script)
return strings.TrimSpace(r.Stdout)
}
// piStart is the script that starts pi's terminal UI in the VM with PATH
// path (shell-quoted), in a terminal of script's (util-linux), lets it
// run for 30 s, then ends it.
func piStart(path string) string {
return "cd ~ && { TERM=xterm-256color PATH=" + path + ` script -qfec 'stty cols 120 rows 40; exec pi --no-session' /dev/null </dev/null >/dev/null 2>&1 & p=$!; }; ` +
`sleep 30; pkill -x pi; kill "$p" 2>/dev/null; wait "$p" 2>/dev/null; true`
}
// opencodeWhy is what the VM's opencode got: the IDs of opencode.sh's
// record and of opencode's credentials (never their values), and the
// lines of the newest run log that name opencode.
func (n *netRun) opencodeWhy(vm string) string {
s := n.s
ids := vmOut(s, vm, `jq -c '[.[].id]' ~/.local/state/devvm/opencode-import.json; ls ~/.local/share/opencode; opencode auth export 2>&1 | jq -c '[.[] | [.id, .integrationID, .active]]'`)
runs := s.Paths.RunLogs()
var lines []string
for _, f := range runs[max(0, len(runs)-4):] {
lines = append(lines, "== "+filepath.Base(f))
for l := range strings.Lines(readText(f)) {
if strings.Contains(l, "opencode") || strings.Contains(l, "Imported") || strings.Contains(l, "sync") {
lines = append(lines, strings.TrimRight(l, "\n"))
}
}
}
return "in the VM: " + ids + "\nthe last run logs' opencode lines:\n" + tailLines(strings.Join(lines, "\n"), 60)
}
// runLogEnd is the last lines of the run log that out's log_end line
// names, nothing when it names none.
func (n *netRun) runLogEnd(out string) string {
m := regexp.MustCompile(`log_end file=(\S+)`).FindStringSubmatch(out)
if m == nil {
return ""
}
return "the run log ends:\n" + tailLines(readText(m[1]), 40)
}
// opencodeLoggedIn is the opencode login's first step that failed,
// nothing when none.
func (n *netRun) opencodeLoggedIn(p *Proc) string {
s := n.s
if rc, ok := waitProc(p, 2*time.Minute); !ok || rc != 0 {
return "devvm agent login ends, saying what the store saved"
}
// The fake's access token names no email, and opencode keeps no id_token.
if !Matches(p.Out(), `^==> agent_login_saved agent=opencode vm=`) {
return "it says the store saved the login"
}
a := filepath.Join(n.logins, "opencode", ".local", "share", "opencode", "auth.json")
if jsonFileAt(a, "openai.refresh") != "rt-e2efake-capture" || !strings.Contains(jsonFileAt(a, "openai.access"), "e2efake") {
return "the store has the real tokens"
}
if has, err := vmHas(s, n.vm, true, "opencode auth export openai", "devvm-placeholder"); err != nil || !has {
return "the VM's opencode holds its login's placeholders"
}
if has, err := vmHas(s, n.vm, true, "opencode auth export openai", "e2efake"); err != nil || has {
return "the VM's opencode holds no token of the login"
}
if !lineWith(readText(n.events), `"event":"login","host":"auth.openai.com","port":443,"method":"POST","path":"/oauth/token","login":"opencode"`) {
return "the events name opencode's login"
}
return ""
}
// codexLoggedIn is the Codex login's first step that failed, nothing when
// none.
func (n *netRun) codexLoggedIn(p *Proc) string {
s := n.s
ctx := s.r.window().ctx
state := loginState(s, p)
if state == "" {
return "Codex shows its address"
}
if _, _, err := (web{timeout: time.Minute}).get(ctx, "http://127.0.0.1:1455/auth/callback?code=e2e-code&state="+state); err != nil {
return "the callback reaches Codex in the VM through the forward, and its success page too: " + err.Error()
}
if rc, ok := waitProc(p, time.Minute); !ok || rc != 0 {
return "devvm agent login ends, saying what the store saved"
}
if !Matches(p.Out(), `^==> agent_login_saved_for agent=codex email=e2e@devvm\.test vm=`) {
return "it names the account"
}
a := filepath.Join(n.logins, "codex", ".codex", "auth.json")
if jsonFileAt(a, "tokens.refresh_token") != "rt-e2efake-capture" || jsonFileAt(a, "OPENAI_API_KEY") != "sk-e2efake-captured-key" {
return "the store has the real tokens and key"
}
r, err := vmRun(s, n.vm, "bash", "-c", "cat ~/.codex/auth.json")
if err != nil || strings.Contains(r.Stdout, "e2efake") || jsonAt([]byte(r.Stdout), "tokens.refresh_token") != "devvm-proxy-managed" {
return "the VM's Codex holds placeholders alone"
}
if !forwardGone(ctx) {
return "the forward is gone"
}
return ""
}
// loginState is the state in the address a login shows, once it shows one
// (60 s at most, within the check's window), nothing when it ends first.
func loginState(s *S, p *Proc) string {
re := regexp.MustCompile(`[?&]state=([A-Za-z0-9_-]*)`)
st := ""
_ = s.Until(time.Minute, func() error {
// The first line that names one, its last (sed's greedy .*).
for l := range strings.Lines(p.Out()) {
if m := re.FindAllStringSubmatch(l, -1); m != nil {
if st = m[len(m)-1][1]; st != "" {
return nil
}
break
}
}
if !p.Running() {
return nil
}
return errors.New("no address yet")
})
return st
}
// waitProc is p's exit status once it ends, within d: false when it
// hasn't.
func waitProc(p *Proc, d time.Duration) (int, bool) {
select {
case <-p.done:
return p.cmd.ProcessState.ExitCode(), true
case <-time.After(d):
return -1, false
}
}
// forwardGone is whether nothing answers on this machine's
// localhost:1455.
func forwardGone(ctx context.Context) bool {
_, _, err := (web{timeout: 5 * time.Second}).get(ctx, "http://127.0.0.1:1455/")
return err != nil
}
// loginWhy is what devvm agent login said, then tokenWhy.
func (n *netRun) loginWhy(p *Proc) string {
return "devvm agent login said:\n" + tailLines(p.Out(), 20) + "\n" + n.tokenWhy()
}
// claudeSetup is claude setup-token's code, as Claude Code 2.1 sends it.
const claudeSetup = `{"grant_type":"authorization_code","code":"e2e-code","redirect_uri":"https://platform.claude.com/oauth/code/callback",` +
`"client_id":"9d1c250a-e61b-44d9-88ed-5944d1962f5e","code_verifier":"e2e","state":"e2e","expires_in":31536000}`
// apiGot is whether the fake API got a request for path that the VM's
// proxy put tok in.
func (n *netRun) apiGot(path, tok string) bool {
return HasLine(readText(n.tokLog), "api "+path+" Bearer "+tok)
}
// Claude Code's logins, captured at Claude's token endpoint, which the
// fake answers too, with Anthropic's API behind it logging the
// Authorization each request brings. This login path is unofficial: the
// requests are Claude Code 2.1's, read from its bundle. First claude
// setup-token's, as curl: the store keeps its token as the claude
// secret, and the VM gets the secret's placeholder, which the API gets the
// token for and the claude.ai connectors' host doesn't, and neither does
// the OAuth login's placeholder there. The proxy saves the
// token in the Keychain under the run's service, through the VM's home's
// link to the Keychains folder (netSetup).
func netClaudeLogins(n *netRun) {
s := n.s
s.Check("claude setup-token's token in the VM is captured: the store keeps it as the claude secret, the VM a placeholder", func() error {
if what := n.setupToken(); what != "" {
return errors.New(what + "\n" + n.tokenWhy())
}
return nil
}, Tail(60))
// devvm agent login claude --setup-token on a terminal: the real
// claude setup-token, full-screen, gets a terminal in the VM and draws
// its address there, which nothing logs in with: the check stops it.
s.Check("devvm agent login claude --setup-token on a terminal shows Claude Code's address", func() error {
if what := setupTokenShows(n); what != "" {
return errors.New(what)
}
return nil
}, Tail(40))
// devvm agent login claude: the real Claude Code's claude auth login in
// the VM, its code pasted on devvm's input as code#state, the state
// from the address it shows. The proxy saves the subscription's login
// in the store, which takes the claude secret away, and the VM is set
// up again: its ~/.claude/.credentials.json holds placeholders, and
// its environment no token.
s.Check("devvm agent login claude logs the VM's Claude Code in, and the store keeps the login, the VM placeholders", n.claudeLogin, Tail(60))
// The host refreshes the login, through the company proxy: the store's
// access token made due, the VM's next request (past the proxy's 30 s
// cache of a login's value) makes the host refresh it at Claude's
// endpoint, through the run's company proxy. The refresh trusts the
// VM's proxy's CAs, the run's among them, so it succeeds: the store
// gets the new tokens, the VM's requests to the API and to the claude.ai
// connectors' host go on with the refreshed access token, and no log
// holds a value.
s.Check("the host refreshes Claude Code's login at Claude's endpoint, through the company proxy, when it's due", func() error {
if what := n.refreshed(); what != "" {
return errors.New(what + "\n" + n.tokenWhy())
}
return nil
}, Tail(40))
// While the fake endpoints still run, and once a secret has been saved
// in the Keychain with the VM's HOME, as netGithub's is.
netCustomSecret(n)
netRequestRules(n)
if n.tok != nil {
n.tok.Stop()
}
if ca, err := NewCA(s); err == nil {
_ = ca.Untrust()
}
s.Check("the next launch, going directly, gives the VM the captured login as placeholders", func() error {
if _, err := s.Run(Cmd{Args: unproxied(s.sb.devvm), Dir: n.n, Stdin: "exit\n", Env: n.home(fakeToken)}); err != nil {
return err
}
if Matches(readText(n.pxc), `^ "upstream": \{`) {
return errors.New("the VM's proxy still goes upstream")
}
r, err := vmRun(s, n.vm, "bash", "-c", "cat ~/.codex/auth.json")
c := []byte(r.Stdout)
switch {
case err != nil || strings.Contains(r.Stdout, "e2efake") || jsonAt(c, "tokens.refresh_token") != "devvm-proxy-managed":
return errors.New("the VM's Codex login holds a token, or no placeholder")
case jsonAt(c, "tokens.access_token") == "" || jsonAt(c, "tokens.access_token") != jsonAt([]byte(n.cap1), "access_token"):
return errors.New("the VM's access token isn't the captured login's placeholder")
case jsonAt(c, "OPENAI_API_KEY") != "sk-devvm-proxy-managed-codex-"+n.salt:
return errors.New("the VM's API key isn't its placeholder")
}
return nil
})
}
// setupToken is claude setup-token's capture's first step that failed,
// nothing when none.
func (n *netRun) setupToken() string {
s := n.s
oauthPH := "sk-ant-oat01-devvm-proxy-managed-oauth-" + n.salt
r, err := s.InVM(n.vm, "curl -fsS -m 30 https://platform.claude.com/v1/oauth/token -H 'Content-Type: application/json' -d '"+claudeSetup+"'")
switch {
case err != nil:
return "setup-token's code"
case strings.Contains(r.Stdout, "e2efake") || jsonAt([]byte(r.Stdout), "access_token") != "sk-ant-oat01-devvm-proxy-managed-"+n.salt:
return "the VM gets the claude secret's placeholder, and no token"
case devvmSecret(s, "claude") != "sk-ant-oat01-e2efake-setup":
return "the claude secret is the token"
case Exists(filepath.Join(n.logins, "claude", ".claude", ".credentials.json")):
return "and devvm keeps no OAuth login beside it"
case inVM(s, n.vm, "curl -sS -m 20 -o /dev/null https://api.anthropic.com/v1/e2e-setup -H 'Authorization: Bearer sk-ant-oat01-devvm-proxy-managed-"+n.salt+"'") != nil:
return "the proxy puts the token in for the placeholder"
case !n.apiGot("/v1/e2e-setup", "sk-ant-oat01-e2efake-setup"):
return "... which the API got"
case inVM(s, n.vm, "curl -sS -m 20 -o /dev/null https://mcp-proxy.anthropic.com/v1/mcp/e2e-setup -H 'Authorization: Bearer sk-ant-oat01-devvm-proxy-managed-"+n.salt+"'") != nil:
return "the VM's request to the connectors' host"
case !n.apiGot("/v1/mcp/e2e-setup", "sk-ant-oat01-devvm-proxy-managed-"+n.salt):
return "... which got the placeholder alone, as the token goes only to the API"
case inVM(s, n.vm, "curl -sS -m 20 -o /dev/null https://api.anthropic.com/v1/e2e-setup-oauth -H 'Authorization: Bearer "+oauthPH+"'") != nil:
return "the VM's request to the API with the OAuth login's placeholder, which it can make from its salt"
case !n.apiGot("/v1/e2e-setup-oauth", "sk-ant-oat01-e2efake-setup"):
return "... which got the token, as a VM that started with the OAuth login gets the token that replaced it"
case inVM(s, n.vm, "curl -sS -m 20 -o /dev/null https://mcp-proxy.anthropic.com/v1/mcp/e2e-setup-oauth -H 'Authorization: Bearer "+oauthPH+"'") != nil:
return "the same placeholder to the connectors' host"
case strings.Contains(readText(n.tokLog), "api /v1/mcp/e2e-setup-oauth Bearer sk-ant-oat01-e2efake-setup"):
return "... which never got the token"
case !strings.Contains(readText(n.events), `"event":"login","host":"platform.claude.com","port":443,"method":"POST","path":"/v1/oauth/token","login":"claude"`):
return "the event names the login"
}
return ""
}
// setupTokenShows runs devvm agent login claude --setup-token on a
// terminal of its own, its input one that never ends (an end of input on
// a terminal is a key of its own), until the terminal shows Claude Code's
// address, its escapes and line breaks taken out, then stops it: the
// first step that failed, nothing when none.
func setupTokenShows(n *netRun) string {
s := n.s
args := append([]string{"sh", "-c", `sleep 600 | "$@"`, "sh"}, OnTTY(s.sb.devvm, "agent", "login", "claude", "--setup-token")...)
p, err := s.Start(Cmd{Args: args, Dir: n.n, Env: n.tokenEnv("DEVVM_MESSAGES=names")})
if err != nil {
return err.Error()
}
// The terminal's devvm is in a session of its own, which Stop's TERM
// doesn't reach: it ends at the hangup, then ends the login in the VM
// (endLogin) through the same pid file the next check's login writes,
// so the next check waits until it's gone.
defer func() {
p.Stop()
_ = s.Until(time.Minute, func() error {
if len(pids(s, "[a]gent login claude --setup-token")) > 0 {
return errors.New("devvm agent login claude --setup-token runs on")
}
return nil
})
}()
escapes := regexp.MustCompile(`\x1b(\[[0-9;?<=>]*[ -/]*[@-~]|\][^\x07\x1b]*(\x07|\x1b\\)|[@-Z\\-_])|[\r\n]`)
shown := func() bool { return strings.Contains(escapes.ReplaceAllString(p.Out(), ""), "oauth/authorize?") }
_ = s.Until(2*time.Minute, func() error {
if shown() || !p.Running() {
return nil
}
return errors.New("no address yet")
})
if !shown() {
return "Claude Code shows its address on the terminal\n" + n.loginWhy(p)
}
return ""
}
// claudeLogin runs devvm agent login claude, its input a FIFO this
// process writes the code into once the address shows. Since 2.1.330,
// Claude Code shows a short address without the state, unless its
// environment has CLAUDE_CODE_FULL_SIGN_IN_LINK=1, which Claude Code's
// docs give scripts that read the address (code.claude.com/docs/en/env-vars,
// 2026-11), so the VM's login shells set it for the rest of the run;
// earlier releases ignore it and always show the full address.
func (n *netRun) claudeLogin() error {
s := n.s
if err := inVM(s, n.vm, "echo 'export CLAUDE_CODE_FULL_SIGN_IN_LINK=1' | sudo tee /etc/profile.d/e2e-claude-link.sh >/dev/null"); err != nil {
return errors.New("the VM's login shells show Claude Code's full address: " + err.Error())
}
fifo := filepath.Join(s.sb.work, "claude.in")
_ = os.Remove(fifo)
madeFile(s.sb, fifo)
if err := syscall.Mkfifo(fifo, 0o600); err != nil {
return err
}
defer os.Remove(fifo)
p, err := s.Start(Cmd{
Args: []string{"sh", "-c", `exec "$0" agent login claude <"$1"`, s.sb.devvm, fifo},
Dir: n.n, Env: n.tokenEnv("DEVVM_MESSAGES=names"),
})
if err != nil {
return err
}
var w *os.File
_ = s.Until(30*time.Second, func() error {
if !p.Running() {
return nil
}
// Open for writing fails until devvm's shell opens it to read.
var err error
if w, err = os.OpenFile(fifo, os.O_WRONLY|syscall.O_NONBLOCK, 0); err != nil {
w = nil
}
return err
})
what := "devvm agent login claude never read its input"
if w != nil {
what = n.claudeLoggedIn(p, w)
_ = w.Close()
}
if what != "" {
p.Stop()
return errors.New(what + "\n" + n.loginWhy(p))
}
return nil
}
// claudeLoggedIn is the Claude Code login's first step that failed,
// nothing when none.
func (n *netRun) claudeLoggedIn(p *Proc, w *os.File) string {
s := n.s
state := loginState(s, p)
if state == "" {
return "Claude Code shows its address"
}
if _, err := w.WriteString("e2e-code#" + state + "\n"); err != nil {
return "the code on devvm's input: " + err.Error()
}
if rc, ok := waitProc(p, time.Minute); !ok || rc != 0 {
return "devvm agent login ends, saying what the store saved"
}
c := filepath.Join(n.logins, "claude", ".claude", ".credentials.json")
switch {
case !Matches(p.Out(), `^==> agent_own_login_saved display=Claude Code vm=`):
return "it says the login is saved"
case jsonFileAt(c, "claudeAiOauth.refreshToken") != "sk-ant-ort01-e2efake-oauth" || jsonFileAt(c, "claudeAiOauth.accessToken") != "sk-ant-oat01-e2efake-oauth" || modeOf(c) != "600":
return "the store has the real tokens, only yours to read"
case devvmSecret(s, "claude") != "":
return "the claude secret is gone"
case !HasLine(readText(n.tokLog), "POST /v1/oauth/token HTTP/1.1 authorization_code"):
return "the code went to the endpoint as a login's, not setup-token's"
case !n.apiGot("/api/oauth/profile", "sk-ant-oat01-e2efake-oauth"):
return "Claude Code read the profile with the login the proxy put in"
}
r, err := vmRun(s, n.vm, "bash", "-c", "cat ~/.claude/.credentials.json")
v := []byte(r.Stdout)
switch {
case err != nil || strings.Contains(r.Stdout, "e2efake") || jsonAt(v, "claudeAiOauth.refreshToken") != "devvm-proxy-managed" ||
jsonAt(v, "claudeAiOauth.accessToken") != "sk-ant-oat01-devvm-proxy-managed-oauth-"+n.salt:
return "the VM's Claude Code holds placeholders alone"
case vmOut(s, n.vm, "printenv CLAUDE_CODE_OAUTH_TOKEN") != "":
return "and no Claude Code token in its environment"
}
return ""
}
// refreshed is the refresh's first step that failed, nothing when none.
func (n *netRun) refreshed() string {
s := n.s
c := filepath.Join(n.logins, "claude", ".claude", ".credentials.json")
if err := expire(c); err != nil {
return "the store's access token is due: " + err.Error()
}
time.Sleep(31 * time.Second)
before := countLine(readText(n.tokLog), "CONNECT platform.claude.com")
switch {
case inVM(s, n.vm, `curl -sS -m 30 -o /dev/null https://api.anthropic.com/v1/e2e-oauth -H "Authorization: Bearer $(jq -r .claudeAiOauth.accessToken ~/.claude/.credentials.json)"`) != nil:
return "the VM's request with its placeholder"
case countLine(readText(n.tokLog), "CONNECT platform.claude.com") <= before:
return "the host's refresh went to Claude's endpoint through the company proxy"
case !HasLine(readText(n.tokLog), "POST /v1/oauth/token HTTP/1.1 refresh_token"):
return "the refresh asked with the store's refresh token"
case jsonFileAt(c, "claudeAiOauth.refreshToken") != "sk-ant-ort01-e2efake-refreshed" || jsonFileAt(c, "claudeAiOauth.accessToken") != "sk-ant-oat01-e2efake-refreshed":
return "the store holds the refreshed login"
case !n.apiGot("/v1/e2e-oauth", "sk-ant-oat01-e2efake-refreshed"):
return "the VM's request went on with the refreshed access token"
case inVM(s, n.vm, `curl -sS -m 30 -o /dev/null https://mcp-proxy.anthropic.com/v1/mcp/e2e-oauth -H "Authorization: Bearer $(jq -r .claudeAiOauth.accessToken ~/.claude/.credentials.json)"`) != nil:
return "the VM's request to a claude.ai connector, with its placeholder"
case !n.apiGot("/v1/mcp/e2e-oauth", "sk-ant-oat01-e2efake-refreshed"):
return "the connectors' host got the access token"
case NoLeak([]string{"e2efake-oauth", "e2efake-setup", "e2efake-refreshed"}, filepath.Join(n.vmd, "logs"), filepath.Join(s.Paths.Devvm, "logs")) != nil:
return "no log holds a value"
}
return ""
}
// expire makes the Claude Code login in the file f due: its expiresAt 0.
func expire(f string) error {
b, err := os.ReadFile(f)
if err != nil {
return err
}
d := json.NewDecoder(bytes.NewReader(b))
d.UseNumber()
var m map[string]any
if err := d.Decode(&m); err != nil {
return err
}
o, ok := m["claudeAiOauth"].(map[string]any)
if !ok {
return errors.New("no claudeAiOauth")
}
o["expiresAt"] = 0
out, err := json.Marshal(m)
if err != nil {
return err
}
return os.WriteFile(f, out, 0o600)
}
// devvm agent save from the VM with network policy: its ~/.claude.json
// holds the MCP server's header as a placeholder, which the save puts the
// store's value back for, and a changed URL around it is refused.
func netSave(n *netRun) {
s := n.s
store := filepath.Join(n.logins, "claude", ".claude.json")
save := func() (Res, error) { return n.dv("y\n", n.home("DEVVM_PIPED_ANSWERS=1"), "agent", "save", "claude") }
_ = inVM(s, n.vm, `jq ".theme = \"e2e-saved\"" ~/.claude.json > ~/.claude.json.t && mv ~/.claude.json.t ~/.claude.json`)
s.Check("devvm agent save puts the store's MCP header back for the VM's placeholder", func() error {
r, err := save()
if err != nil {
return err
}
c := readText(store)
switch {
case !grepF(r.Out, s.Msg("agent_settings_saved", "agent=claude", "dir="+filepath.Join(s.sb.config(), "devvm", "agents", "claude"), "vm="+n.vm)):
return errors.New("devvm agent save didn't say it saved the settings")
case !strings.Contains(c, "e2e-saved") || !strings.Contains(c, "e2efake-mcp"):
return errors.New("the store's ~/.claude.json lacks the VM's change or the MCP header")
case strings.Contains(c, "devvm-proxy-managed") || strings.Contains(c, "projects"):
return errors.New("the store's ~/.claude.json holds a placeholder or the VM's projects")
}
return nil
})
_ = inVM(s, n.vm, `jq ".mcpServers.e2e.url = \"https://example.com/devvm-e2e-mcp\"" ~/.claude.json > ~/.claude.json.t && mv ~/.claude.json.t ~/.claude.json`)
s.Check("... and refuses it when the server's URL changed, so the header never goes to another host", func() error {
r, err := save()
if err != nil {
return err
}
c := readText(store)
switch {
case !grepF(r.Out, s.Msg("agent_save_placeholder", "path=.claude.json", "vm="+n.vm)):
return errors.New("devvm agent save didn't refuse the placeholder")
case !strings.Contains(c, "api.github.com/devvm-e2e-mcp") || strings.Contains(c, "example.com"):
return errors.New("the store's ~/.claude.json took the changed URL")
}
return nil
})
}
// devvm secret with a fake GitHub token: in the Keychain under this run's
// service on macOS, else in the sandbox's config folder; deleted at the
// end. The token goes on devvm's input, so it's in no process's
// arguments.
// netMise installs a project's own tools with mise on the allowlist tier:
// pnpm's aqua download is checked against its GitHub attestations, which
// need Sigstore's trust root.
func netMise(n *netRun) {
s := n.s
s.Check("mise installs a project's own pnpm and node, attestations checked, on the allowlist tier", func() error {
r, err := s.InVM(n.vm, `d=$(mktemp -d) && cd "$d" && printf '[tools]\npnpm = "12.8.1"\nnode = "22"\n' > mise.toml && mise trust -q && mise install && mise exec -- pnpm --version`)
if err != nil {
return failed("mise install failed: "+tailLines(r.Out, 8), err)
}
if !strings.Contains(r.Stdout, "12.8.1") {
return errors.New("pnpm isn't 12.8.1: " + tailLines(r.Out, 4))
}
return nil
})
}
func netGithub(n *netRun) {
s := n.s
const tok = "ghp_E2EFAKE"
secrets := filepath.Join(n.vmd, "run", "secrets.json")
list := func() string { r, _ := s.Run(devvmCmd(s, "", "", n.home(), "secret")); return r.Stdout }
s.Check("devvm secret set saves a token, and devvm secret lists it saved", func() error {
if _, err := s.Run(devvmCmd(s, "", tok+"\n", n.home(), "secret", "set", "github", "--stdin")); err != nil {
return err
}
if !Matches(list(), `^github +yes `) {
return errors.New("devvm secret doesn't list github saved")
}
if runtime.GOOS == "darwin" && devvmSecret(s, "github") != tok {
return errors.New("the Keychain doesn't hold the token")
}
return nil
})
s.Check("no devvm command prints a secret", func() error {
r, err := s.Run(devvmCmd(s, "", "", n.home(), "secret", "get", "github"))
if err == nil || strings.Contains(r.Out, tok) {
return errors.New("devvm secret get printed it, or didn't fail")
}
return nil
})
s.Check("secret use github: its proxy gets the binding at once, the VM its own placeholder on its next launch", func() error {
if _, err := n.dv("", n.home(), "secret", "use", "github"); err != nil {
return err
}
if !strings.Contains(readText(secrets), `"name":"github"`) {
return errors.New("the proxy has no github binding")
}
if _, err := n.dv("exit\n", n.home()); err != nil {
return err
}
if vmOut(s, n.vm, "echo $GH_TOKEN") != "devvm-proxy-managed-github-"+n.salt {
return errors.New("the VM's GH_TOKEN isn't its placeholder")
}
if has, err := vmHas(s, n.vm, true, "env; cat ~/.config/devvm/env", tok); err != nil || has {
return failed("the VM holds the token", err)
}
if !Matches(list(), `^github +yes .* `+regexp.QuoteMeta(n.vm)+`$`) {
return errors.New("devvm secret doesn't list the VM using github")
}
return nil
})
s.Check("the proxy puts the GitHub token in for its host", func() error {
if err := inVM(s, n.vm, `curl -sS -m 20 -o /dev/null https://api.github.com/user -H "Authorization: token $GH_TOKEN"`); err != nil {
return err
}
if !lineWith(readText(n.events), `"path":"/user"`, `"secrets":["github"]`) {
return errors.New("no event shows the github secret put in")
}
return nil
})
s.Check("secret unuse and rm take the binding and the token away at once", func() error {
if _, err := n.dv("", n.home(), "secret", "unuse", "github"); err != nil {
return err
}
if strings.Contains(readText(secrets), `"name":"github"`) {
return errors.New("the proxy still has the github binding")
}
if _, err := s.Run(devvmCmd(s, "", "", n.home(), "secret", "rm", "--force", "github")); err != nil {
return err
}
if !Matches(list(), `^github +no `) {
return errors.New("devvm secret doesn't list github unsaved")
}
if runtime.GOOS == "darwin" {
if _, err := s.Run(Cmd{Args: []string{"security", "find-generic-password", "-s", s.sb.service, "-a", "github"}}); err == nil {
return errors.New("the Keychain still holds the token")
}
}
return nil
})
}
func netSelfChecks(n *netRun) {
s, vm := n.s, n.vm
s.Check("the self-check passes on the VM devvm started confined", func() error {
return s.GoE2E("TestE2ESelfCheck", "netpolicy", "DEVVM_E2E_VM="+vm, "DEVVM_E2E_WANT=")
})
s.Check("a VM started outside devvm is unenforced, and the self-check stops it", func() error {
if _, err := s.Limactl("stop", vm); err != nil {
return err
}
if _, err := s.Limactl("start", "--tty=false", vm); err != nil {
return err
}
if err := s.GoE2E("TestE2ESelfCheck", "netpolicy", "DEVVM_E2E_VM="+vm, "DEVVM_E2E_WANT=net_unenforced_stop vm="+vm); err != nil {
return err
}
if s.Status(vm) != "Stopped" || s.Paths.State(vm)["net.made.check"] != "unenforced" {
return errors.New("the VM isn't stopped, or its self-check isn't unenforced")
}
return nil
})
s.Check("devvm starts it again confined, and the self-check passes", func() error {
if _, err := n.dv("exit\n", n.home()); err != nil {
return err
}
if s.Status(vm) != "Running" || s.Paths.State(vm)["net.made.check"] != "ok" {
return errors.New("the VM isn't running, or its self-check didn't pass")
}
return nil
})
s.Check("devvm stop stops it and its proxy at once, and devvm starts both again", func() error {
if !proxyRuns(s, n.pxc) {
return errors.New("its proxy doesn't run")
}
if _, err := n.dv("", n.home(), "stop"); err != nil {
return err
}
if s.Status(vm) != "Stopped" || proxyLeft(s, n.pxc) {
return errors.New("the VM or its proxy still runs")
}
if _, err := n.dv("exit\n", n.home()); err != nil {
return err
}
if s.Status(vm) != "Running" || !proxyRuns(s, n.pxc) || s.Paths.State(vm)["net.made.check"] != "ok" {
return errors.New("the VM or its proxy doesn't run again, or its self-check didn't pass")
}
return nil
})
}
// sandboxTitles are the checks of the hostagent's sandbox, macOS's alone.
func sandboxTitles() []string {
return []string{
"the hostagent's profile denies every bind but its ssh port's (TCP) and its own unix sockets'; it has no other",
"with Lima's own forwards on, the sandbox refuses the hostagent a VM server's port, TCP or UDP",
"... and the VM works: ssh, devvm's forwarder (on loopback alone), the proxy, the self-check",
"the next start puts Lima's ignore rules back",
}
}
// limaDNSAsked asks Lima's network's DNS (192.168.5.3) and the host's
// address on it for example.com directly: a refusal or no answer passes.
const limaDNSAsked = `python3 - <<'PY'
import socket, sys
q = bytes.fromhex("123401000001000000000000076578616d706c6503636f6d0000010001")
for h in ("192.168.5.3", "192.168.5.2"):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(3)
try:
s.sendto(q, (h, 53))
d = s.recv(512)
except OSError as e:
print(h, e)
continue
print(h, "rcode", d[3] & 15, "answers", int.from_bytes(d[6:8], "big"))
if d[3] & 15 == 0 and int.from_bytes(d[6:8], "big") > 0:
sys.exit("example.com answered by " + h)
PY`
// macOS: the hostagent's Seatbelt profile denies it every bind but its ssh
// port's, so Lima forwards nothing even without its ignore rules. A
// limactl first on PATH puts Lima's own rule (forward everything) back
// for one start, inside the sandbox devvm starts limactl in: Lima tries
// the guest's ports (its log says so), the sandbox refuses them, and
// devvm's forwarder has them.
func netSandbox(n *netRun) {
s, vm := n.s, n.vm
sp := s.Paths.State(vm)["ports.host.ssh"]
lvm := filepath.Join(s.sb.lima, vm)
ha := filepath.Join(lvm, "ha.stderr.log")
p95, p96 := s.Port(47195), s.Port(47196)
haListens := func() string {
pid := strings.TrimSpace(readText(filepath.Join(lvm, "ha.pid")))
r, _ := s.Run(Cmd{Args: []string{"lsof", "-nP", "-a", "-p", pid, "-iTCP", "-sTCP:LISTEN", "-iUDP"}})
var l []string
for i, line := range strings.Split(strings.TrimRight(r.Stdout, "\n"), "\n") {
if f := strings.Fields(line); i > 0 && len(f) >= 9 {
l = append(l, f[7]+" "+f[8])
}
}
return strings.Join(l, "\n")
}
titles := sandboxTitles()
s.Check(titles[0], func() error {
sbf := filepath.Join(n.vmd, "run", "sandbox.sb")
prof := readText(sbf)
physical, _ := filepath.EvalSymlinks(lvm)
switch {
case sp == "" || countWith(prof, "network-bind") != 3:
return errors.New("no ssh port, or the profile doesn't have three network-bind lines")
case !HasLine(prof, "(deny network-bind)") || !HasLine(prof, `(allow network-bind (local tcp "localhost:`+sp+`"))`):
return errors.New("the profile doesn't deny every bind but the ssh port's")
case !HasLine(prof, `(allow network-bind (local unix-socket (subpath "`+physical+`")))`):
return errors.New("the profile doesn't allow the VM's own unix sockets")
}
bind := fmt.Sprintf(`import socket; socket.socket().bind(("127.0.0.1", %d))`, s.Port(47197))
udp := `import socket; socket.socket(socket.AF_INET, socket.SOCK_DGRAM).bind(("127.0.0.1", ` + sp + `))`
for _, py := range []string{bind, udp} {
if _, err := s.Run(Cmd{Args: []string{"python3", "-c", py}}); err != nil {
return err
}
if _, err := s.Run(Cmd{Args: []string{"/usr/bin/sandbox-exec", "-f", sbf, "python3", "-c", py}}); err == nil {
return errors.New("the profile let python3 bind: " + py)
}
}
if l := haListens(); l != "TCP 127.0.0.1:"+sp {
return errors.New("hostagent's: " + l)
}
if !hostResolverOff(readText(filepath.Join(lvm, "lima.yaml"))) {
return errors.New("lima.yaml's hostResolver isn't off")
}
return nil
})
bypass := filepath.Join(s.sb.work, "lima-bypass")
madeFile(s.sb, bypass)
berr := limaBypass(bypass)
s.Check(titles[1], func() error {
if berr != nil {
return berr
}
if _, err := n.dv("", n.home(), "stop"); err != nil {
return err
}
if _, err := n.dv("exit\n", n.home("PATH="+bypass+":"+os.Getenv("PATH"))); err != nil {
return err
}
switch {
case s.Status(vm) != "Running":
return errors.New("the VM isn't running")
case !HasLine(readText(filepath.Join(lvm, "lima.yaml")), "portForwards: []") || s.Paths.State(vm)["ports.host.ssh"] != sp:
return errors.New("Lima's own rule isn't on, or the ssh port changed")
}
err := inVM(s, vm, fmt.Sprintf(`setsid -f python3 -m http.server -b 0.0.0.0 %d </dev/null >/dev/null 2>&1; `+
`setsid -f python3 -c 'import socket,time; s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM); s.bind(("0.0.0.0",%d)); time.sleep(600)' devvm-e2e-udp </dev/null >/dev/null 2>&1`, p95, p96))
if err != nil {
return err
}
err = s.Until(20*time.Second, func() error {
t := readText(ha)
if strings.Contains(t, fmt.Sprintf("listen tcp 127.0.0.1:%d: bind: operation not permitted", p95)) &&
Matches(t, fmt.Sprintf(`listen udp [0-9.]+:%d: bind: operation not permitted`, p96)) {
return nil
}
return errors.New("the hostagent's log doesn't show its binds refused")
})
switch {
case err != nil:
return err
case !strings.Contains(readText(ha), fmt.Sprintf("Forwarding TCP from 0.0.0.0:%d", p95)):
return errors.New("Lima didn't try to forward the server's port")
case limaListens(s, "tcp", p95) || limaListens(s, "udp", p96):
return errors.New("limactl listens on the server's port")
}
if l := haListens(); l != "TCP 127.0.0.1:"+sp {
return errors.New("hostagent's: " + l)
}
return nil
})
s.Check(titles[2], func() error {
switch {
case !ReachesWithin(s, 20*time.Second, Local(p95)):
return fmt.Errorf("nothing forwards %d", p95)
case loopbackOnly(s, p95) != nil:
return loopbackOnly(s, p95)
case len(pids(s, "[_]ports -vm "+vm+" ")) == 0:
return errors.New("no forwarder")
case limaListens(s, "tcp", p95):
return errors.New("limactl listens")
case out1(s.Run(Cmd{Args: []string{"ssh", "-F", filepath.Join(s.Paths.Devvm, "ssh_config"), "devvm-net", "hostname"}})) != "net":
return errors.New("ssh failed")
case inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://github.com") != nil:
return errors.New("the proxy failed")
case s.Paths.State(vm)["net.made.check"] != "ok":
return fmt.Errorf("no self-check: %q", s.Paths.State(vm)["net.made.check"])
}
return nil
})
_ = inVM(s, vm, fmt.Sprintf("pkill -f '[h]ttp.server -b 0.0.0.0 %s'; pkill -f '[d]evvm-e2e-udp'", strconv.Itoa(p95)[:len(strconv.Itoa(p95))-1]))
s.Check(titles[3], func() error {
if _, err := n.dv("", n.home(), "stop"); err != nil {
return err
}
if _, err := n.dv("exit\n", n.home()); err != nil {
return err
}
r, _ := s.Limactl("list", "--format", "{{range .Config.PortForwards}}{{.GuestIP}}={{.Ignore}} {{end}}", vm)
if got := strings.TrimRight(r.Stdout, "\n"); got != "127.0.0.1=true 0.0.0.0=true ::=true " {
return fmt.Errorf("Lima's rules: %q", got)
}
if s.Paths.State(vm)["net.made.check"] != "ok" {
return errors.New("the self-check didn't pass")
}
return nil
})
}
// hostResolverOff is whether lima.yaml's hostResolver says enabled: false,
// on its line or the next (grep -A1).
func hostResolverOff(yaml string) bool {
l := strings.Split(yaml, "\n")
for i, line := range l {
if strings.HasPrefix(line, "hostResolver:") {
if strings.Contains(line, "enabled: false") || (i+1 < len(l) && strings.Contains(l[i+1], "enabled: false")) {
return true
}
}
}
return false
}
// limaBypass writes dir/limactl: Lima's own rule for the start it runs,
// set where limactl start runs, inside devvm's sandbox, then the real
// limactl.
func limaBypass(dir string) error {
lima, err := exec.LookPath("limactl")
if err != nil {
return err
}
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
q := shell.Quote(lima)
script := "#!/bin/bash\n# e2e: Lima's own rule for this start, set where limactl start runs: inside devvm's sandbox.\n" +
"if [[ $1 == start ]]; then " + q + " edit --tty=false --set '.portForwards = []' \"${@: -1}\" >/dev/null 2>&1 || exit 1; fi\n" +
"exec " + q + " \"$@\"\n"
return os.WriteFile(filepath.Join(dir, "limactl"), []byte(script), 0o700) //nolint:gosec // G306: the stand-in runs
}
// Ctrl+C while Lima's start waits for the VM, its hostagent running it:
// devvm stops the VM and its proxy, so it never runs without the record
// of its confined start, and ls shows its policy on.
func netUnenforced(n *netRun) {
s, vm := n.s, n.vm
lsRe := func(status, policy string) string {
return `^` + regexp.QuoteMeta(vm) + ` +` + status + ` +base +[a-z,]*claude[a-z,]* +off +` + policy + ` +- +/`
}
ls := func() string { r, _ := s.Devvm("", "ls"); return r.Stdout }
_, _ = n.dv("", n.home(), "stop")
rc, out := -1, ""
if p, err := s.Start(devvmCmd(s, n.n, "", n.home(fakeToken, "DEVVM_MESSAGES=names"))); err == nil {
sock := filepath.Join(s.sb.lima, vm, "ha.sock")
for i := 0; i < 300 && !isSocket(sock); i++ {
time.Sleep(200 * time.Millisecond)
}
_ = p.cmd.Process.Signal(os.Interrupt)
rc, _ = waitProc(p, 5*time.Minute)
out = p.Out()
}
s.Check("Ctrl+C during its start stops it and its proxy, and ls shows its policy on", func() error {
switch {
case rc != 130:
return fmt.Errorf("devvm exited %d, not 130:\n%s", rc, tailLines(out, 10))
case !HasLine(out, "==> interrupted_start vm="+vm):
return errors.New("devvm didn't say it stopped the start")
case s.Status(vm) != "Stopped" || proxyLeft(s, n.pxc):
return errors.New("the VM or its proxy runs")
case !Matches(ls(), lsRe("Stopped", "allowlist")):
return errors.New("devvm ls doesn't show it stopped, its policy on")
}
return nil
})
_, _ = n.dv("exit\n", n.home(fakeToken))
outside := func() error {
if _, err := s.Limactl("stop", vm); err != nil {
return err
}
_, err := s.Limactl("start", "--tty=false", vm)
return err
}
stopped := FirstLine(s.Msg("net_unenforced_stop", "vm="+vm))
s.Check("ls shows its policy on, and unenforced once started outside devvm", func() error {
if !Matches(ls(), lsRe("Running", "allowlist")) {
return errors.New("devvm ls doesn't show it running, its policy on")
}
if err := outside(); err != nil {
return err
}
if !Matches(ls(), lsRe("Running", "unenforced")) {
return errors.New("devvm ls doesn't show it unenforced")
}
return nil
})
s.Check("started outside devvm again: without a terminal, devvm stops it and opens nothing", func() error {
if err := outside(); err != nil {
return err
}
r, err := n.dv("echo e2e-opened\n", n.home())
switch {
case err == nil || !grepF(r.Out, stopped):
return errors.New("devvm didn't fail, saying it stopped the VM")
case strings.Contains(r.Out, "e2e-opened") || s.Status(vm) != "Stopped":
return errors.New("devvm opened a shell, or the VM runs")
}
return nil
})
s.Check("so does devvm host, which names nothing", func() error {
if _, err := s.Limactl("start", "--tty=false", vm); err != nil {
return err
}
r, err := n.dv("", n.home(), "host")
switch {
case err == nil || !grepF(r.Out, stopped):
return errors.New("devvm host didn't fail, saying it stopped the VM")
case grepF(r.Out, FirstLine(s.Msg("ssh_host_info", "dir="+n.n, "host=devvm-net"))) || s.Status(vm) != "Stopped":
return errors.New("devvm host named the VM, or it runs")
}
return nil
})
}
// isSocket is whether f is a unix socket.
func isSocket(f string) bool {
st, err := os.Stat(f)
return err == nil && st.Mode()&os.ModeSocket != 0
}
// A server on this machine, on every address, and this machine's LAN
// address, for the tiers' checks: an open VM reaches both through its
// proxy, a confined one neither.
func netOpen(n *netRun) {
s, vm := n.s, n.vm
lp := strconv.Itoa(s.Port(47198))
lan, lerr := s.Start(Cmd{Args: []string{"python3", "-I", "-c", lanServerPy, lp}})
if lerr == nil {
lerr = lan.listening(s.r.window().ctx, "127.0.0.1:"+lp, 15*time.Second)
}
n.lan = lanAddr(s.r.window().ctx)
s.r.out.line(" e2e: this machine's LAN address: %s", cmp.Or(n.lan, "none"))
n.lanServer = lan
proxied := `^http://devvm:[0-9a-f]*@192\.168\.5\.2:` + n.pp + `$`
// Stopped by the self-check, it's still unenforced: its last self-check
// failed. Open from its next start: unconfined, so a direct connection
// works, and its proxy, an open one, set in it and answering its
// self-check, reaches this machine's loopback and its LAN address for
// it.
s.Check("devvm network open: its next start is open: direct connections work, its proxy is set and answers, and through it the VM reaches this machine's loopback and LAN", func() error {
if lerr != nil {
return fmt.Errorf("the LAN server: %w", lerr)
}
if n.tier() != "unenforced" {
return errors.New("devvm network doesn't say unenforced")
}
if _, err := s.Devvm(n.n, "network", "open"); err != nil {
return err
}
r, err := n.dv("exit\n", n.home(fakeToken, "DEVVM_MESSAGES=names"))
switch {
case err != nil:
return err
case n.tier() != "open" || strings.Contains(r.Out, "net_proxy_unreachable") || s.Status(vm) != "Running":
return errors.New("it isn't open and running, or its proxy is unreachable")
case !proxyRuns(s, n.pxc) || !strings.Contains(readText(n.pxc), `"open": true`) || !strings.Contains(readText(n.pxc), `"ca"`):
return errors.New("its proxy doesn't run open, with its CA")
}
if _, err := vmRun(s, vm, "curl", "--noproxy", "*", "-sS", "-m", "10", "-o", "/dev/null", "https://1.1.1.1"); err != nil {
return errors.New("a direct connection failed")
}
switch {
case !vmGrep(s, vm, "printenv HTTPS_PROXY", proxied):
return errors.New("the guest's HTTPS_PROXY isn't its proxy")
case vmOut(s, vm, `curl -fsS -m 10 --noproxy "" http://127.0.0.1:`+lp+"/") != "host-lan":
return errors.New("the VM didn't reach this machine's loopback through its proxy")
case n.lan == "" || vmOut(s, vm, "curl -fsS -m 10 http://"+n.lan+":"+lp+"/") != "host-lan":
return errors.New("the VM didn't reach this machine's LAN address")
}
if r, _ := s.Devvm(n.n, "network", "log"); !strings.Contains(r.Stdout, " allowed 127.0.0.1:"+lp+" ") {
return errors.New("devvm network log doesn't show the loopback allowed")
}
return nil
})
s.Check("open, it has no transparent mode: no service, table or stand-in address, and names resolve as Lima's DNS has them", func() error {
return inVM(s, vm, `! systemctl is-active -q devvm-transparent && [ ! -e /usr/local/sbin/devvm-transparent ] &&
! sudo nft list table inet devvm_transparent >/dev/null 2>&1 && ! ip -4 addr show dev lo | grep -q 198.18.0.1 &&
a=$(getent ahostsv4 github.com | awk 'NR == 1 { print $1 }') && [ -n "$a" ] && [ "$a" != 198.18.0.1 ]`)
})
// Open, it holds no real secret either: the token is nowhere in it,
// its environment and Codex's login hold only its own placeholders,
// and its proxy, whose certificates it verifies with its own CA (no
// -k), puts the token in. A request that goes around the proxy
// carries only the placeholder, which the token's host refuses with a
// 401, and the proxy never sees it.
s.Check("an open VM holds no real secret: its proxy puts the token in, and a direct request carries only the placeholder and gets a 401", func() error {
if err := GuestHasNone(s, vm, []string{"e2efake"}, "/etc", "/home", "/root"); err != nil {
return err
}
if has, err := vmHas(s, vm, true, "env", "e2efake"); err != nil || has {
return failed("the guest's environment holds the token", err)
}
c, _ := vmRun(s, vm, "bash", "-c", "cat ~/.codex/auth.json")
switch {
case vmOut(s, vm, "printenv CLAUDE_CODE_OAUTH_TOKEN") != "sk-ant-oat01-devvm-proxy-managed-"+n.salt:
return errors.New("the guest's token isn't its placeholder")
case !strings.Contains(c.Stdout, "devvm-proxy-managed"):
return errors.New("Codex's login holds no placeholder")
case inVM(s, vm, `curl -sS -m 20 -o /dev/null https://api.anthropic.com/v1/models/devvm-e2e-open -H "Authorization: Bearer $CLAUDE_CODE_OAUTH_TOKEN"`) != nil:
return errors.New("the request through the proxy failed")
case !lineWith(readText(n.events), `"path":"/v1/models/devvm-e2e-open"`, `"secrets":["claude"]`):
return errors.New("no event shows the claude secret put in")
}
code := vmOut(s, vm, `curl --noproxy "*" -sS -m 20 -o /dev/null -w %{http_code} https://api.anthropic.com/v1/models/devvm-e2e-direct -H "Authorization: Bearer $CLAUDE_CODE_OAUTH_TOKEN"`)
if code != "401" || strings.Contains(readText(n.events), "devvm-e2e-direct") {
return fmt.Errorf("direct: %s, or the proxy saw it", code)
}
return nil
})
// Open, behind the run's company proxy on this machine's loopback, its
// password fake: the shell's settings reach the VM's proxy, which goes
// through the company proxy, and never the guest or Lima. The password
// is in devvm's environment alone, in no command's arguments.
corp, cerr := StartCorp(s)
if cerr == nil {
n.corps = append(n.corps, corp.Password())
}
s.Check("open behind a company proxy: the guest gets its own proxy, never the shell's settings, its proxy goes through the company proxy, and no log or guest file keeps the password", func() error {
if cerr != nil {
return cerr
}
r, err := n.dv("exit\n", n.home(corp.Env()...), "restart")
switch {
case err != nil:
return err
case n.tier() != "open":
return errors.New("it isn't open")
case !vmGrep(s, vm, "printenv HTTPS_PROXY", proxied):
return errors.New("the guest's HTTPS_PROXY isn't its own proxy")
case inVM(s, vm, "curl -fsS -m 30 -o /dev/null https://github.com") != nil:
return errors.New("github.com in the VM")
case !corp.Saw(`"event":"tunnel","host":"github.com","port":443`):
return errors.New("the company proxy didn't carry github.com")
case strings.Contains(r.Out, corp.Password()):
return errors.New("devvm restart printed the password")
}
lvm := filepath.Join(s.sb.lima, vm)
pw := []string{corp.Password()}
if err := NoLeak(pw, filepath.Join(s.Paths.Devvm, "logs"), filepath.Join(n.vmd, "logs"), filepath.Join(lvm, "ha.stderr.log"), filepath.Join(lvm, "lima.yaml")); err != nil {
return err
}
return GuestHasNone(s, vm, pw, "/etc", "/root", "/home")
}, Tail(60))
if corp != nil {
corp.Stop()
}
}
func netTiers(n *netRun) {
s, vm := n.s, n.vm
lp := strconv.Itoa(s.Port(47198))
direct := func() bool {
_, err := vmRun(s, vm, "curl", "--noproxy", "*", "-sS", "-m", "5", "-o", "/dev/null", "https://1.1.1.1")
return err == nil
}
code := func(u string) string { return vmOut(s, vm, "curl -s -m 20 -o /dev/null -w %{http_code} "+u) }
s.Check("devvm network allowlist while it runs, without a terminal: unenforced until devvm restart starts it confined", func() error {
r, err := s.Devvm(n.n, "network", "allowlist")
if err != nil || !grepF(r.Out, FirstLine(s.Msg("next_start", "count=1", "vm="+vm))) {
return errors.New("devvm network allowlist didn't say it applies at the next start")
}
if n.tier() != "unenforced" {
return errors.New("devvm network doesn't say unenforced")
}
if _, err := n.dv("exit\n", n.home(), "restart"); err != nil {
return err
}
switch {
case n.tier() != "allowlist" || s.Paths.State(vm)["net.made.check"] != "ok" || !proxyRuns(s, n.pxc):
return errors.New("it isn't confined, its self-check passed and its proxy running")
case direct():
return errors.New("a direct connection worked")
case vmOut(s, vm, `curl -s -m 10 -o /dev/null -w %{http_code} --noproxy "" http://127.0.0.1:`+lp+"/") != "403":
return errors.New("the proxy didn't refuse this machine's loopback")
}
return nil
})
// Between confined tiers devvm network applies the change at once: it
// starts the proxy again, the VM running on. internet: every public
// host on any port, never this machine's addresses.
s.Check("devvm network internet: at once, a host off the allowlist and another port work through its proxy, this machine's LAN address doesn't, and a direct connection still fails", func() error {
if _, err := n.dv("", n.home(), "network", "internet"); err != nil {
return err
}
cfg := readText(n.pxc)
switch {
case s.Status(vm) != "Running" || n.tier() != "internet":
return errors.New("it isn't running on the internet tier")
case !strings.Contains(cfg, "network/internet.policy") || !strings.Contains(cfg, `"deny_policies"`):
return errors.New("its proxy's config has no internet policy or deny policies")
case inVM(s, vm, "curl -fsS -m 20 -o /dev/null https://example.com") != nil:
return errors.New("example.com failed")
}
if _, err := vmRun(s, vm, "bash", "-c", "timeout 10 /usr/local/bin/devvm-ssh-proxy one.one.one.one 53 </dev/null; [ $? -ne 1 ]"); err != nil {
return errors.New("another port failed")
}
switch {
case vmOut(s, vm, "curl -s -m 10 -o /dev/null -w %{http_code} http://"+n.lan+":"+lp+"/") != "403":
return errors.New("this machine's LAN address wasn't refused")
case direct():
return errors.New("a direct connection worked")
}
return nil
})
// none: its agents' hosts alone, and your denies on top.
s.Check("devvm network none: github.com is refused, Claude Code's API isn't, and a deny of yours holds", func() error {
if _, err := n.dv("", n.home(), "network", "none"); err != nil {
return err
}
switch {
case n.tier() != "none":
return errors.New("it isn't on the none tier")
case code("http://github.com/") != "403":
return errors.New("github.com wasn't refused")
case code("https://api.anthropic.com/") == "000":
return errors.New("Claude Code's API was refused")
}
if _, err := s.Devvm(n.n, "network", "deny", "api.anthropic.com"); err != nil {
return err
}
if code("https://api.anthropic.com/") != "000" {
return errors.New("your deny didn't hold")
}
_, err := s.Devvm(n.n, "network", "rm", "!api.anthropic.com")
return err
})
_, _ = n.dv("", n.home(), "network", "allowlist")
if n.lanServer != nil {
n.lanServer.Stop()
}
s.Check("devvm network open while it runs confined, without a terminal: launched, it keeps its proxy until it next starts", func() error {
r, err := s.Devvm(n.n, "network", "open")
if err != nil || !grepF(r.Out, FirstLine(s.Msg("next_start", "count=1", "vm="+vm))) {
return errors.New("devvm network open didn't say it applies at the next start")
}
r, err = n.dv("exit\n", n.home())
switch {
case err != nil || !grepF(r.Out, FirstLine(s.Msg("next_start_net_off", "vm="+vm))):
return errors.New("the launch didn't say it keeps its proxy until its next start")
case !proxyRuns(s, n.pxc) || direct():
return errors.New("its proxy doesn't run, or a direct connection worked")
}
return nil
})
s.Check("rm --force deletes it, its proxy and its state", func() error {
if _, err := n.dv("", n.home(), "rm", "--force", "--no-export"); err != nil {
return err
}
r, _ := s.Limactl("list", "-q")
if r.Has(vm) || Exists(n.vmd) || proxyLeft(s, n.pxc) {
return errors.New("the VM, its folder or its proxy is still there")
}
return nil
})
}
// lanServerPy answers every request on its port, every address, with
// host-lan.
const lanServerPy = `import socketserver, sys
class H(socketserver.StreamRequestHandler):
def handle(self):
while self.rfile.readline() not in (b"\r\n", b"\n", b""):
pass
self.wfile.write(b"HTTP/1.0 200 OK\r\nContent-Length: 9\r\n\r\nhost-lan\n")
socketserver.ThreadingTCPServer.allow_reuse_address = True
socketserver.ThreadingTCPServer(("", int(sys.argv[1])), H).serve_forever()
`
// fakeTokenPy is the fake token endpoints: a company proxy that answers a
// CONNECT to auth.openai.com, platform.claude.com, api.anthropic.com,
// api.groq.com, api.openai.com, generativelanguage.googleapis.com,
// openrouter.ai or customHost itself, with its certificate (port, cert, key
// and its log are its arguments), and refuses any other. customHost answers
// as Anthropic's API does, logging each request's headers.
const fakeTokenPy = `import base64, gzip, json, socket, ssl, sys, threading, urllib.parse
port, cert, key, log = int(sys.argv[1]), sys.argv[2], sys.argv[3], sys.argv[4]
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(cert, key)
ctx.set_alpn_protocols(["http/1.1"])
def b64(d): return base64.urlsafe_b64encode(json.dumps(d).encode()).rstrip(b"=").decode()
def jwt(c): return b64({"alg": "none"}) + "." + b64(c) + ".e2efake-sig"
auth = {"chatgpt_account_id": "acct-e2e", "chatgpt_plan_type": "plus"}
ID = jwt({"email": "[email protected]", "exp": 4070908800, "https://api.openai.com/auth": auth})
AT = jwt({"exp": 4070908800, "https://api.openai.com/auth": auth, "e2efake": "access"})
def head(f):
lines = []
while True:
l = f.readline()
if l in (b"\r\n", b"\n", b""):
return lines
lines.append(l.decode().strip())
def answer(q):
g = q.get("grant_type", [""])[0]
if g == "authorization_code":
return {"id_token": ID, "access_token": AT, "refresh_token": "rt-e2efake-capture", "expires_in": 3600, "token_type": "Bearer"}
if g.endswith("token-exchange") and q.get("subject_token", [""])[0] == ID:
return {"access_token": "sk-e2efake-captured-key"}
return None
# The device code login's two requests before its code's, as opencode 2's Codex device code login
# sends them (JSON): the code to show, then the code's exchange, answered at once.
def device(path):
if path == "/api/accounts/deviceauth/usercode":
return {"device_auth_id": "e2e-device", "user_code": "E2E-0000", "interval": "1"}
if path == "/api/accounts/deviceauth/token":
return {"authorization_code": "e2e-code", "code_verifier": "e2e"}
return None
# Claude's token endpoint, as Claude Code 2.1's bundle has it: JSON, claude setup-token's code with
# expires_in, and a subscription login's code, each answered with tokens holding e2efake.
CLAUDE_ACCOUNT = {"account": {"uuid": "acct-e2e", "email_address": "[email protected]"}, "organization": {"uuid": "org-e2e"}}
def claude(q):
g = q.get("grant_type", "")
if g == "authorization_code" and "expires_in" in q:
return dict(CLAUDE_ACCOUNT, access_token="sk-ant-oat01-e2efake-setup", expires_in=31536000, scope="user:inference", token_type="Bearer")
if g == "authorization_code":
return dict(CLAUDE_ACCOUNT, access_token="sk-ant-oat01-e2efake-oauth", refresh_token="sk-ant-ort01-e2efake-oauth",
expires_in=28800, scope="user:inference user:profile", token_type="Bearer")
if g == "refresh_token" and q.get("refresh_token") == "sk-ant-ort01-e2efake-oauth":
return dict(access_token="sk-ant-oat01-e2efake-refreshed", refresh_token="sk-ant-ort01-e2efake-refreshed",
expires_in=28800, scope="user:inference user:profile", token_type="Bearer")
return None
# Anthropic's API, and its connectors' host (mcp-proxy.anthropic.com), whose paths the checks keep
# apart: the profile Claude Code reads as it logs in, and the Authorization of each request logged,
# and its x-api-key when it has one (an API key's), so a check sees which token the VM's proxy put in.
def api(path, h):
a = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("authorization:")]
k = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("x-api-key:")]
with open(log, "a") as o:
o.write("api " + path + " " + (a[0] if a else "-") + (" x-api-key " + k[0] if k else "") + "\n")
if path.startswith("/api/oauth/profile"):
return 200, {"account": {"uuid": "acct-e2e", "email": "[email protected]", "display_name": "e2e"},
"organization": {"uuid": "org-e2e", "organization_type": "claude_max", "rate_limit_tier": "default_claude_max_20x"}}
return 404, {"type": "error", "error": {"type": "not_found_error", "message": "e2e"}}
# Groq's API, as Anthropic's: each request's Authorization logged, under "groq", answered 404.
def groq(path, h):
a = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("authorization:")]
with open(log, "a") as o:
o.write("groq " + path + " " + (a[0] if a else "-") + "\n")
return 404, {"error": {"message": "e2e", "type": "invalid_request_error"}}
# OpenAI's API, as Groq's, under "openai", and whether the body asks for max_output_tokens, which pi
# leaves out for a ChatGPT sign-in's token but sends with an API key.
def openai(path, h, body):
a = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("authorization:")]
with open(log, "a") as o:
o.write("openai " + path + " " + (a[0] if a else "-") + (" max_output_tokens" if '"max_output_tokens"' in body else " -") + "\n")
return 404, {"error": {"message": "e2e", "type": "invalid_request_error"}}
# Gemini's API, as Groq's, under "google": each request's path, query and all, and its x-goog-api-key,
# the header Google's SDK sends an API key in, so a check sees the key there and not in the URL.
def google(path, h):
k = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("x-goog-api-key:")]
with open(log, "a") as o:
o.write("google " + path + " x-goog-api-key " + (k[0] if k else "-") + "\n")
return 404, {"error": {"code": 404, "message": "e2e", "status": "NOT_FOUND"}}
# OpenRouter's site, under "openrouter": each request's path, Authorization and x-api-key, as pi
# sends its key in either, answered 404.
def openrouter(path, h):
a = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("authorization:")]
k = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("x-api-key:")]
with open(log, "a") as o:
o.write("openrouter " + path + " authorization " + (a[0] if a else "-") + " x-api-key " + (k[0] if k else "-") + "\n")
return 404, {"error": {"code": 404, "message": "e2e"}}
# Cursor's API, under "cursor": each request's line and Authorization, and its login poll answered
# as Cursor answers a login that isn't there, or isn't approved yet: 404, "Not found", plain text.
# Its agent's Run answers as a Connect stream with gzipped messages, as Cursor's does, and with a
# token in one of them when the request asks with x-e2e-token.
def frame(flags, msg):
if flags & 1:
msg = gzip.compress(msg)
return bytes([flags]) + len(msg).to_bytes(4, "big") + msg
def cursor(t, h):
a = [x.split(":", 1)[1].strip() for x in h[1:] if x.lower().startswith("authorization:")]
with open(log, "a") as o:
o.write("cursor " + h[0] + " authorization " + (a[0] if a else "-") + "\n")
if h[0].split(" ")[1] == "/agent.v1.AgentService/Run":
out = frame(1, b"\x0a\x0de2e-run-hello")
if any(x.lower().startswith("x-e2e-token:") for x in h[1:]):
out += frame(1, b"\x0a\x40" + jwt({"sub": "e2e"}).encode())
out += frame(0, b"\x12\x0be2e-run-bye") + frame(2, b"{}")
t.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: application/connect+proto\r\nConnect-Content-Encoding: gzip\r\nContent-Length: %d\r\n\r\n" % len(out) + out)
return
t.sendall(b"HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 9\r\n\r\nNot found")
HOSTS = ("auth.openai.com", "platform.claude.com", "api.anthropic.com", "mcp-proxy.anthropic.com", "api.groq.com", "api.openai.com", "generativelanguage.googleapis.com", "openrouter.ai", "` + customHost + `", "` + cursorAPIHost + `")
def serve(c):
try:
req = head(c.makefile("rb"))
host = req[0].split(" ")[1].rsplit(":", 1)[0] if req and req[0].startswith("CONNECT ") else ""
if host not in HOSTS or not req[0].startswith("CONNECT " + host + ":443 "):
c.sendall(b"HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n")
return
c.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")
if host == "platform.claude.com":
with open(log, "a") as o:
o.write("CONNECT " + host + "\n")
t = ctx.wrap_socket(c, server_side=True)
f = t.makefile("rb")
while True:
h = head(f)
if not h:
return
n = sum(int(x.split(":", 1)[1]) for x in h[1:] if x.lower().startswith("content-length:"))
body = f.read(n).decode()
code = 200
if host == "` + cursorAPIHost + `":
cursor(t, h)
continue
if host in ("api.anthropic.com", "mcp-proxy.anthropic.com", "` + customHost + `"):
code, r = api(h[0].split(" ")[1], h)
elif host == "api.groq.com":
code, r = groq(h[0].split(" ")[1], h)
elif host == "api.openai.com":
code, r = openai(h[0].split(" ")[1], h, body)
elif host == "generativelanguage.googleapis.com":
code, r = google(h[0].split(" ")[1], h)
elif host == "openrouter.ai":
code, r = openrouter(h[0].split(" ")[1], h)
elif host == "platform.claude.com":
q = json.loads(body) if body.startswith("{") else {}
with open(log, "a") as o:
o.write(h[0] + " " + str(q.get("grant_type", "")) + (" expires_in" if "expires_in" in q else "") + "\n")
r = claude(q)
elif h[0].split(" ")[1].startswith("/api/accounts/deviceauth/"):
with open(log, "a") as o:
o.write(h[0] + "\n")
r = device(h[0].split(" ")[1])
else:
with open(log, "a") as o:
o.write(h[0] + " " + urllib.parse.parse_qs(body).get("grant_type", [""])[0] + "\n")
r = answer(urllib.parse.parse_qs(body))
if r is None:
code, r = 400, {"error": "invalid_grant"}
out = json.dumps(r).encode()
t.sendall(b"HTTP/1.1 %d %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n" %
(code, b"OK" if code == 200 else b"Error", len(out)) + out)
except Exception as e:
print(e, file=sys.stderr)
finally:
c.close()
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("127.0.0.1", port))
s.listen(16)
while True:
c, _ = s.accept()
threading.Thread(target=serve, args=(c,), daemon=True).start()
`