| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/mac/denylist" |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | // The darwin permission port, on Linux: temp directories stand in for |
| 16 | // the Mac's paths. base/var is a link to base/private/var, as /var is to |
| 17 | // /private/var on macOS, and the worktree's real path is under |
| 18 | // base/private/var. base/Users holds the runner user's home and one |
| 19 | // other person's; base/outside holds files no step may reach. |
| 20 | type darwinRig struct { |
| 21 | t *testing.T |
| 22 | base, root, alias, outside, home string |
| 23 | research, other string |
| 24 | // jose and zoe are two more homes: José in NFC, Zoë in NFD. |
| 25 | jose, zoe string |
| 26 | paths denylist.Paths |
| 27 | policy provider.Policy |
| 28 | } |
| 29 | |
| 30 | func newDarwinRig(t *testing.T) *darwinRig { |
| 31 | t.Helper() |
| 32 | base, err := filepath.EvalSymlinks(t.TempDir()) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | r := &darwinRig{t: t, base: base} |
| 37 | real := filepath.Join(base, "private", "var", "folders", "zz", "T") |
| 38 | r.root = filepath.Join(real, "equipo", "wt") |
| 39 | r.alias = filepath.Join(base, "var", "folders", "zz", "T", "equipo", "wt") |
| 40 | r.outside = filepath.Join(base, "outside") |
| 41 | r.home = filepath.Join(base, "Users", "paulequipo") |
| 42 | r.other = filepath.Join(base, "Users", "paulca") |
| 43 | r.research = filepath.Join(real, "equipo", "research") |
| 44 | r.jose = filepath.Join(base, "Users", "Jos\u00e9") |
| 45 | r.zoe = filepath.Join(base, "Users", "Zoe\u0308") |
| 46 | for _, d := range []string{r.jose, r.zoe, r.root, filepath.Join(r.root, "sub"), filepath.Join(real, "equipo", "sibling"), r.research, filepath.Join(r.outside, "dir"), filepath.Join(r.home, ".ssh"), filepath.Join(r.home, ".equipo", "state"), r.other} { |
| 47 | if err := os.MkdirAll(d, 0700); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | } |
| 51 | if err := os.Symlink(filepath.Join(base, "private", "var"), filepath.Join(base, "var")); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | write := func(path, text string) { |
| 55 | if err := os.WriteFile(path, []byte(text), 0600); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | } |
| 59 | write(filepath.Join(r.root, "README.md"), "hello") |
| 60 | write(filepath.Join(r.root, "sub", "a.go"), "package sub") |
| 61 | write(filepath.Join(real, "equipo", "sibling", "x"), "sibling") |
| 62 | write(filepath.Join(r.research, "img1.png"), "png") |
| 63 | write(filepath.Join(r.outside, "secret"), "secret") |
| 64 | write(filepath.Join(r.outside, "dir", "secret"), "secret") |
| 65 | write(filepath.Join(r.outside, "hard"), "secret") |
| 66 | write(filepath.Join(r.home, ".ssh", "id_ed25519"), "key") |
| 67 | write(filepath.Join(r.other, "notes"), "theirs") |
| 68 | links := map[string]string{ |
| 69 | "inlink": filepath.Join(r.outside, "dir"), |
| 70 | "filelink": filepath.Join(r.outside, "secret"), |
| 71 | "dangle": filepath.Join(r.outside, "new"), |
| 72 | "samelink": filepath.Join(r.root, "README.md"), |
| 73 | } |
| 74 | for name, target := range links { |
| 75 | if err := os.Symlink(target, filepath.Join(r.root, name)); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | } |
| 79 | if err := os.Link(filepath.Join(r.outside, "hard"), filepath.Join(r.root, "hard")); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | r.paths = denylist.Paths{Home: r.home, StateDir: filepath.Join(r.home, ".equipo", "state"), AccountsRoot: filepath.Join(r.home, ".equipo", "accounts"), SessionRoot: filepath.Join(r.home, ".equipo", "sessions"), InstallDir: filepath.Join(r.home, ".equipo", "bin"), OtherHomes: []string{r.other, r.jose, r.zoe}} |
| 83 | r.policy = r.policyWith(r.root, r.research) |
| 84 | return r |
| 85 | } |
| 86 | |
| 87 | // policyWith is a darwin Engineering policy whose manifest carries the |
| 88 | // fields the port reads: the platform, the roots, the §4.2 list as |
| 89 | // DeniedPaths, the native tools and settings that keep every command |
| 90 | // sandboxed with the generated deny rules. |
| 91 | func (r *darwinRig) policyWith(root, research string) provider.Policy { |
| 92 | r.t.Helper() |
| 93 | denied := denylist.DenyList(r.paths) |
| 94 | rules, err := NativeDenyRules(denied) |
| 95 | if err != nil { |
| 96 | r.t.Fatal(err) |
| 97 | } |
| 98 | settings, _ := json.Marshal(map[string]any{ |
| 99 | "disableAllHooks": true, |
| 100 | "sandbox": map[string]any{"enabled": true, "failIfUnavailable": true, "allowUnsandboxedCommands": false, "autoAllowBashIfSandboxed": true, "excludedCommands": []string{}, "filesystem": map[string]any{"denyRead": denied, "denyWrite": denied}, "network": map[string]any{"allowedDomains": []string{}, "strictAllowlist": true}}, |
| 101 | "permissions": map[string]any{"deny": rules, "allow": []string{"mcp__equipo__submit_candidate"}, "ask": []string{}}, |
| 102 | }) |
| 103 | raw, _ := json.Marshal(map[string]any{ |
| 104 | "Platform": PlatformDarwin, "WorkspaceRoot": root, "ResearchRoot": research, |
| 105 | "Action": "implement", "DeniedPaths": denied, "Settings": json.RawMessage(settings), |
| 106 | "Tools": []string{"Bash", "Read", "Write", "Edit", "Glob", "Grep", "mcp__equipo__permission", "mcp__equipo__submit_candidate"}, |
| 107 | }) |
| 108 | return provider.Policy{Manifest: raw} |
| 109 | } |
| 110 | |
| 111 | // answer is the step's answer to one request built from tool and input. |
| 112 | func (r *darwinRig) answer(check func(json.RawMessage, provider.Policy) json.RawMessage, p provider.Policy, tool string, input map[string]any) (string, string) { |
| 113 | r.t.Helper() |
| 114 | in, _ := json.Marshal(input) |
| 115 | raw, _ := json.Marshal(map[string]any{"tool_name": tool, "input": json.RawMessage(in)}) |
| 116 | var v struct{ Behavior, Message string } |
| 117 | if out := check(raw, p); json.Unmarshal(out, &v) != nil { |
| 118 | r.t.Fatalf("not JSON: %s", out) |
| 119 | } |
| 120 | return v.Behavior, v.Message |
| 121 | } |
| 122 | |
| 123 | func (r *darwinRig) allowed(p provider.Policy, tool string, input map[string]any) bool { |
| 124 | b, _ := r.answer(StepPermission, p, tool, input) |
| 125 | return b == "allow" |
| 126 | } |
| 127 | |
| 128 | // TestPermissionRootIsResolved: the worktree is named by its real path. |
| 129 | // A path through a link above it (/var for /private/var) is allowed |
| 130 | // only when it lands inside that real root; a policy whose root is |
| 131 | // itself an alias answers nothing. |
| 132 | func TestPermissionRootIsResolved(t *testing.T) { |
| 133 | r := newDarwinRig(t) |
| 134 | for _, dir := range []string{r.root, r.alias} { |
| 135 | for _, tc := range []struct { |
| 136 | tool string |
| 137 | input map[string]any |
| 138 | }{ |
| 139 | {"Read", map[string]any{"file_path": filepath.Join(dir, "README.md")}}, |
| 140 | {"Edit", map[string]any{"file_path": filepath.Join(dir, "sub", "a.go"), "old_string": "sub", "new_string": "pkg"}}, |
| 141 | {"Write", map[string]any{"file_path": filepath.Join(dir, "new", "deeper", "b.go"), "content": "package b"}}, |
| 142 | {"Glob", map[string]any{"pattern": "**/*.go", "path": dir}}, |
| 143 | {"Grep", map[string]any{"pattern": "package", "path": filepath.Join(dir, "sub"), "glob": "*.go"}}, |
| 144 | } { |
| 145 | if !r.allowed(r.policy, tc.tool, tc.input) { |
| 146 | t.Fatalf("refused inside the real root (%s): %s %v", dir, tc.tool, tc.input) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | // The alias leads elsewhere too: only the real root's inside counts. |
| 151 | sibling := filepath.Join(filepath.Dir(r.alias), "sibling", "x") |
| 152 | if r.allowed(r.policy, "Read", map[string]any{"file_path": sibling}) { |
| 153 | t.Fatal("an alias that lands beside the root was allowed") |
| 154 | } |
| 155 | // A root that is itself an alias, or not there, refuses everything. |
| 156 | for _, root := range []string{r.alias, filepath.Join(r.root, "missing"), r.root + "/", "/", "", "relative/wt"} { |
| 157 | p := r.policyWith(root, "") |
| 158 | if r.allowed(p, "Read", map[string]any{"file_path": filepath.Join(r.root, "README.md")}) { |
| 159 | t.Fatalf("policy root %q answered", root) |
| 160 | } |
| 161 | } |
| 162 | if w, rs := Roots(r.policy); w != r.root || rs != r.research { |
| 163 | t.Fatalf("darwin roots %q %q", w, rs) |
| 164 | } |
| 165 | // Research reads are allowed through the read-only callback too; |
| 166 | // nothing writes there. |
| 167 | for _, check := range []func(json.RawMessage, provider.Policy) json.RawMessage{StepPermission, ReadPermission} { |
| 168 | if b, _ := r.answer(check, r.policy, "Read", map[string]any{"file_path": filepath.Join(r.research, "img1.png")}); b != "allow" { |
| 169 | t.Fatal("research read refused") |
| 170 | } |
| 171 | } |
| 172 | b, msg := r.answer(StepPermission, r.policy, "Write", map[string]any{"file_path": filepath.Join(r.research, "img2.png"), "content": "x"}) |
| 173 | if b == "allow" || !strings.Contains(msg, r.research+" is read-only") { |
| 174 | t.Fatalf("research write: %s %q", b, msg) |
| 175 | } |
| 176 | // A Mac without kept images has no research root to read. |
| 177 | if r.allowed(r.policyWith(r.root, ""), "Read", map[string]any{"file_path": filepath.Join(r.research, "img1.png")}) { |
| 178 | t.Fatal("read a research directory the policy does not name") |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // TestPermissionRefusesEscapes: "..", a glob that would expand outside |
| 183 | // the root, an absolute path outside it, and every link inside the root |
| 184 | // (one pointing out above all) are refused, whatever the tool. |
| 185 | func TestPermissionRefusesEscapes(t *testing.T) { |
| 186 | r := newDarwinRig(t) |
| 187 | type req struct { |
| 188 | tool string |
| 189 | input map[string]any |
| 190 | } |
| 191 | for name, tc := range map[string]req{ |
| 192 | // ".." even where the lexical result stays inside, and after a |
| 193 | // link, where it climbs from the link's target. |
| 194 | "dot-dot inside": {"Read", map[string]any{"file_path": r.root + "/sub/../README.md"}}, |
| 195 | "dot-dot after a link": {"Read", map[string]any{"file_path": r.root + "/inlink/../secret"}}, |
| 196 | "dot-dot write": {"Write", map[string]any{"file_path": r.root + "/sub/../../sibling/x", "content": "x"}}, |
| 197 | "dot-dot through alias": {"Read", map[string]any{"file_path": r.alias + "/../sibling/x"}}, |
| 198 | "glob dot-dot": {"Glob", map[string]any{"pattern": "../sibling/*", "path": r.root}}, |
| 199 | "glob brace dot-dot": {"Glob", map[string]any{"pattern": "{..,sub}/*", "path": r.root}}, |
| 200 | "glob absolute": {"Glob", map[string]any{"pattern": r.outside + "/*", "path": r.root}}, |
| 201 | "glob home": {"Glob", map[string]any{"pattern": "~/.ssh/*", "path": r.root}}, |
| 202 | "glob through a link": {"Glob", map[string]any{"pattern": "inlink/*", "path": r.root}}, |
| 203 | "grep glob dot-dot": {"Grep", map[string]any{"pattern": "x", "path": r.root, "glob": "../**"}}, |
| 204 | "grep glob absolute": {"Grep", map[string]any{"pattern": "x", "path": r.root, "glob": r.outside + "/*"}}, |
| 205 | "absolute outside read": {"Read", map[string]any{"file_path": filepath.Join(r.outside, "secret")}}, |
| 206 | "absolute outside write": {"Write", map[string]any{"file_path": filepath.Join(r.outside, "new"), "content": "x"}}, |
| 207 | "outside search": {"Grep", map[string]any{"pattern": "x", "path": r.outside}}, |
| 208 | "link pointing out": {"Read", map[string]any{"file_path": r.root + "/inlink/secret"}}, |
| 209 | "file link pointing out": {"Read", map[string]any{"file_path": r.root + "/filelink"}}, |
| 210 | "edit through a link": {"Edit", map[string]any{"file_path": r.root + "/filelink", "old_string": "a", "new_string": "b"}}, |
| 211 | "dangling link write": {"Write", map[string]any{"file_path": r.root + "/dangle", "content": "x"}}, |
| 212 | "search through a link": {"Grep", map[string]any{"pattern": "x", "path": r.root + "/inlink"}}, |
| 213 | "link pointing inside": {"Read", map[string]any{"file_path": r.root + "/samelink"}}, |
| 214 | "hard link": {"Read", map[string]any{"file_path": r.root + "/hard"}}, |
| 215 | "relative": {"Read", map[string]any{"file_path": "README.md"}}, |
| 216 | "home": {"Read", map[string]any{"file_path": "~/.ssh/id_ed25519"}}, |
| 217 | "no path": {"Grep", map[string]any{"pattern": "x"}}, |
| 218 | "nul": {"Read", map[string]any{"file_path": r.root + "/README.md\x00"}}, |
| 219 | } { |
| 220 | if r.allowed(r.policy, tc.tool, tc.input) { |
| 221 | t.Errorf("%s: allowed %s %v", name, tc.tool, tc.input) |
| 222 | } |
| 223 | } |
| 224 | // The §4.2 places are refused as protected, not merely outside: a |
| 225 | // person is never asked about them, in any spelling of their case. |
| 226 | for name, path := range map[string]string{ |
| 227 | "ssh": filepath.Join(r.home, ".ssh", "id_ed25519"), |
| 228 | "state": filepath.Join(r.home, ".equipo", "state", "key"), |
| 229 | "other home": filepath.Join(r.other, "notes"), |
| 230 | "case": filepath.Join(r.base, "USERS", "PaulCA", "notes"), |
| 231 | "volumes": "/Volumes/Backup/x", |
| 232 | } { |
| 233 | b, msg := r.answer(StepPermission, r.policy, "Read", map[string]any{"file_path": path}) |
| 234 | if b == "allow" || !strings.Contains(msg, "protected") { |
| 235 | t.Errorf("%s: %s %q, want protected", name, b, msg) |
| 236 | } |
| 237 | q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": path})} |
| 238 | if !protectedNativePath(q, r.policy) { |
| 239 | t.Errorf("%s: not protected for the person's path", name) |
| 240 | } |
| 241 | } |
| 242 | // A search of a directory holding one is protected too. |
| 243 | if b, msg := r.answer(StepPermission, r.policy, "Grep", map[string]any{"pattern": "x", "path": filepath.Join(r.base, "Users")}); b == "allow" || !strings.Contains(msg, "protected") { |
| 244 | t.Errorf("search over the homes: %s %q", b, msg) |
| 245 | } |
| 246 | // A person's approval cannot widen an escape either, and none counts |
| 247 | // as the workspace (which Equipo would answer itself). |
| 248 | for _, path := range []string{r.root + "/inlink/secret", r.root + "/filelink", r.root + "/sub/../README.md", r.alias + "/../sibling/x"} { |
| 249 | q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": path})} |
| 250 | if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil { |
| 251 | t.Errorf("approval allowed %s", path) |
| 252 | } |
| 253 | if workspaceNativePath(q, r.policy) { |
| 254 | t.Errorf("%s counted as the workspace", path) |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // TestPermissionDeniedPathsGenerated: the CLI's own deny rules name each |
| 260 | // §4.2 path (denylist.DenyList, shared with checks) as Tool(/<path>) and |
| 261 | // Tool(/<path>/**) for all five native file tools. |
| 262 | func TestPermissionDeniedPathsGenerated(t *testing.T) { |
| 263 | r := newDarwinRig(t) |
| 264 | denied := denylist.DenyList(r.paths) |
| 265 | if len(denied) < 9 { |
| 266 | t.Fatalf("deny list %v", denied) |
| 267 | } |
| 268 | rules, err := NativeDenyRules(denied) |
| 269 | if err != nil { |
| 270 | t.Fatal(err) |
| 271 | } |
| 272 | for _, path := range denied { |
| 273 | for _, tool := range []string{"Read", "Edit", "Write", "Glob", "Grep"} { |
| 274 | for _, want := range []string{tool + "(/" + path + ")", tool + "(/" + path + "/**)"} { |
| 275 | if !slices.Contains(rules, want) { |
| 276 | t.Errorf("missing %s", want) |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | if len(rules) != 10*len(denied) { |
| 282 | t.Fatalf("%d rules for %d paths", len(rules), len(denied)) |
| 283 | } |
| 284 | for _, bad := range [][]string{{"relative"}, {"/a/../b"}, {"/a/*"}, {"/a(b)"}, {"/a/"}} { |
| 285 | if _, err := NativeDenyRules(bad); err == nil { |
| 286 | t.Errorf("rule for %v", bad) |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // TestDarwinPortShellAndNetwork: with the CLI's env scrub, a Mac's |
| 292 | // sandboxed Bash commands reach the callback (no auto-allow), so the |
| 293 | // port allows them under the same rule as Linux: the policy keeps every |
| 294 | // command in the sandbox. A request for network access from inside the |
| 295 | // sandbox is always denied. The read-only callback runs no shell and |
| 296 | // writes nothing. |
| 297 | func TestDarwinPortShellAndNetwork(t *testing.T) { |
| 298 | r := newDarwinRig(t) |
| 299 | if b, _ := r.answer(StepPermission, r.policy, "Bash", map[string]any{"command": "swift build", "description": "d"}); b != "allow" { |
| 300 | t.Fatal("sandboxed shell refused") |
| 301 | } |
| 302 | if b, _ := r.answer(StepPermission, r.policy, "Bash", map[string]any{"command": "ls", "dangerouslyDisableSandbox": true}); b == "allow" { |
| 303 | t.Fatal("unsandboxed shell allowed") |
| 304 | } |
| 305 | if b, _ := r.answer(StepPermission, withSandboxSettings(t, r.policy, func(s map[string]any) { s["allowUnsandboxedCommands"] = true }), "Bash", map[string]any{"command": "ls"}); b == "allow" { |
| 306 | t.Fatal("shell allowed under a policy that lets commands leave the sandbox") |
| 307 | } |
| 308 | for _, check := range []func(json.RawMessage, provider.Policy) json.RawMessage{StepPermission, ReadPermission} { |
| 309 | if b, _ := r.answer(check, r.policy, "SandboxNetworkAccess", map[string]any{"host": "example.com"}); b != "deny" { |
| 310 | t.Fatal("network access allowed") |
| 311 | } |
| 312 | } |
| 313 | for _, tc := range []struct { |
| 314 | tool string |
| 315 | input map[string]any |
| 316 | }{ |
| 317 | {"Bash", map[string]any{"command": "ls"}}, |
| 318 | {"Write", map[string]any{"file_path": filepath.Join(r.root, "c.go"), "content": "x"}}, |
| 319 | } { |
| 320 | b, msg := r.answer(ReadPermission, r.policy, tc.tool, tc.input) |
| 321 | if b == "allow" || !strings.Contains(msg, "under "+r.root) { |
| 322 | t.Fatalf("read-only %s: %s %q", tc.tool, b, msg) |
| 323 | } |
| 324 | } |
| 325 | if b, _ := r.answer(ReadPermission, r.policy, "Grep", map[string]any{"pattern": "package", "path": filepath.Join(r.root, "sub")}); b != "allow" { |
| 326 | t.Fatal("read-only search refused") |
| 327 | } |
| 328 | // Refusals name the policy's own root, never /workspace. |
| 329 | for _, input := range []map[string]any{{"file_path": filepath.Join(r.outside, "secret")}, {"file_path": "README.md"}} { |
| 330 | _, msg := r.answer(StepPermission, r.policy, "Read", input) |
| 331 | if !strings.Contains(msg, r.root) || strings.Contains(msg, "/workspace") { |
| 332 | t.Fatalf("refusal %q", msg) |
| 333 | } |
| 334 | } |
| 335 | // An unknown platform answers nothing. |
| 336 | var m map[string]any |
| 337 | json.Unmarshal(r.policy.Manifest, &m) |
| 338 | m["Platform"] = "windows" |
| 339 | odd := provider.Policy{} |
| 340 | odd.Manifest, _ = json.Marshal(m) |
| 341 | if r.allowed(odd, "Read", map[string]any{"file_path": filepath.Join(r.root, "README.md")}) { |
| 342 | t.Fatal("unknown platform answered") |
| 343 | } |
| 344 | // Nor does a Linux manifest that names a root of its own. |
| 345 | f := fixtureFor(t, "Engineering") |
| 346 | var lm map[string]any |
| 347 | json.Unmarshal(f.launch.Policy.Manifest, &lm) |
| 348 | lm["WorkspaceRoot"] = r.root |
| 349 | linux := f.launch.Policy |
| 350 | linux.Manifest, _ = json.Marshal(lm) |
| 351 | if b, _ := r.answer(StepPermission, linux, "Read", map[string]any{"file_path": "/workspace/README.md"}); b == "allow" { |
| 352 | t.Fatal("Linux manifest with a root answered") |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | func inputJSON(v any) json.RawMessage { |
| 357 | raw, _ := json.Marshal(v) |
| 358 | return raw |
| 359 | } |
| 360 | |
| 361 | // TestPermissionGrepReadsNoHardLink: Grep reads file contents, so a |
| 362 | // hard link in the worktree to a file elsewhere must not be searchable, |
| 363 | // named directly or met inside a searched directory. Before answering a |
| 364 | // directory search the port walks it, following no link, and refuses a |
| 365 | // hard-linked file, a link that does not resolve inside the root, or |
| 366 | // more entries than its bound. |
| 367 | func TestPermissionGrepReadsNoHardLink(t *testing.T) { |
| 368 | r := newDarwinRig(t) |
| 369 | clean := filepath.Join(r.root, "clean") |
| 370 | nested := filepath.Join(r.root, "deep", "a", "b") |
| 371 | out := filepath.Join(r.root, "pointsout") |
| 372 | dangling := filepath.Join(r.root, "dangling") |
| 373 | for _, d := range []string{clean, nested, out, dangling} { |
| 374 | if err := os.MkdirAll(d, 0700); err != nil { |
| 375 | t.Fatal(err) |
| 376 | } |
| 377 | } |
| 378 | os.WriteFile(filepath.Join(clean, "a.txt"), []byte("hello"), 0600) |
| 379 | must := func(err error) { |
| 380 | if err != nil { |
| 381 | t.Fatal(err) |
| 382 | } |
| 383 | } |
| 384 | must(os.Symlink(filepath.Join(clean, "a.txt"), filepath.Join(clean, "alias.txt"))) |
| 385 | must(os.Link(filepath.Join(r.outside, "hard"), filepath.Join(nested, "x"))) |
| 386 | must(os.Symlink(filepath.Join(r.outside, "secret"), filepath.Join(out, "s"))) |
| 387 | must(os.Symlink(filepath.Join(r.outside, "gone"), filepath.Join(dangling, "s"))) |
| 388 | for _, check := range []func(json.RawMessage, provider.Policy) json.RawMessage{StepPermission, ReadPermission} { |
| 389 | for name, path := range map[string]string{ |
| 390 | "the hard link itself": filepath.Join(r.root, "hard"), |
| 391 | "the root holding it": r.root, |
| 392 | "a deep hard link": filepath.Join(r.root, "deep"), |
| 393 | "a link out": out, |
| 394 | "a link to nowhere": dangling, |
| 395 | "the alias of the root": r.alias, |
| 396 | } { |
| 397 | for _, mode := range []string{"content", "files_with_matches"} { |
| 398 | if b, _ := r.answer(check, r.policy, "Grep", map[string]any{"pattern": ".*", "path": path, "output_mode": mode}); b == "allow" { |
| 399 | t.Errorf("%s (%s): Grep allowed", name, mode) |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | // A directory with only its own files and a link inside the root |
| 404 | // is searched. |
| 405 | if b, _ := r.answer(check, r.policy, "Grep", map[string]any{"pattern": "hello", "path": clean}); b != "allow" { |
| 406 | t.Error("clean directory refused") |
| 407 | } |
| 408 | } |
| 409 | // The walk stops at its bound and refuses past it. |
| 410 | m, _ := policyManifest(r.policy) |
| 411 | if !searchable(clean, r.root, deniedForms(m), 10) || searchable(clean, r.root, deniedForms(m), 1) { |
| 412 | t.Fatal("walk bound not applied") |
| 413 | } |
| 414 | if grepWalkLimit < 200000 { |
| 415 | t.Fatalf("bound %d is too small for an Xcode project", grepWalkLimit) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // TestPermissionDeniedUnicode: APFS treats NFC and NFD spellings, and |
| 420 | // case, as the same name, so a denied home is protected in every |
| 421 | // spelling: José's (denied in NFC) asked for in NFD, Zoë's (denied in |
| 422 | // NFD) asked for in NFC, and either in capitals. None may be allowed, |
| 423 | // by the step or by a person. |
| 424 | func TestPermissionDeniedUnicode(t *testing.T) { |
| 425 | r := newDarwinRig(t) |
| 426 | for _, home := range []string{"Jose\u0301", "JOSE\u0301", "jos\u00e9", "Zo\u00eb", "ZO\u00cb", "zoe\u0308"} { |
| 427 | path := filepath.Join(r.base, "Users", home, "notes") |
| 428 | b, msg := r.answer(StepPermission, r.policy, "Read", map[string]any{"file_path": path}) |
| 429 | if b == "allow" || !strings.Contains(msg, "protected") { |
| 430 | t.Errorf("%q: %s %q, want protected", home, b, msg) |
| 431 | } |
| 432 | q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": path})} |
| 433 | if !protectedNativePath(q, r.policy) { |
| 434 | t.Errorf("%q would reach a person", home) |
| 435 | } |
| 436 | if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil { |
| 437 | t.Errorf("%q allowed by an approval", home) |
| 438 | } |
| 439 | } |
| 440 | // Folding compares whole components, never a prefix of one. |
| 441 | if foldedWithin("/Users/Jos\u00e9", "/Users/Jose\u0301x/notes") || !foldedWithin("/Users/Jos\u00e9", "/users/JOSE\u0301/notes") { |
| 442 | t.Fatal("component comparison") |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // TestPermissionOutsideNeverApproved: on a Mac nobody is asked about a |
| 447 | // file action outside the roots. Anything not provably inside is denied |
| 448 | // outright: it never reaches a person, and an approval cannot allow it. |
| 449 | func TestPermissionOutsideNeverApproved(t *testing.T) { |
| 450 | r := newDarwinRig(t) |
| 451 | for _, tc := range []struct { |
| 452 | tool string |
| 453 | input map[string]any |
| 454 | }{ |
| 455 | {"Read", map[string]any{"file_path": filepath.Join(r.outside, "secret")}}, |
| 456 | {"Write", map[string]any{"file_path": "/etc/hosts", "content": "x"}}, |
| 457 | {"Grep", map[string]any{"pattern": "x", "path": r.outside}}, |
| 458 | {"Write", map[string]any{"file_path": filepath.Join(r.research, "img2.png"), "content": "x"}}, |
| 459 | {"Edit", map[string]any{"file_path": filepath.Join(r.research, "img1.png"), "old_string": "p", "new_string": "q"}}, |
| 460 | } { |
| 461 | q := permissionRequest{Tool: tc.tool, Input: inputJSON(tc.input)} |
| 462 | if !protectedNativePath(q, r.policy) { |
| 463 | t.Errorf("%s %v would reach a person", tc.tool, tc.input) |
| 464 | } |
| 465 | if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil { |
| 466 | t.Errorf("%s %v allowed by an approval", tc.tool, tc.input) |
| 467 | } |
| 468 | } |
| 469 | // The step still says which rule refused. |
| 470 | if _, msg := r.answer(StepPermission, r.policy, "Read", map[string]any{"file_path": filepath.Join(r.outside, "secret")}); !strings.Contains(msg, "only under "+r.root) { |
| 471 | t.Fatalf("outside refusal %q", msg) |
| 472 | } |
| 473 | // Inside the root only the port's own decision allows: a person's |
| 474 | // approval allows no file action on a Mac, inside or out. |
| 475 | q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": filepath.Join(r.root, "README.md")})} |
| 476 | if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil || !protectedNativePath(q, r.policy) || workspaceNativePath(q, r.policy) { |
| 477 | t.Fatalf("inside read approvable: %v", err) |
| 478 | } |
| 479 | if !r.allowed(r.policy, "Read", map[string]any{"file_path": filepath.Join(r.root, "README.md")}) { |
| 480 | t.Fatal("inside read refused by the step") |
| 481 | } |
| 482 | } |
| 483 | |