| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func testPaths() Paths { |
| 11 | p := homePaths() |
| 12 | p.UserTemp = "/private/var/folders/4m/abc123_def/T" |
| 13 | return p |
| 14 | } |
| 15 | |
| 16 | func homePaths() Paths { |
| 17 | return Paths{Home: "/Users/equipo-paul", StateDir: "/Users/equipo-paul/.local/share/equipo-runner/state", |
| 18 | AccountsRoot: "/Users/equipo-paul/.local/share/equipo/accounts", SessionRoot: "/Users/equipo-paul/.local/share/equipo-runner/sessions", |
| 19 | InstallDir: "/Users/equipo-paul/.local/lib/equipo-runner", OtherHomes: []string{"/Users/paul", "/Users/Shared"}} |
| 20 | } |
| 21 | |
| 22 | // The checks profile is §4.4 over the §4.2 deny list, in srt 0.0.81's |
| 23 | // settings shape: every required key present, no network, writes only |
| 24 | // where a build writes, every secret place denied both ways. |
| 25 | func TestChecksSettings(t *testing.T) { |
| 26 | in := ChecksInput{Paths: testPaths(), Tree: "/Users/equipo-paul/.local/share/equipo-runner/scratch/worker-steps/k/tree", |
| 27 | Attempt: "/Users/equipo-paul/.local/share/equipo-runner/scratch/worker-steps/k/attempt", Mach: CheckMachServices} |
| 28 | raw, err := ChecksSettings(in) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | var generic struct{ Network, Filesystem map[string]any } |
| 33 | if err := json.Unmarshal(raw, &generic); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | for block, keys := range map[string][]string{"network": {"allowedDomains", "deniedDomains", "allowMachLookup"}, "filesystem": {"denyRead", "allowWrite", "denyWrite"}} { |
| 37 | m := generic.Network |
| 38 | if block == "filesystem" { |
| 39 | m = generic.Filesystem |
| 40 | } |
| 41 | for _, k := range keys { |
| 42 | if _, ok := m[k]; !ok { |
| 43 | t.Fatalf("%s.%s missing: %s", block, k, raw) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | var s Settings |
| 48 | if err := json.Unmarshal(raw, &s); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if len(s.Network.AllowedDomains) != 0 || s.Network.AllowLocalBinding || s.AllowPty || s.EnableWeakerNetworkIsolation || s.AllowAppleEvents { |
| 52 | t.Fatalf("checks may reach something: %s", raw) |
| 53 | } |
| 54 | 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"} { |
| 55 | if !slices.Contains(s.Filesystem.DenyRead, p) || !slices.Contains(s.Filesystem.DenyWrite, p) { |
| 56 | t.Fatalf("%s not denied both ways: %s", p, raw) |
| 57 | } |
| 58 | } |
| 59 | if !slices.Contains(s.Filesystem.DenyWrite, in.InstallDir) || slices.Contains(s.Filesystem.DenyRead, in.InstallDir) { |
| 60 | t.Fatalf("install dir: writes denied, reads allowed (srt runs from it): %s", raw) |
| 61 | } |
| 62 | // The user's T is the one place added by the coordinator's ruling |
| 63 | // (Xcode writes there whatever $TMPDIR says); nothing else widened. |
| 64 | 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"} |
| 65 | if !slices.Equal(s.Filesystem.AllowWrite, want) { |
| 66 | t.Fatalf("allowWrite %v, want %v", s.Filesystem.AllowWrite, want) |
| 67 | } |
| 68 | if !slices.Equal(s.Network.AllowMachLookup, CheckMachServices) { |
| 69 | t.Fatalf("mach %v", s.Network.AllowMachLookup) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // A profile that would not mean what it says is refused: a tree inside |
| 74 | // the state dir (unwritable, srt's denyWrite wins), a glob or relative |
| 75 | // path, a Mach wildcard that opens everything. |
| 76 | func TestChecksSettingsRefusals(t *testing.T) { |
| 77 | good := ChecksInput{Paths: testPaths(), Tree: "/Users/equipo-paul/scratch/k/tree", Attempt: "/Users/equipo-paul/scratch/k/attempt", Mach: CheckMachServices} |
| 78 | if _, err := ChecksSettings(good); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | for name, bad := range map[string]func(*ChecksInput){ |
| 82 | "tree in the state dir": func(in *ChecksInput) { in.Tree = in.StateDir + "/worker/steps/k/tree" }, |
| 83 | "attempt in another home": func(in *ChecksInput) { in.Attempt = "/Users/paul/tmp" }, |
| 84 | "tree holding the keys": func(in *ChecksInput) { in.Tree = "/Users/equipo-paul" }, |
| 85 | "glob tree": func(in *ChecksInput) { in.Tree = "/Users/equipo-paul/scratch/*/tree" }, |
| 86 | "relative attempt": func(in *ChecksInput) { in.Attempt = "scratch/attempt" }, |
| 87 | "glob home": func(in *ChecksInput) { in.OtherHomes = []string{"/Users/[ab]"} }, |
| 88 | "every mach service": func(in *ChecksInput) { in.Mach = []string{"*"} }, |
| 89 | "inner mach wildcard": func(in *ChecksInput) { in.Mach = []string{"com.*.dt"} }, |
| 90 | "no user temp": func(in *ChecksInput) { in.UserTemp = "" }, |
| 91 | "user temp elsewhere": func(in *ChecksInput) { in.UserTemp = "/Users/equipo-paul/tmp" }, |
| 92 | "all users' temp": func(in *ChecksInput) { in.UserTemp = UserTempPrefix }, |
| 93 | "glob user temp": func(in *ChecksInput) { in.UserTemp = UserTempPrefix + "/*/T" }, |
| 94 | } { |
| 95 | in := good |
| 96 | in.Paths.OtherHomes = slices.Clone(good.OtherHomes) |
| 97 | bad(&in) |
| 98 | if _, err := ChecksSettings(in); !errors.Is(err, ErrProfile) { |
| 99 | t.Fatalf("%s accepted", name) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |