package mac import ( "encoding/json" "errors" "slices" "testing" ) func testPaths() Paths { p := homePaths() p.UserTemp = "/private/var/folders/4m/abc123_def/T" return p } func homePaths() Paths { return Paths{Home: "/Users/equipo-paul", StateDir: "/Users/equipo-paul/.local/share/equipo-runner/state", AccountsRoot: "/Users/equipo-paul/.local/share/equipo/accounts", SessionRoot: "/Users/equipo-paul/.local/share/equipo-runner/sessions", InstallDir: "/Users/equipo-paul/.local/lib/equipo-runner", OtherHomes: []string{"/Users/paul", "/Users/Shared"}} } // The checks profile is §4.4 over the §4.2 deny list, in srt 0.0.81's // settings shape: every required key present, no network, writes only // where a build writes, every secret place denied both ways. func TestChecksSettings(t *testing.T) { in := ChecksInput{Paths: testPaths(), Tree: "/Users/equipo-paul/.local/share/equipo-runner/scratch/worker-steps/k/tree", Attempt: "/Users/equipo-paul/.local/share/equipo-runner/scratch/worker-steps/k/attempt", Mach: CheckMachServices} raw, err := ChecksSettings(in) if err != nil { t.Fatal(err) } var generic struct{ Network, Filesystem map[string]any } if err := json.Unmarshal(raw, &generic); err != nil { t.Fatal(err) } for block, keys := range map[string][]string{"network": {"allowedDomains", "deniedDomains", "allowMachLookup"}, "filesystem": {"denyRead", "allowWrite", "denyWrite"}} { m := generic.Network if block == "filesystem" { m = generic.Filesystem } for _, k := range keys { if _, ok := m[k]; !ok { t.Fatalf("%s.%s missing: %s", block, k, raw) } } } var s Settings if err := json.Unmarshal(raw, &s); err != nil { t.Fatal(err) } if len(s.Network.AllowedDomains) != 0 || s.Network.AllowLocalBinding || s.AllowPty || s.EnableWeakerNetworkIsolation || s.AllowAppleEvents { t.Fatalf("checks may reach something: %s", raw) } for _, p := range []string{in.StateDir, in.AccountsRoot, in.SessionRoot, "/Users/equipo-paul/.ssh", "/Users/equipo-paul/.aws", "/Users/equipo-paul/.gnupg", "/Users/equipo-paul/.config/op", "/Users/equipo-paul/Library/Keychains", "/Users/paul", "/Users/Shared", "/Volumes"} { if !slices.Contains(s.Filesystem.DenyRead, p) || !slices.Contains(s.Filesystem.DenyWrite, p) { t.Fatalf("%s not denied both ways: %s", p, raw) } } if !slices.Contains(s.Filesystem.DenyWrite, in.InstallDir) || slices.Contains(s.Filesystem.DenyRead, in.InstallDir) { t.Fatalf("install dir: writes denied, reads allowed (srt runs from it): %s", raw) } // The user's T is the one place added by the coordinator's ruling // (Xcode writes there whatever $TMPDIR says); nothing else widened. want := []string{in.Tree, in.Attempt, "/private/var/folders/4m/abc123_def/T", "/Users/equipo-paul/Library/Developer/Xcode/DerivedData", "/Users/equipo-paul/Library/Caches/org.swift.swiftpm", "/Users/equipo-paul/Library/org.swift.swiftpm"} if !slices.Equal(s.Filesystem.AllowWrite, want) { t.Fatalf("allowWrite %v, want %v", s.Filesystem.AllowWrite, want) } if !slices.Equal(s.Network.AllowMachLookup, CheckMachServices) { t.Fatalf("mach %v", s.Network.AllowMachLookup) } } // A profile that would not mean what it says is refused: a tree inside // the state dir (unwritable, srt's denyWrite wins), a glob or relative // path, a Mach wildcard that opens everything. func TestChecksSettingsRefusals(t *testing.T) { good := ChecksInput{Paths: testPaths(), Tree: "/Users/equipo-paul/scratch/k/tree", Attempt: "/Users/equipo-paul/scratch/k/attempt", Mach: CheckMachServices} if _, err := ChecksSettings(good); err != nil { t.Fatal(err) } for name, bad := range map[string]func(*ChecksInput){ "tree in the state dir": func(in *ChecksInput) { in.Tree = in.StateDir + "/worker/steps/k/tree" }, "attempt in another home": func(in *ChecksInput) { in.Attempt = "/Users/paul/tmp" }, "tree holding the keys": func(in *ChecksInput) { in.Tree = "/Users/equipo-paul" }, "glob tree": func(in *ChecksInput) { in.Tree = "/Users/equipo-paul/scratch/*/tree" }, "relative attempt": func(in *ChecksInput) { in.Attempt = "scratch/attempt" }, "glob home": func(in *ChecksInput) { in.OtherHomes = []string{"/Users/[ab]"} }, "every mach service": func(in *ChecksInput) { in.Mach = []string{"*"} }, "inner mach wildcard": func(in *ChecksInput) { in.Mach = []string{"com.*.dt"} }, "no user temp": func(in *ChecksInput) { in.UserTemp = "" }, "user temp elsewhere": func(in *ChecksInput) { in.UserTemp = "/Users/equipo-paul/tmp" }, "all users' temp": func(in *ChecksInput) { in.UserTemp = UserTempPrefix }, "glob user temp": func(in *ChecksInput) { in.UserTemp = UserTempPrefix + "/*/T" }, } { in := good in.Paths.OtherHomes = slices.Clone(good.OtherHomes) bad(&in) if _, err := ChecksSettings(in); !errors.Is(err, ErrProfile) { t.Fatalf("%s accepted", name) } } }