| 1 | //go:build testbuild |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/fakeclaude" |
| 14 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 15 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 16 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 17 | "amadan.net/oficina/equipo/internal/wire" |
| 18 | ) |
| 19 | |
| 20 | // D1 (Paul, 2026-11-28): once a Mac holds a Claude credential, its checks |
| 21 | // run sandboxed or not at all. The worker refuses a dispatch that carries |
| 22 | // the creator's choice to run outside the sandbox (sandbox_unproven: |
| 23 | // nothing run, nothing tainted) for as long as its own durable hold is |
| 24 | // set: a token file, a connection in progress, or only a Keychain item a |
| 25 | // refused sign-in left, across a restart. Once the hold is gone the |
| 26 | // choice runs, outside the sandbox, and taints as before. |
| 27 | func TestWorkerRefusesUnsandboxedWithToken(t *testing.T) { |
| 28 | r := newDarwinRig(t) |
| 29 | w := r.worker() |
| 30 | tree := filepath.Join(t.TempDir(), "tree") |
| 31 | if err := os.MkdirAll(tree, 0o700); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | // An unproven Mac (no probe report: the bare gate), its checks |
| 35 | // against the worker's real credential operations. |
| 36 | checks := func(creds *macCreds) *macChecks { |
| 37 | 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"}}} |
| 38 | } |
| 39 | run := func(m *macChecks) (gateResult, int) { |
| 40 | recorded := 0 |
| 41 | res := m.Run(withUnsandboxed(context.Background(), true), "checks-t-1", tree, []string{"/bin/true"}, time.Minute, func(string) error { recorded++; return nil }) |
| 42 | return res, recorded |
| 43 | } |
| 44 | refused := func(what string, m *macChecks) { |
| 45 | t.Helper() |
| 46 | if m.pick() != m.bare { |
| 47 | t.Fatalf("%s: rig: not the bare gate", what) |
| 48 | } |
| 49 | res, recorded := run(m) |
| 50 | if res.reason != wire.ReasonSandboxUnproven || !res.terminated || recorded != 0 || m.taint.Tainted() { |
| 51 | t.Fatalf("%s: ran outside the sandbox: %+v recorded %d tainted %v", what, res, recorded, m.taint.Tainted()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Its connections not opened yet: nothing says the Mac holds none. |
| 56 | refused("no hold known", checks(&macCreds{})) |
| 57 | refused("no credential operations", checks(nil)) |
| 58 | |
| 59 | // A Mac that never held a credential: the choice runs, and taints. |
| 60 | fresh := checks(r.m) |
| 61 | if res, recorded := run(fresh); res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !fresh.taint.Tainted() { |
| 62 | t.Fatalf("a Mac holding nothing refused the choice: %+v recorded %d", res, recorded) |
| 63 | } |
| 64 | |
| 65 | // A token file. |
| 66 | home, end := r.paste(w.connector, "c1", fakeclaude.Token("b@example.com")) |
| 67 | if end.State != wire.ConnHealthy { |
| 68 | t.Fatalf("rig: paste %+v", end) |
| 69 | } |
| 70 | if _, err := os.Stat(filepath.Join(home, provider.TokenFile)); err != nil { |
| 71 | t.Fatalf("rig: no token file: %v", err) |
| 72 | } |
| 73 | refused("token file", checks(r.m)) |
| 74 | // Signed out: the files go, the hold with them. |
| 75 | if err := w.connector.Disconnect(r.ctx, "f-out-1", wire.DisconnectCommand{Connection: "c1", Person: connPerson, LocalID: filepath.Base(home), RunnerID: tokenRunner}); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if entries, _ := os.ReadDir(r.root); len(entries) != 0 || w.held(r.ctx) { |
| 79 | t.Fatalf("rig: still held after the sign-out: %d entries", len(entries)) |
| 80 | } |
| 81 | |
| 82 | // A sign-in in progress, refused, leaving only a Keychain item; then |
| 83 | // a restart. |
| 84 | r.kc.set(true) |
| 85 | link, _ := r.signIn(w.connector, "s1") |
| 86 | if link == "" || !w.held(r.ctx) { |
| 87 | t.Fatal("rig: no sign-in in progress") |
| 88 | } |
| 89 | refused("connecting", checks(r.m)) |
| 90 | item := r.own(r.signInDir()) |
| 91 | r.kc.add(item) |
| 92 | r.codeIn(w.connector, "s1", link, "c@example.com") |
| 93 | if end := r.ended("s1"); end.Reason != "provider_refused" { |
| 94 | t.Fatalf("rig: refused sign-in %+v", end) |
| 95 | } |
| 96 | if entries, _ := os.ReadDir(r.root); len(entries) != 0 || len(r.m.keychain.Residue()) == 0 { |
| 97 | t.Fatalf("rig: not only the Keychain item left: %d entries, residue %v", len(entries), r.m.keychain.Residue()) |
| 98 | } |
| 99 | refused("Keychain residue", checks(r.m)) |
| 100 | again := darwinRigAt(t, r.dir) |
| 101 | again.kc = r.kc |
| 102 | again.m.keychain.List, again.m.keychain.Delete = r.kc.list, r.kc.delete |
| 103 | again.kc.r = again |
| 104 | w2 := openWorkerConns(again.cfg, again.workerOptions(0), again.state, runnerproto.RunnerCredential{RunnerID: tokenRunner, Person: connPerson}, again.m, nil, nil) |
| 105 | refused("Keychain residue after a restart", checks(again.m)) |
| 106 | |
| 107 | // The item goes; the restart's settling clears the hold: the choice |
| 108 | // runs outside the sandbox, and taints. |
| 109 | r.kc.set(false) |
| 110 | w2.connector.spawn = func(f func()) { f() } |
| 111 | if err := w2.start(again.ctx); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | if w2.held(again.ctx) { |
| 115 | t.Fatal("rig: still held once everything is gone") |
| 116 | } |
| 117 | free := checks(again.m) |
| 118 | if res, recorded := run(free); res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !free.taint.Tainted() { |
| 119 | t.Fatalf("no hold: the choice did not run: %+v recorded %d tainted %v", res, recorded, free.taint.Tainted()) |
| 120 | } |
| 121 | |
| 122 | // A connection that starts as the check starts: the hold is read |
| 123 | // again once the taint is set, so a store that got past the taint's |
| 124 | // check before it was set never shares the Mac with the check. |
| 125 | var reads atomic.Int32 |
| 126 | racing := checks(&macCreds{held: func(context.Context) bool { return reads.Add(1) > 1 }}) |
| 127 | res, recorded := run(racing) |
| 128 | if res.reason != wire.ReasonSandboxUnproven || recorded != 0 || reads.Load() < 2 { |
| 129 | t.Fatalf("a hold that came as the check started: %+v recorded %d reads %d", res, recorded, reads.Load()) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // The capability report names the Mac's runner user, so the Machines |
| 134 | // page can say as whom to close a folder; a name the wire would refuse |
| 135 | // is left out, never sent to sink the report. |
| 136 | func TestMacCapsNameTheRunnerUser(t *testing.T) { |
| 137 | r := newDarwinRig(t) |
| 138 | h := &macHost{creds: r.m, taint: r.m.taint} |
| 139 | caps := wire.Capabilities{} |
| 140 | h.credentialCaps(r.ctx, &caps) |
| 141 | if !wire.ValidRunnerUser(r.user) || caps.RunnerUser != r.user || wire.ValidCapabilities(caps) != nil { |
| 142 | t.Fatalf("runner user %q, want %q", caps.RunnerUser, r.user) |
| 143 | } |
| 144 | r.m.user = "not a name" |
| 145 | caps = wire.Capabilities{} |
| 146 | h.credentialCaps(r.ctx, &caps) |
| 147 | if caps.RunnerUser != "" || wire.ValidCapabilities(caps) != nil { |
| 148 | t.Fatalf("an invalid runner user sent: %q", caps.RunnerUser) |
| 149 | } |
| 150 | } |
| 151 | |