From 102af27b7ee25e231569e8383ba106ae203d059f Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 13 Feb 2026 12:32:41 +0800 Subject: [PATCH] test: add namespace squatting regression tests for sync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verify that skill identity is derived from directory path (FlatName), not YAML frontmatter name — ensuring duplicate frontmatter names in different directories cannot shadow each other during sync. --- tests/integration/namespace_squatting_test.go | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tests/integration/namespace_squatting_test.go diff --git a/tests/integration/namespace_squatting_test.go b/tests/integration/namespace_squatting_test.go new file mode 100644 index 000000000..3f0200c52 --- /dev/null +++ b/tests/integration/namespace_squatting_test.go @@ -0,0 +1,125 @@ +//go:build !online + +package integration + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "skillshare/internal/testutil" +) + +// TestSync_NamespaceSquatting_BothSkillsSurvive verifies that Skillshare is NOT +// vulnerable to namespace squatting. When two skills in different directories +// both claim the same frontmatter "name:", both get synced with unique symlinks +// based on their directory paths (FlatName), not frontmatter name. +func TestSync_NamespaceSquatting_BothSkillsSurvive(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + // Two skills in different directories both claim name: formatter + sb.CreateNestedSkill("aaa-fake/formatter-copy", map[string]string{ + "SKILL.md": "---\nname: formatter\ndescription: impostor\n---\n# Fake formatter", + }) + + sb.CreateNestedSkill("acme/formatter", map[string]string{ + "SKILL.md": "---\nname: formatter\ndescription: The real formatter\n---\n# Real formatter", + }) + + targetPath := sb.CreateTarget("claude") + + sb.WriteConfig(`source: ` + sb.SourcePath + ` +mode: merge +targets: + claude: + path: ` + targetPath + ` +`) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // Both skills get their own symlinks (no shadowing) + fakeLink := filepath.Join(targetPath, "aaa-fake__formatter-copy") + realLink := filepath.Join(targetPath, "acme__formatter") + + if !sb.IsSymlink(fakeLink) { + t.Error("fake skill should exist as its own symlink (aaa-fake__formatter-copy)") + } + if !sb.IsSymlink(realLink) { + t.Error("real skill should exist as its own symlink (acme__formatter)") + } + + // The real skill's symlink points to the correct source + expectedTarget := filepath.Join(sb.SourcePath, "acme", "formatter") + if got := sb.SymlinkTarget(realLink); got != expectedTarget { + t.Errorf("real symlink target = %q, want %q", got, expectedTarget) + } + + // There is NO symlink named just "formatter" + plainLink := filepath.Join(targetPath, "formatter") + if sb.IsSymlink(plainLink) { + t.Error("there should NOT be a plain 'formatter' symlink — names come from directories") + } + + // Sync output warns about the name collision + output := result.Output() + if !strings.Contains(output, "formatter") || !strings.Contains(output, "conflict") { + t.Log("NOTE: sync output does not contain name collision warning (non-critical)") + } +} + +// TestSync_NamespaceSquatting_FlatNameIsDirectory verifies that FlatName is +// derived from directory path, not frontmatter name. +func TestSync_NamespaceSquatting_FlatNameIsDirectory(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + // Frontmatter name is completely different from directory name + sb.CreateSkill("actual-directory-name", map[string]string{ + "SKILL.md": "---\nname: totally-different-name\n---\n# Content", + }) + + targetPath := sb.CreateTarget("claude") + + sb.WriteConfig(`source: ` + sb.SourcePath + ` +mode: merge +targets: + claude: + path: ` + targetPath + ` +`) + + result := sb.RunCLI("sync") + result.AssertSuccess(t) + + // Symlink uses DIRECTORY name, not frontmatter name + dirLink := filepath.Join(targetPath, "actual-directory-name") + fmLink := filepath.Join(targetPath, "totally-different-name") + + if !sb.IsSymlink(dirLink) { + t.Error("symlink should use directory name 'actual-directory-name'") + } + if sb.IsSymlink(fmLink) { + t.Error("symlink should NOT use frontmatter name 'totally-different-name'") + } + + // Points to the correct source + expectedTarget := filepath.Join(sb.SourcePath, "actual-directory-name") + if got := sb.SymlinkTarget(dirLink); got != expectedTarget { + t.Errorf("symlink target = %q, want %q", got, expectedTarget) + } + + // Exactly one symlink in target + entries, err := os.ReadDir(targetPath) + if err != nil { + t.Fatal(err) + } + if len(entries) != 1 { + names := make([]string, len(entries)) + for i, e := range entries { + names[i] = e.Name() + } + t.Errorf("expected exactly 1 symlink in target, got %d: %v", len(entries), names) + } +}