| 1 | package creds |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // The fixtures, each named for its date and machine: |
| 13 | // - ls-led-users-2026-11-28-tito-studio.txt is recorded: `ls -led |
| 14 | // /Users/*` on tito-studio as paulca, 2026-11-28 17:55Z (task 1, R2). |
| 15 | // - ls-led-constructed-2026-11-29-docs-t1.txt is constructed on docs-t1 |
| 16 | // in the recording's format, for the ACL cases no recording shows. |
| 17 | |
| 18 | // uids are the Studio's (R2: paulequipo is 506). |
| 19 | var uids = map[string]int{"paulequipo": 506, "paulca": 502, "vicky": 507, "root": 0, "billhorsman": 501, "ci": 503, "drbparsons": 504, "murraysum": 505} |
| 20 | |
| 21 | // facts reads a fixture into the guard's reader, each owner's uid as the |
| 22 | // Studio has it. |
| 23 | func facts(t *testing.T, names ...string) map[string]Fact { |
| 24 | t.Helper() |
| 25 | out := map[string]Fact{} |
| 26 | for _, name := range names { |
| 27 | raw, err := os.ReadFile(filepath.Join("testdata", name)) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | list, err := ParseLS(string(raw)) |
| 32 | if err != nil { |
| 33 | t.Fatalf("%s: %v", name, err) |
| 34 | } |
| 35 | for _, f := range list { |
| 36 | f.UID = uids[f.Owner] |
| 37 | out[f.Path] = f |
| 38 | } |
| 39 | } |
| 40 | return out |
| 41 | } |
| 42 | |
| 43 | func reader(m map[string]Fact) func(string) (Fact, error) { |
| 44 | return func(p string) (Fact, error) { |
| 45 | f, ok := m[p] |
| 46 | if !ok { |
| 47 | return Fact{Path: p, Missing: true, UID: -1}, nil |
| 48 | } |
| 49 | return f, nil |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // private is a 0700 directory of paulequipo's with no ACL. |
| 54 | func private(p string) Fact { |
| 55 | return Fact{Path: p, Kind: 'd', Perm: 0700, Owner: "paulequipo", UID: 506} |
| 56 | } |
| 57 | |
| 58 | const home = "/Users/paulequipo" |
| 59 | |
| 60 | // studio is the Studio's runner layout, every folder private: the state |
| 61 | // dir, the config dir and the accounts root, and the folders between. |
| 62 | func studio() map[string]Fact { |
| 63 | m := map[string]Fact{} |
| 64 | for _, p := range []string{home, home + "/.local", home + "/.local/share", home + "/.local/share/equipo-runner", home + "/.local/share/equipo-runner/state", home + "/.config", home + "/.config/equipo-runner", home + "/.local/share/equipo", home + "/.local/share/equipo/accounts"} { |
| 65 | m[p] = private(p) |
| 66 | } |
| 67 | return m |
| 68 | } |
| 69 | |
| 70 | var studioIn = Input{User: "paulequipo", UID: 506, Home: home, Roots: []string{home + "/.local/share/equipo-runner/state", home + "/.config/equipo-runner", home + "/.local/share/equipo/accounts"}} |
| 71 | |
| 72 | // codes is each problem as path=code. |
| 73 | func codes(r Result) []string { |
| 74 | var out []string |
| 75 | for _, p := range r.Problems { |
| 76 | out = append(out, p.Path+"="+p.Code) |
| 77 | } |
| 78 | return out |
| 79 | } |
| 80 | |
| 81 | // TestHomeGuard: the guard passes only a private layout, and names what |
| 82 | // to change for each way another user could reach it. |
| 83 | func TestHomeGuard(t *testing.T) { |
| 84 | t.Run("0700 passes", func(t *testing.T) { |
| 85 | r := Check(studioIn, reader(studio())) |
| 86 | if !r.OK() || r.Reason() != "" || r.Fix() != "" { |
| 87 | t.Fatalf("a private layout failed: %v", codes(r)) |
| 88 | } |
| 89 | }) |
| 90 | t.Run("an allow entry for the user itself passes", func(t *testing.T) { |
| 91 | m := studio() |
| 92 | f := facts(t, "ls-led-constructed-2026-11-29-docs-t1.txt")[home+"/.self"] |
| 93 | f.Path = home |
| 94 | m[home] = f |
| 95 | if r := Check(studioIn, reader(m)); !r.OK() { |
| 96 | t.Fatalf("the user's own entry and a deny failed it: %v", codes(r)) |
| 97 | } |
| 98 | }) |
| 99 | // The Studio's own home today (recorded, R2): drwxr-x---+ group |
| 100 | // staff, and every person is in staff. It must fail, naming chmod 700 ~. |
| 101 | t.Run("the Studio's paulequipo today", func(t *testing.T) { |
| 102 | m := studio() |
| 103 | m[home] = facts(t, "ls-led-users-2026-11-28-tito-studio.txt")[home] |
| 104 | r := Check(studioIn, reader(m)) |
| 105 | if r.OK() || r.Reason() != CodeHomeMode || r.Fix() != "chmod 700 ~" || len(r.Problems) != 1 { |
| 106 | t.Fatalf("paulequipo's recorded home: %v %q", codes(r), r.Fix()) |
| 107 | } |
| 108 | }) |
| 109 | t.Run("every recorded home fails for its own user", func(t *testing.T) { |
| 110 | for path, f := range facts(t, "ls-led-users-2026-11-28-tito-studio.txt") { |
| 111 | if f.Owner == "root" { |
| 112 | continue |
| 113 | } |
| 114 | r := Check(Input{User: f.Owner, UID: f.UID, Home: path}, reader(map[string]Fact{path: f})) |
| 115 | if r.Reason() != CodeHomeMode { |
| 116 | t.Fatalf("%s: %v", path, codes(r)) |
| 117 | } |
| 118 | } |
| 119 | }) |
| 120 | // Each case changes one folder of the private layout to the fixture's |
| 121 | // folder of that name; the guard must fail with code, naming fix. |
| 122 | cases := []struct { |
| 123 | name, at, fixture, code, fix string |
| 124 | }{ |
| 125 | {"0750 on a root", home + "/.config/equipo-runner", ".g750", CodeMode, "chmod 700 ~/.config/equipo-runner"}, |
| 126 | {"0750 on the home", home, ".g750", CodeHomeMode, "chmod 700 ~"}, |
| 127 | {"group read alone on the home", home, ".g740", CodeHomeMode, "chmod 700 ~"}, |
| 128 | {"group read alone on a root", home + "/.local/share/equipo/accounts", ".g740", CodeMode, "chmod 700 ~/.local/share/equipo/accounts"}, |
| 129 | {"other read alone on a root", home + "/.local/share/equipo-runner/state", ".o704", CodeMode, "chmod 700 ~/.local/share/equipo-runner/state"}, |
| 130 | {"an ACL grant to everyone", home, ".everyone", CodeACL, "chmod -a# 0 ~"}, |
| 131 | {"an ACL grant to one user", home + "/.local/share/equipo/accounts", ".vicky", CodeACL, "chmod -a# 0 ~/.local/share/equipo/accounts"}, |
| 132 | {"a write grant", home + "/.local/share/equipo-runner/state", ".write", CodeACL, "chmod -a# 0 ~/.local/share/equipo-runner/state"}, |
| 133 | {"a writesecurity grant", home + "/.config/equipo-runner", ".writesecurity", CodeACL, "chmod -a# 0 ~/.config/equipo-runner"}, |
| 134 | {"a chown grant to a group", home + "/.config/equipo-runner", ".chown", CodeACL, "chmod -a# 0 ~/.config/equipo-runner"}, |
| 135 | {"an inherited grant", home + "/.local/share/equipo/accounts", ".inherited", CodeACL, "chmod -a# 1 ~/.local/share/equipo/accounts"}, |
| 136 | {"an ACL grant on an ancestor", home + "/.local/share", ".vicky", CodeACL, "chmod -a# 0 ~/.local/share"}, |
| 137 | {"a symlinked ancestor", home + "/.local", ".link", CodeLink, "replace the link ~/.local with a folder"}, |
| 138 | {"a symlinked root", home + "/.config/equipo-runner", ".link", CodeLink, "replace the link ~/.config/equipo-runner with a folder"}, |
| 139 | {"an ancestor owned by another user", home + "/.local/share", ".vickys", CodeOwner, "sudo chown paulequipo ~/.local/share (an administrator)"}, |
| 140 | {"a root owned by another user", home + "/.local/share/equipo/accounts", ".vickys", CodeOwner, "sudo chown paulequipo ~/.local/share/equipo/accounts (an administrator)"}, |
| 141 | } |
| 142 | constructed := facts(t, "ls-led-constructed-2026-11-29-docs-t1.txt") |
| 143 | for _, c := range cases { |
| 144 | t.Run(c.name, func(t *testing.T) { |
| 145 | m := studio() |
| 146 | f := constructed[home+"/"+c.fixture] |
| 147 | f.Path = c.at |
| 148 | m[c.at] = f |
| 149 | r := Check(studioIn, reader(m)) |
| 150 | if r.OK() || !slices.Contains(codes(r), c.at+"="+c.code) || !strings.Contains(r.Fix(), c.fix) { |
| 151 | t.Fatalf("%s: %v %q", c.name, codes(r), r.Fix()) |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | t.Run("an ancestor readable by others but not writable passes", func(t *testing.T) { |
| 156 | m := studio() |
| 157 | f := m[home+"/.config"] |
| 158 | f.Perm = 0755 |
| 159 | m[home+"/.config"] = f |
| 160 | if r := Check(studioIn, reader(m)); !r.OK() { |
| 161 | t.Fatalf("a 0755 ancestor failed: %v", codes(r)) |
| 162 | } |
| 163 | }) |
| 164 | t.Run("an ancestor writable by its group fails", func(t *testing.T) { |
| 165 | m := studio() |
| 166 | f := m[home+"/.config"] |
| 167 | f.Perm = 0770 |
| 168 | m[home+"/.config"] = f |
| 169 | r := Check(studioIn, reader(m)) |
| 170 | if !slices.Contains(codes(r), home+"/.config="+CodeMode) || !strings.Contains(r.Fix(), "chmod go-w ~/.config") { |
| 171 | t.Fatalf("a group-writable ancestor: %v %q", codes(r), r.Fix()) |
| 172 | } |
| 173 | }) |
| 174 | // Fix round 1, #5: a protected root is held to the root rule however |
| 175 | // it is reached, first as another root's ancestor or not. |
| 176 | t.Run("nested roots, both orders", func(t *testing.T) { |
| 177 | m := studio() |
| 178 | cfg, state := home+"/.config/equipo-runner", home+"/.config/equipo-runner/state" |
| 179 | m[state] = private(state) |
| 180 | f := m[cfg] |
| 181 | f.Perm = 0o755 |
| 182 | m[cfg] = f |
| 183 | for _, roots := range [][]string{{state, cfg}, {cfg, state}} { |
| 184 | r := Check(Input{User: "paulequipo", UID: 506, Home: home, Roots: roots}, reader(m)) |
| 185 | if !slices.Contains(codes(r), cfg+"="+CodeMode) || !strings.Contains(r.Fix(), "chmod 700 ~/.config/equipo-runner") { |
| 186 | t.Fatalf("roots %v: %v %q", roots, codes(r), r.Fix()) |
| 187 | } |
| 188 | } |
| 189 | }) |
| 190 | t.Run("a root outside the home", func(t *testing.T) { |
| 191 | in := studioIn |
| 192 | in.Roots = append(slices.Clone(in.Roots), "/Users/Shared/equipo") |
| 193 | m := studio() |
| 194 | m["/Users/Shared/equipo"] = private("/Users/Shared/equipo") |
| 195 | r := Check(in, reader(m)) |
| 196 | if !slices.Contains(codes(r), "/Users/Shared/equipo="+CodeOutside) { |
| 197 | t.Fatalf("a root outside the home: %v", codes(r)) |
| 198 | } |
| 199 | }) |
| 200 | t.Run("a root that escapes through ..", func(t *testing.T) { |
| 201 | in := studioIn |
| 202 | in.Roots = []string{home + "/../vicky/equipo"} |
| 203 | if r := Check(in, reader(studio())); r.Reason() != CodeOutside { |
| 204 | t.Fatalf("a root through ..: %v", codes(r)) |
| 205 | } |
| 206 | }) |
| 207 | t.Run("a missing root", func(t *testing.T) { |
| 208 | m := studio() |
| 209 | delete(m, home+"/.local/share/equipo/accounts") |
| 210 | r := Check(studioIn, reader(m)) |
| 211 | if r.Reason() != CodeMissing || !strings.Contains(r.Fix(), "mkdir -m 700 ~/.local/share/equipo/accounts") { |
| 212 | t.Fatalf("a missing root: %v %q", codes(r), r.Fix()) |
| 213 | } |
| 214 | }) |
| 215 | t.Run("an unreadable fact decides nothing", func(t *testing.T) { |
| 216 | r := Check(studioIn, func(p string) (Fact, error) { |
| 217 | if p == home+"/.config" { |
| 218 | return Fact{}, ErrUnreadable |
| 219 | } |
| 220 | return reader(studio())(p) |
| 221 | }) |
| 222 | if r.OK() || !slices.Contains(codes(r), home+"/.config="+CodeUnknown) { |
| 223 | t.Fatalf("an unreadable ancestor: %v", codes(r)) |
| 224 | } |
| 225 | }) |
| 226 | t.Run("a uid that is not the user's", func(t *testing.T) { |
| 227 | m := studio() |
| 228 | f := m[home] |
| 229 | f.UID = 507 |
| 230 | m[home] = f |
| 231 | if r := Check(studioIn, reader(m)); r.Reason() != CodeOwner { |
| 232 | t.Fatalf("the home's uid is another's: %v", codes(r)) |
| 233 | } |
| 234 | }) |
| 235 | } |
| 236 | |
| 237 | // TestParseLSRefusesWhatItCannotRead: a line it does not understand, or |
| 238 | // a "+" with no entries, or entries out of order, is ErrUnreadable. |
| 239 | func TestParseLSRefusesWhatItCannotRead(t *testing.T) { |
| 240 | for _, out := range []string{ |
| 241 | "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n", |
| 242 | "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n 0: user:vicky maybe read\n", |
| 243 | "drwx------+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paulequipo\n 1: user:vicky allow read\n", |
| 244 | "total 0\n", |
| 245 | " 0: user:vicky allow read\n", |
| 246 | } { |
| 247 | if _, err := ParseLS(out); !errors.Is(err, ErrUnreadable) { |
| 248 | t.Fatalf("read %q: %v", out, err) |
| 249 | } |
| 250 | } |
| 251 | f, err := ParseLS("drwxr-x---+ 19 paulequipo staff 608 28 Nov 15:22 /Users/paul equipo\n 0: group:everyone deny delete\n") |
| 252 | if err != nil || len(f) != 1 || f[0].Path != "/Users/paul equipo" || f[0].Perm != 0750 || len(f[0].ACL) != 1 || f[0].ACL[0].Allow { |
| 253 | t.Fatalf("a path with a space: %+v %v", f, err) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // This boot has an identity and a time, the same on every read. |
| 258 | func TestBootIsStable(t *testing.T) { |
| 259 | a, err := Boot() |
| 260 | if err != nil || a.ID == "" || a.Time.IsZero() { |
| 261 | t.Fatalf("%+v %v", a, err) |
| 262 | } |
| 263 | if b, err := Boot(); err != nil || b != a { |
| 264 | t.Fatalf("%+v then %+v (%v)", a, b, err) |
| 265 | } |
| 266 | } |
| 267 | |