oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package containment
4
5import (
6 "context"
7 "crypto/sha256"
8 "encoding/hex"
9 "fmt"
10 "io"
11 "os"
12 "path/filepath"
13 "strings"
14 "testing"
15)
16
17func TestNamespacePinnedMountSwap(t *testing.T) {
18 m, l := fixture(t)
19 if e := os.WriteFile(filepath.Join(l.Workspace, "marker"), []byte("workspace"), 0600); e != nil {
20 t.Fatal(e)
21 }
22 outside := t.TempDir()
23 if e := os.WriteFile(filepath.Join(outside, "marker"), []byte("protected"), 0600); e != nil {
24 t.Fatal(e)
25 }
26 dir, e := os.MkdirTemp(m.cfg.ScratchRoot, "attempt-")
27 if e != nil {
28 t.Fatal(e)
29 }
30 for _, name := range []string{"tmp", "home"} {
31 if e = os.Mkdir(filepath.Join(dir, name), 0700); e != nil {
32 t.Fatal(e)
33 }
34 }
35 plan, e := m.mountPlan(l, dir)
36 if e != nil {
37 t.Fatal(e)
38 }
39 if _, e = m.namespaceManifest(l, []string{"/usr/bin/cat", "/workspace/marker"}, l.Policy.Hash, dir, plan); e != nil {
40 t.Fatal(e)
41 }
42 if e = os.Rename(l.Workspace, l.Workspace+"-original"); e != nil {
43 t.Fatal(e)
44 }
45 if e = os.Symlink(outside, l.Workspace); e != nil {
46 t.Fatal(e)
47 }
48 boot, e := bootID()
49 if e != nil {
50 t.Fatal(e)
51 }
52 r := Record{ID: "01234567890123456789012345678901", BootID: boot, AttemptKey: l.AttemptKey, ManifestHash: l.Policy.Hash}
53 if e = m.persist(r); e != nil {
54 t.Fatal(e)
55 }
56 result := make(chan startResult, 1)
57 go m.lifecycle(context.Background(), r, []string{"/usr/bin/cat", "/workspace/marker"}, plan, dir, result, nil, false)
58 started := <-result
59 if started.err != nil {
60 t.Fatal(started.err)
61 }
62 p := started.p
63 defer p.Stdin().Close()
64 defer p.Stdout().Close()
65 defer p.Stderr().Close()
66 b, e := io.ReadAll(p.Stdout())
67 if e != nil {
68 t.Fatal(e)
69 }
70 exit, e := p.Wait(context.Background())
71 if e != nil || exit.Code != 0 || string(b) != "workspace" {
72 t.Fatalf("mutable path replaced pinned source: %q %+v %v", b, exit, e)
73 }
74}
75func TestNamespaceRejectsBroadWorkspace(t *testing.T) {
76 m, l := fixture(t)
77 for _, path := range []string{"/home/another-user", "/tmp", "/root"} {
78 bad := l
79 bad.Workspace = path
80 // Inspect validation before any open; these paths need not exist.
81 if _, e := m.mountPlan(bad, m.cfg.ScratchRoot); e == nil {
82 t.Fatalf("broad workspace accepted: %s", path)
83 }
84 }
85}
86
87func TestNamespaceClaudeEnvironment(t *testing.T) {
88 m, l := fixture(t)
89 dir, e := os.MkdirTemp(m.cfg.ScratchRoot, "claude-env-")
90 if e != nil {
91 t.Fatal(e)
92 }
93 for _, name := range []string{"tmp", "home"} {
94 if e = os.Mkdir(filepath.Join(dir, name), 0700); e != nil {
95 t.Fatal(e)
96 }
97 }
98 for _, key := range []string{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "DISABLE_AUTOUPDATER"} {
99 t.Run(key, func(t *testing.T) {
100 l.Policy.Env = map[string]string{key: "1"}
101 plan, e := m.mountPlan(l, dir)
102 if e != nil {
103 t.Fatalf("required Claude environment refused: %v", e)
104 }
105 defer plan.close()
106 found := false
107 for i, arg := range plan.args {
108 if arg == "--setenv" && i+2 < len(plan.args) && plan.args[i+1] == key && plan.args[i+2] == "1" {
109 found = true
110 }
111 }
112 if !found {
113 t.Fatal("required Claude environment omitted")
114 }
115 })
116 }
117 l.Policy.Env = map[string]string{"CLAUDE_CODE_DISABLE_AUTO_UPDATE": "1"}
118 if plan, e := m.mountPlan(l, dir); e == nil {
119 plan.close()
120 t.Fatal("unsupported updater environment accepted")
121 }
122}
123
124func TestCoordinatorToolNoCheckout(t *testing.T) {
125 m, l := fixture(t)
126 l.Policy.Manifest = []byte(`{"Action":"coordinate"}`)
127 hash := sha256.Sum256(l.Policy.Manifest)
128 l.Policy.ManifestHash = hex.EncodeToString(hash[:])
129 dir, err := os.MkdirTemp(m.cfg.ScratchRoot, "coordinator-")
130 if err != nil {
131 t.Fatal(err)
132 }
133 defer os.RemoveAll(dir)
134 for _, name := range []string{"tmp", "home"} {
135 if err = os.Mkdir(filepath.Join(dir, name), 0700); err != nil {
136 t.Fatal(err)
137 }
138 }
139 plan, err := m.mountPlan(l, dir)
140 if err != nil {
141 t.Fatal(err)
142 }
143 defer plan.close()
144 for i, arg := range plan.args {
145 if arg == "/workspace" && i > 0 && strings.Contains(plan.args[i-1], "bind") {
146 t.Fatal("checkout mounted")
147 }
148 }
149 found := false
150 for i, arg := range plan.args {
151 if arg == "--tmpfs" && i+1 < len(plan.args) && plan.args[i+1] == "/workspace" {
152 found = true
153 }
154 }
155 if !found {
156 t.Fatal("empty workspace absent")
157 }
158 for _, f := range plan.files {
159 name, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", f.Fd()))
160 if err != nil {
161 t.Fatal(err)
162 }
163 if name == l.Workspace {
164 t.Fatal("checkout descriptor passed to namespace")
165 }
166 }
167}
168
169// The thin coordinator's desk is its /workspace, read-only: it sees its
170// notes and nothing beside them (its session home, other desks), and
171// cannot write there; only the runner's write_note does.
172func TestNamespaceConverseDeskReadOnly(t *testing.T) {
173 m, l := fixture(t)
174 raw, err := os.ReadFile(m.cfg.Binary)
175 if err != nil {
176 t.Fatal(err)
177 }
178 bwrap := sha256.Sum256(raw)
179 l.Policy.Manifest = []byte(fmt.Sprintf(`{"Action":"converse","Files":[{"Mount":{"Source":%q},"SHA256":%q}]}`, m.cfg.Binary, hex.EncodeToString(bwrap[:])))
180 hash := sha256.Sum256(l.Policy.Manifest)
181 l.Policy.ManifestHash = hex.EncodeToString(hash[:])
182 desk := filepath.Join(l.Workspace, "person", "desk")
183 if err := os.MkdirAll(desk, 0700); err != nil {
184 t.Fatal(err)
185 }
186 if err := os.WriteFile(filepath.Join(l.Workspace, "person", "session.jsonl"), []byte("transcript"), 0600); err != nil {
187 t.Fatal(err)
188 }
189 if err := os.WriteFile(filepath.Join(desk, "notebook.md"), []byte("# Notebook"), 0600); err != nil {
190 t.Fatal(err)
191 }
192 l.Workspace = desk
193 script := `import os
194print(sorted(os.listdir("/workspace")), open("/workspace/notebook.md").read())
195for path in ("/workspace/planted.md", "/workspace/notebook.md"):
196 try:
197 open(path, "a").write("x")
198 print("wrote", path)
199 except OSError as e:
200 print("refused", e.errno)
201print(os.path.exists("/workspace/../session.jsonl"))`
202 argv := []string{"/usr/bin/python3", "-u", "-c", script}
203 dir, err := os.MkdirTemp(m.cfg.ScratchRoot, "attempt-")
204 defer os.RemoveAll(dir)
205 if err != nil {
206 t.Fatal(err)
207 }
208 for _, name := range []string{"tmp", "home"} {
209 if err = os.Mkdir(filepath.Join(dir, name), 0700); err != nil {
210 t.Fatal(err)
211 }
212 }
213 plan, err := m.mountPlan(l, dir)
214 if err != nil {
215 t.Fatal(err)
216 }
217 if _, err = m.namespaceManifest(l, argv, l.Policy.Hash, dir, plan); err != nil {
218 t.Fatal(err)
219 }
220 boot, err := bootID()
221 if err != nil {
222 t.Fatal(err)
223 }
224 r := Record{ID: "01234567890123456789012345678902", BootID: boot, AttemptKey: l.AttemptKey, ManifestHash: l.Policy.Hash}
225 if err = m.persist(r); err != nil {
226 t.Fatal(err)
227 }
228 result := make(chan startResult, 1)
229 go m.lifecycle(context.Background(), r, argv, plan, dir, result, nil, false)
230 started := <-result
231 if started.err != nil {
232 t.Fatal(started.err)
233 }
234 p := started.p
235 defer p.Stderr().Close()
236 p.Stdin().Close()
237 out, err := io.ReadAll(p.Stdout())
238 if err != nil {
239 t.Fatal(err)
240 }
241 if _, err := p.Wait(context.Background()); err != nil {
242 t.Fatal(err)
243 }
244 want := "['notebook.md'] # Notebook\nrefused 30\nrefused 30\nFalse\n"
245 if string(out) != want {
246 t.Fatalf("desk inside the namespace:\n%s\nwant\n%s", out, want)
247 }
248 if raw, _ := os.ReadFile(filepath.Join(desk, "notebook.md")); string(raw) != "# Notebook" {
249 t.Fatalf("notebook changed from inside: %q", raw)
250 }
251}
252