diff --git a/internal/install/install_tracked.go b/internal/install/install_tracked.go index 2153f3950..b738ca5b6 100644 --- a/internal/install/install_tracked.go +++ b/internal/install/install_tracked.go @@ -21,6 +21,39 @@ func localFileURL(path string) string { return (&url.URL{Scheme: "file", Path: p}).String() } +// localCloneSource returns the filesystem path a local source clones from, or +// "" for a remote one. +func localCloneSource(source *Source) string { + if source.Path != "" { + return source.Path + } + u, err := url.Parse(source.CloneURL) + if err != nil || u.Scheme != "file" || u.Host != "" { + return "" + } + // file:///C:/repo carries the drive after a leading slash. + if p := u.Path; len(p) > 2 && p[0] == '/' && p[2] == ':' { + return filepath.FromSlash(p[1:]) + } + return filepath.FromSlash(u.Path) +} + +// pathWithin reports whether path is dir itself or lies below it, after +// resolving links. +func pathWithin(dir, path string) bool { + resolve := func(p string) string { + if abs, err := filepath.Abs(p); err == nil { + p = abs + } + if r, err := filepath.EvalSymlinks(p); err == nil { + p = r + } + return p + } + rel, err := filepath.Rel(resolve(dir), resolve(path)) + return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) +} + // normalizeTrackSource turns a local path that is a git repository into the // file:// form that parseFileURL builds, and rejects other non-git sources. func normalizeTrackSource(source *Source) error { @@ -92,10 +125,10 @@ func installTrackedRepoImpl(source *Source, sourceDir string, opts InstallOption } // Check if already exists - if destInfo, err := os.Stat(destPath); err == nil { + if _, err := os.Stat(destPath); err == nil { // --force removes the destination before cloning; never do that to the clone source. - if srcInfo, srcErr := os.Stat(source.Path); source.Path != "" && srcErr == nil && os.SameFile(srcInfo, destInfo) { - return nil, fmt.Errorf("source %s is the install destination; nothing to install", source.Path) + if p := localCloneSource(source); p != "" && pathWithin(destPath, p) { + return nil, fmt.Errorf("source %s is inside the install destination %s; nothing to install", p, destPath) } if opts.Update { return updateTrackedRepo(destPath, result, opts) diff --git a/internal/install/install_tracked_test.go b/internal/install/install_tracked_test.go index 9fce9848d..d8f81b2dd 100644 --- a/internal/install/install_tracked_test.go +++ b/internal/install/install_tracked_test.go @@ -315,22 +315,40 @@ func TestInferTrackedKind_LocalBareRepoPath(t *testing.T) { } } -func TestInstallTrackedRepo_RefusesLocalSourceThatIsTheDestination(t *testing.T) { - sourceDir := t.TempDir() - dest := filepath.Join(sourceDir, "_foo") - mustRunGit(t, "", "init", "-b", "main", dest) - if err := os.WriteFile(filepath.Join(dest, "SKILL.md"), []byte("# unpushed"), 0644); err != nil { - t.Fatal(err) +func TestInstallTrackedRepo_RefusesLocalSourceInsideTheDestination(t *testing.T) { + cases := []struct { + name string + repo string // relative to the destination + source func(repo string) string + dryRun bool + }{ + {"same path", "", func(p string) string { return p }, false}, + {"nested path", "nested", func(p string) string { return p }, false}, + {"explicit file URL", "nested", func(p string) string { return fileURL(p) }, false}, + {"dry run", "nested", func(p string) string { return p }, true}, } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + sourceDir := t.TempDir() + dest := filepath.Join(sourceDir, "_foo") + repo := filepath.Join(dest, tc.repo) + mustRunGit(t, "", "init", "-b", "main", repo) + marker := filepath.Join(repo, "SKILL.md") + if err := os.WriteFile(marker, []byte("# unpushed"), 0644); err != nil { + t.Fatal(err) + } - source, err := ParseSource(dest) - if err != nil { - t.Fatal(err) - } - if _, err := InstallTrackedRepo(source, sourceDir, InstallOptions{Name: "foo", Force: true}); err == nil { - t.Fatal("expected an error when the source is the install destination") - } - if _, err := os.Stat(filepath.Join(dest, "SKILL.md")); err != nil { - t.Fatalf("destination must be left intact: %v", err) + source, err := ParseSource(tc.source(repo)) + if err != nil { + t.Fatal(err) + } + _, err = InstallTrackedRepo(source, sourceDir, InstallOptions{Name: "foo", Force: true, DryRun: tc.dryRun}) + if err == nil { + t.Fatal("expected an error when the source is inside the install destination") + } + if _, err := os.Stat(marker); err != nil { + t.Fatalf("source must be left intact: %v", err) + } + }) } } diff --git a/internal/sync/pull.go b/internal/sync/pull.go index 4505599a1..9d3ddbc45 100644 --- a/internal/sync/pull.go +++ b/internal/sync/pull.go @@ -42,11 +42,11 @@ type PullResult struct { Failed map[string]error } -// HasSkillFile reports whether dir holds a SKILL.md file; a folder without one -// (a scratch dir) is not a skill. +// HasSkillFile reports whether dir holds a regular SKILL.md file; a folder +// without one (a scratch dir) is not a skill, and a FIFO would block a copy. func HasSkillFile(dir string) bool { info, err := os.Stat(filepath.Join(dir, "SKILL.md")) - return err == nil && !info.IsDir() + return err == nil && info.Mode().IsRegular() } // FindLocalSkills finds all local (non-symlinked) skills in a target directory. diff --git a/internal/sync/pull_fifo_unix_test.go b/internal/sync/pull_fifo_unix_test.go new file mode 100644 index 000000000..1fd28c5b5 --- /dev/null +++ b/internal/sync/pull_fifo_unix_test.go @@ -0,0 +1,20 @@ +//go:build !windows + +package sync + +import ( + "path/filepath" + "syscall" + "testing" +) + +// A FIFO named SKILL.md would block collect when it is read. +func TestHasSkillFile_RejectsFifoSkillMd(t *testing.T) { + dir := t.TempDir() + if err := syscall.Mkfifo(filepath.Join(dir, "SKILL.md"), 0644); err != nil { + t.Skipf("mkfifo unavailable: %v", err) + } + if HasSkillFile(dir) { + t.Error("a FIFO named SKILL.md is not a skill file") + } +} diff --git a/internal/sync/pull_test.go b/internal/sync/pull_test.go index 6215e9bcb..a9cdccfc4 100644 --- a/internal/sync/pull_test.go +++ b/internal/sync/pull_test.go @@ -371,3 +371,13 @@ func TestFindLocalSkills_SkipsDirWithoutSkillMd(t *testing.T) { t.Fatalf("expected only my-local, got %+v", skills) } } + +func TestHasSkillFile_RejectsDirectoryNamedSkillMd(t *testing.T) { + dir := t.TempDir() + if err := os.Mkdir(filepath.Join(dir, "SKILL.md"), 0755); err != nil { + t.Fatal(err) + } + if HasSkillFile(dir) { + t.Error("a directory named SKILL.md is not a skill file") + } +} diff --git a/tests/integration/install_track_root_skill_test.go b/tests/integration/install_track_root_skill_test.go index 99c9605d4..2dcd93cc5 100644 --- a/tests/integration/install_track_root_skill_test.go +++ b/tests/integration/install_track_root_skill_test.go @@ -388,3 +388,24 @@ func TestInstall_Track_LocalBareRepoPath(t *testing.T) { t.Fatalf("tracked repo should be cloned to _bare-local") } } + +// TestInstall_Track_Force_KeepsLocalSourceInsideDestination verifies that +// --force never removes the repository it is meant to clone from. +func TestInstall_Track_Force_KeepsLocalSourceInsideDestination(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + setupGlobalConfig(sb) + + dest := filepath.Join(sb.SourcePath, "_foo") + for _, repo := range []string{dest, filepath.Join(dest, "nested")} { + run(t, "", "git", "init", "--initial-branch=main", repo) + os.WriteFile(filepath.Join(repo, "SKILL.md"), []byte("---\nname: foo\n---\n# unpushed\n"), 0644) + + result := sb.RunCLI("install", repo, "--track", "--name", "foo", "--force", "--skip-audit") + result.AssertFailure(t) + result.AssertAnyOutputContains(t, "inside the install destination") + if !sb.FileExists(filepath.Join(repo, "SKILL.md")) { + t.Fatalf("source %s must be left intact", repo) + } + } +}