//go:build testbuild package thin import ( "context" "os" "path/filepath" "sync/atomic" "testing" "time" "amadan.net/oficina/equipo/internal/fakeclaude" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "amadan.net/oficina/equipo/internal/wire" ) // D1 (Paul, 2026-11-28): once a Mac holds a Claude credential, its checks // run sandboxed or not at all. The worker refuses a dispatch that carries // the creator's choice to run outside the sandbox (sandbox_unproven: // nothing run, nothing tainted) for as long as its own durable hold is // set: a token file, a connection in progress, or only a Keychain item a // refused sign-in left, across a restart. Once the hold is gone the // choice runs, outside the sandbox, and taints as before. func TestWorkerRefusesUnsandboxedWithToken(t *testing.T) { r := newDarwinRig(t) w := r.worker() tree := filepath.Join(t.TempDir(), "tree") if err := os.MkdirAll(tree, 0o700); err != nil { t.Fatal(err) } // An unproven Mac (no probe report: the bare gate), its checks // against the worker's real credential operations. checks := func(creds *macCreds) *macChecks { return &macChecks{probes: newProbeRig(t).p, taint: loadTaint(filepath.Join(t.TempDir(), "tainted")), creds: creds, srt: "/x/srt", sandboxed: &mac.Gate{Sandbox: wire.SandboxSeatbelt}, unproven: &mac.Gate{SRT: "/x/srt", Sandbox: wire.SandboxNone}, bare: &mac.Gate{Sandbox: wire.SandboxNone, Env: []string{"PATH=/usr/bin:/bin"}}} } run := func(m *macChecks) (gateResult, int) { recorded := 0 res := m.Run(withUnsandboxed(context.Background(), true), "checks-t-1", tree, []string{"/bin/true"}, time.Minute, func(string) error { recorded++; return nil }) return res, recorded } refused := func(what string, m *macChecks) { t.Helper() if m.pick() != m.bare { t.Fatalf("%s: rig: not the bare gate", what) } res, recorded := run(m) if res.reason != wire.ReasonSandboxUnproven || !res.terminated || recorded != 0 || m.taint.Tainted() { t.Fatalf("%s: ran outside the sandbox: %+v recorded %d tainted %v", what, res, recorded, m.taint.Tainted()) } } // Its connections not opened yet: nothing says the Mac holds none. refused("no hold known", checks(&macCreds{})) refused("no credential operations", checks(nil)) // A Mac that never held a credential: the choice runs, and taints. fresh := checks(r.m) if res, recorded := run(fresh); res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !fresh.taint.Tainted() { t.Fatalf("a Mac holding nothing refused the choice: %+v recorded %d", res, recorded) } // A token file. home, end := r.paste(w.connector, "c1", fakeclaude.Token("b@example.com")) if end.State != wire.ConnHealthy { t.Fatalf("rig: paste %+v", end) } if _, err := os.Stat(filepath.Join(home, provider.TokenFile)); err != nil { t.Fatalf("rig: no token file: %v", err) } refused("token file", checks(r.m)) // Signed out: the files go, the hold with them. if err := w.connector.Disconnect(r.ctx, "f-out-1", wire.DisconnectCommand{Connection: "c1", Person: connPerson, LocalID: filepath.Base(home), RunnerID: tokenRunner}); err != nil { t.Fatal(err) } if entries, _ := os.ReadDir(r.root); len(entries) != 0 || w.held(r.ctx) { t.Fatalf("rig: still held after the sign-out: %d entries", len(entries)) } // A sign-in in progress, refused, leaving only a Keychain item; then // a restart. r.kc.set(true) link, _ := r.signIn(w.connector, "s1") if link == "" || !w.held(r.ctx) { t.Fatal("rig: no sign-in in progress") } refused("connecting", checks(r.m)) item := r.own(r.signInDir()) r.kc.add(item) r.codeIn(w.connector, "s1", link, "c@example.com") if end := r.ended("s1"); end.Reason != "provider_refused" { t.Fatalf("rig: refused sign-in %+v", end) } if entries, _ := os.ReadDir(r.root); len(entries) != 0 || len(r.m.keychain.Residue()) == 0 { t.Fatalf("rig: not only the Keychain item left: %d entries, residue %v", len(entries), r.m.keychain.Residue()) } refused("Keychain residue", checks(r.m)) again := darwinRigAt(t, r.dir) again.kc = r.kc again.m.keychain.List, again.m.keychain.Delete = r.kc.list, r.kc.delete again.kc.r = again w2 := openWorkerConns(again.cfg, again.workerOptions(0), again.state, runnerproto.RunnerCredential{RunnerID: tokenRunner, Person: connPerson}, again.m, nil, nil) refused("Keychain residue after a restart", checks(again.m)) // The item goes; the restart's settling clears the hold: the choice // runs outside the sandbox, and taints. r.kc.set(false) w2.connector.spawn = func(f func()) { f() } if err := w2.start(again.ctx); err != nil { t.Fatal(err) } if w2.held(again.ctx) { t.Fatal("rig: still held once everything is gone") } free := checks(again.m) if res, recorded := run(free); res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !free.taint.Tainted() { t.Fatalf("no hold: the choice did not run: %+v recorded %d tainted %v", res, recorded, free.taint.Tainted()) } // A connection that starts as the check starts: the hold is read // again once the taint is set, so a store that got past the taint's // check before it was set never shares the Mac with the check. var reads atomic.Int32 racing := checks(&macCreds{held: func(context.Context) bool { return reads.Add(1) > 1 }}) res, recorded := run(racing) if res.reason != wire.ReasonSandboxUnproven || recorded != 0 || reads.Load() < 2 { t.Fatalf("a hold that came as the check started: %+v recorded %d reads %d", res, recorded, reads.Load()) } } // The capability report names the Mac's runner user, so the Machines // page can say as whom to close a folder; a name the wire would refuse // is left out, never sent to sink the report. func TestMacCapsNameTheRunnerUser(t *testing.T) { r := newDarwinRig(t) h := &macHost{creds: r.m, taint: r.m.taint} caps := wire.Capabilities{} h.credentialCaps(r.ctx, &caps) if !wire.ValidRunnerUser(r.user) || caps.RunnerUser != r.user || wire.ValidCapabilities(caps) != nil { t.Fatalf("runner user %q, want %q", caps.RunnerUser, r.user) } r.m.user = "not a name" caps = wire.Capabilities{} h.credentialCaps(r.ctx, &caps) if caps.RunnerUser != "" || wire.ValidCapabilities(caps) != nil { t.Fatalf("an invalid runner user sent: %q", caps.RunnerUser) } }