//go:build linux package containment import ( "bufio" "bytes" "io/fs" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) const canary = "sk-ant-oat01-canary-containment-5e1d" // A token connection's token reaches the harness's environment inside // the namespace and nowhere else: not bubblewrap's argv, the manifest or // the attempt's record. func TestNamespaceSecretEnvironment(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 } // The script does not hold the token: it reads it from its environment. script := `import os,sys tok=os.environ.get("CLAUDE_CODE_OAUTH_TOKEN","") argv=open("/proc/self/cmdline").read() sys.stdout.write(tok+"|"+os.environ.get("CLAUDE_CODE_SUBPROCESS_ENV_SCRUB","")+"|"+str(tok!="" and tok in argv)+"\n") sys.stdout.flush() sys.stdin.read() ` p := launch(t, m, l, script) line, err := bufio.NewReader(p.Stdout()).ReadString('\n') if err != nil { t.Fatal(err) } if line != canary+"|1|False\n" { t.Fatalf("namespace environment %q", strings.ReplaceAll(line, canary, "")) } // While the harness runs: no host process has the token in its argv. procs, _ := filepath.Glob("/proc/[0-9]*/cmdline") for _, path := range procs { if raw, err := os.ReadFile(path); err == nil && bytes.Contains(raw, []byte(canary)) { t.Errorf("the token is in the argv of %s", filepath.Dir(path)) } } // Nor any record or manifest the manager keeps. _ = filepath.WalkDir(m.cfg.ScratchRoot, func(path string, d fs.DirEntry, err error) error { if err != nil || !d.Type().IsRegular() { return nil } if raw, err := os.ReadFile(path); err == nil && bytes.Contains(raw, []byte(canary)) { t.Errorf("the token is in %s", path) } return nil }) if record(t, m, p).ManifestHash == "" { t.Fatal("no record") } } // Only the token and the scrub may be passed this way, and no value // that would split the arguments. func TestNamespaceSecretEnvironmentRefused(t *testing.T) { for _, env := range []map[string]string{ {"ANTHROPIC_API_KEY": "x"}, {provider.OAuthTokenEnv: "a\x00--bind"}, {provider.OAuthTokenEnv: "a\nb"}, {provider.OAuthTokenEnv: ""}, } { p := &mounts{} if err := p.addSecretEnv(env); err == nil { p.close() t.Fatalf("accepted %q", env) } } }