package thin import ( "context" "encoding/json" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/wire" ) // A Mac whose sandbox is not proven runs nothing outside it unless the // dispatch carries the person's choice: no report (bare) or a report // whose P2 failed (srt, unproven) refuses with sandbox_unproven, runs // nothing and taints nothing. With the choice it runs bare and taints. func TestMacChecksRefuseUnprovenWithoutConsent(t *testing.T) { tree := filepath.Join(t.TempDir(), "tree") if err := os.MkdirAll(tree, 0700); err != nil { t.Fatal(err) } newChecks := func(t *testing.T, probes *macProbes) *macChecks { return &macChecks{probes: probes, taint: loadTaint(filepath.Join(t.TempDir(), "tainted")), creds: holdsNothing(), 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(ctx context.Context, m *macChecks) (gateResult, int) { recorded := 0 res := m.Run(ctx, "checks-t-1", tree, []string{"/bin/true"}, time.Minute, func(string) error { recorded++; return nil }) return res, recorded } // No report yet: the bare gate. bare := newChecks(t, newProbeRig(t).p) if bare.pick() != bare.bare { t.Fatal("rig: not bare") } res, recorded := run(context.Background(), bare) if res.reason != wire.ReasonSandboxUnproven || !res.terminated || recorded != 0 || bare.taint.Tainted() { t.Fatalf("bare without consent: %+v recorded %d tainted %v", res, recorded, bare.taint.Tainted()) } // P2 failed but the fixture built under srt: unproven, still refused. x := newProbeRig(t) x.verdict = wire.ProbeFailed x.started(t) unproven := newChecks(t, x.p) if unproven.pick() != unproven.unproven { t.Fatal("rig: not unproven") } res, recorded = run(context.Background(), unproven) if res.reason != wire.ReasonSandboxUnproven || recorded != 0 || unproven.taint.Tainted() { t.Fatalf("unproven without consent: %+v recorded %d tainted %v", res, recorded, unproven.taint.Tainted()) } // The person chose it for this attempt: it runs bare and taints. res, recorded = run(withUnsandboxed(context.Background(), true), bare) if res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !bare.taint.Tainted() { t.Fatalf("bare with consent: %+v recorded %d tainted %v", res, recorded, bare.taint.Tainted()) } if withUnsandboxed(context.Background(), false) != context.Background() || unsandboxedChosen(context.Background()) { t.Fatal("no choice carried as one") } } // holdsNothing is the credential operations of a Mac that holds no // model credential: its hold is known, and clear (D1 allows a choice). func holdsNothing() *macCreds { return &macCreds{held: func(context.Context) bool { return false }} } // consentGate is a gate that records whether each run carried the // person's choice, and refuses like an unproven Mac without it. type consentGate struct { stubGate chosen []bool } func (g *consentGate) Run(ctx context.Context, a, dir string, argv []string, l time.Duration, record func(string) error) gateResult { g.chosen = append(g.chosen, unsandboxedChosen(ctx)) if !unsandboxedChosen(ctx) { return gateResult{reason: wire.ReasonSandboxUnproven, terminated: true, sandbox: wire.SandboxNone} } return g.stubGate.Run(ctx, a, dir, argv, l, record) } // The worker carries the dispatch's choice to the gate, and a refusal is // its own report: failed sandbox_unproven, terminated, with no checks // outcome, so the instance puts the step back to wait. func TestWorkerCarriesSandboxConsent(t *testing.T) { x := newWorkerRig(t) g := &consentGate{stubGate: stubGate{sandbox: wire.SandboxNone}} x.w.Gate = g x.chunk(t, "c1", x.header(), x.pack, 0, 1, "") rep := x.run() if rep.State != wire.StepFailed || rep.Reason != wire.ReasonSandboxUnproven || !rep.Termination || rep.Checks != "" || rep.ExitCode != nil || len(g.chosen) != 1 || g.chosen[0] { t.Fatalf("refused attempt %+v chosen %v", rep, g.chosen) } x.r.Attempt, x.r.Unsandboxed = 2, true h := x.header() h.ID = "tr_2" x.chunk(t, "c2", h, x.pack, 0, 1, "") rep = x.run() if rep.State != wire.StepDone || rep.Checks != wire.ChecksPassed || rep.ChecksSandbox != wire.SandboxNone || len(g.chosen) != 2 || !g.chosen[1] { t.Fatalf("chosen attempt %+v chosen %v", rep, g.chosen) } } // Mixed versions: a dispatch on a connection below protocol 7 is never // consent, whatever it carries; at protocol 7 the choice is kept. func TestOlderProtocolDispatchIsNoConsent(t *testing.T) { for _, protocol := range []int{wire.ProtocolEmptyRepos, wire.ProtocolSandboxConsent} { inst := newFakeInstance(t) _, state, _, _ := startLink(t, inst.url, &stubCoordinator{}) c := <-inst.conns read(t, c, wire.FrameHello) welcome, _ := json.Marshal(wire.Frame{ID: "w-1", Type: wire.FrameWelcome, Data: json.RawMessage(`{"generation":3,"runner_id":"RUNNER1","protocol":` + itoa(int64(protocol)) + `,"role":"primary"}`)}) write(t, c, welcome) read(t, c, wire.FrameSync) d := wire.DispatchCommand{Person: "owner@example.com", TaskID: "tk1", StepID: "s1", Attempt: 1, Specialist: wire.SpecialistChecks, Repo: "amadan.net/oficina/sample", Branch: "equipo/tk1", Findings: []wire.Finding{}, Unsandboxed: true} f, err := wire.NewFrame("c9", wire.FrameDispatch, d) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(f) write(t, c, raw) read(t, c, wire.FrameAck) r, ok, err := state.nextStep(context.Background()) if err != nil || !ok { t.Fatalf("protocol %d: dispatch not kept: %v", protocol, err) } if want := protocol >= wire.ProtocolSandboxConsent; r.Unsandboxed != want { t.Fatalf("protocol %d: consent %v, want %v", protocol, r.Unsandboxed, want) } } }