oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package checks
2
3import (
4 "context"
5 "crypto/rand"
6 "encoding/json"
7 "errors"
8 "io"
9 "path/filepath"
10 "sort"
11 "strings"
12 "time"
13
14 "amadan.net/oficina/equipo/internal/runner/provider"
15)
16
17func (s *CodexSandbox) runNamespace(ctx context.Context, c Command, work string, policy provider.ReadPolicy, stdout, stderr io.Writer) (int, error) {
18 roots := map[string]string{":minimal": "read", "/workspace": "write", "/tmp": "write", "/opt/go": "read", "/opt/go-mod": "read", "/opt/codex": "read", "/home/runner": "deny", filepath.Join(s.home, ".cache/go-build"): "deny"}
19 // Host paths have meaning here only through an outer mount. In particular,
20 // host /tmp is replaced by private scratch, so missing host deny paths must
21 // not become masks inside that scratch directory.
22 mounts := []provider.Mount{
23 {Source: work, Target: "/workspace"},
24 {Source: s.goRoot, Target: "/opt/go"},
25 {Source: s.direct.ModuleCache, Target: "/opt/go-mod"},
26 {Source: s.binary, Target: "/opt/codex"},
27 {Source: s.direct.RunnerBinary, Target: "/equipo-namespace"},
28 {Source: s.direct.Bwrap, Target: "/opt/bwrap"},
29 {Source: "/usr", Target: "/usr"},
30 {Source: "/etc", Target: "/etc"},
31 }
32 for _, path := range append(append([]string(nil), s.protected...), policy.DeniedPaths...) {
33 resolved, err := canonicalPath(path)
34 if err != nil {
35 return -1, err
36 }
37 for _, denied := range []string{path, resolved} {
38 for _, mount := range mounts {
39 if within(denied, mount.Source) {
40 roots[mount.Target] = "deny"
41 } else if within(mount.Source, denied) {
42 rel, _ := filepath.Rel(mount.Source, denied)
43 roots[filepath.Join(mount.Target, rel)] = "deny"
44 }
45 }
46 }
47 }
48 keys := make([]string, 0, len(roots))
49 for k := range roots {
50 keys = append(keys, k)
51 }
52 sort.Strings(keys)
53 entries := make([]string, 0, len(keys))
54 for _, k := range keys {
55 key, _ := json.Marshal(k)
56 v, _ := json.Marshal(roots[k])
57 entries = append(entries, string(key)+"="+string(v))
58 }
59 argv := []string{"/opt/codex", "sandbox", "-P", "equipo-checks", "-C", filepath.Join("/workspace", c.Dir), "-c", "permissions.equipo-checks.filesystem={" + strings.Join(entries, ",") + "}", "-c", "permissions.equipo-checks.network.enabled=false", "--"}
60 command := append([]string(nil), c.Argv...)
61 if command[0] == "go" {
62 command[0] = "/opt/go/bin/go"
63 }
64 argv = append(argv, command...)
65 env := map[string]string{"CODEX_HOME": "/home/runner", "GOCACHE": "/tmp/go-build", "GOTMPDIR": "/tmp/go-tmp", "GOMODCACHE": "/opt/go-mod", "GOROOT": "/opt/go", "GOTOOLCHAIN": "local", "GOWORK": "off", "GOENV": "off", "GOTELEMETRY": "off", "GOPROXY": "off", "GOSUMDB": "off", "CGO_ENABLED": "0", "XDG_CONFIG_HOME": "/tmp/xdg", "PATH": "/opt/go/bin:/usr/bin:/bin"}
66 p, err := provider.BuildCheckPolicy(provider.CheckPolicyInputs{Workspace: work, Binary: s.binary, RunnerBinary: s.direct.RunnerBinary, Bwrap: s.direct.Bwrap, GoRoot: s.goRoot, ModuleCache: s.direct.ModuleCache, Argv: argv, Env: env})
67 if err != nil {
68 return -1, err
69 }
70 ctx, cancel := context.WithTimeout(ctx, time.Duration(c.TimeoutSeconds)*time.Second)
71 defer cancel()
72 s.setContained(work, false)
73 proc, err := s.direct.Host.Start(ctx, provider.Launch{AttemptKey: "check-" + rand.Text(), Workspace: work, Policy: p}, argv)
74 if err != nil {
75 return -1, err
76 }
77 defer proc.Stdin().Close()
78 defer proc.Stdout().Close()
79 defer proc.Stderr().Close()
80 if err = proc.Stdin().Close(); err != nil {
81 return -1, err
82 }
83 done := make(chan error, 2)
84 go func() { _, err := io.Copy(stdout, proc.Stdout()); done <- err }()
85 go func() { _, err := io.Copy(stderr, proc.Stderr()); done <- err }()
86 exit, err := proc.Wait(ctx)
87 if err == nil {
88 s.setContained(work, true)
89 }
90 if err != nil {
91 cancel()
92 cleanup, stop := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
93 defer stop()
94 _, waitErr := proc.Wait(cleanup)
95 if waitErr == nil {
96 s.setContained(work, true)
97 }
98 if waitErr != nil {
99 proc.Stdout().Close()
100 proc.Stderr().Close()
101 }
102 err = errors.Join(err, waitErr)
103 }
104 err = errors.Join(err, <-done, <-done)
105 if err != nil {
106 return -1, err
107 }
108 return exit.Code, nil
109}
110
111func (s *CodexSandbox) Contained(work string) bool {
112 s.containMu.Lock()
113 defer s.containMu.Unlock()
114 return !s.uncontained[work]
115}
116func (s *CodexSandbox) setContained(work string, contained bool) {
117 s.containMu.Lock()
118 defer s.containMu.Unlock()
119 if s.uncontained == nil {
120 s.uncontained = map[string]bool{}
121 }
122 if contained {
123 delete(s.uncontained, work)
124 } else {
125 s.uncontained[work] = true
126 }
127}
128