package pi import ( "net" "os" "os/exec" "path/filepath" "strings" "testing" ) const piGoProbe = `package main import("bytes";"fmt";"net";"os";"path/filepath";"time") func main(){ mode:=os.Args[1]; exposed:=false switch mode { case "env": _,exposed=os.LookupEnv("TENSORX_API_KEY") case "proc": files,_:=filepath.Glob("/proc/[0-9]*/environ") tasks,_:=filepath.Glob("/proc/[0-9]*/task/*/environ") for _,p:=range append(files,tasks...){ b,_:=os.ReadFile(p);if bytes.Contains(b,[]byte("TENSORX_API_KEY=")){exposed=true} } case "processes": files,_:=filepath.Glob("/proc/[0-9]*/cmdline") for _,p:=range files { b,_:=os.ReadFile(p);if (bytes.HasPrefix(b,[]byte("pi-rpc\x00"))||bytes.HasPrefix(b,[]byte("pi\x00"))){exposed=true} } case "file": b,e:=os.ReadFile(os.Args[2]);exposed=e==nil&&len(b)>0 case "tcp", "unix": c,e:=net.DialTimeout(mode,os.Args[2],time.Second);if e==nil{exposed=true;c.Close()} } if exposed{fmt.Println("exposed")}else{fmt.Println("isolated")} } ` func goHarness(t *testing.T) *harness { t.Helper() h := newHarness(t, realInstall(t)) raw, err := exec.Command("go", "env", "GOROOT").Output() if err != nil { t.Fatal(err) } h.goRoot, err = filepath.EvalSymlinks(strings.TrimSpace(string(raw))) if err != nil { t.Fatal(err) } h.moduleCache = t.TempDir() return h } func TestPiGoBinaryIsolation(t *testing.T) { h := goHarness(t) work := t.TempDir() if err := os.WriteFile(filepath.Join(work, "probe.go"), []byte(piGoProbe), 0600); err != nil { t.Fatal(err) } r := h.open(work, "implement", []string{"submit_candidate"}, "") // A synthetic key file in the CLI home tests the inner boundary as well // as the real credential home's absence from the outer namespace. if err := os.WriteFile(filepath.Join(r.home, ".oauth-token"), []byte(testKey), 0600); err != nil { t.Fatal(err) } shellOK(t, h, r, "go build -o /workspace/probe /workspace/probe.go && echo built", "built") tcp, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } defer tcp.Close() socket := filepath.Join(t.TempDir(), "probe.sock") unix, err := net.Listen("unix", socket) if err != nil { t.Fatal(err) } defer unix.Close() for _, tc := range []struct{ mode, control, isolated string }{ {"env", "unused", "unused"}, {"proc", "unused", "unused"}, {"processes", "unused", "unused"}, {"file", filepath.Join(h.keyHome, ".oauth-token"), filepath.Join(h.keyHome, ".oauth-token")}, {"file", filepath.Join(h.keyHome, ".oauth-token"), "/home/runner/.oauth-token"}, {"tcp", tcp.Addr().String(), "127.0.0.1:18080"}, {"unix", socket, "/egress/proxy.sock"}, } { t.Run(tc.mode, func(t *testing.T) { // Execute the exact binary built by Pi outside the shell as a positive // control. It reports presence only, never any credential bytes. cmd := exec.Command(filepath.Join(work, "probe"), tc.mode, tc.control) cmd.Env = append(os.Environ(), "TENSORX_API_KEY="+testKey) out, err := cmd.Output() if err != nil || string(out) != "exposed\n" { t.Fatal("positive control failed") } shellOK(t, h, r, "/workspace/probe "+tc.mode+" "+tc.isolated, "isolated") }) } }