| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/xml" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // The installer runs only as a Standard user with equipo in its name: |
| 16 | // never root, never an administrator, never a shared "equipo" or a |
| 17 | // person's own account (own-Mac mode is not in scope). |
| 18 | func TestRefuseAccount(t *testing.T) { |
| 19 | for _, c := range []struct { |
| 20 | a Account |
| 21 | want string |
| 22 | }{ |
| 23 | {Account{Name: "equipo-paul", UID: 502, Groups: []string{"staff", "everyone", "_developer"}}, ""}, |
| 24 | {Account{Name: "root", UID: 0, Groups: []string{"wheel"}}, "runner.mac.root"}, |
| 25 | {Account{Name: "equipo-paul", UID: 0}, "runner.mac.root"}, |
| 26 | {Account{Name: "equipo-paul", UID: 502, Groups: []string{"staff", "admin"}}, "runner.mac.admin"}, |
| 27 | {Account{Name: "paul", UID: 501, Groups: []string{"staff"}}, "runner.mac.user"}, |
| 28 | {Account{Name: "paulequip", UID: 503, Groups: []string{"staff"}}, "runner.mac.user"}, |
| 29 | // macOS's Add User dialog makes "paulequipo" from "Paul Equipo": |
| 30 | // any name with equipo in it, in any case, is a dedicated user. |
| 31 | {Account{Name: "paulequipo", UID: 503, Groups: []string{"staff"}}, ""}, |
| 32 | {Account{Name: "paul-equipo", UID: 503, Groups: []string{"staff"}}, ""}, |
| 33 | {Account{Name: "PaulEquipo", UID: 503, Groups: []string{"staff"}}, ""}, |
| 34 | {Account{Name: "paulequipo", UID: 503, Groups: []string{"staff", "admin"}}, "runner.mac.admin"}, |
| 35 | {Account{Name: "paulequipo", UID: 0}, "runner.mac.root"}, |
| 36 | } { |
| 37 | if got := RefuseAccount(c.a); got != c.want { |
| 38 | t.Fatalf("%+v: %q, want %q", c.a, got, c.want) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestReadAccount(t *testing.T) { |
| 44 | out := map[string]string{"-un": "equipo-paul\n", "-u": "502\n", "-Gn": "staff everyone localaccounts _appserverusr admin _developer\n"} |
| 45 | a, err := ReadAccount(context.Background(), func(_ context.Context, name string, args ...string) (string, error) { |
| 46 | if name != "/usr/bin/id" { |
| 47 | return "", errors.New(name) |
| 48 | } |
| 49 | return out[args[0]], nil |
| 50 | }) |
| 51 | if err != nil || a.Name != "equipo-paul" || a.UID != 502 || RefuseAccount(a) != "runner.mac.admin" { |
| 52 | t.Fatalf("%+v %v", a, err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Another person's home counts as closed only when it can be neither |
| 57 | // listed, passed through, nor read: a home this user can traverse (but |
| 58 | // not list) is open, as is one with a readable file. Symlinked homes are |
| 59 | // listed with where they lead; a /Users that cannot be read is an error. |
| 60 | func TestHomes(t *testing.T) { |
| 61 | if os.Geteuid() == 0 { |
| 62 | t.Skip("root reads every home") |
| 63 | } |
| 64 | users := t.TempDir() |
| 65 | own := filepath.Join(users, "equipo-paul") |
| 66 | elsewhere := t.TempDir() |
| 67 | for _, d := range []string{"equipo-paul", "paul", "Shared", "tito", "ana"} { |
| 68 | if err := os.MkdirAll(filepath.Join(users, d), 0700); err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | } |
| 72 | if err := os.WriteFile(filepath.Join(users, ".localized"), nil, 0600); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := os.Symlink(elsewhere, filepath.Join(users, "linked")); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if err := os.Symlink(own, filepath.Join(users, "me")); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | others, err := OtherHomes(users, own) |
| 82 | want := []string{elsewhere, filepath.Join(users, "Shared"), filepath.Join(users, "ana"), filepath.Join(users, "linked"), filepath.Join(users, "paul"), filepath.Join(users, "tito")} |
| 83 | slices.Sort(want) |
| 84 | if err != nil || !slices.Equal(others, want) { |
| 85 | t.Fatalf("%v %v", others, err) |
| 86 | } |
| 87 | // paul: closed. tito: traversable only (0711). ana: closed but... a |
| 88 | // readable file inside needs traversal, so give ana 0711 and a file. |
| 89 | for d, mode := range map[string]os.FileMode{"paul": 0, "tito": 0o111, "ana": 0o111} { |
| 90 | if d == "ana" { |
| 91 | if err := os.WriteFile(filepath.Join(users, d, ".zshrc"), []byte("x"), 0644); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | } |
| 95 | if err := os.Chmod(filepath.Join(users, d), mode); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | defer os.Chmod(filepath.Join(users, d), 0700) |
| 99 | } |
| 100 | if err := os.Chmod(elsewhere, 0); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | defer os.Chmod(elsewhere, 0700) |
| 104 | got := ExposedHomes(others, Exposed) |
| 105 | if !slices.Equal(got, []string{filepath.Join(users, "ana"), filepath.Join(users, "tito")}) { |
| 106 | t.Fatalf("exposed %v", got) |
| 107 | } |
| 108 | if Exposed(filepath.Join(users, "paul")) || Exposed(elsewhere) { |
| 109 | t.Fatal("a closed home taken as open") |
| 110 | } |
| 111 | if _, err := OtherHomes(filepath.Join(users, "missing"), own); err == nil { |
| 112 | t.Fatal("an unreadable /Users taken as no homes") |
| 113 | } |
| 114 | if !slices.Contains(DenyList(Paths{Home: own, HomesUnknown: true}), "/Users") { |
| 115 | t.Fatal("unknown homes not denied wholesale") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // The launchd agent: a well-formed plist, the runner with its env file, |
| 120 | // kept alive only after a failure (an update exits 75 and restarts; a |
| 121 | // superseded runner exits 0 and stays down), a Standard process, paths |
| 122 | // escaped. |
| 123 | func TestLaunchAgent(t *testing.T) { |
| 124 | raw := LaunchAgent("/Users/equipo-paul/.local/lib/equipo-runner/equipo-runner", "/Users/equipo-paul/.config/equipo-runner/runner.env", "/Users/equipo-paul/Library/Logs/equipo-runner & <co>") |
| 125 | plist := parsePlist(t, raw) |
| 126 | if plist["Label"] != AgentLabel || plist["RunAtLoad"] != "true" || plist["ProcessType"] != "Standard" || plist["KeepAlive.SuccessfulExit"] != "false" { |
| 127 | t.Fatalf("%v", plist) |
| 128 | } |
| 129 | if plist["ProgramArguments"] != "/Users/equipo-paul/.local/lib/equipo-runner/equipo-runner|run|--env-file|/Users/equipo-paul/.config/equipo-runner/runner.env" { |
| 130 | t.Fatalf("arguments %q", plist["ProgramArguments"]) |
| 131 | } |
| 132 | if plist["StandardErrorPath"] != "/Users/equipo-paul/Library/Logs/equipo-runner & <co>/runner.log" || plist["Umask"] != "63" { |
| 133 | t.Fatalf("%v", plist) |
| 134 | } |
| 135 | if ExitUpdated == 0 { |
| 136 | t.Fatal("an update must exit non-zero to be restarted") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // parsePlist flattens a plist dict: key to value, arrays joined by |, |
| 141 | // nested dicts as parent.child. |
| 142 | func parsePlist(t *testing.T, raw []byte) map[string]string { |
| 143 | t.Helper() |
| 144 | d := xml.NewDecoder(strings.NewReader(string(raw))) |
| 145 | d.Strict = true |
| 146 | out := map[string]string{} |
| 147 | var stack []string |
| 148 | key, inArray := "", "" |
| 149 | var arr []string |
| 150 | for { |
| 151 | tok, err := d.Token() |
| 152 | if err == io.EOF { |
| 153 | break |
| 154 | } |
| 155 | if err != nil { |
| 156 | t.Fatalf("not well formed: %v", err) |
| 157 | } |
| 158 | switch e := tok.(type) { |
| 159 | case xml.StartElement: |
| 160 | switch e.Name.Local { |
| 161 | case "key": |
| 162 | var k string |
| 163 | if err := d.DecodeElement(&k, &e); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | key = strings.Join(append(append([]string{}, stack...), k), ".") |
| 167 | case "string", "integer": |
| 168 | var v string |
| 169 | if err := d.DecodeElement(&v, &e); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | if inArray != "" { |
| 173 | arr = append(arr, v) |
| 174 | } else { |
| 175 | out[key] = v |
| 176 | } |
| 177 | case "true", "false": |
| 178 | out[key] = e.Name.Local |
| 179 | case "array": |
| 180 | inArray, arr = key, nil |
| 181 | case "dict": |
| 182 | if key != "" { |
| 183 | stack = append(stack, key[strings.LastIndex(key, ".")+1:]) |
| 184 | } |
| 185 | } |
| 186 | case xml.EndElement: |
| 187 | switch e.Name.Local { |
| 188 | case "array": |
| 189 | out[inArray] = strings.Join(arr, "|") |
| 190 | inArray = "" |
| 191 | case "dict": |
| 192 | if len(stack) > 0 { |
| 193 | stack = stack[:len(stack)-1] |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | return out |
| 199 | } |
| 200 | |
| 201 | // srt is pinned: the lockfile the installer ships names 0.0.81 with its |
| 202 | // integrity hash, and every package in it has one. |
| 203 | func TestSRTPinned(t *testing.T) { |
| 204 | if !SRTPinned() { |
| 205 | t.Fatal("srt not pinned") |
| 206 | } |
| 207 | _, lock := SRTPackage() |
| 208 | if strings.Count(string(lock), `"integrity": "sha512-`) < 5 { |
| 209 | t.Fatal("a package without an integrity hash") |
| 210 | } |
| 211 | } |
| 212 | |