//go:build linux package containment import ( "context" "io" "os" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/runner/provider" ) func TestNamespaceCheckGatesReadOnly(t *testing.T) { m, l := fixture(t) trusted := filepath.Join(m.cfg.ScratchRoot, "trusted-gate") if err := os.WriteFile(trusted, []byte("trusted base\n"), 0600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(l.Workspace, "Makefile"), []byte("candidate\n"), 0600); err != nil { t.Fatal(err) } argv := []string{"/usr/bin/bash", "-c", `cat Makefile; if echo bypass > Makefile 2>/dev/null; then exit 41; fi; if rm Makefile 2>/dev/null; then exit 42; fi; echo built > output`} p, err := provider.BuildCheckPolicy(provider.CheckPolicyInputs{Workspace: l.Workspace, Env: map[string]string{}, Bwrap: m.cfg.Binary, Argv: argv, GateFiles: []provider.Mount{{Source: trusted, Target: "/workspace/Makefile", ReadOnly: true}}}) if err != nil { t.Fatal(err) } l.Policy = p ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() proc, err := m.Start(ctx, l, argv) if err != nil { t.Fatal(err) } proc.Stdin().Close() out, err := io.ReadAll(proc.Stdout()) if err != nil { t.Fatal(err) } tail, _ := io.ReadAll(proc.Stderr()) exit, err := proc.Wait(ctx) if err != nil || exit.Code != 0 || strings.TrimSpace(string(out)) != "trusted base" { t.Fatalf("gate not pinned read-only: code=%d err=%v out=%s tail=%s", exit.Code, err, out, tail) } if _, err := os.Stat(filepath.Join(l.Workspace, "output")); err != nil { t.Fatal("candidate build workspace not writable", err) } } func TestNamespaceCheckGateDirectoryReadOnly(t *testing.T) { m, l := fixture(t) trusted := filepath.Join(m.cfg.ScratchRoot, "trusted-directory") if err := os.Mkdir(trusted, 0700); err != nil { t.Fatal(err) } t.Cleanup(func() { _ = os.Chmod(trusted, 0700) }) if err := os.WriteFile(filepath.Join(trusted, "check.py"), []byte("trusted base\n"), 0600); err != nil { t.Fatal(err) } if err := provider.SealTree(trusted); err != nil { t.Fatal(err) } if err := os.Mkdir(filepath.Join(l.Workspace, "scripts"), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(l.Workspace, "scripts", "check.py"), []byte("candidate\n"), 0600); err != nil { t.Fatal(err) } argv := []string{"/usr/bin/bash", "-c", `cat scripts/check.py; chmod u+w scripts 2>/dev/null || true; if echo bypass > scripts/json.py 2>/dev/null; then exit 41; fi; if rm scripts/check.py 2>/dev/null; then exit 42; fi; echo built > output`} p, err := provider.BuildCheckPolicy(provider.CheckPolicyInputs{Workspace: l.Workspace, Env: map[string]string{}, Bwrap: m.cfg.Binary, Argv: argv, GateFiles: []provider.Mount{{Source: trusted, Target: "/workspace/scripts", ReadOnly: true}}}) if err != nil { t.Fatal(err) } l.Policy = p ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() proc, err := m.Start(ctx, l, argv) if err != nil { t.Fatal(err) } proc.Stdin().Close() out, err := io.ReadAll(proc.Stdout()) if err != nil { t.Fatal(err) } tail, _ := io.ReadAll(proc.Stderr()) exit, err := proc.Wait(ctx) if err != nil || exit.Code != 0 || strings.TrimSpace(string(out)) != "trusted base" { t.Fatalf("gate not pinned read-only: code=%d err=%v out=%s tail=%s", exit.Code, err, out, tail) } if _, err := os.Stat(filepath.Join(l.Workspace, "output")); err != nil { t.Fatal("candidate build workspace not writable", err) } }