package claude import ( "amadan.net/oficina/equipo/internal/runner/provider" "encoding/json" "os" "path/filepath" "testing" ) func TestWorkspacePathsMustBeAbsolute(t *testing.T) { f := fixtureFor(t, "Engineering") for _, tool := range []string{"Read", "Write", "Edit", "Glob", "Grep"} { for _, path := range []string{"ordinary.txt", ".", "./ordinary.txt", "../ordinary.txt", "../../home/runner/auth", "sub/../../home/runner/auth"} { for _, key := range []string{"file_path", "path"} { q := permissionRequest{Tool: tool, Input: marshalProbe(map[string]string{key: path, "pattern": "*.txt"})} if workspaceNativePath(q, f.launch.Policy) { t.Errorf("%s auto-allows %s=%s", tool, key, path) } if !protectedNativePath(q, f.launch.Policy) { t.Errorf("%s relative path not refused: %s", tool, path) } if _, err := permissionResult(q, provider.Answer{Behavior: "allow"}, f.launch.Policy); err == nil { t.Errorf("relative path approvable: %s", path) } } } } } func TestGlobLiteralBaseProtected(t *testing.T) { f := fixtureFor(t, "Engineering") if err := os.Mkdir(filepath.Join(f.launch.Workspace, "safe"), 0700); err != nil { t.Fatal(err) } if err := os.Symlink("/home/runner", filepath.Join(f.launch.Workspace, "safe", "alias")); err != nil { t.Fatal(err) } for _, tc := range []struct { path, pattern string denied bool }{ {"/workspace", "safe/alias/*.txt", true}, {"/workspace/safe", "alias/**/*.txt", true}, {"/workspace", "safe/alias", true}, {"/workspace", "safe/[ab]*.txt", false}, {"/workspace", "safe/{a,b}/*.txt", false}, {"/workspace", "safe/*.txt", false}, {"/workspace", "../home/*", true}, {"/workspace", "/home/*", true}, } { q := permissionRequest{Tool: "Glob", Input: marshalProbe(map[string]string{"path": tc.path, "pattern": tc.pattern})} if got := protectedNativePath(q, f.launch.Policy); got != tc.denied { t.Errorf("%s + %s denied=%v", tc.path, tc.pattern, got) } } } func TestGlobBaseDeniedPath(t *testing.T) { f := fixtureFor(t, "Engineering") var m manifest if err := json.Unmarshal(f.launch.Policy.Manifest, &m); err != nil { t.Fatal(err) } m.DeniedPaths = append(m.DeniedPaths, "/workspace/private") f.launch.Policy.Manifest = marshalProbe(m) q := permissionRequest{Tool: "Glob", Input: marshalProbe(map[string]string{"path": "/workspace", "pattern": "private/*.txt"})} if !protectedNativePath(q, f.launch.Policy) { t.Fatal("Glob literal base bypasses denied paths") } } func TestWorkspacePathMustBeExplicit(t *testing.T) { f := fixtureFor(t, "Engineering") for _, tool := range []string{"Read", "Write", "Edit", "Glob", "Grep"} { q := permissionRequest{Tool: tool, Input: marshalProbe(map[string]string{"pattern": "*.txt"})} if workspaceNativePath(q, f.launch.Policy) || !protectedNativePath(q, f.launch.Policy) { t.Errorf("%s assumes the current directory is /workspace", tool) } } }