| 1 | package containment |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "net" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | "amadan.net/oficina/equipo/internal/testci" |
| 15 | ) |
| 16 | |
| 17 | // piLaunch is a Pi launch on m: a sealed package tree, the pinned files |
| 18 | // (a copy of true stands in for Node), the runtime home under the |
| 19 | // scratch root's homes, a key, a tool socket and a workspace. |
| 20 | func piLaunch(t *testing.T, m *Manager) (provider.Launch, []string, string) { |
| 21 | t.Helper() |
| 22 | root := t.TempDir() |
| 23 | put := func(name, content string, mode os.FileMode) string { |
| 24 | p := filepath.Join(root, name) |
| 25 | if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | if err := os.WriteFile(p, []byte(content), mode); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | return p |
| 32 | } |
| 33 | trueBin, err := os.ReadFile("/usr/bin/true") |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | node := put("node", string(trueBin), 0755) |
| 38 | tree := filepath.Join(root, "pi", "node_modules") |
| 39 | put("pi/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", "cli", 0644) |
| 40 | put("pi/node_modules/dep/index.js", "dep", 0644) |
| 41 | if err := provider.SealTree(tree); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | t.Cleanup(func() { unsealTree(filepath.Dir(tree)) }) |
| 45 | sum, err := provider.TreeSHA256(tree) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | home := filepath.Join(m.cfg.ScratchRoot, "homes", "pi-home") |
| 50 | if err := os.MkdirAll(filepath.Join(home, "agent"), 0700); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | os.Chmod(filepath.Join(m.cfg.ScratchRoot, "homes"), 0700) |
| 54 | os.Chmod(home, 0700) |
| 55 | work := filepath.Join(root, "work") |
| 56 | os.Mkdir(work, 0700) |
| 57 | keyHome := filepath.Join(root, "key") |
| 58 | os.Mkdir(keyHome, 0700) |
| 59 | put("key/"+provider.TokenFile, "tx-test-key", 0600) |
| 60 | sockDir := filepath.Join(root, "tool") |
| 61 | os.Mkdir(sockDir, 0700) |
| 62 | ln, err := net.Listen("unix", filepath.Join(sockDir, "equipo.sock")) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | t.Cleanup(func() { ln.Close() }) |
| 67 | os.Chmod(filepath.Join(sockDir, "equipo.sock"), 0600) |
| 68 | nodeSum := sha256Hex(trueBin) |
| 69 | in := provider.PiPolicyInputs{Node: node, NodeSHA256: nodeSum, Tree: tree, TreeSHA256: sum, Bwrap: "/usr/bin/bwrap", ToolProxy: put("proxy", "proxy", 0755), Extension: put("equipo.ts", string(provider.PiExtensionSource()), 0600), |
| 70 | Shell: put("bash", string(provider.PiShellSource()), 0700), Home: home, Workspace: work, Action: "inspect", Provider: "tensorx", Model: "z-ai/glm-5.3", KeyEnv: "TENSORX_API_KEY", Tools: []string{"read", "grep", "find", "ls", "mcp__equipo__submit_review"}, EgressHosts: []string{"api.tensorx.ai"}, |
| 71 | Models: json.RawMessage(`{"providers":{"tensorx":{"api":"openai-completions","apiKey":"$TENSORX_API_KEY","baseUrl":"https://api.tensorx.ai/v1","models":[{"id":"z-ai/glm-5.3"}]}}}`), |
| 72 | Settings: provider.PiSettings, |
| 73 | MCP: json.RawMessage(`{"autoEnableCodemode":false,"mcpServers":{"equipo":{"args":["--socket","/tool/equipo.sock"],"command":"/opt/equipo-tool-proxy","exposure":"direct"}}}`)} |
| 74 | if err := provider.WritePiConfig(in); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | p, err := provider.BuildPiPolicy(in) |
| 78 | if err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | l := provider.Launch{ToolSocket: filepath.Join(sockDir, "equipo.sock"), AttemptKey: "pi-swap", Workspace: work, Selection: provider.Selection{Provider: "tensorx", Model: "z-ai/glm-5.3"}, Binding: provider.PiBinding("tensorx-a", home, keyHome), Policy: p} |
| 82 | return l, p.PiArgv(), tree |
| 83 | } |
| 84 | |
| 85 | func unsealTree(dir string) { |
| 86 | filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { |
| 87 | if err == nil && info.IsDir() { |
| 88 | os.Chmod(p, 0755) |
| 89 | } |
| 90 | return nil |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | // A Pi launch mounts the very tree it hashed: replaced after the |
| 95 | // attestation and before the mount (a file rewritten, or the whole |
| 96 | // directory swapped at its path), the launch is refused; untouched, it |
| 97 | // starts. |
| 98 | func TestNamespacePiTreeSwapAfterAttestation(t *testing.T) { |
| 99 | testci.Tools(t, "bwrap") |
| 100 | for _, swap := range []string{"none", "file", "directory"} { |
| 101 | t.Run(swap, func(t *testing.T) { |
| 102 | m, _ := fixture(t) |
| 103 | l, argv, tree := piLaunch(t, m) |
| 104 | called := false |
| 105 | m.beforePin = func() { |
| 106 | called = true |
| 107 | switch swap { |
| 108 | case "file": |
| 109 | f := filepath.Join(tree, "dep", "index.js") |
| 110 | os.Chmod(filepath.Dir(f), 0755) |
| 111 | os.Chmod(f, 0644) |
| 112 | if err := os.WriteFile(f, []byte("changed"), 0644); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | provider.SealTree(tree) |
| 116 | case "directory": |
| 117 | other := tree + "-other" |
| 118 | if err := os.MkdirAll(filepath.Join(other, "dep"), 0755); err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | os.WriteFile(filepath.Join(other, "dep", "index.js"), []byte("other"), 0644) |
| 122 | provider.SealTree(other) |
| 123 | os.Chmod(filepath.Dir(tree), 0755) |
| 124 | os.Chmod(tree, 0755) |
| 125 | if err := os.Rename(tree, tree+"-old"); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | os.Chmod(other, 0755) |
| 129 | if err := os.Rename(other, tree); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | os.Chmod(tree, 0555) |
| 133 | } |
| 134 | } |
| 135 | p, err := m.Start(context.Background(), l, argv) |
| 136 | if p != nil { |
| 137 | p.(*process).kill() |
| 138 | p.Wait(context.Background()) |
| 139 | m.Stop(context.Background(), p.ID()) |
| 140 | p.Stdin().Close() |
| 141 | p.Stdout().Close() |
| 142 | p.Stderr().Close() |
| 143 | } |
| 144 | if !called { |
| 145 | t.Fatal("did not reach the attestation-to-mount boundary") |
| 146 | } |
| 147 | if swap == "none" && err != nil { |
| 148 | t.Fatalf("the untouched tree was refused: %v", err) |
| 149 | } |
| 150 | if swap != "none" && err == nil { |
| 151 | t.Fatal("a tree changed after its attestation was mounted") |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func sha256Hex(raw []byte) string { |
| 158 | sum := sha256.Sum256(raw) |
| 159 | return hex.EncodeToString(sum[:]) |
| 160 | } |
| 161 | |