Files
w3cjandClaude Opus 5.5 9c74dac11e fix(logs): a VM's logs are held to their caps while it runs
internal/vmlog owns every log a VM can drive but the proxy's own:
Limit (moved from vmssh's logLimit, with Allow for the link session's
warnings), Capped and Trim for ports.log and links.log while they run,
and KeepVM, which the VM's proxy (proxy.IO.KeepLogs, Config.VMLogs from
backend.Run.Logs) and a Lima build run on the serial consoles' and the
hostagent's logs: redacted as they grow (redact.FileFrom), so a reboot
in place can't leave the proxy password there, and rotated past 10 MiB,
counting what a log holds past the hole its writer left
(platform.DataStart).

The proxy's http.Server logs through its own limited log, and its
copied log counts what reached it as stderr; the build proxy's log is
held as a VM's proxy's is. git-pins.jsonl has a cap. Run logs take at
most 256 MiB together, never the newest or a live run's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:04:32 -07:00

200 lines
9.4 KiB
Go

package netpolicy
import (
"encoding/base64"
"encoding/json"
"net/url"
"os"
"reflect"
"slices"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/agentlogin"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/proxy"
"github.com/devvmhq/devvm/internal/redact"
"github.com/devvmhq/devvm/internal/secrets"
"github.com/devvmhq/devvm/internal/state"
"github.com/devvmhq/devvm/internal/vmname"
)
// Every VM's proxy captures logins at OpenAI's and Claude's token endpoints, on every tier,
// whatever its image holds: its config names every agent's (agentlogin.Endpoints). In a Codex VM, the capture is
// Codex's, whatever sends it, and it saves the login in devvm's store, gives the VM its
// placeholders, and writes the VM's bindings again, so the proxy puts the login in at once.
func TestCapturer(t *testing.T) {
d, st := sandbox(t, "base", "codex")
p, _ := testProxy(t, d)
all := agentlogin.Endpoints()
codex := proxy.Capture{Agent: "codex", Host: "auth.openai.com", Path: "/oauth/token"}
if !slices.ContainsFunc(all, func(c proxy.Capture) bool { return reflect.DeepEqual(c, codex) }) {
t.Fatalf("Endpoints() = %+v, without Codex's", all)
}
for _, vm := range []*state.VM{st, newVM(t, d, "devvm-plain-0123456789abc4", "base")} {
for _, tier := range []Tier{TierAllowlist, TierInternet, TierNone, TierOpen} {
cfg, err := p.config(vm, tier, redact.NewSecret("k3y"), backend.Run{}, common{})
if err != nil || !reflect.DeepEqual(cfg.Captures, all) {
t.Errorf("%s, %s: captures %+v, %v", vm.Name(), tier, cfg.Captures, err)
}
}
}
salt, err := Salt(t.Context(), st)
must(t, err)
c := Capturer{Dirs: d, Secrets: p.Secrets}
req := []byte(url.Values{"grant_type": {"authorization_code"}, "code": {"c"}}.Encode())
x := proxy.Exchange{VM: st.Name(), Agents: []string{"codex", "opencode"}, UserAgent: "Bun/1.2", ContentType: "application/x-www-form-urlencoded", Body: req}
if b, answer, err := c.Request(t.Context(), x); err != nil || string(b) != string(req) || answer != nil {
t.Fatalf("Request = %q, %q, %v", b, answer, err)
}
// A JWT-shaped token, as OpenAI's are.
tok := "eyJhbGciOiJub25lIn0.eyJleHAiOjF9.sig"
reply, _ := json.Marshal(map[string]string{"id_token": tok, "access_token": tok + "-at", "refresh_token": "rt-real"})
out, agent, err := c.Response(t.Context(), x, reply)
if err != nil || agent != "codex" || strings.Contains(string(out), "rt-real") || strings.Contains(string(out), tok) {
t.Fatalf("Response = %s, %q, %v", out, agent, err)
}
if b, err := os.ReadFile(secrets.LoginFile(d, "codex")); err != nil || !strings.Contains(string(b), "rt-real") {
t.Errorf("the store's login: %s, %v", b, err)
}
var vm map[string]any
must(t, json.Unmarshal(out, &vm))
l, err := proxy.ReadBindings(d.VM(st.Name()).Secrets())
if err != nil || len(l.Bindings) != 1 || l.Bindings[0].Name != "codex" || l.Bindings[0].Placeholder != vm["access_token"] {
t.Fatalf("bindings %+v, %v; the VM's access token %v", l.Bindings, err, vm["access_token"])
}
// The VM's own placeholder: its claims end in the VM's salt.
parts := strings.Split(l.Bindings[0].Placeholder, ".")
if claims, err := base64.RawURLEncoding.DecodeString(parts[1]); err != nil || !strings.Contains(string(claims), salt) {
t.Errorf("the placeholder's claims %s, %v; want the salt %s", claims, err, salt)
}
if pol, _ := os.ReadFile(d.VM(st.Name()).SecretsPolicy()); !strings.Contains(string(pol), "auth.openai.com:443\n") {
t.Errorf("the VM's allowlist = %q", pol)
}
// No VM by that name, or one with no placeholders yet: nothing is captured.
other := newVM(t, d, vmV, "base", "codex")
for _, vm := range []vmname.VM{"devvm-gone-0123456789abc5", other.Name()} {
x.VM = vm
if _, _, err := c.Request(t.Context(), x); err == nil {
t.Errorf("%s: Request went on", vm)
}
if _, _, err := c.Response(t.Context(), x, reply); err == nil {
t.Errorf("%s: Response saved", vm)
}
}
// Claude Code's login, from a VM whose image hasn't Claude Code: nothing is captured, so it
// can't replace the login every VM with Claude Code uses.
x.VM, x.Agents, x.ContentType, x.Body = st.Name(), []string{"claude"}, "application/json", []byte(`{"grant_type":"authorization_code","code":"c"}`)
refused := "login_capture_refused agent=claude vm=" + st.Name().String()
if _, _, err := c.Request(t.Context(), x); msg.NamesOf(err) != refused {
t.Errorf("Claude Code's login from a VM without it: %v", msg.NamesOf(err))
}
claudeReply := []byte(`{"access_token":"sk-ant-oat01-x","refresh_token":"r","scope":"user:inference"}`)
if _, _, err := c.Response(t.Context(), x, claudeReply); msg.NamesOf(err) != refused {
t.Errorf("Claude Code's login from a VM without it was saved: %v", msg.NamesOf(err))
}
cl := newVM(t, d, "devvm-claude-0123456789abc6", "base", "claude")
_, err = Salt(t.Context(), cl)
must(t, err)
x.VM = cl.Name()
if _, agent, err := c.Response(t.Context(), x, claudeReply); err != nil || agent != "claude" {
t.Errorf("Claude Code's login from its VM: %q, %v", agent, err)
}
// An agent the VM's image doesn't have, such as one devvm keeps no logins of.
x.VM, x.Agents = st.Name(), []string{"aider"}
if _, _, err := c.Request(t.Context(), x); msg.IDOf(err) != "login_capture_refused" {
t.Errorf("another agent: %v", msg.NamesOf(err))
}
}
// A login polled for with a GET (Cursor's): the store reads its query as the request, nothing goes
// on as a body, and the poll's success is saved with the VM given placeholders and its binding.
func TestCapturerGet(t *testing.T) {
d, st := sandbox(t, "base", "cursor")
p, _ := testProxy(t, d)
_, err := Salt(t.Context(), st)
must(t, err)
c := Capturer{Dirs: d, Secrets: p.Secrets}
x := proxy.Exchange{VM: st.Name(), Agents: []string{"cursor"}, Method: "GET", Query: "uuid=u&verifier=v"}
if b, answer, err := c.Request(t.Context(), x); err != nil || b != nil || answer != nil {
t.Fatalf("Request = %q, %q, %v", b, answer, err)
}
bad := x
bad.Query = "uuid=u"
if _, _, err := c.Request(t.Context(), bad); err == nil {
t.Error("a poll without its verifier went on")
}
out, agent, err := c.Response(t.Context(), x, []byte(`{"accessToken":"at-real","refreshToken":"rt-real"}`))
if err != nil || agent != "cursor" || strings.Contains(string(out), "-real") {
t.Fatalf("Response = %s, %q, %v", out, agent, err)
}
if b, err := os.ReadFile(secrets.LoginFile(d, "cursor")); err != nil || !strings.Contains(string(b), "at-real") {
t.Errorf("the store's login: %s, %v", b, err)
}
l, err := proxy.ReadBindings(d.VM(st.Name()).Secrets())
// One binding for each of the CLI's routes, all of the login's placeholder.
if err != nil || len(l.Bindings) < 2 {
t.Fatalf("bindings %+v, %v", l.Bindings, err)
}
for _, b := range l.Bindings {
if b.Name != "cursor" || b.Path == "" || !strings.Contains(string(out), b.Placeholder) {
t.Errorf("binding %+v; the VM got %s", b, out)
}
}
}
// A VM captures a login only for an agent its image has, so a VM can't replace the store's
// login of an agent it doesn't have, which every VM with that agent uses. Where the endpoint is
// shared by both of the VM's agents, the User-Agent picks one of them.
func TestCaptureOwnAgents(t *testing.T) {
d, plain := sandbox(t, "base")
p, _ := testProxy(t, d)
c := Capturer{Dirs: d, Secrets: p.Secrets}
opencode := newVM(t, d, "devvm-oc-0123456789abc7", "base", "opencode")
codex := newVM(t, d, "devvm-cx-0123456789abc8", "base", "codex")
both := newVM(t, d, "devvm-both-0123456789abc9", "base", "codex", "opencode")
for _, vm := range []*state.VM{plain, opencode, codex, both} {
_, err := Salt(t.Context(), vm)
must(t, err)
}
const codexUA, bunUA = "codex_cli_rs/0.166.0", "Bun/1.2"
tok := "eyJhbGciOiJub25lIn0.eyJleHAiOjF9.sig"
reply, _ := json.Marshal(map[string]string{"id_token": tok, "access_token": tok + "-at", "refresh_token": "rt-real"})
req := []byte(url.Values{"grant_type": {"authorization_code"}, "code": {"c"}}.Encode())
for _, tc := range []struct {
vm *state.VM
ua string
agent string // the login saved, or "" when refused
refused string // the agent the refusal names
}{
{vm: plain, ua: codexUA, refused: "codex"},
{vm: plain, ua: bunUA, refused: "opencode"},
{vm: opencode, ua: codexUA, agent: "opencode"},
{vm: opencode, ua: bunUA, agent: "opencode"},
{vm: codex, ua: bunUA, agent: "codex"},
{vm: both, ua: bunUA, agent: "opencode"},
{vm: both, ua: codexUA, agent: "codex"},
} {
for _, a := range []string{"codex", "opencode"} {
_ = os.Remove(secrets.LoginFile(d, a))
}
x := proxy.Exchange{VM: tc.vm.Name(), Agents: []string{"codex", "opencode"}, UserAgent: tc.ua, ContentType: "application/x-www-form-urlencoded", Body: req}
_, _, reqErr := c.Request(t.Context(), x)
_, agent, err := c.Response(t.Context(), x, reply)
if tc.agent == "" {
want := "login_capture_refused agent=" + tc.refused + " vm=" + tc.vm.Name().String()
if msg.NamesOf(reqErr) != want || msg.NamesOf(err) != want {
t.Errorf("%s, %s: Request %v, Response %v; want %s", tc.vm.Name(), tc.ua, msg.NamesOf(reqErr), msg.NamesOf(err), want)
}
} else if reqErr != nil || err != nil || agent != tc.agent {
t.Errorf("%s, %s: Request %v, Response %q, %v; want %s", tc.vm.Name(), tc.ua, reqErr, agent, err, tc.agent)
}
for _, a := range []string{"codex", "opencode"} {
if _, err := os.Stat(secrets.LoginFile(d, a)); (err == nil) != (a == tc.agent) {
t.Errorf("%s, %s: the store's %s login: %v", tc.vm.Name(), tc.ua, a, err)
}
}
}
}