oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package pi
2
3import (
4 "fmt"
5 "golang.org/x/mod/sumdb/dirhash"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10 "time"
11)
12
13func shellOK(t *testing.T, h *harness, r *run, command, marker string) {
14 t.Helper()
15 evs := r.prompt(t, call("bash", map[string]string{"command": command}), "deny")
16 res := results(evs)
17 if len(res) != 1 || res[0].IsError || !strings.Contains(lastTool(h), marker) {
18 // Never include tool output: a regressed probe may have read a key.
19 t.Fatalf("shell probe failed: %s", marker)
20 }
21}
22
23func TestPiShellIsolation(t *testing.T) {
24 h := newHarness(t, realInstall(t))
25 r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "")
26 if err := os.WriteFile(filepath.Join(r.home, ".oauth-token"), []byte(testKey), 0600); err != nil {
27 t.Fatal(err)
28 }
29 script := fmt.Sprintf(`python3 - <<'PY'
30import os,glob,socket
31assert 'TENSORX_API_KEY' not in os.environ, 'inherited key'
32for p in glob.glob('/proc/[0-9]*/environ')+glob.glob('/proc/[0-9]*/task/*/environ'):
33 try: raw=open(p,'rb').read()
34 except OSError: continue
35 assert b'TENSORX_API_KEY=' not in raw, 'CLI environment reachable'
36for p in [%q, '/home/runner/.oauth-token', '/home/runner/agent/models.json', '/config/pi/equipo.ts', '/egress/proxy.sock', '/tool/equipo.sock']:
37 assert not os.path.exists(p), 'protected path reachable'
38for family,addr in [(socket.AF_INET,('127.0.0.1',18080)),(socket.AF_UNIX,'/egress/proxy.sock'),(socket.AF_UNIX,'/tool/equipo.sock')]:
39 s=socket.socket(family);s.settimeout(1)
40 try: s.connect(addr)
41 except OSError: pass
42 else: raise AssertionError('relay reachable')
43 s.close()
44print('shell-isolated')
45PY`, filepath.Join(h.keyHome, ".oauth-token"))
46 shellOK(t, h, r, script, "shell-isolated")
47}
48
49func TestPiEngineeringOfflineGo(t *testing.T) {
50 h := goHarness(t)
51 work := t.TempDir()
52 for name, body := range map[string]string{
53 "go.mod": "module fixture.invalid/app\n\ngo 1.26\nrequire fixture.invalid/cached v1.0.0\n",
54 "app_test.go": "package app\nimport (\"testing\"; dep \"fixture.invalid/cached\")\nfunc TestOffline(t *testing.T) { if dep.Value != 42 {t.Fatal(dep.Value)} }\n",
55 } {
56 if err := os.WriteFile(filepath.Join(work, name), []byte(body), 0600); err != nil {
57 t.Fatal(err)
58 }
59 }
60 for name, body := range map[string]string{
61 "fixture.invalid/cached@v1.0.0/go.mod": "module fixture.invalid/cached\n\ngo 1.26\n",
62 "fixture.invalid/cached@v1.0.0/dep.go": "package cached\nconst Value=42\n",
63 "cache/download/fixture.invalid/cached/@v/v1.0.0.mod": "module fixture.invalid/cached\n\ngo 1.26\n",
64 } {
65 p := filepath.Join(h.moduleCache, name)
66 if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil {
67 t.Fatal(err)
68 }
69 if err := os.WriteFile(p, []byte(body), 0600); err != nil {
70 t.Fatal(err)
71 }
72 }
73 sum, err := dirhash.HashDir(filepath.Join(h.moduleCache, "fixture.invalid/cached@v1.0.0"), "fixture.invalid/cached@v1.0.0", dirhash.Hash1)
74 if err != nil {
75 t.Fatal(err)
76 }
77 if err := os.WriteFile(filepath.Join(h.moduleCache, "cache/download/fixture.invalid/cached/@v/v1.0.0.ziphash"), []byte(sum), 0600); err != nil {
78 t.Fatal(err)
79 }
80 r := h.open(work, "implement", []string{"submit_candidate"}, "")
81 shellOK(t, h, r, `set -eu
82 test "$GOPROXY" = off
83 test "$GOSUMDB" = off
84 test "$GOTOOLCHAIN" = local
85 go test -mod=mod -run '^TestOffline$' .
86 python3 - <<'PY'
87import os
88for root in ['/opt/go','/opt/go-mod']:
89 assert os.statvfs(root).f_flag & os.ST_RDONLY, 'writable tree'
90 try: open(root+('/VERSION' if root=='/opt/go' else '/fixture.invalid/cached@v1.0.0/dep.go'),'r+')
91 except OSError: pass
92 else: raise AssertionError('writable tree')
93import subprocess
94os.mkdir('/workspace/missing')
95open('/workspace/missing/go.mod','w').write('module missing.invalid/app\ngo 1.26\n')
96open('/workspace/missing/app.go','w').write('package app\nimport _ "missing.invalid/uncached"\n')
97r=subprocess.run(['go','test','-mod=mod','-run','^TestMissing$','.'],cwd='/workspace/missing',capture_output=True,text=True)
98assert r.returncode!=0 and 'module lookup disabled by GOPROXY=off' in r.stderr
99print('offline-go-ok')
100PY`, "offline-go-ok")
101}
102
103func TestPiRPCShellIsolation(t *testing.T) {
104 h := newHarness(t, realInstall(t))
105 r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "")
106 state, err := r.launcher.state(r.session)
107 if err != nil {
108 t.Fatal(err)
109 }
110 ctx, cancel := contextTimeout(10 * time.Second)
111 defer cancel()
112 reply, err := state.client.Call(ctx, map[string]any{"type": "bash", "command": `test -z "${TENSORX_API_KEY+x}" && test ! -e /egress/proxy.sock && echo rpc-isolated`})
113 if err != nil || !strings.Contains(string(reply.Data), "rpc-isolated") {
114 t.Fatal("RPC shell bypassed isolation")
115 }
116}
117
118func TestPiNativeConfigGuard(t *testing.T) {
119 h := newHarness(t, realInstall(t))
120 work := t.TempDir()
121 if err := os.Symlink("/home/runner/agent", filepath.Join(work, "escape")); err != nil {
122 t.Fatal(err)
123 }
124 r := h.open(work, "implement", []string{"submit_candidate"}, "")
125 for _, path := range []string{"/home/runner/agent/settings.json", "escape/settings.json", "escape/new-file"} {
126 evs := r.prompt(t, call("write", map[string]string{"path": path, "content": "{}"}), "deny")
127 if res := results(evs); len(res) != 1 || !res[0].IsError {
128 t.Fatal("native write escaped workspace")
129 }
130 }
131 // Even an erroneous approval cannot overwrite the mounted settings file.
132 evs := r.prompt(t, call("write", map[string]string{"path": "/home/runner/agent/settings.json", "content": "{}"}), "allow")
133 if res := results(evs); len(res) != 1 || !res[0].IsError {
134 t.Fatal("settings file was writable")
135 }
136 shellOK(t, h, r, "test -z \"${TENSORX_API_KEY+x}\" && echo still-isolated", "still-isolated")
137}
138
139func TestPiShellIgnoresCLIPath(t *testing.T) {
140 h := newHarness(t, realInstall(t))
141 r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "")
142 bin := filepath.Join(r.home, "agent", "bin")
143 if err := os.MkdirAll(bin, 0700); err != nil {
144 t.Fatal(err)
145 }
146 if err := os.WriteFile(filepath.Join(bin, "mkdir"), []byte("#!/bin/sh\nexit 125\n"), 0700); err != nil {
147 t.Fatal(err)
148 }
149 shellOK(t, h, r, "echo fixed-path", "fixed-path")
150}
151