package pi import ( "fmt" "golang.org/x/mod/sumdb/dirhash" "os" "path/filepath" "strings" "testing" "time" ) func shellOK(t *testing.T, h *harness, r *run, command, marker string) { t.Helper() evs := r.prompt(t, call("bash", map[string]string{"command": command}), "deny") res := results(evs) if len(res) != 1 || res[0].IsError || !strings.Contains(lastTool(h), marker) { // Never include tool output: a regressed probe may have read a key. t.Fatalf("shell probe failed: %s", marker) } } func TestPiShellIsolation(t *testing.T) { h := newHarness(t, realInstall(t)) r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "") if err := os.WriteFile(filepath.Join(r.home, ".oauth-token"), []byte(testKey), 0600); err != nil { t.Fatal(err) } script := fmt.Sprintf(`python3 - <<'PY' import os,glob,socket assert 'TENSORX_API_KEY' not in os.environ, 'inherited key' for p in glob.glob('/proc/[0-9]*/environ')+glob.glob('/proc/[0-9]*/task/*/environ'): try: raw=open(p,'rb').read() except OSError: continue assert b'TENSORX_API_KEY=' not in raw, 'CLI environment reachable' for p in [%q, '/home/runner/.oauth-token', '/home/runner/agent/models.json', '/config/pi/equipo.ts', '/egress/proxy.sock', '/tool/equipo.sock']: assert not os.path.exists(p), 'protected path reachable' for family,addr in [(socket.AF_INET,('127.0.0.1',18080)),(socket.AF_UNIX,'/egress/proxy.sock'),(socket.AF_UNIX,'/tool/equipo.sock')]: s=socket.socket(family);s.settimeout(1) try: s.connect(addr) except OSError: pass else: raise AssertionError('relay reachable') s.close() print('shell-isolated') PY`, filepath.Join(h.keyHome, ".oauth-token")) shellOK(t, h, r, script, "shell-isolated") } func TestPiEngineeringOfflineGo(t *testing.T) { h := goHarness(t) work := t.TempDir() for name, body := range map[string]string{ "go.mod": "module fixture.invalid/app\n\ngo 1.26\nrequire fixture.invalid/cached v1.0.0\n", "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", } { if err := os.WriteFile(filepath.Join(work, name), []byte(body), 0600); err != nil { t.Fatal(err) } } for name, body := range map[string]string{ "fixture.invalid/cached@v1.0.0/go.mod": "module fixture.invalid/cached\n\ngo 1.26\n", "fixture.invalid/cached@v1.0.0/dep.go": "package cached\nconst Value=42\n", "cache/download/fixture.invalid/cached/@v/v1.0.0.mod": "module fixture.invalid/cached\n\ngo 1.26\n", } { p := filepath.Join(h.moduleCache, name) if err := os.MkdirAll(filepath.Dir(p), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(p, []byte(body), 0600); err != nil { t.Fatal(err) } } sum, err := dirhash.HashDir(filepath.Join(h.moduleCache, "fixture.invalid/cached@v1.0.0"), "fixture.invalid/cached@v1.0.0", dirhash.Hash1) if err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(h.moduleCache, "cache/download/fixture.invalid/cached/@v/v1.0.0.ziphash"), []byte(sum), 0600); err != nil { t.Fatal(err) } r := h.open(work, "implement", []string{"submit_candidate"}, "") shellOK(t, h, r, `set -eu test "$GOPROXY" = off test "$GOSUMDB" = off test "$GOTOOLCHAIN" = local go test -mod=mod -run '^TestOffline$' . python3 - <<'PY' import os for root in ['/opt/go','/opt/go-mod']: assert os.statvfs(root).f_flag & os.ST_RDONLY, 'writable tree' try: open(root+('/VERSION' if root=='/opt/go' else '/fixture.invalid/cached@v1.0.0/dep.go'),'r+') except OSError: pass else: raise AssertionError('writable tree') import subprocess os.mkdir('/workspace/missing') open('/workspace/missing/go.mod','w').write('module missing.invalid/app\ngo 1.26\n') open('/workspace/missing/app.go','w').write('package app\nimport _ "missing.invalid/uncached"\n') r=subprocess.run(['go','test','-mod=mod','-run','^TestMissing$','.'],cwd='/workspace/missing',capture_output=True,text=True) assert r.returncode!=0 and 'module lookup disabled by GOPROXY=off' in r.stderr print('offline-go-ok') PY`, "offline-go-ok") } func TestPiRPCShellIsolation(t *testing.T) { h := newHarness(t, realInstall(t)) r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "") state, err := r.launcher.state(r.session) if err != nil { t.Fatal(err) } ctx, cancel := contextTimeout(10 * time.Second) defer cancel() 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`}) if err != nil || !strings.Contains(string(reply.Data), "rpc-isolated") { t.Fatal("RPC shell bypassed isolation") } } func TestPiNativeConfigGuard(t *testing.T) { h := newHarness(t, realInstall(t)) work := t.TempDir() if err := os.Symlink("/home/runner/agent", filepath.Join(work, "escape")); err != nil { t.Fatal(err) } r := h.open(work, "implement", []string{"submit_candidate"}, "") for _, path := range []string{"/home/runner/agent/settings.json", "escape/settings.json", "escape/new-file"} { evs := r.prompt(t, call("write", map[string]string{"path": path, "content": "{}"}), "deny") if res := results(evs); len(res) != 1 || !res[0].IsError { t.Fatal("native write escaped workspace") } } // Even an erroneous approval cannot overwrite the mounted settings file. evs := r.prompt(t, call("write", map[string]string{"path": "/home/runner/agent/settings.json", "content": "{}"}), "allow") if res := results(evs); len(res) != 1 || !res[0].IsError { t.Fatal("settings file was writable") } shellOK(t, h, r, "test -z \"${TENSORX_API_KEY+x}\" && echo still-isolated", "still-isolated") } func TestPiShellIgnoresCLIPath(t *testing.T) { h := newHarness(t, realInstall(t)) r := h.open(t.TempDir(), "implement", []string{"submit_candidate"}, "") bin := filepath.Join(r.home, "agent", "bin") if err := os.MkdirAll(bin, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(bin, "mkdir"), []byte("#!/bin/sh\nexit 125\n"), 0700); err != nil { t.Fatal(err) } shellOK(t, h, r, "echo fixed-path", "fixed-path") }