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 "net"
5 "os"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12const piGoProbe = `package main
13import("bytes";"fmt";"net";"os";"path/filepath";"time")
14func main(){
15 mode:=os.Args[1]; exposed:=false
16 switch mode {
17 case "env": _,exposed=os.LookupEnv("TENSORX_API_KEY")
18 case "proc":
19 files,_:=filepath.Glob("/proc/[0-9]*/environ")
20 tasks,_:=filepath.Glob("/proc/[0-9]*/task/*/environ")
21 for _,p:=range append(files,tasks...){ b,_:=os.ReadFile(p);if bytes.Contains(b,[]byte("TENSORX_API_KEY=")){exposed=true} }
22 case "processes":
23 files,_:=filepath.Glob("/proc/[0-9]*/cmdline")
24 for _,p:=range files { b,_:=os.ReadFile(p);if (bytes.HasPrefix(b,[]byte("pi-rpc\x00"))||bytes.HasPrefix(b,[]byte("pi\x00"))){exposed=true} }
25 case "file": b,e:=os.ReadFile(os.Args[2]);exposed=e==nil&&len(b)>0
26 case "tcp", "unix": c,e:=net.DialTimeout(mode,os.Args[2],time.Second);if e==nil{exposed=true;c.Close()}
27 }
28 if exposed{fmt.Println("exposed")}else{fmt.Println("isolated")}
29}
30`
31
32func goHarness(t *testing.T) *harness {
33 t.Helper()
34 h := newHarness(t, realInstall(t))
35 raw, err := exec.Command("go", "env", "GOROOT").Output()
36 if err != nil {
37 t.Fatal(err)
38 }
39 h.goRoot, err = filepath.EvalSymlinks(strings.TrimSpace(string(raw)))
40 if err != nil {
41 t.Fatal(err)
42 }
43 h.moduleCache = t.TempDir()
44 return h
45}
46
47func TestPiGoBinaryIsolation(t *testing.T) {
48 h := goHarness(t)
49 work := t.TempDir()
50 if err := os.WriteFile(filepath.Join(work, "probe.go"), []byte(piGoProbe), 0600); err != nil {
51 t.Fatal(err)
52 }
53 r := h.open(work, "implement", []string{"submit_candidate"}, "")
54 // A synthetic key file in the CLI home tests the inner boundary as well
55 // as the real credential home's absence from the outer namespace.
56 if err := os.WriteFile(filepath.Join(r.home, ".oauth-token"), []byte(testKey), 0600); err != nil {
57 t.Fatal(err)
58 }
59 shellOK(t, h, r, "go build -o /workspace/probe /workspace/probe.go && echo built", "built")
60 tcp, err := net.Listen("tcp", "127.0.0.1:0")
61 if err != nil {
62 t.Fatal(err)
63 }
64 defer tcp.Close()
65 socket := filepath.Join(t.TempDir(), "probe.sock")
66 unix, err := net.Listen("unix", socket)
67 if err != nil {
68 t.Fatal(err)
69 }
70 defer unix.Close()
71 for _, tc := range []struct{ mode, control, isolated string }{
72 {"env", "unused", "unused"}, {"proc", "unused", "unused"}, {"processes", "unused", "unused"},
73 {"file", filepath.Join(h.keyHome, ".oauth-token"), filepath.Join(h.keyHome, ".oauth-token")},
74 {"file", filepath.Join(h.keyHome, ".oauth-token"), "/home/runner/.oauth-token"},
75 {"tcp", tcp.Addr().String(), "127.0.0.1:18080"},
76 {"unix", socket, "/egress/proxy.sock"},
77 } {
78 t.Run(tc.mode, func(t *testing.T) {
79 // Execute the exact binary built by Pi outside the shell as a positive
80 // control. It reports presence only, never any credential bytes.
81 cmd := exec.Command(filepath.Join(work, "probe"), tc.mode, tc.control)
82 cmd.Env = append(os.Environ(), "TENSORX_API_KEY="+testKey)
83 out, err := cmd.Output()
84 if err != nil || string(out) != "exposed\n" {
85 t.Fatal("positive control failed")
86 }
87 shellOK(t, h, r, "/workspace/probe "+tc.mode+" "+tc.isolated, "isolated")
88 })
89 }
90}
91