//go:build linux package containment import ( "errors" "os" "path/filepath" "strings" "syscall" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) // renderDir makes a runner-style render directory: 0700, the runner's, // regular files only (the step's captures so far). func renderDir(t *testing.T) string { t.Helper() dir := filepath.Join(t.TempDir(), "render") if err := os.Mkdir(dir, 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "1-work-1.png"), []byte("capture bytes"), 0600); err != nil { t.Fatal(err) } return dir } // The Design step sees its captures read-only at /render, including one // the runner writes after the launch (render_page runs outside the // namespace, into the same directory); nothing inside writes there. func TestNamespaceRenderDirReadOnly(t *testing.T) { m, l := fixture(t) l = withAction(t, m, l, provider.DesignStepAction) l.RenderDir = renderDir(t) script := `import os, sys print(sorted(os.listdir("/render")), open("/render/1-work-1.png").read()) for path in ("/render/1-work-1.png", "/render/planted.png"): try: open(path, "a").write("x") print("wrote", path) except OSError as e: print("refused", e.errno) print("research", os.path.exists("/research")) ` out := runPlanned(t, m, l, []string{"/usr/bin/python3", "-u", "-c", script}, "01234567890123456789012345678913") want := "['1-work-1.png'] capture bytes\nrefused 30\nrefused 30\nresearch False\n" if out != want { t.Fatalf("/render inside the namespace:\n%s\nwant\n%s", out, want) } if raw, _ := os.ReadFile(filepath.Join(l.RenderDir, "1-work-1.png")); string(raw) != "capture bytes" { t.Fatalf("capture changed: %q", raw) } } // The /render bind is refused for any launch but the Design step's // (Engineering and the legacy design action above all), and for a // directory that is not the runner's own flat directory of regular // files apart from every other mount. func TestRenderDirRefusals(t *testing.T) { m, base := fixture(t) for _, c := range []struct { name, reason string setup func(t *testing.T, l *provider.Launch) }{ {"engineering", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "implement") }}, {"legacy design", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "design") }}, {"review", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "inspect") }}, {"research step", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, provider.ResearchStepAction) }}, {"coordinator", "launch", func(t *testing.T, l *provider.Launch) { *l = withAction(t, m, *l, "converse") }}, {"no action", "launch", func(t *testing.T, l *provider.Launch) { l.Policy.Manifest, l.Policy.ManifestHash = []byte(`{}`), "" }}, {"tampered manifest", "launch", func(t *testing.T, l *provider.Launch) { l.Policy.ManifestHash = provider.ManifestDigest([]byte(`{}`)) }}, {"with research images", "launch", func(t *testing.T, l *provider.Launch) { l.ResearchDir = researchDir(t) }}, {"symlinked directory", "", func(t *testing.T, l *provider.Launch) { link := filepath.Join(t.TempDir(), "render") os.Symlink(l.RenderDir, link) l.RenderDir = link }}, {"relative path", "", func(t *testing.T, l *provider.Launch) { l.RenderDir = "render" }}, {"group readable", "mode", func(t *testing.T, l *provider.Launch) { os.Chmod(l.RenderDir, 0750) }}, {"subdirectory", "entry", func(t *testing.T, l *provider.Launch) { os.Mkdir(filepath.Join(l.RenderDir, "nested"), 0700) }}, {"symlink entry", "entry", func(t *testing.T, l *provider.Launch) { os.Symlink("/etc/hostname", filepath.Join(l.RenderDir, "2-work-1.png")) }}, {"fifo entry", "entry", func(t *testing.T, l *provider.Launch) { syscall.Mkfifo(filepath.Join(l.RenderDir, "2-work-1.png"), 0600) }}, {"inside the workspace", "overlap", func(t *testing.T, l *provider.Launch) { dir := filepath.Join(l.Workspace, "render") os.Mkdir(dir, 0700) l.RenderDir = dir }}, {"is the workspace", "overlap", func(t *testing.T, l *provider.Launch) { l.RenderDir = l.Workspace }}, {"inside the scratch root", "overlap", func(t *testing.T, l *provider.Launch) { dir := filepath.Join(m.cfg.ScratchRoot, "render") os.Mkdir(dir, 0700) l.RenderDir = dir }}, } { t.Run(c.name, func(t *testing.T) { l := withAction(t, m, base, provider.DesignStepAction) ws := filepath.Join(t.TempDir(), "workspace") os.Mkdir(ws, 0700) l.Workspace = ws l.RenderDir = renderDir(t) c.setup(t, &l) plan, err := m.mountPlan(l, attemptDir(t, m)) if err == nil { plan.close() t.Fatal("render directory accepted") } if !errors.Is(err, errMount) || !strings.Contains(err.Error(), c.reason) { t.Fatalf("refused for the wrong reason: %v (want %q)", err, c.reason) } }) } // The Design step's launch with no render directory has no /render. l := withAction(t, m, base, provider.DesignStepAction) plan, err := m.mountPlan(l, attemptDir(t, m)) if err != nil { t.Fatalf("design launch without captures: %v", err) } defer plan.close() for _, arg := range plan.args { if arg == "/render" { t.Fatal("/render bound with no directory") } } }