//go:build linux package containment import ( "context" "encoding/json" "fmt" "io" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) // toolSandbox is the bubblewrap command line the pinned Claude Code 2.1.312 // builds for every Bash tool command on Linux (its sandbox's R1 builder), // with the filesystem and network binds reduced to a read-only root: a // new session, a new user, PID and network namespace, and a fresh /proc. // The CLI mounts the fresh /proc unless sandbox.enableWeakerNestedSandbox // is true, when it binds the inherited one instead; the launcher refuses // settings that set it (PolicyInputs in provider/claude). var toolSandbox = []string{"/usr/bin/bwrap", "--new-session", "--die-with-parent", "--unshare-net", "--ro-bind", "/", "/", "--dev", "/dev", "--unshare-pid", "--unshare-user", "--proc", "/proc", "--unsetenv", "SRT_ALLOW_NESTED_USERNS", "--"} // procProbe is what a model's Bash command can try against the process // that holds the token (argument 1, its PID in the namespace the CLI runs // in; argument 2, its start time, which no PID namespace changes). It // reads every environ it can name: the PID itself, its parent, every // visible process and thread, and the aliases through /proc/self/.., // task/ and another process's root. It prints one "exposed" line // per read that shows the token's variable, a "visible" line where it // can see the token holder itself, and the number of paths it tried. const procProbe = `hp=$1; hstart=$2; tried=0 try() { tried=$((tried+1)) if tr '\0' '\n' <"$1" 2>/dev/null | grep -q '^CLAUDE_CODE_OAUTH_TOKEN='; then echo "exposed $1"; fi } pids="$hp $PPID 1 2 $(ls /proc | grep -E '^[0-9]+$')" for p in $pids; do try "/proc/$p/environ" try "/proc/self/../$p/environ" try "/proc/$p/root/proc/$hp/environ" for task in /proc/$p/task/*; do try "$task/environ"; done stat=$(cat /proc/$p/stat 2>/dev/null) || continue fields=(${stat##*) }) if [ "${fields[19]}" = "$hstart" ]; then echo "visible $p"; fi done try "/proc/$hp/task/$hp/environ" try "/proc/self/root/proc/$hp/environ" try "/proc/1/root/proc/$hp/environ" try "/proc/$PPID/root/proc/$hp/environ" echo "tried $tried" ` // The harness stands in for the claude CLI: it holds the token in its // environment, as LaunchSecretEnv delivers it, and starts the probe as // the CLI starts a Bash tool command: without the token in the child's // environment (CLAUDE_CODE_SUBPROCESS_ENV_SCRUB), once directly and once // in the CLI's tool sandbox. const procHarness = `import json,os,subprocess,sys assert os.environ.get("CLAUDE_CODE_OAUTH_TOKEN") env={k:v for k,v in os.environ.items() if k!="CLAUDE_CODE_OAUTH_TOKEN"} sandbox=json.loads(%q); probe=json.loads(%q) me=str(os.getpid()); start=open("/proc/self/stat").read().rsplit(")",1)[1].split()[19] out={"pid":me} for name,prefix in [("direct",[]),("sandboxed",sandbox)]: r=subprocess.run(prefix+["/usr/bin/bash","-c",probe,"probe",me,start],env=env,stdin=subprocess.DEVNULL,capture_output=True,text=True,timeout=20) out[name]={"code":r.returncode,"out":r.stdout,"err":r.stderr[-400:]} print(json.dumps(out),flush=True) ` type probeRun struct { Code int Out, Err string } // The pasted token is in the CLI's environment. A command the model runs // through the Bash tool must not read it from /proc: the tool sandbox's // fresh PID namespace and /proc hide the CLI and every ancestor, under // every alias. The direct run is the control: without the namespace the // same probe reads the token, so the probe is not vacuous. func TestToolCannotReadCLIEnvironment(t *testing.T) { m, l := fixture(t) m.secretEnv = func(provider.Launch) (map[string]string, error) { return map[string]string{provider.OAuthTokenEnv: canary, provider.ScrubEnv: "1"}, nil } sandbox, _ := json.Marshal(toolSandbox) probe, _ := json.Marshal(procProbe) p := launch(t, m, l, fmt.Sprintf(procHarness, sandbox, probe)) raw, err := io.ReadAll(p.Stdout()) if err != nil { t.Fatal(err) } if exit, err := p.Wait(context.Background()); err != nil || exit.Code != 0 { errout, _ := io.ReadAll(p.Stderr()) t.Fatalf("harness: %+v %v %s", exit, err, errout) } if strings.Contains(string(raw), canary) { t.Fatal("the token's value reached the probe's output") } var out struct { PID string Direct, Sandboxed probeRun } if err = json.Unmarshal(raw, &out); err != nil { t.Fatalf("harness output %q: %v", raw, err) } // Control: run directly, the probe reads the CLI's environment. if out.Direct.Code != 0 || !strings.Contains(out.Direct.Out, "exposed /proc/"+out.PID+"/environ\n") || !strings.Contains(out.Direct.Out, "visible "+out.PID+"\n") { t.Fatalf("control probe found nothing, so it proves nothing: %+v", out.Direct) } // The namespace helper (PID 1) holds the token too, but is not // dumpable: not even a direct child of the CLI reads its environment. if strings.Contains(out.Direct.Out, "exposed /proc/1/") { t.Fatalf("the namespace helper's environment is readable: %s", out.Direct.Out) } run := out.Sandboxed if run.Code != 0 || !strings.HasPrefix(lastLine(run.Out), "tried ") { t.Fatalf("tool sandbox did not start inside the namespace: %+v", run) } var tried int if _, err = fmt.Sscanf(lastLine(run.Out), "tried %d", &tried); err != nil || tried < 12 { t.Fatalf("probe tried %d paths", tried) } t.Logf("control: %d reads exposed the token; sandboxed: %d paths tried", strings.Count(out.Direct.Out, "exposed "), tried) for _, line := range strings.Split(strings.TrimSpace(run.Out), "\n") { if strings.HasPrefix(line, "exposed ") || strings.HasPrefix(line, "visible ") { t.Errorf("from the tool sandbox: %s", line) } } } func lastLine(s string) string { lines := strings.Split(strings.TrimSpace(s), "\n") return lines[len(lines)-1] }