| index 19ef5c75..05adb3c0 100644 |
| --- a/internal/runner/provider/claude/answers_darwin_policy_test.go |
| +++ b/internal/runner/provider/claude/answers_darwin_policy_test.go |
| @@ -21,8 +21,10 @@ type darwinRig struct { |
| t *testing.T |
| base, root, alias, outside, home string |
| research, other string |
| - paths mac.Paths |
| - policy provider.Policy |
| + // jose and zoe are two more homes: José in NFC, Zoë in NFD. |
| + jose, zoe string |
| + paths mac.Paths |
| + policy provider.Policy |
| } |
| |
| func newDarwinRig(t *testing.T) *darwinRig { |
| @@ -39,7 +41,9 @@ func newDarwinRig(t *testing.T) *darwinRig { |
| r.home = filepath.Join(base, "Users", "paulequipo") |
| r.other = filepath.Join(base, "Users", "paulca") |
| r.research = filepath.Join(real, "equipo", "research") |
| - for _, d := range []string{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} { |
| + r.jose = filepath.Join(base, "Users", "Jos\u00e9") |
| + r.zoe = filepath.Join(base, "Users", "Zoe\u0308") |
| + 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} { |
| if err := os.MkdirAll(d, 0700); err != nil { |
| t.Fatal(err) |
| } |
| @@ -75,7 +79,7 @@ func newDarwinRig(t *testing.T) *darwinRig { |
| if err := os.Link(filepath.Join(r.outside, "hard"), filepath.Join(r.root, "hard")); err != nil { |
| t.Fatal(err) |
| } |
| - r.paths = mac.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.paths = mac.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}} |
| r.policy = r.policyWith(r.root, r.research) |
| return r |
| } |
| @@ -136,7 +140,7 @@ func TestPermissionRootIsResolved(t *testing.T) { |
| {"Edit", map[string]any{"file_path": filepath.Join(dir, "sub", "a.go"), "old_string": "sub", "new_string": "pkg"}}, |
| {"Write", map[string]any{"file_path": filepath.Join(dir, "new", "deeper", "b.go"), "content": "package b"}}, |
| {"Glob", map[string]any{"pattern": "**/*.go", "path": dir}}, |
| - {"Grep", map[string]any{"pattern": "hello", "path": dir, "glob": "*.md"}}, |
| + {"Grep", map[string]any{"pattern": "package", "path": filepath.Join(dir, "sub"), "glob": "*.go"}}, |
| } { |
| if !r.allowed(r.policy, tc.tool, tc.input) { |
| t.Fatalf("refused inside the real root (%s): %s %v", dir, tc.tool, tc.input) |
| @@ -322,7 +326,7 @@ func TestDarwinPortShellAndNetwork(t *testing.T) { |
| t.Fatalf("read-only %s: %s %q", tc.tool, b, msg) |
| } |
| } |
| - if b, _ := r.answer(ReadPermission, r.policy, "Grep", map[string]any{"pattern": "hello", "path": r.root}); b != "allow" { |
| + if b, _ := r.answer(ReadPermission, r.policy, "Grep", map[string]any{"pattern": "package", "path": filepath.Join(r.root, "sub")}); b != "allow" { |
| t.Fatal("read-only search refused") |
| } |
| // Refusals name the policy's own root, never /workspace. |
| @@ -357,3 +361,122 @@ func inputJSON(v any) json.RawMessage { |
| raw, _ := json.Marshal(v) |
| return raw |
| } |
| + |
| +// TestPermissionGrepReadsNoHardLink: Grep reads file contents, so a |
| +// hard link in the worktree to a file elsewhere must not be searchable, |
| +// named directly or met inside a searched directory. Before answering a |
| +// directory search the port walks it, following no link, and refuses a |
| +// hard-linked file, a link that does not resolve inside the root, or |
| +// more entries than its bound. |
| +func TestPermissionGrepReadsNoHardLink(t *testing.T) { |
| + r := newDarwinRig(t) |
| + clean := filepath.Join(r.root, "clean") |
| + nested := filepath.Join(r.root, "deep", "a", "b") |
| + out := filepath.Join(r.root, "pointsout") |
| + dangling := filepath.Join(r.root, "dangling") |
| + for _, d := range []string{clean, nested, out, dangling} { |
| + if err := os.MkdirAll(d, 0700); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + os.WriteFile(filepath.Join(clean, "a.txt"), []byte("hello"), 0600) |
| + must := func(err error) { |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + must(os.Symlink(filepath.Join(clean, "a.txt"), filepath.Join(clean, "alias.txt"))) |
| + must(os.Link(filepath.Join(r.outside, "hard"), filepath.Join(nested, "x"))) |
| + must(os.Symlink(filepath.Join(r.outside, "secret"), filepath.Join(out, "s"))) |
| + must(os.Symlink(filepath.Join(r.outside, "gone"), filepath.Join(dangling, "s"))) |
| + for _, check := range []func(json.RawMessage, provider.Policy) json.RawMessage{StepPermission, ReadPermission} { |
| + for name, path := range map[string]string{ |
| + "the hard link itself": filepath.Join(r.root, "hard"), |
| + "the root holding it": r.root, |
| + "a deep hard link": filepath.Join(r.root, "deep"), |
| + "a link out": out, |
| + "a link to nowhere": dangling, |
| + "the alias of the root": r.alias, |
| + } { |
| + for _, mode := range []string{"content", "files_with_matches"} { |
| + if b, _ := r.answer(check, r.policy, "Grep", map[string]any{"pattern": ".*", "path": path, "output_mode": mode}); b == "allow" { |
| + t.Errorf("%s (%s): Grep allowed", name, mode) |
| + } |
| + } |
| + } |
| + // A directory with only its own files and a link inside the root |
| + // is searched. |
| + if b, _ := r.answer(check, r.policy, "Grep", map[string]any{"pattern": "hello", "path": clean}); b != "allow" { |
| + t.Error("clean directory refused") |
| + } |
| + } |
| + // The walk stops at its bound and refuses past it. |
| + m, _ := policyManifest(r.policy) |
| + if !searchable(clean, r.root, deniedForms(m), 10) || searchable(clean, r.root, deniedForms(m), 2) { |
| + t.Fatal("walk bound not applied") |
| + } |
| + if grepWalkLimit < 200000 { |
| + t.Fatalf("bound %d is too small for an Xcode project", grepWalkLimit) |
| + } |
| +} |
| + |
| +// TestPermissionDeniedUnicode: APFS treats NFC and NFD spellings, and |
| +// case, as the same name, so a denied home is protected in every |
| +// spelling: José's (denied in NFC) asked for in NFD, Zoë's (denied in |
| +// NFD) asked for in NFC, and either in capitals. None may be allowed, |
| +// by the step or by a person. |
| +func TestPermissionDeniedUnicode(t *testing.T) { |
| + r := newDarwinRig(t) |
| + for _, home := range []string{"Jose\u0301", "JOSE\u0301", "jos\u00e9", "Zo\u00eb", "ZO\u00cb", "zoe\u0308"} { |
| + path := filepath.Join(r.base, "Users", home, "notes") |
| + b, msg := r.answer(StepPermission, r.policy, "Read", map[string]any{"file_path": path}) |
| + if b == "allow" || !strings.Contains(msg, "protected") { |
| + t.Errorf("%q: %s %q, want protected", home, b, msg) |
| + } |
| + q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": path})} |
| + if !protectedNativePath(q, r.policy) { |
| + t.Errorf("%q would reach a person", home) |
| + } |
| + if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil { |
| + t.Errorf("%q allowed by an approval", home) |
| + } |
| + } |
| + // Folding compares whole components, never a prefix of one. |
| + if foldedWithin("/Users/Jos\u00e9", "/Users/Jose\u0301x/notes") || !foldedWithin("/Users/Jos\u00e9", "/users/JOSE\u0301/notes") { |
| + t.Fatal("component comparison") |
| + } |
| +} |
| + |
| +// TestPermissionOutsideNeverApproved: on a Mac nobody is asked about a |
| +// file action outside the roots. Anything not provably inside is denied |
| +// outright: it never reaches a person, and an approval cannot allow it. |
| +func TestPermissionOutsideNeverApproved(t *testing.T) { |
| + r := newDarwinRig(t) |
| + for _, tc := range []struct { |
| + tool string |
| + input map[string]any |
| + }{ |
| + {"Read", map[string]any{"file_path": filepath.Join(r.outside, "secret")}}, |
| + {"Write", map[string]any{"file_path": "/etc/hosts", "content": "x"}}, |
| + {"Grep", map[string]any{"pattern": "x", "path": r.outside}}, |
| + {"Write", map[string]any{"file_path": filepath.Join(r.research, "img2.png"), "content": "x"}}, |
| + {"Edit", map[string]any{"file_path": filepath.Join(r.research, "img1.png"), "old_string": "p", "new_string": "q"}}, |
| + } { |
| + q := permissionRequest{Tool: tc.tool, Input: inputJSON(tc.input)} |
| + if !protectedNativePath(q, r.policy) { |
| + t.Errorf("%s %v would reach a person", tc.tool, tc.input) |
| + } |
| + if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err == nil { |
| + t.Errorf("%s %v allowed by an approval", tc.tool, tc.input) |
| + } |
| + } |
| + // The step still says which rule refused. |
| + 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) { |
| + t.Fatalf("outside refusal %q", msg) |
| + } |
| + // Inside the root an approval of the original action still works. |
| + q := permissionRequest{Tool: "Read", Input: inputJSON(map[string]any{"file_path": filepath.Join(r.root, "README.md")})} |
| + if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, r.policy); err != nil || protectedNativePath(q, r.policy) { |
| + t.Fatalf("inside read: %v", err) |
| + } |
| +} |