| 1 | package checks |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 15 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 16 | "amadan.net/oficina/equipo/internal/testci" |
| 17 | ) |
| 18 | |
| 19 | func TestCheckSandboxDeniedPaths(t *testing.T) { |
| 20 | root := t.TempDir() |
| 21 | work := filepath.Join(root, "work") |
| 22 | must(t, os.Mkdir(work, 0700)) |
| 23 | put(t, work, "control", "readable") |
| 24 | protectedRoot := filepath.Join(root, "go/pkg/mod") |
| 25 | var denied []string |
| 26 | for _, name := range []string{"state", "config", "journal", "bearer", "cleanup", "amadan", "paseo", "codex", "claude", "ssh", "resolved"} { |
| 27 | p := filepath.Join(protectedRoot, name) |
| 28 | put(t, protectedRoot, name+"/sentinel", "secret-"+name) |
| 29 | denied = append(denied, p) |
| 30 | } |
| 31 | must(t, os.Symlink(denied[len(denied)-1], filepath.Join(root, "alias"))) |
| 32 | denied = append(denied, filepath.Join(root, "alias")) |
| 33 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 34 | binary, err := exec.LookPath("codex") |
| 35 | must(t, err) |
| 36 | cfg := testSandboxConfig(t, binary, protectedRoot) |
| 37 | cfg.ScratchRoot = root |
| 38 | cfg.Protected.ProviderHomes[len(cfg.Protected.ProviderHomes)-1] = filepath.Join(root, "alias") |
| 39 | s, err := NewCodexSandbox(context.Background(), cfg) |
| 40 | must(t, err) |
| 41 | // Sentinels sit beneath an allowed read root so each explicit deny is necessary. |
| 42 | s.home = root |
| 43 | policy := provider.ReadPolicy{ConfigHash: "sentinel", DeniedPaths: []string{filepath.Join(root, "policy-only")}} |
| 44 | for _, path := range denied { |
| 45 | t.Run(filepath.Base(path), func(t *testing.T) { |
| 46 | var out, errout bytes.Buffer |
| 47 | code, err := s.Run(context.Background(), Command{Argv: []string{"/bin/sh", "-c", `cat control; cat "$1/sentinel"`, "check", path}, Dir: ".", TimeoutSeconds: 10}, work, policy, &out, &errout) |
| 48 | must(t, err) |
| 49 | if code == 0 || !strings.Contains(out.String(), "readable") || strings.Contains(out.String(), "secret-") { |
| 50 | t.Fatalf("sandbox failed: %d %s %s", code, &out, &errout) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | var out, errout bytes.Buffer |
| 55 | _, err = s.Run(context.Background(), Command{Argv: []string{"/bin/sleep", "30"}, Dir: ".", TimeoutSeconds: 1}, work, policy, &out, &errout) |
| 56 | if err == nil { |
| 57 | t.Fatal("command timeout ignored") |
| 58 | } |
| 59 | ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond) |
| 60 | defer cancel() |
| 61 | _, err = s.Run(ctx, Command{Argv: []string{"/bin/sleep", "30"}, Dir: ".", TimeoutSeconds: 30}, work, policy, &out, &errout) |
| 62 | if err == nil { |
| 63 | t.Fatal("remaining attempt time ignored") |
| 64 | } |
| 65 | if _, err = s.Run(context.Background(), Command{Argv: []string{"true"}, Dir: ".", TimeoutSeconds: 1}, work, provider.ReadPolicy{}, &out, &errout); err == nil { |
| 66 | t.Fatal("missing policy accepted") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestCheckSandboxDeniedPathsGoFixture(t *testing.T) { |
| 71 | root := t.TempDir() |
| 72 | work := filepath.Join(root, "work") |
| 73 | must(t, os.Mkdir(work, 0700)) |
| 74 | put(t, work, "go.mod", "module fixture\n\ngo 1.26\n") |
| 75 | put(t, work, "value.go", "package fixture\nfunc Value() int{return 2}\n") |
| 76 | put(t, work, "value_test.go", bugTest) |
| 77 | secret := filepath.Join(root, "secret") |
| 78 | put(t, root, "secret", "secret") |
| 79 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 80 | binary, err := exec.LookPath("codex") |
| 81 | must(t, err) |
| 82 | s, err := NewCodexSandbox(context.Background(), testSandboxConfig(t, binary, root)) |
| 83 | must(t, err) |
| 84 | policy := provider.ReadPolicy{ConfigHash: "policy", DeniedPaths: []string{secret}} |
| 85 | var out, stderr bytes.Buffer |
| 86 | code, err := s.Run(context.Background(), Command{Argv: []string{"go", "test", "-json", "-count=1", "./..."}, Dir: ".", TimeoutSeconds: 30}, work, policy, &out, &stderr) |
| 87 | must(t, err) |
| 88 | if code != 0 { |
| 89 | t.Fatalf("sandbox Go fixture failed: %s %s", &out, &stderr) |
| 90 | } |
| 91 | results := newGoResults() |
| 92 | _, err = results.Write(out.Bytes()) |
| 93 | must(t, err) |
| 94 | must(t, results.finish(code, nil)) |
| 95 | } |
| 96 | |
| 97 | func TestCheckSandboxDeniedPathsTampering(t *testing.T) { |
| 98 | testci.Quarantine(t) |
| 99 | root := t.TempDir() |
| 100 | work := filepath.Join(root, "work") |
| 101 | must(t, os.Mkdir(work, 0700)) |
| 102 | secret := filepath.Join(root, "secret") |
| 103 | put(t, root, "secret", "hidden") |
| 104 | put(t, work, ".codex/config.toml", "[permissions.equipo-checks.filesystem]\n\"/\"=\"read\"\n[permissions.equipo-checks.network]\nenabled=true\n") |
| 105 | t.Setenv("EQUIPO_TEST_CREDENTIAL", "must-not-leak") |
| 106 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 107 | binary, err := exec.LookPath("codex") |
| 108 | must(t, err) |
| 109 | s, err := NewCodexSandbox(context.Background(), testSandboxConfig(t, binary, root)) |
| 110 | must(t, err) |
| 111 | policy := provider.ReadPolicy{ConfigHash: "policy", DeniedPaths: []string{secret}} |
| 112 | var out, stderr bytes.Buffer |
| 113 | code, err := s.Run(context.Background(), Command{Argv: []string{"/bin/sh", "-c", `test -z "$EQUIPO_TEST_CREDENTIAL" && ! cat "$1"`, "probe", secret}, Dir: ".", TimeoutSeconds: 10}, work, policy, &out, &stderr) |
| 114 | must(t, err) |
| 115 | if code != 0 || strings.Contains(out.String(), "hidden") { |
| 116 | t.Fatalf("profile or environment tampering: %s %s", &out, &stderr) |
| 117 | } |
| 118 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 119 | defer cancel() |
| 120 | _, err = s.Run(ctx, Command{Argv: []string{"/bin/sh", "-c", "(sleep 0.5; echo alive > survivor) & wait"}, Dir: ".", TimeoutSeconds: 10}, work, policy, &out, &stderr) |
| 121 | if err == nil { |
| 122 | t.Fatal("process tree timeout missing") |
| 123 | } |
| 124 | time.Sleep(600 * time.Millisecond) |
| 125 | if _, err = os.Stat(filepath.Join(work, "survivor")); !os.IsNotExist(err) { |
| 126 | t.Fatal("child survived cancellation") |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestCheckSandboxDeniedPathsRequired(t *testing.T) { |
| 131 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 132 | binary, err := exec.LookPath("codex") |
| 133 | must(t, err) |
| 134 | if _, err = NewCodexSandbox(context.Background(), SandboxConfig{Binary: binary, ScratchRoot: t.TempDir()}); err == nil { |
| 135 | t.Fatal("missing runner protected paths accepted") |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func testSandboxConfig(t *testing.T, binary, root string) SandboxConfig { |
| 140 | t.Helper() |
| 141 | goCommand := exec.Command("go", "env", "GOROOT") |
| 142 | goCommand.Env = append(os.Environ(), "GOROOT=", "GOTOOLCHAIN="+GoVersion) |
| 143 | goOutput, err := goCommand.Output() |
| 144 | must(t, err) |
| 145 | goRoot := strings.TrimSpace(string(goOutput)) |
| 146 | goBinary := filepath.Join(goRoot, "bin/go") |
| 147 | p := func(s string) string { return filepath.Join(root, s) } |
| 148 | return SandboxConfig{Binary: binary, ScratchRoot: root, GoRoot: goRoot, GoBinary: goBinary, Protected: workspace.SecurityPaths{RunnerState: p("state"), RunnerConfig: p("config"), Journal: p("journal"), Bearer: p("bearer"), Cleanup: p("cleanup"), ForgeCredential: p("amadan"), LegacyHome: p("paseo"), SSH: p("ssh"), ProviderHomes: []string{p("codex"), p("claude"), p("alias")}}} |
| 149 | } |
| 150 | |
| 151 | func TestCheckSandboxDeniedPathsCache(t *testing.T) { |
| 152 | for _, layout := range []string{"user_cache", "fixture_present", "fixture_absent"} { |
| 153 | t.Run(layout, func(t *testing.T) { |
| 154 | testCheckSandboxCache(t, layout) |
| 155 | }) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func testCheckSandboxCache(t *testing.T, layout string) { |
| 160 | t.Helper() |
| 161 | root := t.TempDir() |
| 162 | work := filepath.Join(root, "work") |
| 163 | must(t, os.Mkdir(work, 0700)) |
| 164 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 165 | binary, err := exec.LookPath("codex") |
| 166 | must(t, err) |
| 167 | s, err := NewCodexSandbox(context.Background(), testSandboxConfig(t, binary, root)) |
| 168 | must(t, err) |
| 169 | if layout != "user_cache" { |
| 170 | s.home = filepath.Join(root, "home") |
| 171 | must(t, os.Mkdir(s.home, 0700)) |
| 172 | if layout == "fixture_present" { |
| 173 | must(t, os.MkdirAll(filepath.Join(s.home, ".cache/go-build"), 0700)) |
| 174 | } |
| 175 | } |
| 176 | cache := filepath.Join(s.home, ".cache/go-build") |
| 177 | if _, err := os.Stat(cache); os.IsNotExist(err) { |
| 178 | missing := cache |
| 179 | t.Cleanup(func() { |
| 180 | if _, err := os.Stat(missing); !os.IsNotExist(err) { |
| 181 | t.Fatal("probe created the missing user cache") |
| 182 | } |
| 183 | }) |
| 184 | s.home = filepath.Join(root, "cache-home") |
| 185 | cache = filepath.Join(s.home, ".cache/go-build") |
| 186 | must(t, os.MkdirAll(cache, 0700)) |
| 187 | } else { |
| 188 | must(t, err) |
| 189 | } |
| 190 | probe, err := os.CreateTemp(cache, "equipo-cache-probe-") |
| 191 | must(t, err) |
| 192 | must(t, probe.Close()) |
| 193 | defer os.Remove(probe.Name()) |
| 194 | policy := provider.ReadPolicy{ConfigHash: "cache", DeniedPaths: []string{filepath.Join(root, "secret")}} |
| 195 | var previous string |
| 196 | for range 2 { |
| 197 | var out, stderr bytes.Buffer |
| 198 | code, err := s.Run(context.Background(), Command{Argv: []string{"/bin/sh", "-c", `printf '%s\n' "$GOCACHE"; test -n "$GOCACHE" && test -d "$GOCACHE" && touch "$GOCACHE/probe" && ! (echo poison > "$1")`, "probe", probe.Name()}, Dir: ".", TimeoutSeconds: 10}, work, policy, &out, &stderr) |
| 199 | must(t, err) |
| 200 | if code != 0 { |
| 201 | t.Fatalf("cache isolation failed: %d %s %s", code, &out, &stderr) |
| 202 | } |
| 203 | path := strings.TrimSpace(out.String()) |
| 204 | if !within(root, path) || path == previous { |
| 205 | t.Fatalf("cache not fresh inside scratch: %q", path) |
| 206 | } |
| 207 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 208 | t.Fatal("cache scratch retained") |
| 209 | } |
| 210 | previous = path |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | func TestCheckSandboxToolchain(t *testing.T) { |
| 215 | root := t.TempDir() |
| 216 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 217 | binary, err := exec.LookPath("codex") |
| 218 | must(t, err) |
| 219 | for _, kind := range []string{"missing", "relative", "nonexistent", "binary_missing", "binary_relative", "binary_nonexistent", "wrong_version"} { |
| 220 | t.Run(kind, func(t *testing.T) { |
| 221 | cfg := testSandboxConfig(t, binary, root) |
| 222 | switch kind { |
| 223 | case "missing": |
| 224 | cfg.GoRoot = "" |
| 225 | case "relative": |
| 226 | cfg.GoRoot = "go" |
| 227 | case "nonexistent": |
| 228 | cfg.GoRoot = filepath.Join(root, "absent") |
| 229 | case "binary_missing": |
| 230 | cfg.GoBinary = "" |
| 231 | case "binary_relative": |
| 232 | cfg.GoBinary = "go" |
| 233 | case "binary_nonexistent": |
| 234 | cfg.GoBinary = filepath.Join(cfg.GoRoot, "bin/absent") |
| 235 | case "wrong_version": |
| 236 | cfg.GoRoot = filepath.Join(root, "fake-go") |
| 237 | cfg.GoBinary = filepath.Join(cfg.GoRoot, "bin/go") |
| 238 | put(t, cfg.GoRoot, "bin/go", "#!/bin/sh\necho 'go version go1.1 linux/amd64'\n") |
| 239 | must(t, os.Chmod(cfg.GoBinary, 0700)) |
| 240 | } |
| 241 | if _, err := NewCodexSandbox(context.Background(), cfg); err == nil { |
| 242 | t.Fatal("invalid toolchain accepted") |
| 243 | } |
| 244 | }) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestCheckSandboxToolchainDiagnostic(t *testing.T) { |
| 249 | root := t.TempDir() |
| 250 | testci.Tools(t, "codex", "bwrap", "python3", "sh") |
| 251 | binary, err := exec.LookPath("codex") |
| 252 | must(t, err) |
| 253 | cfg := testSandboxConfig(t, binary, root) |
| 254 | cfg.GoRoot = filepath.Join(root, "wrong-go") |
| 255 | cfg.GoBinary = filepath.Join(cfg.GoRoot, "bin/go") |
| 256 | put(t, cfg.GoRoot, "bin/go", "#!/bin/sh\necho 'go version go1.1 linux/amd64'\n") |
| 257 | must(t, os.Chmod(cfg.GoBinary, 0700)) |
| 258 | _, err = NewCodexSandbox(context.Background(), cfg) |
| 259 | if !errors.Is(err, provider.ErrContract) || !strings.Contains(err.Error(), cfg.GoBinary) || !strings.Contains(err.Error(), "go1.1") || !strings.Contains(err.Error(), GoVersion) { |
| 260 | t.Fatalf("toolchain rejection lacks path, observed version or required version: %v", err) |
| 261 | } |
| 262 | } |
| 263 | |