package mac import ( "context" "errors" "io/fs" "os" "path/filepath" "slices" "testing" "time" ) // install makes a runner's home and install dir as the installer does // (0700 each) and returns both. func install(t *testing.T) (home, dir string) { t.Helper() home = t.TempDir() dir = filepath.Join(home, ".local", "lib", "equipo-runner") // A umask of 002 leaves t.TempDir's group-writable. if err := os.Chmod(home, 0700); err != nil { t.Fatal(err) } if err := os.MkdirAll(dir, 0700); err != nil { t.Fatal(err) } return home, dir } func homeCLI(home, dir string, body []byte) ClaudeCLI { c := NewClaudeCLI(home, dir) c.pin = digest(body) return c } // Every directory from the home down to the binary is a real directory // (never a link), the runner user's, written by no one else; the tool // directories are exactly 0700 (a looser one of ours is tightened by // Provision and refused by Verify). func TestClaudeChainIsPrivate(t *testing.T) { ctx := context.Background() body := fakeClaude() refused := func(t *testing.T, cli ClaudeCLI, why string) { t.Helper() if err := cli.Provision(ctx, serve(body, nil)); err == nil { t.Fatalf("provisioned %s", why) } if _, err := cli.Verify(); err == nil { t.Fatalf("verified %s", why) } } t.Run("install dir outside the home", func(t *testing.T) { home, _ := install(t) other := t.TempDir() refused(t, homeCLI(home, other, body), "outside the home") }) t.Run("the home is a symlink", func(t *testing.T) { home, dir := install(t) link := filepath.Join(t.TempDir(), "home") if err := os.Symlink(home, link); err != nil { t.Fatal(err) } rel, _ := filepath.Rel(home, dir) refused(t, homeCLI(link, filepath.Join(link, rel), body), "under a symlinked home") }) t.Run("symlinked install dir", func(t *testing.T) { home, dir := install(t) // A sibling inside lib: os.Root alone would follow it. elsewhere := filepath.Join(filepath.Dir(dir), "elsewhere") if err := os.Mkdir(elsewhere, 0700); err != nil { t.Fatal(err) } if err := os.Remove(dir); err != nil { t.Fatal(err) } if err := os.Symlink("elsewhere", dir); err != nil { t.Fatal(err) } refused(t, homeCLI(home, dir, body), "through a symlinked install dir") if left := entries(t, elsewhere); len(left) != 0 { t.Fatalf("wrote through the link: %v", left) } }) t.Run("group-writable ancestor", func(t *testing.T) { home, dir := install(t) lib := filepath.Join(home, ".local") if err := os.Chmod(lib, 0770); err != nil { t.Fatal(err) } refused(t, homeCLI(home, dir, body), "under a group-writable .local") if left := entries(t, dir); len(left) != 0 { t.Fatalf("wrote %v", left) } if err := os.Chmod(lib, 0702); err != nil { t.Fatal(err) } refused(t, homeCLI(home, dir, body), "under an other-writable .local") }) t.Run("a group-writable home", func(t *testing.T) { home, dir := install(t) if err := os.Chmod(home, 0770); err != nil { t.Fatal(err) } defer os.Chmod(home, 0700) refused(t, homeCLI(home, dir, body), "in a group-writable home") }) t.Run("a 0755 tool directory", func(t *testing.T) { home, dir := install(t) tools := filepath.Join(dir, "tools") if err := os.Mkdir(tools, 0700); err != nil { t.Fatal(err) } if err := os.Chmod(tools, 0755); err != nil { t.Fatal(err) } cli := homeCLI(home, dir, body) if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatalf("a 0755 tools directory of ours: %v", err) } if info, _ := os.Lstat(tools); info.Mode().Perm() != 0700 { t.Fatalf("tools left %v", info.Mode()) } // Loosened after provisioning: no launch until Provision tightens it. if err := os.Chmod(ClaudeDir(dir), 0755); err != nil { t.Fatal(err) } if _, err := cli.Verify(); err == nil { t.Fatal("verified under a 0755 version directory") } if err := cli.Launch(ctx, func(context.Context, []string) error { t.Fatal("started"); return nil }); !isUnavailable(err) { t.Fatalf("launch under a 0755 version directory: %v", err) } if err := cli.Provision(ctx, noFetch(t)); err != nil { t.Fatal(err) } if _, err := cli.Verify(); err != nil { t.Fatalf("after tightening: %v", err) } }) t.Run("a foreign-owned directory", func(t *testing.T) { home, dir := install(t) defer func(was func(fs.FileInfo) int) { ownerOf = was }(ownerOf) real := ownerOf for _, name := range []string{filepath.Base(home), "lib", "tools", "claude-2.1.312", "claude"} { cli := homeCLI(home, dir, body) ownerOf = real if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatal(err) } ownerOf = func(fi fs.FileInfo) int { if fi.Name() == name || name == filepath.Base(home) && fi.Name() == "." { return 4242 } return real(fi) } if _, err := cli.Verify(); err == nil { t.Fatalf("verified with %s owned by another user", name) } if err := cli.Provision(ctx, serve(body, nil)); err == nil && name != "claude" { t.Fatalf("provisioned with %s owned by another user", name) } } }) } func isUnavailable(err error) bool { var u *ClaudeUnavailable return errors.As(err, &u) && u.Reason() == "engineering_unavailable" } // swapIn renames a changed CLI over the verified one, as a writer racing // the launch would. func swapIn(t *testing.T, dir string, body []byte) { t.Helper() b := slices.Clone(body) b[0] ^= 1 next := ClaudePath(dir) + ".next" if err := os.WriteFile(next, b, 0700); err != nil { t.Error(err) } if err := os.Rename(next, ClaudePath(dir)); err != nil { t.Error(err) } } // A file swapped while it is being hashed, or after the hash and before // start, stops the launch: what the hash read is not what the path names. func TestLaunchRefusesARacingSwap(t *testing.T) { ctx := context.Background() body := fakeClaude() start := func(t *testing.T) func(context.Context, []string) error { return func(context.Context, []string) error { t.Fatal("started a CLI swapped during its launch") return nil } } for _, at := range []struct { name string hook *func() }{{"during hashing", &claudeDuringHash}, {"before start", &claudeBeforeStart}} { t.Run(at.name+": renamed over", func(t *testing.T) { home, dir := install(t) cli := homeCLI(home, dir, body) if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatal(err) } defer func() { *at.hook = nil }() *at.hook = func() { swapIn(t, dir, body) } if err := cli.Launch(ctx, start(t)); !isUnavailable(err) { t.Fatalf("a swap %s: %v", at.name, err) } }) } // Written in place during hashing (a part already hashed), the // modification time put back: the change time still moves. t.Run("during hashing: written in place", func(t *testing.T) { home, dir := install(t) cli := homeCLI(home, dir, body) if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatal(err) } info, err := os.Lstat(ClaudePath(dir)) if err != nil { t.Fatal(err) } defer func() { claudeDuringHash = nil }() claudeDuringHash = func() { time.Sleep(10 * time.Millisecond) f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0) if err != nil { t.Error(err) return } _, err = f.WriteAt([]byte{body[0] ^ 1}, 0) f.Close() if err == nil { err = os.Chtimes(ClaudePath(dir), info.ModTime(), info.ModTime()) } if err != nil { t.Error(err) } } if err := cli.Launch(ctx, start(t)); !isUnavailable(err) { t.Fatalf("written in place during hashing: %v", err) } }) // The capability report too: a file written while Verify hashes it // does not verify. t.Run("Verify: written in place during hashing", func(t *testing.T) { home, dir := install(t) cli := homeCLI(home, dir, body) if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatal(err) } defer func() { claudeDuringHash = nil }() claudeDuringHash = func() { time.Sleep(10 * time.Millisecond) if f, err := os.OpenFile(ClaudePath(dir), os.O_WRONLY, 0); err == nil { _, _ = f.WriteAt(body[:1], 0) f.Close() } } if _, err := cli.Verify(); err == nil { t.Fatal("verified a file written while it was hashed") } }) // Untouched: it starts, and the CLI's file stays open until start // returns (its inode cannot be reused for another file meanwhile). t.Run("untouched", func(t *testing.T) { home, dir := install(t) cli := homeCLI(home, dir, body) if err := cli.Provision(ctx, serve(body, nil)); err != nil { t.Fatal(err) } ran := false if err := cli.Launch(ctx, func(_ context.Context, argv []string) error { ran = argv[0] == ClaudePath(dir) return nil }); err != nil || !ran { t.Fatalf("untouched: %v %v", err, ran) } }) }