//go:build linux package containment import ( "context" "crypto/sha256" "encoding/hex" "fmt" "io" "os" "path/filepath" "strings" "testing" ) func TestNamespacePinnedMountSwap(t *testing.T) { m, l := fixture(t) if e := os.WriteFile(filepath.Join(l.Workspace, "marker"), []byte("workspace"), 0600); e != nil { t.Fatal(e) } outside := t.TempDir() if e := os.WriteFile(filepath.Join(outside, "marker"), []byte("protected"), 0600); e != nil { t.Fatal(e) } dir, e := os.MkdirTemp(m.cfg.ScratchRoot, "attempt-") if e != nil { t.Fatal(e) } for _, name := range []string{"tmp", "home"} { if e = os.Mkdir(filepath.Join(dir, name), 0700); e != nil { t.Fatal(e) } } plan, e := m.mountPlan(l, dir) if e != nil { t.Fatal(e) } if _, e = m.namespaceManifest(l, []string{"/usr/bin/cat", "/workspace/marker"}, l.Policy.Hash, dir, plan); e != nil { t.Fatal(e) } if e = os.Rename(l.Workspace, l.Workspace+"-original"); e != nil { t.Fatal(e) } if e = os.Symlink(outside, l.Workspace); e != nil { t.Fatal(e) } boot, e := bootID() if e != nil { t.Fatal(e) } r := Record{ID: "01234567890123456789012345678901", BootID: boot, AttemptKey: l.AttemptKey, ManifestHash: l.Policy.Hash} if e = m.persist(r); e != nil { t.Fatal(e) } result := make(chan startResult, 1) go m.lifecycle(context.Background(), r, []string{"/usr/bin/cat", "/workspace/marker"}, plan, dir, result, nil, false) started := <-result if started.err != nil { t.Fatal(started.err) } p := started.p defer p.Stdin().Close() defer p.Stdout().Close() defer p.Stderr().Close() b, e := io.ReadAll(p.Stdout()) if e != nil { t.Fatal(e) } exit, e := p.Wait(context.Background()) if e != nil || exit.Code != 0 || string(b) != "workspace" { t.Fatalf("mutable path replaced pinned source: %q %+v %v", b, exit, e) } } func TestNamespaceRejectsBroadWorkspace(t *testing.T) { m, l := fixture(t) for _, path := range []string{"/home/another-user", "/tmp", "/root"} { bad := l bad.Workspace = path // Inspect validation before any open; these paths need not exist. if _, e := m.mountPlan(bad, m.cfg.ScratchRoot); e == nil { t.Fatalf("broad workspace accepted: %s", path) } } } func TestNamespaceClaudeEnvironment(t *testing.T) { m, l := fixture(t) dir, e := os.MkdirTemp(m.cfg.ScratchRoot, "claude-env-") if e != nil { t.Fatal(e) } for _, name := range []string{"tmp", "home"} { if e = os.Mkdir(filepath.Join(dir, name), 0700); e != nil { t.Fatal(e) } } for _, key := range []string{"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "DISABLE_AUTOUPDATER"} { t.Run(key, func(t *testing.T) { l.Policy.Env = map[string]string{key: "1"} plan, e := m.mountPlan(l, dir) if e != nil { t.Fatalf("required Claude environment refused: %v", e) } defer plan.close() found := false for i, arg := range plan.args { if arg == "--setenv" && i+2 < len(plan.args) && plan.args[i+1] == key && plan.args[i+2] == "1" { found = true } } if !found { t.Fatal("required Claude environment omitted") } }) } l.Policy.Env = map[string]string{"CLAUDE_CODE_DISABLE_AUTO_UPDATE": "1"} if plan, e := m.mountPlan(l, dir); e == nil { plan.close() t.Fatal("unsupported updater environment accepted") } } func TestCoordinatorToolNoCheckout(t *testing.T) { m, l := fixture(t) l.Policy.Manifest = []byte(`{"Action":"coordinate"}`) hash := sha256.Sum256(l.Policy.Manifest) l.Policy.ManifestHash = hex.EncodeToString(hash[:]) dir, err := os.MkdirTemp(m.cfg.ScratchRoot, "coordinator-") if err != nil { t.Fatal(err) } defer os.RemoveAll(dir) for _, name := range []string{"tmp", "home"} { if err = os.Mkdir(filepath.Join(dir, name), 0700); err != nil { t.Fatal(err) } } plan, err := m.mountPlan(l, dir) if err != nil { t.Fatal(err) } defer plan.close() for i, arg := range plan.args { if arg == "/workspace" && i > 0 && strings.Contains(plan.args[i-1], "bind") { t.Fatal("checkout mounted") } } found := false for i, arg := range plan.args { if arg == "--tmpfs" && i+1 < len(plan.args) && plan.args[i+1] == "/workspace" { found = true } } if !found { t.Fatal("empty workspace absent") } for _, f := range plan.files { name, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", f.Fd())) if err != nil { t.Fatal(err) } if name == l.Workspace { t.Fatal("checkout descriptor passed to namespace") } } } // The thin coordinator's desk is its /workspace, read-only: it sees its // notes and nothing beside them (its session home, other desks), and // cannot write there; only the runner's write_note does. func TestNamespaceConverseDeskReadOnly(t *testing.T) { m, l := fixture(t) raw, err := os.ReadFile(m.cfg.Binary) if err != nil { t.Fatal(err) } bwrap := sha256.Sum256(raw) l.Policy.Manifest = []byte(fmt.Sprintf(`{"Action":"converse","Files":[{"Mount":{"Source":%q},"SHA256":%q}]}`, m.cfg.Binary, hex.EncodeToString(bwrap[:]))) hash := sha256.Sum256(l.Policy.Manifest) l.Policy.ManifestHash = hex.EncodeToString(hash[:]) desk := filepath.Join(l.Workspace, "person", "desk") if err := os.MkdirAll(desk, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(l.Workspace, "person", "session.jsonl"), []byte("transcript"), 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(desk, "notebook.md"), []byte("# Notebook"), 0600); err != nil { t.Fatal(err) } l.Workspace = desk script := `import os print(sorted(os.listdir("/workspace")), open("/workspace/notebook.md").read()) for path in ("/workspace/planted.md", "/workspace/notebook.md"): try: open(path, "a").write("x") print("wrote", path) except OSError as e: print("refused", e.errno) print(os.path.exists("/workspace/../session.jsonl"))` argv := []string{"/usr/bin/python3", "-u", "-c", script} dir, err := os.MkdirTemp(m.cfg.ScratchRoot, "attempt-") defer os.RemoveAll(dir) if err != nil { t.Fatal(err) } for _, name := range []string{"tmp", "home"} { if err = os.Mkdir(filepath.Join(dir, name), 0700); err != nil { t.Fatal(err) } } plan, err := m.mountPlan(l, dir) if err != nil { t.Fatal(err) } if _, err = m.namespaceManifest(l, argv, l.Policy.Hash, dir, plan); err != nil { t.Fatal(err) } boot, err := bootID() if err != nil { t.Fatal(err) } r := Record{ID: "01234567890123456789012345678902", BootID: boot, AttemptKey: l.AttemptKey, ManifestHash: l.Policy.Hash} if err = m.persist(r); err != nil { t.Fatal(err) } result := make(chan startResult, 1) go m.lifecycle(context.Background(), r, argv, plan, dir, result, nil, false) started := <-result if started.err != nil { t.Fatal(started.err) } p := started.p defer p.Stderr().Close() p.Stdin().Close() out, err := io.ReadAll(p.Stdout()) if err != nil { t.Fatal(err) } if _, err := p.Wait(context.Background()); err != nil { t.Fatal(err) } want := "['notebook.md'] # Notebook\nrefused 30\nrefused 30\nFalse\n" if string(out) != want { t.Fatalf("desk inside the namespace:\n%s\nwant\n%s", out, want) } if raw, _ := os.ReadFile(filepath.Join(desk, "notebook.md")); string(raw) != "# Notebook" { t.Fatalf("notebook changed from inside: %q", raw) } }