oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package containment
2
3import (
4 "context"
5 "net"
6 "os"
7 "path/filepath"
8 "testing"
9
10 "amadan.net/oficina/equipo/internal/runner/provider"
11 "amadan.net/oficina/equipo/internal/testci"
12)
13
14// opencodeLaunch is an OpenCode launch on m: a sealed tree holding the
15// binary (a copy of true), the runtime home under the scratch root's
16// homes, a key, a tool socket and a workspace.
17func opencodeLaunch(t *testing.T, m *Manager) (provider.Launch, string) {
18 t.Helper()
19 root := t.TempDir()
20 trueBin, err := os.ReadFile("/usr/bin/true")
21 if err != nil {
22 t.Fatal(err)
23 }
24 tree := filepath.Join(root, "opencode", "node_modules")
25 binary := filepath.Join(tree, "@opencode", "cli-test", "bin", "opencode")
26 os.MkdirAll(filepath.Dir(binary), 0755)
27 if err := os.WriteFile(binary, trueBin, 0755); err != nil {
28 t.Fatal(err)
29 }
30 if err := provider.SealTree(tree); err != nil {
31 t.Fatal(err)
32 }
33 t.Cleanup(func() { unsealTree(filepath.Dir(tree)) })
34 sum, err := provider.TreeSHA256(tree)
35 if err != nil {
36 t.Fatal(err)
37 }
38 home := filepath.Join(m.cfg.ScratchRoot, "homes", "opencode-home")
39 os.MkdirAll(home, 0700)
40 os.Chmod(filepath.Join(m.cfg.ScratchRoot, "homes"), 0700)
41 work := filepath.Join(root, "work")
42 os.Mkdir(work, 0700)
43 keyHome := filepath.Join(root, "key")
44 os.Mkdir(keyHome, 0700)
45 os.WriteFile(filepath.Join(keyHome, provider.TokenFile), []byte("sk-go-test"), 0600)
46 proxy := filepath.Join(root, "proxy")
47 os.WriteFile(proxy, []byte("proxy"), 0755)
48 sockDir := filepath.Join(root, "tool")
49 os.Mkdir(sockDir, 0700)
50 ln, err := net.Listen("unix", filepath.Join(sockDir, "equipo.sock"))
51 if err != nil {
52 t.Fatal(err)
53 }
54 t.Cleanup(func() { ln.Close() })
55 os.Chmod(filepath.Join(sockDir, "equipo.sock"), 0600)
56 tools := provider.OpenCodeTools("inspect", []string{"submit_review"})
57 in := provider.OpenCodePolicyInputs{Binary: binary, BinarySHA256: sha256Hex(trueBin), Tree: tree, TreeSHA256: sum, Bwrap: "/usr/bin/bwrap", ToolProxy: proxy,
58 Home: home, Workspace: work, Action: "inspect", Provider: "opencode-go", Model: "glm-5.3", KeyEnv: "OPENCODE_GO_API_KEY", Tools: tools, EgressHosts: []string{"opencode.ai"},
59 Config: provider.OpenCodeConfig("opencode-go", "glm-5.3", tools, true)}
60 if err := provider.WriteOpenCodeConfig(in); err != nil {
61 t.Fatal(err)
62 }
63 p, err := provider.BuildOpenCodePolicy(in)
64 if err != nil {
65 t.Fatal(err)
66 }
67 return provider.Launch{ToolSocket: filepath.Join(sockDir, "equipo.sock"), AttemptKey: "opencode-swap", Workspace: work, Selection: provider.Selection{Provider: "opencode-go", Model: "glm-5.3"}, Binding: provider.OpenCodeBinding("opencode-go-a", home, keyHome), Policy: p}, binary
68}
69
70// Security round finding 2: an OpenCode launch mounts the binary it
71// attested, held to its seal through the descriptor bubblewrap mounts.
72// Between the attestation and the mount, a binary rewritten, made
73// writable, given a second name or replaced at its path is refused;
74// untouched, it starts.
75func TestNamespaceOpenCodeBinaryAfterAttestation(t *testing.T) {
76 testci.Tools(t, "bwrap")
77 for _, change := range []string{"none", "content", "writable", "second name", "replaced"} {
78 t.Run(change, func(t *testing.T) {
79 m, _ := fixture(t)
80 l, binary := opencodeLaunch(t, m)
81 dir := filepath.Dir(binary)
82 called := false
83 m.beforePin = func() {
84 called = true
85 switch change {
86 case "content":
87 os.Chmod(dir, 0755)
88 os.Chmod(binary, 0755)
89 os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0755)
90 os.Chmod(binary, 0555)
91 os.Chmod(dir, 0555)
92 case "writable":
93 os.Chmod(binary, 0755)
94 case "second name":
95 if err := os.Link(binary, filepath.Join(t.TempDir(), "other")); err != nil {
96 t.Fatal(err)
97 }
98 case "replaced":
99 // Other bytes, sealed as the install seals them, renamed
100 // over the attested path.
101 os.Chmod(dir, 0755)
102 other := binary + ".new"
103 os.WriteFile(other, []byte("#!/bin/sh\nexit 0\n"), 0555)
104 if err := os.Rename(other, binary); err != nil {
105 t.Fatal(err)
106 }
107 os.Chmod(dir, 0555)
108 }
109 }
110 p, err := m.Start(context.Background(), l, provider.OpenCodeArgv())
111 if p != nil {
112 p.(*process).kill()
113 p.Wait(context.Background())
114 m.Stop(context.Background(), p.ID())
115 p.Stdin().Close()
116 p.Stdout().Close()
117 p.Stderr().Close()
118 }
119 if !called {
120 t.Fatal("did not reach the attestation-to-mount boundary")
121 }
122 if change == "none" && err != nil {
123 t.Fatalf("the untouched binary was refused: %v", err)
124 }
125 if change != "none" && err == nil {
126 t.Fatal("a binary changed after its attestation was mounted")
127 }
128 })
129 }
130}
131