oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package mac is the Mac worker (Mac runners spec §§3 to 5): the srt
2// (Seatbelt) settings for checks, the process-group gate with its
3// termination proof, the capability report, the xcodebuild gate a
4// repository without one gets, the launchd agent, the installer's
5// refusals and the self-probes P1 to P7. Everything here builds and is
6// tested on Linux with recorded outputs; what only a Mac can show is
7// left to the probes.
8package mac
9
10import (
11 "encoding/json"
12 "errors"
13 "path/filepath"
14 "slices"
15 "strings"
16
17 "amadan.net/oficina/equipo/internal/runner/mac/denylist"
18)
19
20// Paths are the places the §4.2 deny list protects on this machine
21// (denylist.Paths: the list lives in a leaf package so provider/claude
22// shares it too).
23type Paths = denylist.Paths
24
25// Mach services a checks profile allows by default: what xcodebuild test
26// against a simulator is expected to need. These are candidates until
27// probe P2 on the Mac records the real list (spec §4.4); a service
28// missing here makes xcodebuild fail under srt, which P2 reports, and
29// checks then run outside the sandbox, labelled so.
30var CheckMachServices = []string{
31 "com.apple.CoreSimulator.*",
32 "com.apple.dt.*",
33 "com.apple.testmanagerd*",
34 "com.apple.coresymbolicationd",
35}
36
37// ErrProfile refuses a profile that would not say what it means: a path
38// that is relative, unclean or a glob, or a writable place inside a
39// denied one (srt's denyWrite wins over allowWrite).
40var ErrProfile = errors.New("mac: srt profile refused")
41
42// DenyList is §4.2: what no step may read or write (denylist.DenyList).
43func DenyList(p Paths) []string { return denylist.DenyList(p) }
44
45// ChecksInput is one checks attempt: the tree (the exact candidate) and
46// the attempt's scratch (TMPDIR, DerivedData of a synthesised gate).
47type ChecksInput struct {
48 Paths
49 Tree, Attempt string
50 // Mach are the Mach service names allowed (CheckMachServices).
51 Mach []string
52}
53
54// Settings is srt's settings file (sandbox-config.ts in
55// @anthropic-ai/sandbox-runtime 0.0.81): network.allowedDomains and
56// deniedDomains, filesystem.denyRead, allowWrite and denyWrite are
57// required; an empty allowedDomains means no network and an empty
58// allowWrite no writes.
59type Settings struct {
60 Network Network `json:"network"`
61 Filesystem Filesystem `json:"filesystem"`
62 // Explicitly off: no pseudo-terminal, no trustd, no Apple events.
63 AllowPty bool `json:"allowPty"`
64 EnableWeakerNetworkIsolation bool `json:"enableWeakerNetworkIsolation"`
65 AllowAppleEvents bool `json:"allowAppleEvents"`
66}
67
68// Network is srt's network block.
69type Network struct {
70 AllowedDomains []string `json:"allowedDomains"`
71 DeniedDomains []string `json:"deniedDomains"`
72 AllowLocalBinding bool `json:"allowLocalBinding"`
73 AllowMachLookup []string `json:"allowMachLookup"`
74}
75
76// Filesystem is srt's filesystem block.
77type Filesystem struct {
78 DenyRead []string `json:"denyRead"`
79 AllowRead []string `json:"allowRead"`
80 AllowWrite []string `json:"allowWrite"`
81 DenyWrite []string `json:"denyWrite"`
82}
83
84// ChecksSettings is the per-attempt checks profile of §4.4: writes only
85// to the tree, the attempt's scratch, the runner user's T and Xcode's
86// and SwiftPM's caches;
87// the §4.2 denies for reads and writes, plus the install dir for
88// writes; no network; the Mach services xcodebuild needs.
89func ChecksSettings(in ChecksInput) ([]byte, error) {
90 deny := DenyList(in.Paths)
91 places, err := xcodePlaces(in.Paths)
92 if err != nil {
93 return nil, err
94 }
95 write := append([]string{in.Tree, in.Attempt}, places...)
96 denyWrite := compact(append(slices.Clone(deny), in.InstallDir))
97 for _, p := range append(append(slices.Clone(denyWrite), write...), in.Home) {
98 if !literalPath(p) {
99 return nil, ErrProfile
100 }
101 }
102 // A writable place inside a denied one would not be writable (srt's
103 // denyWrite wins), and a tree inside the state dir would be the
104 // state dir: refuse rather than run a gate that cannot write.
105 for _, w := range write[:3] {
106 for _, d := range denyWrite {
107 if within(d, w) || within(w, d) {
108 return nil, ErrProfile
109 }
110 }
111 }
112 if err := machNames(in.Mach); err != nil {
113 return nil, err
114 }
115 s := Settings{
116 Network: Network{AllowedDomains: []string{}, DeniedDomains: []string{}, AllowMachLookup: append([]string{}, in.Mach...)},
117 Filesystem: Filesystem{DenyRead: deny, AllowRead: []string{}, AllowWrite: write, DenyWrite: denyWrite},
118 }
119 return json.MarshalIndent(s, "", " ")
120}
121
122// xcodePlaces are where a build writes outside its tree and scratch,
123// shared by the checks profile and Engineering with sandboxed Xcode
124// access: the runner user's T (resolved and checked, Xcode writes there
125// whatever $TMPDIR says), Xcode's DerivedData and SwiftPM's caches.
126func xcodePlaces(p Paths) ([]string, error) {
127 if p.UserTemp == "" || p.UserTemp == UserTempPrefix || !within(UserTempPrefix, p.UserTemp) {
128 return nil, ErrProfile
129 }
130 return []string{p.UserTemp,
131 filepath.Join(p.Home, "Library/Developer/Xcode/DerivedData"),
132 filepath.Join(p.Home, "Library/Caches/org.swift.swiftpm"),
133 filepath.Join(p.Home, "Library/org.swift.swiftpm")}, nil
134}
135
136// machNames refuses a Mach service list that opens more than it names:
137// an empty name, a bare wildcard, a wildcard anywhere but the end (the
138// runtime takes one trailing *), or a character no service name has.
139func machNames(names []string) error {
140 for _, m := range names {
141 if m == "" || m == "*" || strings.ContainsAny(m, "/ \"") || strings.Contains(strings.TrimSuffix(m, "*"), "*") {
142 return ErrProfile
143 }
144 }
145 return nil
146}
147
148// literalPath is an absolute, clean path srt cannot read as a pattern.
149func literalPath(p string) bool {
150 return p != "" && filepath.IsAbs(p) && filepath.Clean(p) == p && p != "/" && !strings.ContainsAny(p, "*?[]{}\\\n\x00")
151}
152
153// within reports whether path is root or below it.
154func within(root, path string) bool {
155 r, err := filepath.Rel(root, path)
156 return err == nil && (r == "." || r != ".." && !strings.HasPrefix(r, "../"))
157}
158
159func compact(in []string) []string {
160 out := []string{}
161 for _, p := range in {
162 if p != "" && !slices.Contains(out, p) {
163 out = append(out, p)
164 }
165 }
166 return out
167}
168