| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // The token sources each hostile place tries (P2 and F5; slice F task |
| 15 | // 14, fix round 1 #4), one attempt per source so a collector that could |
| 16 | // not run is a missing line, never a denial: the token file by path, a |
| 17 | // hard link of it, the environment, KERN_PROCARGS2 of every own process |
| 18 | // (the shell through the runner's direct reader), launchd's environment, |
| 19 | // and every open descriptor. |
| 20 | var tokenSources = []string{"tokenfile", "tokenlink", "tokenenv", "tokenargs", "tokenlaunchd", "tokenfd"} |
| 21 | |
| 22 | // P2 is denied only when every place's every token source says denied, |
| 23 | // the sentinel token was planted (the control), and no line is missing. |
| 24 | func TestP2TriesTheToken(t *testing.T) { |
| 25 | got := ParseAttempts(allLines("denied")["script"] + allLines("denied")["plugin"] + allLines("denied")["test"]) |
| 26 | facts := P2Facts{Home: true, DNS: true, Keychain: true, Token: true, Accepted: map[string]bool{}} |
| 27 | if v, g := JudgeP2(got, facts); v != wire.ProbeDenied || len(g) != 0 { |
| 28 | t.Fatalf("all denied: %s %v", v, g) |
| 29 | } |
| 30 | facts.Token = false |
| 31 | if v, g := JudgeP2(got, facts); v != wire.ProbeFailed || len(g["unknown"]) != 3*len(tokenSources) { |
| 32 | t.Fatalf("the sentinel token was not planted, so denied proves nothing: %s %v", v, g) |
| 33 | } |
| 34 | facts.Token = true |
| 35 | // A collector that ran and could not collect (fix round 2, #4) says |
| 36 | // failed, which is never a denial, and P2's detail says so. |
| 37 | got["plugin.tokenargs"] = "failed" |
| 38 | if v, g := JudgeP2(got, facts); v != wire.ProbeFailed || len(g["failed"]) != 1 || !strings.Contains(p2Detail(g, 33, true, true, ""), "not_collected:plugin.tokenargs") { |
| 39 | t.Fatalf("a failed collection: %s %v", v, g) |
| 40 | } |
| 41 | if ParseAttempts("EQUIPO-PROBE test tokenargs failed\n")["test.tokenargs"] != "failed" { |
| 42 | t.Fatal("a failed line is not read") |
| 43 | } |
| 44 | got["plugin.tokenargs"] = "denied" |
| 45 | for _, src := range tokenSources { |
| 46 | delete(got, "plugin."+src) |
| 47 | if v, g := JudgeP2(got, facts); v != wire.ProbeFailed || len(g["unknown"]) != 1 || g["unknown"][0] != "plugin."+src { |
| 48 | t.Fatalf("a source not collected (%s): %s %v", src, v, g) |
| 49 | } |
| 50 | got["plugin."+src] = "denied" |
| 51 | } |
| 52 | |
| 53 | // The run: the test place reads the token file the probe planted in |
| 54 | // the accounts root (outside any sandbox here) and says so. |
| 55 | f := newProbeFixture(t, allLines("denied"), "FAKE_READ_TOKEN=1") |
| 56 | caps := Capabilities(context.Background(), f.h.Exec, f.h.Tools) |
| 57 | if p, _, _ := probeP2(context.Background(), f.h, caps); p.Verdict != wire.ProbeFailed || !strings.Contains(p.Detail, "allowed:test.tokenfile") { |
| 58 | t.Fatalf("token read: %+v", p) |
| 59 | } |
| 60 | if left, _ := filepath.Glob(filepath.Join(f.h.Paths.AccountsRoot, "*")); len(left) != 0 { |
| 61 | t.Fatalf("the sentinel token was left: %v", left) |
| 62 | } |
| 63 | f = newProbeFixture(t, allLines("denied")) |
| 64 | f.h.Paths.AccountsRoot = "" |
| 65 | if p, _, _ := probeP2(context.Background(), f.h, caps); p.Verdict != wire.ProbeFailed || !strings.Contains(p.Detail, "not_shown_denied:") { |
| 66 | t.Fatalf("no accounts root: %+v", p) |
| 67 | } |
| 68 | // The targets name the runner's binary, the direct reader. |
| 69 | f = newProbeFixture(t, allLines("denied")) |
| 70 | f.h.Scratch, f.h.Runner = filepath.Join(f.dir, "scratch"), "/opt/equipo-runner" |
| 71 | targets := "" |
| 72 | f.h.Gate = func(sandboxed bool) *Gate { |
| 73 | g := &Gate{Env: []string{"PATH=/usr/bin:/bin"}, Lines: 200, Sandbox: wire.SandboxNone} |
| 74 | g.Watch = func(string) {} |
| 75 | return g |
| 76 | } |
| 77 | inner := f.h.Exec |
| 78 | f.h.Exec = func(ctx context.Context, name string, args ...string) (string, error) { |
| 79 | if m, _ := filepath.Glob(filepath.Join(f.h.Scratch, "probe-*", "hostile", "equipo-targets.txt")); len(m) == 1 && targets == "" { |
| 80 | raw, _ := os.ReadFile(m[0]) |
| 81 | targets = string(raw) |
| 82 | } |
| 83 | return inner(ctx, name, args...) |
| 84 | } |
| 85 | probeP2(context.Background(), f.h, caps) |
| 86 | if !strings.Contains(targets, "helper=/opt/equipo-runner\n") { |
| 87 | t.Fatalf("targets %q", targets) |
| 88 | } |
| 89 | |
| 90 | // The shipped script phase really tries each source: run as it is, |
| 91 | // outside any sandbox (Linux), with a stand-in for the runner's |
| 92 | // reader. A source whose collector cannot run says nothing. |
| 93 | bash, err := exec.LookPath("bash") |
| 94 | if err != nil { |
| 95 | t.Skip("no bash") |
| 96 | } |
| 97 | root, err := CopyFixture(t.TempDir()) |
| 98 | if err != nil { |
| 99 | t.Fatal(err) |
| 100 | } |
| 101 | dir := t.TempDir() |
| 102 | token := "sk-ant-oat01-probe-0123456789abcdef" |
| 103 | if err := os.WriteFile(filepath.Join(dir, ".oauth-token"), []byte(token), 0600); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | helper := filepath.Join(dir, "helper") |
| 107 | // The stand-in reads only the hostile shell (its caller's parent, past |
| 108 | // the command substitution): this box's other processes are not ours. |
| 109 | if err := os.WriteFile(helper, []byte("#!/bin/bash\nmode=$2; shift 2\npp=$(ps -o ppid= -p $PPID | tr -d ' ')\ncase \"$mode\" in pids) echo $PPID; echo $pp ;; procargs) for p in \"$@\"; do echo \"procargs-start $p\"; if { [ \"$p\" = \"$PPID\" ] || [ \"$p\" = \"$pp\" ]; } && tr '\\0' '\\n' < /proc/$p/environ; then echo \"procargs-end $p ok\"; else echo \"procargs-end $p refused\"; fi; done ;; fds) for f in /proc/self/fd/*; do echo \"fd ${f##*/} file\"; done; echo fds-done ;; *) exit 2 ;; esac\n"), 0700); err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | run := func(targets string, env ...string) map[string]string { |
| 113 | t.Helper() |
| 114 | if err := os.WriteFile(filepath.Join(root, "equipo-targets.txt"), []byte(targets), 0600); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | cmd := exec.Command(bash, filepath.Join(root, "project", "hostile.sh")) |
| 118 | cmd.Env = append([]string{"PATH=/usr/bin:/bin", "SRCROOT=" + filepath.Join(root, "project"), "TMPDIR=" + t.TempDir()}, env...) |
| 119 | out, _ := cmd.CombinedOutput() |
| 120 | if strings.Contains(string(out), token) { |
| 121 | t.Fatalf("the script printed the token: %s", out) |
| 122 | } |
| 123 | return ParseAttempts(string(out)) |
| 124 | } |
| 125 | base := "state=/nonexistent\ndns=example.invalid\nkeychain=io.amadan.equipo.probe.x\nport_script=1\ntoken=" + token + "\n" |
| 126 | got = run(base + "token_file=" + filepath.Join(dir, ".oauth-token") + "\nhelper=" + helper + "\n") |
| 127 | for src, want := range map[string]string{"tokenfile": "allowed", "tokenlink": "allowed", "tokenenv": "denied", "tokenargs": "denied", "tokenfd": "denied"} { |
| 128 | if got["script."+src] != want { |
| 129 | t.Fatalf("%s: %q, want %s (%v)", src, got["script."+src], want, got) |
| 130 | } |
| 131 | } |
| 132 | if _, ok := got["script.tokenlaunchd"]; ok { |
| 133 | t.Fatalf("launchctl is not here, so launchd's environment cannot be shown: %v", got) |
| 134 | } |
| 135 | if got = run(base+"token_file="+filepath.Join(dir, ".oauth-token")+"\nhelper="+helper+"\n", "CLAUDE_CODE_OAUTH_TOKEN="+token); got["script.tokenenv"] != "allowed" || got["script.tokenargs"] != "allowed" { |
| 136 | t.Fatalf("the token in the environment: %v", got) |
| 137 | } |
| 138 | got = run(base + "token_file=" + filepath.Join(dir, "missing") + "\nhelper=" + filepath.Join(dir, "no-helper") + "\n") |
| 139 | if got["script.tokenfile"] != "denied" || got["script.tokenlink"] != "denied" { |
| 140 | t.Fatalf("a token file it could not read: %v", got) |
| 141 | } |
| 142 | for _, src := range []string{"tokenargs", "tokenfd"} { |
| 143 | if _, ok := got["script."+src]; ok { |
| 144 | t.Fatalf("%s said without its reader: %v", src, got) |
| 145 | } |
| 146 | } |
| 147 | // A reader the kernel refused for the shell itself (its control) |
| 148 | // shows no process's environment: no line. |
| 149 | refusing := filepath.Join(dir, "refusing") |
| 150 | if err := os.WriteFile(refusing, []byte("#!/bin/bash\nmode=$2; shift 2\ncase \"$mode\" in procargs) for p in \"$@\"; do echo \"procargs-start $p\"; echo \"procargs-end $p refused\"; done; exit 1 ;; fds) echo fds-done ;; esac\n"), 0700); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if got = run(base + "token_file=" + filepath.Join(dir, ".oauth-token") + "\nhelper=" + refusing + "\n"); got["script.tokenargs"] != "failed" { |
| 154 | t.Fatalf("tokenargs not failed though the reader read nothing: %v", got) |
| 155 | } |
| 156 | // The process list could not be read, or did not show the shell |
| 157 | // itself (its control): tokenargs failed, never denied. |
| 158 | for name, body := range map[string]string{ |
| 159 | "nolist": "#!/bin/bash\ncase \"$2\" in pids) exit 1 ;; procargs) shift 2; for p in \"$@\"; do echo \"procargs-start $p\"; echo \"procargs-end $p ok\"; done ;; fds) echo fds-done ;; esac\n", |
| 160 | "notself": "#!/bin/bash\ncase \"$2\" in pids) echo 1 ;; procargs) shift 2; for p in \"$@\"; do echo \"procargs-start $p\"; echo \"procargs-end $p ok\"; done ;; fds) echo fds-done ;; esac\n", |
| 161 | } { |
| 162 | h := filepath.Join(dir, name) |
| 163 | if err := os.WriteFile(h, []byte(body), 0700); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if got = run(base + "token_file=" + filepath.Join(dir, ".oauth-token") + "\nhelper=" + h + "\n"); got["script.tokenargs"] != "failed" { |
| 167 | t.Fatalf("%s: tokenargs %q, want failed (%v)", name, got["script.tokenargs"], got) |
| 168 | } |
| 169 | } |
| 170 | // Every hostile part tries every source, the Swift parts by |
| 171 | // KERN_PROCARGS2 and posix_spawn (no Process on iOS). |
| 172 | for _, file := range []string{"package/Plugins/HostilePlugin/HostilePlugin.swift", "package/Tests/HostileTests/HostileTests.swift", "project/hostile.sh"} { |
| 173 | raw, _ := os.ReadFile(filepath.Join(root, file)) |
| 174 | for _, src := range tokenSources { |
| 175 | if !strings.Contains(string(raw), `"`+src+`"`) && !strings.Contains(string(raw), "say "+src) && !strings.Contains(string(raw), "src "+src) { |
| 176 | t.Fatalf("%s does not try %s", file, src) |
| 177 | } |
| 178 | } |
| 179 | if strings.HasSuffix(file, ".swift") && (!strings.Contains(string(raw), "KERN_PROCARGS2") || !strings.Contains(string(raw), "posix_spawn") || strings.Contains(string(raw), "#if os(macOS)\n sources") || !strings.Contains(string(raw), `("tokenargs", "failed")`)) { |
| 180 | t.Fatalf("%s: the reader and launchctl on every platform", file) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |