| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "fmt" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | const goProcProbe = `package main |
| 16 | import("bytes";"fmt";"os";"path/filepath";"strings") |
| 17 | func main(){ |
| 18 | holder,start:=os.Args[1],os.Args[2];tried:=0 |
| 19 | try:=func(path string){tried++;raw,_:=os.ReadFile(path);if bytes.Contains(raw,[]byte("CLAUDE_CODE_OAUTH_TOKEN=")){fmt.Println("exposed",path)}} |
| 20 | procs,_:=filepath.Glob("/proc/[0-9]*") |
| 21 | for _,p:=range append(procs,"/proc/"+holder){ |
| 22 | try(p+"/environ");try("/proc/self/../"+filepath.Base(p)+"/environ");try(p+"/root/proc/"+holder+"/environ") |
| 23 | threads,_:=filepath.Glob(p+"/task/*/environ");for _,t:=range threads{try(t)} |
| 24 | raw,_:=os.ReadFile(p+"/stat");parts:=strings.SplitAfterN(string(raw),") ",2) |
| 25 | if len(parts)==2 {fields:=strings.Fields(parts[1]);if len(fields)>19&&fields[19]==start{fmt.Println("visible",p)}} |
| 26 | } |
| 27 | for _,p:=range []string{"/proc/self/root/proc/","/proc/1/root/proc/"}{try(p+holder+"/environ")} |
| 28 | fmt.Println("tried",tried) |
| 29 | } |
| 30 | ` |
| 31 | |
| 32 | func TestEngineeringGoTokenIsolation(t *testing.T) { |
| 33 | m, l := egressFixture(t) |
| 34 | m.secretEnv = func(provider.Launch) (map[string]string, error) { |
| 35 | return map[string]string{provider.OAuthTokenEnv: canary, provider.ScrubEnv: "1"}, nil |
| 36 | } |
| 37 | source, _ := json.Marshal(goProcProbe) |
| 38 | build := engineeringShell(t, "claude", fmt.Sprintf(`import subprocess |
| 39 | open('/workspace/probe.go','w').write(%s) |
| 40 | subprocess.run(['go','build','-o','/workspace/probe','/workspace/probe.go'],check=True) |
| 41 | `, source)) |
| 42 | buildJSON, _ := json.Marshal(build) |
| 43 | sandbox := engineeringShell(t, "claude", `import os,subprocess |
| 44 | r=subprocess.run(['/workspace/probe',os.environ['HOLDER_PID'],os.environ['HOLDER_START']],capture_output=True,text=True) |
| 45 | assert r.returncode==0 |
| 46 | assert 'exposed ' not in r.stdout and 'visible ' not in r.stdout,r.stdout |
| 47 | assert 'tried ' in r.stdout |
| 48 | print('go-token-isolation-ok') |
| 49 | `) |
| 50 | sandboxJSON, _ := json.Marshal(sandbox) |
| 51 | script := fmt.Sprintf(`import os,subprocess |
| 52 | exec(%s) |
| 53 | os.environ['HOLDER_PID']=str(os.getpid()) |
| 54 | os.environ['HOLDER_START']=open('/proc/self/stat').read().rsplit(')',1)[1].split()[19] |
| 55 | env={k:v for k,v in os.environ.items() if k!='CLAUDE_CODE_OAUTH_TOKEN'} |
| 56 | r=subprocess.run(['/workspace/probe',os.environ['HOLDER_PID'],os.environ['HOLDER_START']],env=env,capture_output=True,text=True) |
| 57 | assert r.returncode==0 and 'exposed /proc/'+os.environ['HOLDER_PID']+'/environ' in r.stdout |
| 58 | exec(%s) |
| 59 | `, buildJSON, sandboxJSON) |
| 60 | l, argv, _ := engineeringGoPolicy(t, m, l, script) |
| 61 | ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second) |
| 62 | defer cancel() |
| 63 | p, err := m.Start(ctx, l, argv) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | defer p.Stdin().Close() |
| 68 | defer p.Stdout().Close() |
| 69 | defer p.Stderr().Close() |
| 70 | out, stderr := engineeringOutput(p) |
| 71 | exit, err := p.Wait(ctx) |
| 72 | if strings.Contains(string(out)+string(stderr), canary) { |
| 73 | t.Fatal("probe leaked token value") |
| 74 | } |
| 75 | if err != nil || exit.Code != 0 || !strings.Contains(string(out), "go-token-isolation-ok") { |
| 76 | t.Fatalf("Go probe: %+v %v %s %s", exit, err, out, stderr) |
| 77 | } |
| 78 | } |
| 79 | |