mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
fix(install,collect): keep a tracked source inside its destination; reject special SKILL.md files
--track --force removed the destination before cloning, so a local source nested below it (or given as an explicit file:// URL) was deleted along with any unpushed work, and the clone then failed. Resolve the source and refuse when it is the destination or lies under it, for dry runs too. HasSkillFile used a not-a-directory test, so a FIFO named SKILL.md counted as a skill and the collect copy blocked on it. Require a regular file; a symlinked SKILL.md still resolves through os.Stat.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user