oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package mac
2
3import (
4 "context"
5 "errors"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "slices"
10 "testing"
11 "time"
12)
13
14// install makes a runner's home and install dir as the installer does
15// (0700 each) and returns both.
16func install(t *testing.T) (home, dir string) {
17 t.Helper()
18 home = t.TempDir()
19 dir = filepath.Join(home, ".local", "lib", "equipo-runner")
20 // A umask of 002 leaves t.TempDir's group-writable.
21 if err := os.Chmod(home, 0700); err != nil {
22 t.Fatal(err)
23 }
24 if err := os.MkdirAll(dir, 0700); err != nil {
25 t.Fatal(err)
26 }
27 return home, dir
28}
29
30func homeCLI(home, dir string, body []byte) ClaudeCLI {
31 c := NewClaudeCLI(home, dir)
32 c.pin = digest(body)
33 return c
34}
35
36// Every directory from the home down to the binary is a real directory
37// (never a link), the runner user's, written by no one else; the tool
38// directories are exactly 0700 (a looser one of ours is tightened by
39// Provision and refused by Verify).
40func TestClaudeChainIsPrivate(t *testing.T) {
41 ctx := context.Background()
42 body := fakeClaude()
43 refused := func(t *testing.T, cli ClaudeCLI, why string) {
44 t.Helper()
45 if err := cli.Provision(ctx, serve(body, nil)); err == nil {
46 t.Fatalf("provisioned %s", why)
47 }
48 if _, err := cli.Verify(); err == nil {
49 t.Fatalf("verified %s", why)
50 }
51 }
52
53 t.Run("install dir outside the home", func(t *testing.T) {
54 home, _ := install(t)
55 other := t.TempDir()
56 refused(t, homeCLI(home, other, body), "outside the home")
57 })
58
59 t.Run("the home is a symlink", func(t *testing.T) {
60 home, dir := install(t)
61 link := filepath.Join(t.TempDir(), "home")
62 if err := os.Symlink(home, link); err != nil {
63 t.Fatal(err)
64 }
65 rel, _ := filepath.Rel(home, dir)
66 refused(t, homeCLI(link, filepath.Join(link, rel), body), "under a symlinked home")
67 })
68
69 t.Run("symlinked install dir", func(t *testing.T) {
70 home, dir := install(t)
71 // A sibling inside lib: os.Root alone would follow it.
72 elsewhere := filepath.Join(filepath.Dir(dir), "elsewhere")
73 if err := os.Mkdir(elsewhere, 0700); err != nil {
74 t.Fatal(err)
75 }
76 if err := os.Remove(dir); err != nil {
77 t.Fatal(err)
78 }
79 if err := os.Symlink("elsewhere", dir); err != nil {
80 t.Fatal(err)
81 }
82 refused(t, homeCLI(home, dir, body), "through a symlinked install dir")
83 if left := entries(t, elsewhere); len(left) != 0 {
84 t.Fatalf("wrote through the link: %v", left)
85 }
86 })
87
88 t.Run("group-writable ancestor", func(t *testing.T) {
89 home, dir := install(t)
90 lib := filepath.Join(home, ".local")
91 if err := os.Chmod(lib, 0770); err != nil {
92 t.Fatal(err)
93 }
94 refused(t, homeCLI(home, dir, body), "under a group-writable .local")
95 if left := entries(t, dir); len(left) != 0 {
96 t.Fatalf("wrote %v", left)
97 }
98 if err := os.Chmod(lib, 0702); err != nil {
99 t.Fatal(err)
100 }
101 refused(t, homeCLI(home, dir, body), "under an other-writable .local")
102 })
103
104 t.Run("a group-writable home", func(t *testing.T) {
105 home, dir := install(t)
106 if err := os.Chmod(home, 0770); err != nil {
107 t.Fatal(err)
108 }
109 defer os.Chmod(home, 0700)
110 refused(t, homeCLI(home, dir, body), "in a group-writable home")
111 })
112
113 t.Run("a 0755 tool directory", func(t *testing.T) {
114 home, dir := install(t)
115 tools := filepath.Join(dir, "tools")
116 if err := os.Mkdir(tools, 0700); err != nil {
117 t.Fatal(err)
118 }
119 if err := os.Chmod(tools, 0755); err != nil {
120 t.Fatal(err)
121 }
122 cli := homeCLI(home, dir, body)
123 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
124 t.Fatalf("a 0755 tools directory of ours: %v", err)
125 }
126 if info, _ := os.Lstat(tools); info.Mode().Perm() != 0700 {
127 t.Fatalf("tools left %v", info.Mode())
128 }
129 // Loosened after provisioning: no launch until Provision tightens it.
130 if err := os.Chmod(ClaudeDir(dir), 0755); err != nil {
131 t.Fatal(err)
132 }
133 if _, err := cli.Verify(); err == nil {
134 t.Fatal("verified under a 0755 version directory")
135 }
136 if err := cli.Launch(ctx, func(context.Context, []string) error { t.Fatal("started"); return nil }); !isUnavailable(err) {
137 t.Fatalf("launch under a 0755 version directory: %v", err)
138 }
139 if err := cli.Provision(ctx, noFetch(t)); err != nil {
140 t.Fatal(err)
141 }
142 if _, err := cli.Verify(); err != nil {
143 t.Fatalf("after tightening: %v", err)
144 }
145 })
146
147 t.Run("a foreign-owned directory", func(t *testing.T) {
148 home, dir := install(t)
149 defer func(was func(fs.FileInfo) int) { ownerOf = was }(ownerOf)
150 real := ownerOf
151 for _, name := range []string{filepath.Base(home), "lib", "tools", "claude-2.1.312", "claude"} {
152 cli := homeCLI(home, dir, body)
153 ownerOf = real
154 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
155 t.Fatal(err)
156 }
157 ownerOf = func(fi fs.FileInfo) int {
158 if fi.Name() == name || name == filepath.Base(home) && fi.Name() == "." {
159 return 4242
160 }
161 return real(fi)
162 }
163 if _, err := cli.Verify(); err == nil {
164 t.Fatalf("verified with %s owned by another user", name)
165 }
166 if err := cli.Provision(ctx, serve(body, nil)); err == nil && name != "claude" {
167 t.Fatalf("provisioned with %s owned by another user", name)
168 }
169 }
170 })
171}
172
173func isUnavailable(err error) bool {
174 var u *ClaudeUnavailable
175 return errors.As(err, &u) && u.Reason() == "engineering_unavailable"
176}
177
178// swapIn renames a changed CLI over the verified one, as a writer racing
179// the launch would.
180func swapIn(t *testing.T, dir string, body []byte) {
181 t.Helper()
182 b := slices.Clone(body)
183 b[0] ^= 1
184 next := ClaudePath(dir) + ".next"
185 if err := os.WriteFile(next, b, 0700); err != nil {
186 t.Error(err)
187 }
188 if err := os.Rename(next, ClaudePath(dir)); err != nil {
189 t.Error(err)
190 }
191}
192
193// A file swapped while it is being hashed, or after the hash and before
194// start, stops the launch: what the hash read is not what the path names.
195func TestLaunchRefusesARacingSwap(t *testing.T) {
196 ctx := context.Background()
197 body := fakeClaude()
198 start := func(t *testing.T) func(context.Context, []string) error {
199 return func(context.Context, []string) error {
200 t.Fatal("started a CLI swapped during its launch")
201 return nil
202 }
203 }
204 for _, at := range []struct {
205 name string
206 hook *func()
207 }{{"during hashing", &claudeDuringHash}, {"before start", &claudeBeforeStart}} {
208 t.Run(at.name+": renamed over", func(t *testing.T) {
209 home, dir := install(t)
210 cli := homeCLI(home, dir, body)
211 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
212 t.Fatal(err)
213 }
214 defer func() { *at.hook = nil }()
215 *at.hook = func() { swapIn(t, dir, body) }
216 if err := cli.Launch(ctx, start(t)); !isUnavailable(err) {
217 t.Fatalf("a swap %s: %v", at.name, err)
218 }
219 })
220 }
221 // Written in place during hashing (a part already hashed), the
222 // modification time put back: the change time still moves.
223 t.Run("during hashing: written in place", func(t *testing.T) {
224 home, dir := install(t)
225 cli := homeCLI(home, dir, body)
226 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
227 t.Fatal(err)
228 }
229 info, err := os.Lstat(ClaudePath(dir))
230 if err != nil {
231 t.Fatal(err)
232 }
233 defer func() { claudeDuringHash = nil }()
234 claudeDuringHash = func() {
235 time.Sleep(10 * time.Millisecond)
236 f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0)
237 if err != nil {
238 t.Error(err)
239 return
240 }
241 _, err = f.WriteAt([]byte{body[0] ^ 1}, 0)
242 f.Close()
243 if err == nil {
244 err = os.Chtimes(ClaudePath(dir), info.ModTime(), info.ModTime())
245 }
246 if err != nil {
247 t.Error(err)
248 }
249 }
250 if err := cli.Launch(ctx, start(t)); !isUnavailable(err) {
251 t.Fatalf("written in place during hashing: %v", err)
252 }
253 })
254 // The capability report too: a file written while Verify hashes it
255 // does not verify.
256 t.Run("Verify: written in place during hashing", func(t *testing.T) {
257 home, dir := install(t)
258 cli := homeCLI(home, dir, body)
259 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
260 t.Fatal(err)
261 }
262 defer func() { claudeDuringHash = nil }()
263 claudeDuringHash = func() {
264 time.Sleep(10 * time.Millisecond)
265 if f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0); err == nil {
266 _, _ = f.WriteAt(body[:1], 0)
267 f.Close()
268 }
269 }
270 if _, err := cli.Verify(); err == nil {
271 t.Fatal("verified a file written while it was hashed")
272 }
273 })
274 // Untouched: it starts, and the CLI's file stays open until start
275 // returns (its inode cannot be reused for another file meanwhile).
276 t.Run("untouched", func(t *testing.T) {
277 home, dir := install(t)
278 cli := homeCLI(home, dir, body)
279 if err := cli.Provision(ctx, serve(body, nil)); err != nil {
280 t.Fatal(err)
281 }
282 ran := false
283 if err := cli.Launch(ctx, func(_ context.Context, argv []string) error {
284 ran = argv[0] == ClaudePath(dir)
285 return nil
286 }); err != nil || !ran {
287 t.Fatalf("untouched: %v %v", err, ran)
288 }
289 })
290}
291