oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "context"
5 "encoding/json"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12func policyFixture(t *testing.T) (Binding, PolicyInputs) {
13 t.Helper()
14 b, r := accountFixture(t)
15 l := openFixture(t, b, r)
16 preflightFixture(t, l)
17 binary := filepath.Join(t.TempDir(), "codex")
18 writeFake(t, binary, "fake binary")
19 companion := filepath.Join(filepath.Dir(binary), "helper")
20 writeFake(t, companion, "fake helper")
21 work, scratch := filepath.Join(t.TempDir(), "work"), filepath.Join(t.TempDir(), "scratch")
22 os.Mkdir(work, 0700)
23 os.Mkdir(scratch, 0700)
24 return l.Binding(), PolicyInputs{RuntimeFiles: []Mount{{Source: binary, Target: "/opt/codex", ReadOnly: true}, {Source: companion, Target: "/opt/helper", ReadOnly: true}}, DeniedPaths: []string{b.Home}, EgressHosts: []string{"chatgpt.com"}, ProviderVersion: "0.165.0", LauncherVersion: "2", BwrapVersion: "0.11.0", Settings: json.RawMessage(`{"sandbox":"read-only"}`), MCP: json.RawMessage(`{"servers":{}}`), ThreadPolicy: json.RawMessage(`{"start":{"permissions":"probe"},"resume":{"permissions":"probe"}}`), TurnPolicy: json.RawMessage(`{}`), Tools: []string{"read", "shell"}, Argv: []string{"/opt/codex", "app-server"}, Workspace: work, Scratch: scratch}
25}
26func mustPolicy(t *testing.T, b Binding, in PolicyInputs) Policy {
27 t.Helper()
28 p, e := BuildPolicy(b, in)
29 if e != nil {
30 t.Fatal(e)
31 }
32 return p
33}
34func TestEffectivePolicyHash(t *testing.T) {
35 for _, field := range []string{"binary", "settings", "routing", "tools", "mcp", "managed", "env", "mount", "thread-start", "thread-resume", "turn", "provider-version", "launcher-version", "bwrap-version", "argv", "denied"} {
36 t.Run(field, func(t *testing.T) {
37 b, in := policyFixture(t)
38 before := mustPolicy(t, b, in)
39 switch field {
40 case "binary":
41 writeFake(t, in.RuntimeFiles[0].Source, "different binary")
42 case "settings":
43 in.Settings = json.RawMessage(`{"sandbox":"workspace-write"}`)
44 case "routing":
45 in.EgressHosts = append(in.EgressHosts, "refresh.example")
46 case "tools":
47 in.Tools = append(in.Tools, "image")
48 case "mcp":
49 in.MCP = json.RawMessage(`{"servers":{"equipo":{"command":"/opt/mcp"}}}`)
50 case "managed":
51 file := filepath.Join(t.TempDir(), "managed.json")
52 writeFake(t, file, `{}`)
53 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: file, Target: "/etc/codex/managed.json", ReadOnly: true})
54 case "env":
55 in.Env = map[string]string{"LANG": "en_IE.UTF-8"}
56 case "mount":
57 in.RuntimeFiles[1].Target = "/opt/other"
58 case "thread-start":
59 in.ThreadPolicy = json.RawMessage(`{"start":{"permissions":"other"},"resume":{"permissions":"probe"}}`)
60 case "thread-resume":
61 in.ThreadPolicy = json.RawMessage(`{"start":{"permissions":"probe"},"resume":{"permissions":"other"}}`)
62 case "turn":
63 in.TurnPolicy = json.RawMessage(`{"effort":"high"}`)
64 case "provider-version":
65 in.ProviderVersion = "new"
66 case "launcher-version":
67 in.LauncherVersion = "new"
68 case "bwrap-version":
69 in.BwrapVersion = "new"
70 case "argv":
71 in.Argv = append(in.Argv, "--flag")
72 case "denied":
73 in.DeniedPaths = append(in.DeniedPaths, "/protected")
74 }
75 after := mustPolicy(t, b, in)
76 if before.Hash == after.Hash || before.ManifestHash == after.ManifestHash {
77 t.Fatal("manifest change not hashed")
78 }
79 })
80 }
81 t.Run("canonical and concrete", func(t *testing.T) {
82 b, in := policyFixture(t)
83 p := mustPolicy(t, b, in)
84 in.Settings = json.RawMessage("{ \"sandbox\" : \"read-only\" }")
85 q := mustPolicy(t, b, in)
86 if p.Hash != q.Hash {
87 t.Fatal("JSON not canonical")
88 }
89 in.Workspace = filepath.Join(t.TempDir(), "next")
90 os.Mkdir(in.Workspace, 0700)
91 q = mustPolicy(t, b, in)
92 if p.Hash != q.Hash || p.ManifestHash == q.ManifestHash {
93 t.Fatal("workspace role or concrete hash wrong")
94 }
95 if strings.Contains(string(p.Manifest), "fake-one") {
96 t.Fatal("credential leaked")
97 }
98 })
99 t.Run("account exact", func(t *testing.T) {
100 b, in := policyFixture(t)
101 p := mustPolicy(t, b, in)
102 other, _ := policyFixture(t)
103 q := mustPolicy(t, other, in)
104 if p.Hash == q.Hash {
105 t.Fatal("different account home reused certificate")
106 }
107 })
108 t.Run("instructions separate", func(t *testing.T) {
109 b, in := policyFixture(t)
110 path := filepath.Join(t.TempDir(), "instructions")
111 writeFake(t, path, "one")
112 in.InstructionFiles = []Mount{{Source: path, Target: "/config/instructions", ReadOnly: true}}
113 p := mustPolicy(t, b, in)
114 writeFake(t, path, "two")
115 q := mustPolicy(t, b, in)
116 if p.Hash != q.Hash || p.InstructionHash == q.InstructionHash || p.ManifestHash == q.ManifestHash {
117 t.Fatal("instructions not separately hashed")
118 }
119 })
120}
121func TestUnexpectedConfigLayer(t *testing.T) {
122 b, in := policyFixture(t)
123 p := mustPolicy(t, b, in)
124 good := ConfigReadback{Settings: in.Settings, MCP: in.MCP, ThreadPolicy: in.ThreadPolicy, TurnPolicy: in.TurnPolicy, Layers: []string{"equipo"}, HooksDisabled: true, PluginsDisabled: true}
125 if e := p.ValidateReadback(good); e != nil {
126 t.Fatal(e)
127 }
128 for _, kind := range []string{"user", "project", "managed", "remote", "ignored", "hooks", "plugins", "turn override"} {
129 t.Run(kind, func(t *testing.T) {
130 bad := good
131 switch kind {
132 case "remote":
133 bad.RemoteControl = true
134 case "ignored":
135 bad.Settings = json.RawMessage(`{}`)
136 case "hooks":
137 bad.HooksDisabled = false
138 case "plugins":
139 bad.PluginsDisabled = false
140 case "turn override":
141 bad.TurnPolicy = json.RawMessage(`{"sandboxPolicy":{}}`)
142 default:
143 bad.Layers = append([]string{"equipo"}, kind)
144 }
145 if p.ValidateReadback(bad) == nil {
146 t.Fatal("unexpected config accepted")
147 }
148 })
149 }
150 for _, key := range []string{"ANTHROPIC_API_KEY", "OPENAI_API_KEY", "HTTPS_PROXY", "SSH_AUTH_SOCK", "DBUS_SESSION_BUS_ADDRESS", "PASEO_HOME", "CLAUDE_CODE_OAUTH_TOKEN"} {
151 t.Run(key, func(t *testing.T) {
152 t.Setenv(key, "parent-secret")
153 p := mustPolicy(t, b, in)
154 if strings.Contains(string(p.Manifest), "parent-secret") {
155 t.Fatal("inherited environment")
156 }
157 bad := in
158 bad.Env = map[string]string{key: "injected"}
159 if _, e := BuildPolicy(b, bad); e == nil {
160 t.Fatal("injected environment accepted")
161 }
162 })
163 }
164 for _, key := range []string{"sandboxPolicy", "approvalPolicy", "permissions", "permissionProfile", "cwd", "model"} {
165 bad := in
166 bad.TurnPolicy = json.RawMessage(`{"` + key + `":"override"}`)
167 if _, e := BuildPolicy(b, bad); e == nil {
168 t.Fatalf("turn override %s accepted", key)
169 }
170 }
171 forged := b
172 forged.provenance = nil
173 if _, e := BuildPolicy(forged, in); e == nil {
174 t.Fatal("unverified binding accepted")
175 }
176}
177
178func TestEffectivePolicyLaunchAttestation(t *testing.T) {
179 b, in := policyFixture(t)
180 p := mustPolicy(t, b, in)
181 l := Launch{AttemptKey: "attempt", Workspace: in.Workspace, Binding: b, Policy: p, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID, Model: "model", Family: "gpt", ConnectionID: "person-connection"}}
182 hash, e := AttestLaunch(l, in.Argv)
183 if e != nil || hash == "" || hash == p.Hash {
184 t.Fatal("missing concrete attempt hash", e)
185 }
186 for _, kind := range []string{"argv", "workspace", "env", "mount", "binary", "account", "manifest", "manifesthash", "policyhash"} {
187 t.Run(kind, func(t *testing.T) {
188 bad := l
189 argv := append([]string(nil), in.Argv...)
190 switch kind {
191 case "argv":
192 argv = append(argv, "--override")
193 case "workspace":
194 bad.Workspace = "/different"
195 case "env":
196 bad.Policy.Env = map[string]string{"LANG": "bad"}
197 case "mount":
198 bad.Policy.Mounts = nil
199 case "binary":
200 writeFake(t, in.RuntimeFiles[0].Source, "changed")
201 defer writeFake(t, in.RuntimeFiles[0].Source, "fake binary")
202 case "account":
203 bad.Selection.AccountID = "other"
204 case "manifest":
205 bad.Policy.Manifest = json.RawMessage(`{}`)
206 case "manifesthash":
207 bad.Policy.ManifestHash = "changed"
208 case "policyhash":
209 bad.Policy.Hash = "changed"
210 }
211 if _, e := AttestLaunch(bad, argv); e == nil {
212 t.Fatal("changed launch accepted")
213 }
214 })
215 }
216 if e := p.ValidateNetwork(b.Provider, []string{"other.example"}); e == nil {
217 t.Fatal("egress drift accepted")
218 }
219}
220
221func TestUnexpectedConfigInputs(t *testing.T) {
222 for _, kind := range []string{"duplicate JSON", "secret JSON", "missing binary", "writable mount", "unprotected role", "duplicate tool", "symlink binary", "network number"} {
223 t.Run(kind, func(t *testing.T) {
224 b, in := policyFixture(t)
225 switch kind {
226 case "duplicate JSON":
227 in.Settings = json.RawMessage(`{"a":1,"a":2}`)
228 case "secret JSON":
229 in.MCP = json.RawMessage(`{"apiKey":"secret"}`)
230 case "missing binary":
231 in.RuntimeFiles = nil
232 case "writable mount":
233 in.RuntimeFiles[0].ReadOnly = false
234 case "unprotected role":
235 in.Workspace = b.CredentialHome
236 case "duplicate tool":
237 in.Tools = append(in.Tools, in.Tools[0])
238 case "symlink binary":
239 alias := filepath.Join(t.TempDir(), "alias")
240 os.Symlink(in.RuntimeFiles[0].Source, alias)
241 in.RuntimeFiles[0].Source = alias
242 case "network number":
243 in.EgressHosts = []string{"127.0.0.1"}
244 }
245 if _, e := BuildPolicy(b, in); e == nil {
246 t.Fatal("unsafe policy input accepted")
247 }
248 })
249 }
250}
251func TestEffectivePolicyPreflightLaunch(t *testing.T) {
252 b, r := accountFixture(t)
253 l := openFixture(t, b, r)
254 _, in := policyFixture(t)
255 e := l.Preflight(context.Background(), func(ctx context.Context, b Binding) (Identity, error) {
256 p, e := BuildPolicy(b, in)
257 if e != nil {
258 return Identity{}, e
259 }
260 _, e = AttestLaunch(Launch{AttemptKey: "auth-check", Binding: b, Policy: p, Workspace: in.Workspace, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID}}, in.Argv)
261 return b.Expected, e
262 })
263 if e != nil {
264 t.Fatal("preflight could not launch status check", e)
265 }
266}
267
268func TestEffectivePolicyHostBinary(t *testing.T) {
269 b, in := policyFixture(t)
270 p := mustPolicy(t, b, in)
271 if p.ValidateRuntimeBinaries(in.RuntimeFiles[0].Source) != nil {
272 t.Fatal("hashed binary rejected")
273 }
274 if p.ValidateRuntimeBinaries("/missing/binary") == nil {
275 t.Fatal("unhashed host binary accepted")
276 }
277}
278
279func TestEffectivePolicyManifestBoundary(t *testing.T) {
280 b, in := policyFixture(t)
281 p := mustPolicy(t, b, in)
282 var value map[string]any
283 json.Unmarshal(p.Manifest, &value)
284 if value["OuterMounts"] == nil || value["NamespaceArgs"] == nil || value["GeneratedMounts"] == nil {
285 t.Fatal("outer mount policy missing from manifest")
286 }
287 if p.ValidateFileDigest(in.RuntimeFiles[0].Source, "changed") == nil {
288 t.Fatal("changed pinned file digest accepted")
289 }
290}
291
292func TestPrivateHomeRejectsCredentialMount(t *testing.T) {
293 b, in := policyFixture(t)
294 fake := filepath.Join(t.TempDir(), "other-home")
295 if e := os.Mkdir(fake, 0700); e != nil {
296 t.Fatal(e)
297 }
298 secret := filepath.Join(fake, "auth.json")
299 writeFake(t, secret, `{"token":"other-fake-login"}`)
300 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: secret, Target: "/config/credential", ReadOnly: true})
301 if _, e := BuildPolicy(b, in); e == nil {
302 t.Fatal("credential source entered public manifest")
303 }
304 alias := filepath.Join(t.TempDir(), "aliased-runtime")
305 if e := os.Link(secret, alias); e != nil {
306 t.Fatal(e)
307 }
308 in.RuntimeFiles[len(in.RuntimeFiles)-1].Source = alias
309 if _, e := BuildPolicy(b, in); e == nil {
310 t.Fatal("credential inode alias entered public manifest")
311 }
312}
313
314func TestEffectivePolicyTurnSources(t *testing.T) {
315 for _, kind := range []string{"settings", "mcp", "managed"} {
316 t.Run(kind, func(t *testing.T) {
317 b, in := policyFixture(t)
318 managed := filepath.Join(t.TempDir(), "managed.json")
319 writeFake(t, managed, `{}`)
320 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: managed, Target: "/etc/codex/managed.json", ReadOnly: true})
321 p := mustPolicy(t, b, in)
322 ctx := context.Background()
323 if e := b.provenance.BeginTurn(ctx); e != nil {
324 t.Fatal(e)
325 }
326 path := managed
327 for _, m := range p.Mounts {
328 if (kind == "settings" && m.Target == SettingsPath) || (kind == "mcp" && m.Target == MCPPath) {
329 path = m.Source
330 }
331 }
332 writeFake(t, path, `{"changed":true}`)
333 _ = b.provenance.EndTurn(ctx)
334 if e := b.provenance.BeginTurn(ctx); e == nil {
335 t.Fatal("changed policy admitted next turn")
336 }
337 })
338 }
339}
340func TestPrivateHomeConfigOutsideWritableMounts(t *testing.T) {
341 b, in := policyFixture(t)
342 p := mustPolicy(t, b, in)
343 for _, m := range p.Mounts {
344 if m.Target == SettingsPath || m.Target == MCPPath {
345 for _, w := range []string{b.Home, in.Workspace, in.Scratch} {
346 if withinPath(m.Source, w) {
347 t.Fatal("config source has writable namespace alias")
348 }
349 }
350 }
351 }
352}
353func TestEffectivePolicyRejectsWritableSource(t *testing.T) {
354 for _, kind := range []string{"workspace", "scratch"} {
355 t.Run(kind, func(t *testing.T) {
356 b, in := policyFixture(t)
357 root := in.Workspace
358 if kind == "scratch" {
359 root = in.Scratch
360 }
361 path := filepath.Join(root, "managed.json")
362 writeFake(t, path, `{}`)
363 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: path, Target: "/etc/codex/managed.json", ReadOnly: true})
364 if _, e := BuildPolicy(b, in); e == nil {
365 t.Fatal("writable managed alias accepted")
366 }
367 })
368 }
369}
370
371func TestEffectivePolicyProviderRequiresManifest(t *testing.T) {
372 if _, e := AttestLaunch(Launch{Selection: Selection{Provider: "codex"}, Policy: Policy{Hash: "legacy"}}, []string{"/opt/codex"}); e == nil {
373 t.Fatal("direct provider accepted legacy attestation")
374 }
375}
376
377func TestEffectivePolicyConfigDirectory(t *testing.T) {
378 for _, kind := range []string{"replacement", "mode", "cleanup", "resume"} {
379 t.Run(kind, func(t *testing.T) {
380 b, in := policyFixture(t)
381 p := mustPolicy(t, b, in)
382 l := b.provenance
383 switch kind {
384 case "replacement":
385 if e := os.Rename(l.config, l.config+"-old"); e != nil {
386 t.Fatal(e)
387 }
388 if e := os.Mkdir(l.config, 0700); e != nil {
389 t.Fatal(e)
390 }
391 writeFake(t, filepath.Join(l.config, "equipo-settings.json"), string(p.Settings))
392 writeFake(t, filepath.Join(l.config, "equipo-mcp.json"), string(p.MCP))
393 defer func() { os.RemoveAll(l.config); os.Rename(l.config+"-old", l.config) }()
394 case "mode":
395 if e := os.Chmod(l.config, 0755); e != nil {
396 t.Fatal(e)
397 }
398 case "cleanup":
399 if e := l.Close(); e != nil {
400 t.Fatal(e)
401 }
402 if _, e := os.Stat(l.config); !os.IsNotExist(e) {
403 t.Fatal("config directory survived lease close")
404 }
405 return
406 case "resume":
407 writeFake(t, filepath.Join(l.config, "equipo-settings.json"), `{"changed":true}`)
408 }
409 if e := l.ValidatePolicy(context.Background()); e == nil {
410 t.Fatal("changed config admitted resume")
411 }
412 })
413 }
414}
415