// Package mac is the Mac worker (Mac runners spec §§3 to 5): the srt // (Seatbelt) settings for checks, the process-group gate with its // termination proof, the capability report, the xcodebuild gate a // repository without one gets, the launchd agent, the installer's // refusals and the self-probes P1 to P7. Everything here builds and is // tested on Linux with recorded outputs; what only a Mac can show is // left to the probes. package mac import ( "encoding/json" "errors" "path/filepath" "slices" "strings" "amadan.net/oficina/equipo/internal/runner/mac/denylist" ) // Paths are the places the §4.2 deny list protects on this machine // (denylist.Paths: the list lives in a leaf package so provider/claude // shares it too). type Paths = denylist.Paths // Mach services a checks profile allows by default: what xcodebuild test // against a simulator is expected to need. These are candidates until // probe P2 on the Mac records the real list (spec §4.4); a service // missing here makes xcodebuild fail under srt, which P2 reports, and // checks then run outside the sandbox, labelled so. var CheckMachServices = []string{ "com.apple.CoreSimulator.*", "com.apple.dt.*", "com.apple.testmanagerd*", "com.apple.coresymbolicationd", } // ErrProfile refuses a profile that would not say what it means: a path // that is relative, unclean or a glob, or a writable place inside a // denied one (srt's denyWrite wins over allowWrite). var ErrProfile = errors.New("mac: srt profile refused") // DenyList is §4.2: what no step may read or write (denylist.DenyList). func DenyList(p Paths) []string { return denylist.DenyList(p) } // ChecksInput is one checks attempt: the tree (the exact candidate) and // the attempt's scratch (TMPDIR, DerivedData of a synthesised gate). type ChecksInput struct { Paths Tree, Attempt string // Mach are the Mach service names allowed (CheckMachServices). Mach []string } // Settings is srt's settings file (sandbox-config.ts in // @anthropic-ai/sandbox-runtime 0.0.81): network.allowedDomains and // deniedDomains, filesystem.denyRead, allowWrite and denyWrite are // required; an empty allowedDomains means no network and an empty // allowWrite no writes. type Settings struct { Network Network `json:"network"` Filesystem Filesystem `json:"filesystem"` // Explicitly off: no pseudo-terminal, no trustd, no Apple events. AllowPty bool `json:"allowPty"` EnableWeakerNetworkIsolation bool `json:"enableWeakerNetworkIsolation"` AllowAppleEvents bool `json:"allowAppleEvents"` } // Network is srt's network block. type Network struct { AllowedDomains []string `json:"allowedDomains"` DeniedDomains []string `json:"deniedDomains"` AllowLocalBinding bool `json:"allowLocalBinding"` AllowMachLookup []string `json:"allowMachLookup"` } // Filesystem is srt's filesystem block. type Filesystem struct { DenyRead []string `json:"denyRead"` AllowRead []string `json:"allowRead"` AllowWrite []string `json:"allowWrite"` DenyWrite []string `json:"denyWrite"` } // ChecksSettings is the per-attempt checks profile of §4.4: writes only // to the tree, the attempt's scratch, the runner user's T and Xcode's // and SwiftPM's caches; // the §4.2 denies for reads and writes, plus the install dir for // writes; no network; the Mach services xcodebuild needs. func ChecksSettings(in ChecksInput) ([]byte, error) { deny := DenyList(in.Paths) places, err := xcodePlaces(in.Paths) if err != nil { return nil, err } write := append([]string{in.Tree, in.Attempt}, places...) denyWrite := compact(append(slices.Clone(deny), in.InstallDir)) for _, p := range append(append(slices.Clone(denyWrite), write...), in.Home) { if !literalPath(p) { return nil, ErrProfile } } // A writable place inside a denied one would not be writable (srt's // denyWrite wins), and a tree inside the state dir would be the // state dir: refuse rather than run a gate that cannot write. for _, w := range write[:3] { for _, d := range denyWrite { if within(d, w) || within(w, d) { return nil, ErrProfile } } } if err := machNames(in.Mach); err != nil { return nil, err } s := Settings{ Network: Network{AllowedDomains: []string{}, DeniedDomains: []string{}, AllowMachLookup: append([]string{}, in.Mach...)}, Filesystem: Filesystem{DenyRead: deny, AllowRead: []string{}, AllowWrite: write, DenyWrite: denyWrite}, } return json.MarshalIndent(s, "", " ") } // xcodePlaces are where a build writes outside its tree and scratch, // shared by the checks profile and Engineering with sandboxed Xcode // access: the runner user's T (resolved and checked, Xcode writes there // whatever $TMPDIR says), Xcode's DerivedData and SwiftPM's caches. func xcodePlaces(p Paths) ([]string, error) { if p.UserTemp == "" || p.UserTemp == UserTempPrefix || !within(UserTempPrefix, p.UserTemp) { return nil, ErrProfile } return []string{p.UserTemp, filepath.Join(p.Home, "Library/Developer/Xcode/DerivedData"), filepath.Join(p.Home, "Library/Caches/org.swift.swiftpm"), filepath.Join(p.Home, "Library/org.swift.swiftpm")}, nil } // machNames refuses a Mach service list that opens more than it names: // an empty name, a bare wildcard, a wildcard anywhere but the end (the // runtime takes one trailing *), or a character no service name has. func machNames(names []string) error { for _, m := range names { if m == "" || m == "*" || strings.ContainsAny(m, "/ \"") || strings.Contains(strings.TrimSuffix(m, "*"), "*") { return ErrProfile } } return nil } // literalPath is an absolute, clean path srt cannot read as a pattern. func literalPath(p string) bool { return p != "" && filepath.IsAbs(p) && filepath.Clean(p) == p && p != "/" && !strings.ContainsAny(p, "*?[]{}\\\n\x00") } // within reports whether path is root or below it. func within(root, path string) bool { r, err := filepath.Rel(root, path) return err == nil && (r == "." || r != ".." && !strings.HasPrefix(r, "../")) } func compact(in []string) []string { out := []string{} for _, p := range in { if p != "" && !slices.Contains(out, p) { out = append(out, p) } } return out }