| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/mac" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // A Mac whose sandbox is not proven runs nothing outside it unless the |
| 16 | // dispatch carries the person's choice: no report (bare) or a report |
| 17 | // whose P2 failed (srt, unproven) refuses with sandbox_unproven, runs |
| 18 | // nothing and taints nothing. With the choice it runs bare and taints. |
| 19 | func TestMacChecksRefuseUnprovenWithoutConsent(t *testing.T) { |
| 20 | tree := filepath.Join(t.TempDir(), "tree") |
| 21 | if err := os.MkdirAll(tree, 0700); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | newChecks := func(t *testing.T, probes *macProbes) *macChecks { |
| 25 | 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"}}} |
| 26 | } |
| 27 | run := func(ctx context.Context, m *macChecks) (gateResult, int) { |
| 28 | recorded := 0 |
| 29 | res := m.Run(ctx, "checks-t-1", tree, []string{"/bin/true"}, time.Minute, func(string) error { recorded++; return nil }) |
| 30 | return res, recorded |
| 31 | } |
| 32 | |
| 33 | // No report yet: the bare gate. |
| 34 | bare := newChecks(t, newProbeRig(t).p) |
| 35 | if bare.pick() != bare.bare { |
| 36 | t.Fatal("rig: not bare") |
| 37 | } |
| 38 | res, recorded := run(context.Background(), bare) |
| 39 | if res.reason != wire.ReasonSandboxUnproven || !res.terminated || recorded != 0 || bare.taint.Tainted() { |
| 40 | t.Fatalf("bare without consent: %+v recorded %d tainted %v", res, recorded, bare.taint.Tainted()) |
| 41 | } |
| 42 | |
| 43 | // P2 failed but the fixture built under srt: unproven, still refused. |
| 44 | x := newProbeRig(t) |
| 45 | x.verdict = wire.ProbeFailed |
| 46 | x.started(t) |
| 47 | unproven := newChecks(t, x.p) |
| 48 | if unproven.pick() != unproven.unproven { |
| 49 | t.Fatal("rig: not unproven") |
| 50 | } |
| 51 | res, recorded = run(context.Background(), unproven) |
| 52 | if res.reason != wire.ReasonSandboxUnproven || recorded != 0 || unproven.taint.Tainted() { |
| 53 | t.Fatalf("unproven without consent: %+v recorded %d tainted %v", res, recorded, unproven.taint.Tainted()) |
| 54 | } |
| 55 | |
| 56 | // The person chose it for this attempt: it runs bare and taints. |
| 57 | res, recorded = run(withUnsandboxed(context.Background(), true), bare) |
| 58 | if res.reason != "" || res.sandbox != wire.SandboxNone || recorded == 0 || !bare.taint.Tainted() { |
| 59 | t.Fatalf("bare with consent: %+v recorded %d tainted %v", res, recorded, bare.taint.Tainted()) |
| 60 | } |
| 61 | if withUnsandboxed(context.Background(), false) != context.Background() || unsandboxedChosen(context.Background()) { |
| 62 | t.Fatal("no choice carried as one") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // holdsNothing is the credential operations of a Mac that holds no |
| 67 | // model credential: its hold is known, and clear (D1 allows a choice). |
| 68 | func holdsNothing() *macCreds { |
| 69 | return &macCreds{held: func(context.Context) bool { return false }} |
| 70 | } |
| 71 | |
| 72 | // consentGate is a gate that records whether each run carried the |
| 73 | // person's choice, and refuses like an unproven Mac without it. |
| 74 | type consentGate struct { |
| 75 | stubGate |
| 76 | chosen []bool |
| 77 | } |
| 78 | |
| 79 | func (g *consentGate) Run(ctx context.Context, a, dir string, argv []string, l time.Duration, record func(string) error) gateResult { |
| 80 | g.chosen = append(g.chosen, unsandboxedChosen(ctx)) |
| 81 | if !unsandboxedChosen(ctx) { |
| 82 | return gateResult{reason: wire.ReasonSandboxUnproven, terminated: true, sandbox: wire.SandboxNone} |
| 83 | } |
| 84 | return g.stubGate.Run(ctx, a, dir, argv, l, record) |
| 85 | } |
| 86 | |
| 87 | // The worker carries the dispatch's choice to the gate, and a refusal is |
| 88 | // its own report: failed sandbox_unproven, terminated, with no checks |
| 89 | // outcome, so the instance puts the step back to wait. |
| 90 | func TestWorkerCarriesSandboxConsent(t *testing.T) { |
| 91 | x := newWorkerRig(t) |
| 92 | g := &consentGate{stubGate: stubGate{sandbox: wire.SandboxNone}} |
| 93 | x.w.Gate = g |
| 94 | x.chunk(t, "c1", x.header(), x.pack, 0, 1, "") |
| 95 | rep := x.run() |
| 96 | if rep.State != wire.StepFailed || rep.Reason != wire.ReasonSandboxUnproven || !rep.Termination || rep.Checks != "" || rep.ExitCode != nil || len(g.chosen) != 1 || g.chosen[0] { |
| 97 | t.Fatalf("refused attempt %+v chosen %v", rep, g.chosen) |
| 98 | } |
| 99 | x.r.Attempt, x.r.Unsandboxed = 2, true |
| 100 | h := x.header() |
| 101 | h.ID = "tr_2" |
| 102 | x.chunk(t, "c2", h, x.pack, 0, 1, "") |
| 103 | rep = x.run() |
| 104 | if rep.State != wire.StepDone || rep.Checks != wire.ChecksPassed || rep.ChecksSandbox != wire.SandboxNone || len(g.chosen) != 2 || !g.chosen[1] { |
| 105 | t.Fatalf("chosen attempt %+v chosen %v", rep, g.chosen) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Mixed versions: a dispatch on a connection below protocol 7 is never |
| 110 | // consent, whatever it carries; at protocol 7 the choice is kept. |
| 111 | func TestOlderProtocolDispatchIsNoConsent(t *testing.T) { |
| 112 | for _, protocol := range []int{wire.ProtocolEmptyRepos, wire.ProtocolSandboxConsent} { |
| 113 | inst := newFakeInstance(t) |
| 114 | _, state, _, _ := startLink(t, inst.url, &stubCoordinator{}) |
| 115 | c := <-inst.conns |
| 116 | read(t, c, wire.FrameHello) |
| 117 | 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"}`)}) |
| 118 | write(t, c, welcome) |
| 119 | read(t, c, wire.FrameSync) |
| 120 | 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} |
| 121 | f, err := wire.NewFrame("c9", wire.FrameDispatch, d) |
| 122 | if err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | raw, _ := json.Marshal(f) |
| 126 | write(t, c, raw) |
| 127 | read(t, c, wire.FrameAck) |
| 128 | r, ok, err := state.nextStep(context.Background()) |
| 129 | if err != nil || !ok { |
| 130 | t.Fatalf("protocol %d: dispatch not kept: %v", protocol, err) |
| 131 | } |
| 132 | if want := protocol >= wire.ProtocolSandboxConsent; r.Unsandboxed != want { |
| 133 | t.Fatalf("protocol %d: consent %v, want %v", protocol, r.Unsandboxed, want) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |