| 1 | package claude |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestWorkspacePathsMustBeAbsolute(t *testing.T) { |
| 12 | f := fixtureFor(t, "Engineering") |
| 13 | for _, tool := range []string{"Read", "Write", "Edit", "Glob", "Grep"} { |
| 14 | for _, path := range []string{"ordinary.txt", ".", "./ordinary.txt", "../ordinary.txt", "../../home/runner/auth", "sub/../../home/runner/auth"} { |
| 15 | for _, key := range []string{"file_path", "path"} { |
| 16 | q := permissionRequest{Tool: tool, Input: marshalProbe(map[string]string{key: path, "pattern": "*.txt"})} |
| 17 | if workspaceNativePath(q, f.launch.Policy) { |
| 18 | t.Errorf("%s auto-allows %s=%s", tool, key, path) |
| 19 | } |
| 20 | if !protectedNativePath(q, f.launch.Policy) { |
| 21 | t.Errorf("%s relative path not refused: %s", tool, path) |
| 22 | } |
| 23 | if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, f.launch.Policy); err == nil { |
| 24 | t.Errorf("relative path approvable: %s", path) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestGlobLiteralBaseProtected(t *testing.T) { |
| 32 | f := fixtureFor(t, "Engineering") |
| 33 | if err := os.Mkdir(filepath.Join(f.launch.Workspace, "safe"), 0700); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | if err := os.Symlink("/home/runner", filepath.Join(f.launch.Workspace, "safe", "alias")); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | for _, tc := range []struct { |
| 40 | path, pattern string |
| 41 | denied bool |
| 42 | }{ |
| 43 | {"/workspace", "safe/alias/*.txt", true}, |
| 44 | {"/workspace/safe", "alias/**/*.txt", true}, |
| 45 | {"/workspace", "safe/alias", true}, |
| 46 | {"/workspace", "safe/[ab]*.txt", false}, |
| 47 | {"/workspace", "safe/{a,b}/*.txt", false}, |
| 48 | {"/workspace", "safe/*.txt", false}, |
| 49 | {"/workspace", "../home/*", true}, |
| 50 | {"/workspace", "/home/*", true}, |
| 51 | } { |
| 52 | q := permissionRequest{Tool: "Glob", Input: marshalProbe(map[string]string{"path": tc.path, "pattern": tc.pattern})} |
| 53 | if got := protectedNativePath(q, f.launch.Policy); got != tc.denied { |
| 54 | t.Errorf("%s + %s denied=%v", tc.path, tc.pattern, got) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestGlobBaseDeniedPath(t *testing.T) { |
| 60 | f := fixtureFor(t, "Engineering") |
| 61 | var m manifest |
| 62 | if err := json.Unmarshal(f.launch.Policy.Manifest, &m); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | m.DeniedPaths = append(m.DeniedPaths, "/workspace/private") |
| 66 | f.launch.Policy.Manifest = marshalProbe(m) |
| 67 | q := permissionRequest{Tool: "Glob", Input: marshalProbe(map[string]string{"path": "/workspace", "pattern": "private/*.txt"})} |
| 68 | if !protectedNativePath(q, f.launch.Policy) { |
| 69 | t.Fatal("Glob literal base bypasses denied paths") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestWorkspacePathMustBeExplicit(t *testing.T) { |
| 74 | f := fixtureFor(t, "Engineering") |
| 75 | for _, tool := range []string{"Read", "Write", "Edit", "Glob", "Grep"} { |
| 76 | q := permissionRequest{Tool: tool, Input: marshalProbe(map[string]string{"pattern": "*.txt"})} |
| 77 | if workspaceNativePath(q, f.launch.Policy) || !protectedNativePath(q, f.launch.Policy) { |
| 78 | t.Errorf("%s assumes the current directory is /workspace", tool) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |