| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "bufio" |
| 7 | "bytes" |
| 8 | "io/fs" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 15 | ) |
| 16 | |
| 17 | const canary = "sk-ant-oat01-canary-containment-5e1d" |
| 18 | |
| 19 | // A token connection's token reaches the harness's environment inside |
| 20 | // the namespace and nowhere else: not bubblewrap's argv, the manifest or |
| 21 | // the attempt's record. |
| 22 | func TestNamespaceSecretEnvironment(t *testing.T) { |
| 23 | m, l := fixture(t) |
| 24 | m.secretEnv = func(provider.Launch) (map[string]string, error) { |
| 25 | return map[string]string{provider.OAuthTokenEnv: canary, provider.ScrubEnv: "1"}, nil |
| 26 | } |
| 27 | // The script does not hold the token: it reads it from its environment. |
| 28 | script := `import os,sys |
| 29 | tok=os.environ.get("CLAUDE_CODE_OAUTH_TOKEN","") |
| 30 | argv=open("/proc/self/cmdline").read() |
| 31 | sys.stdout.write(tok+"|"+os.environ.get("CLAUDE_CODE_SUBPROCESS_ENV_SCRUB","")+"|"+str(tok!="" and tok in argv)+"\n") |
| 32 | sys.stdout.flush() |
| 33 | sys.stdin.read() |
| 34 | ` |
| 35 | p := launch(t, m, l, script) |
| 36 | line, err := bufio.NewReader(p.Stdout()).ReadString('\n') |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if line != canary+"|1|False\n" { |
| 41 | t.Fatalf("namespace environment %q", strings.ReplaceAll(line, canary, "<token>")) |
| 42 | } |
| 43 | // While the harness runs: no host process has the token in its argv. |
| 44 | procs, _ := filepath.Glob("/proc/[0-9]*/cmdline") |
| 45 | for _, path := range procs { |
| 46 | if raw, err := os.ReadFile(path); err == nil && bytes.Contains(raw, []byte(canary)) { |
| 47 | t.Errorf("the token is in the argv of %s", filepath.Dir(path)) |
| 48 | } |
| 49 | } |
| 50 | // Nor any record or manifest the manager keeps. |
| 51 | _ = filepath.WalkDir(m.cfg.ScratchRoot, func(path string, d fs.DirEntry, err error) error { |
| 52 | if err != nil || !d.Type().IsRegular() { |
| 53 | return nil |
| 54 | } |
| 55 | if raw, err := os.ReadFile(path); err == nil && bytes.Contains(raw, []byte(canary)) { |
| 56 | t.Errorf("the token is in %s", path) |
| 57 | } |
| 58 | return nil |
| 59 | }) |
| 60 | if record(t, m, p).ManifestHash == "" { |
| 61 | t.Fatal("no record") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Only the token and the scrub may be passed this way, and no value |
| 66 | // that would split the arguments. |
| 67 | func TestNamespaceSecretEnvironmentRefused(t *testing.T) { |
| 68 | for _, env := range []map[string]string{ |
| 69 | {"ANTHROPIC_API_KEY": "x"}, |
| 70 | {provider.OAuthTokenEnv: "a\x00--bind"}, |
| 71 | {provider.OAuthTokenEnv: "a\nb"}, |
| 72 | {provider.OAuthTokenEnv: ""}, |
| 73 | } { |
| 74 | p := &mounts{} |
| 75 | if err := p.addSecretEnv(env); err == nil { |
| 76 | p.close() |
| 77 | t.Fatalf("accepted %q", env) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |