oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package containment
4
5import (
6 "context"
7 "io"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12 "time"
13
14 "amadan.net/oficina/equipo/internal/runner/provider"
15)
16
17func TestNamespaceCheckGatesReadOnly(t *testing.T) {
18 m, l := fixture(t)
19 trusted := filepath.Join(m.cfg.ScratchRoot, "trusted-gate")
20 if err := os.WriteFile(trusted, []byte("trusted base\n"), 0600); err != nil {
21 t.Fatal(err)
22 }
23 if err := os.WriteFile(filepath.Join(l.Workspace, "Makefile"), []byte("candidate\n"), 0600); err != nil {
24 t.Fatal(err)
25 }
26 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`}
27 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}}})
28 if err != nil {
29 t.Fatal(err)
30 }
31 l.Policy = p
32 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
33 defer cancel()
34 proc, err := m.Start(ctx, l, argv)
35 if err != nil {
36 t.Fatal(err)
37 }
38 proc.Stdin().Close()
39 out, err := io.ReadAll(proc.Stdout())
40 if err != nil {
41 t.Fatal(err)
42 }
43 tail, _ := io.ReadAll(proc.Stderr())
44 exit, err := proc.Wait(ctx)
45 if err != nil || exit.Code != 0 || strings.TrimSpace(string(out)) != "trusted base" {
46 t.Fatalf("gate not pinned read-only: code=%d err=%v out=%s tail=%s", exit.Code, err, out, tail)
47 }
48 if _, err := os.Stat(filepath.Join(l.Workspace, "output")); err != nil {
49 t.Fatal("candidate build workspace not writable", err)
50 }
51}
52
53func TestNamespaceCheckGateDirectoryReadOnly(t *testing.T) {
54 m, l := fixture(t)
55 trusted := filepath.Join(m.cfg.ScratchRoot, "trusted-directory")
56 if err := os.Mkdir(trusted, 0700); err != nil {
57 t.Fatal(err)
58 }
59 t.Cleanup(func() { _ = os.Chmod(trusted, 0700) })
60 if err := os.WriteFile(filepath.Join(trusted, "check.py"), []byte("trusted base\n"), 0600); err != nil {
61 t.Fatal(err)
62 }
63 if err := provider.SealTree(trusted); err != nil {
64 t.Fatal(err)
65 }
66 if err := os.Mkdir(filepath.Join(l.Workspace, "scripts"), 0700); err != nil {
67 t.Fatal(err)
68 }
69 if err := os.WriteFile(filepath.Join(l.Workspace, "scripts", "check.py"), []byte("candidate\n"), 0600); err != nil {
70 t.Fatal(err)
71 }
72 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`}
73 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}}})
74 if err != nil {
75 t.Fatal(err)
76 }
77 l.Policy = p
78 ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
79 defer cancel()
80 proc, err := m.Start(ctx, l, argv)
81 if err != nil {
82 t.Fatal(err)
83 }
84 proc.Stdin().Close()
85 out, err := io.ReadAll(proc.Stdout())
86 if err != nil {
87 t.Fatal(err)
88 }
89 tail, _ := io.ReadAll(proc.Stderr())
90 exit, err := proc.Wait(ctx)
91 if err != nil || exit.Code != 0 || strings.TrimSpace(string(out)) != "trusted base" {
92 t.Fatalf("gate not pinned read-only: code=%d err=%v out=%s tail=%s", exit.Code, err, out, tail)
93 }
94 if _, err := os.Stat(filepath.Join(l.Workspace, "output")); err != nil {
95 t.Fatal("candidate build workspace not writable", err)
96 }
97}
98