Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-7275: fix(skills): normalize Windows archive entry paths (#8297)
Dante8bf3046f2203
diff --git a/server/internal/handler/skill_import_archive.go b/server/internal/handler/skill_import_archive.goindex 196a337622..1f40b8b7aa 100644--- a/server/internal/handler/skill_import_archive.go+++ b/server/internal/handler/skill_import_archive.go@@ -105,17 +105,23 @@ func parseSkillArchive(data []byte, filename string) (*importedSkill, error) {// primary content — keeping every accepted entry zip-slip-safe.var skillMd *zip.FilerootPrefix := ""+ skillMdEntries := make(map[string]string)for _, f := range zr.File {- if f.FileInfo().IsDir() {+ // Compress-Archive directory markers may have no directory attributes.+ if f.FileInfo().IsDir() || strings.HasSuffix(f.Name, `\`) {continue}- clean := path.Clean(f.Name)+ clean := cleanArchiveEntryName(f.Name)if !strings.EqualFold(path.Base(clean), skillpkg.ContentFilename) {continue}- if !validateFilePath(clean) {+ if !validateArchiveFilePath(clean) {continue}+ if previous, exists := skillMdEntries[clean]; exists {+ return nil, fmt.Errorf("archive entries %q and %q resolve to the same path %q", previous, f.Name, clean)+ }+ skillMdEntries[clean] = f.Nameprefix := archiveEntryPrefix(clean)if skillMd == nil || len(prefix) < len(rootPrefix) {skillMd = f@@ -145,11 +151,13 @@ func parseSkillArchive(data []byte, filename string) (*importedSkill, error) {content: content,}+ seenFiles := make(map[string]string)for _, f := range zr.File {- if f.FileInfo().IsDir() {+ // Compress-Archive directory markers may have no directory attributes.+ if f.FileInfo().IsDir() || strings.HasSuffix(f.Name, `\`) {continue}- clean := path.Clean(f.Name)+ clean := cleanArchiveEntryName(f.Name)// Only files under the resolved skill root belong to this skill.if rootPrefix != "" && !strings.HasPrefix(clean, rootPrefix) {continue@@ -168,9 +176,13 @@ func parseSkillArchive(data []byte, filename string) (*importedSkill, error) {continue}// zip-slip / absolute-path guard.- if !validateFilePath(rel) {+ if !validateArchiveFilePath(rel) {continue}+ if previous, exists := seenFiles[rel]; exists {+ return nil, fmt.Errorf("archive entries %q and %q resolve to the same path %q", previous, f.Name, rel)+ }+ seenFiles[rel] = f.NamefileContent, ferr := readZipFile(f, maxImportFileSize)if ferr != nil {// An oversize or unreadable individual asset is skipped rather than@@ -190,6 +202,32 @@ func parseSkillArchive(data []byte, filename string) (*importedSkill, error) {return imported, nil}+// cleanArchiveEntryName canonicalizes the non-standard backslash separators+// emitted by Windows PowerShell's Compress-Archive before applying ZIP path+// semantics. Validation still runs on the cleaned result, so backslash-based+// absolute and traversal entries cannot bypass the zip-slip guards.+func cleanArchiveEntryName(name string) string {+ return path.Clean(strings.ReplaceAll(name, "\\", "/"))+}++// validateArchiveFilePath extends the existing import validation with portable+// archive constraints. Windows drive syntax is not recognized by filepath.IsAbs+// on a Unix server, and NUL bytes would be removed before persistence. Neither+// can safely be passed to the daemon as a bundle path.+func validateArchiveFilePath(p string) bool {+ if strings.ContainsRune(p, '\x00') {+ return false+ }+ if len(p) >= 2 && isASCIIAlpha(p[0]) && p[1] == ':' {+ return false+ }+ return validateFilePath(p)+}++func isASCIIAlpha(c byte) bool {+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'+}+// archiveEntryPrefix returns the directory prefix (with trailing slash) of a// cleaned, slash-delimited archive entry: "" for a root entry, "my-skill/" for// "my-skill/SKILL.md".diff --git a/server/internal/handler/skill_import_archive_directory_markers_test.go b/server/internal/handler/skill_import_archive_directory_markers_test.gonew file mode 100644index 0000000000..2b312bc4c1--- /dev/null+++ b/server/internal/handler/skill_import_archive_directory_markers_test.go@@ -0,0 +1,53 @@+package handler++import (+ "archive/zip"+ "bytes"+ "testing"+)++func TestParseSkillArchive_SkipsWindowsDirectoryMarkers(t *testing.T) {+ for _, prefix := range []string{"", `review-helper\`} {+ t.Run(prefix, func(t *testing.T) {+ var buf bytes.Buffer+ zw := zip.NewWriter(&buf)+ for name, content := range map[string]string{+ prefix + "SKILL.md": "---\nname: review-helper\n---\n# Review",+ prefix + `references\`: "",+ prefix + `references\guides\setup.md`: "setup guide",+ prefix + `assets\`: "",+ `SKILL.md\`: "",+ } {+ w, err := zw.Create(name)+ if err != nil {+ t.Fatal(err)+ }+ if _, err := w.Write([]byte(content)); err != nil {+ t.Fatal(err)+ }+ }+ if err := zw.Close(); err != nil {+ t.Fatal(err)+ }+ zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))+ if err != nil {+ t.Fatal(err)+ }+ for _, f := range zr.File {+ if f.FileInfo().IsDir() {+ t.Fatalf("fixture unexpectedly has directory attributes: %q", f.Name)+ }+ }+ imported, err := parseSkillArchive(buf.Bytes(), "review-helper.zip")+ if err != nil {+ t.Fatal(err)+ }+ if imported.content != "---\nname: review-helper\n---\n# Review" {+ t.Fatalf("wrong primary content: %q", imported.content)+ }+ if len(imported.files) != 1 || imported.files[0].path != "references/guides/setup.md" || imported.files[0].content != "setup guide" {+ t.Fatalf("expected only the nested supporting file, got %#v", imported.files)+ }+ })+ }+}diff --git a/server/internal/handler/skill_import_archive_test.go b/server/internal/handler/skill_import_archive_test.goindex 0ab1ed052e..a861363f4f 100644--- a/server/internal/handler/skill_import_archive_test.go+++ b/server/internal/handler/skill_import_archive_test.go@@ -119,6 +119,45 @@ func TestParseSkillArchive_RootLayout(t *testing.T) {}}+func TestParseSkillArchive_NormalizesWindowsEntrySeparators(t *testing.T) {+ for _, tc := range []struct {+ name string+ entries map[string]string+ wantPath string+ }{+ {+ name: "root layout",+ entries: map[string]string{+ "SKILL.md": testSkillMd,+ `agents\openai.yaml`: "model: openai",+ },+ wantPath: "agents/openai.yaml",+ },+ {+ name: "wrapper layout",+ entries: map[string]string{+ `review-helper\SKILL.md`: testSkillMd,+ `review-helper\agents\openai.yaml`: "model: openai",+ },+ wantPath: "agents/openai.yaml",+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ data := buildTestZip(t, tc.entries)+ imported, err := parseSkillArchive(data, "review-helper.zip")+ if err != nil {+ t.Fatalf("parseSkillArchive: %v", err)+ }+ if got := filePaths(imported); len(got) != 1 || got[0] != tc.wantPath {+ t.Fatalf("files = %v, want [%s]", got, tc.wantPath)+ }+ if strings.Contains(imported.files[0].path, `\`) {+ t.Fatalf("stored path %q still contains a backslash", imported.files[0].path)+ }+ })+ }+}+func TestParseSkillArchive_NoSkillMd(t *testing.T) {data := buildTestZip(t, map[string]string{"my-skill/notes.md": "hello",@@ -137,7 +176,17 @@ func TestParseSkillArchive_InvalidZip(t *testing.T) {func TestParseSkillArchive_RejectsUnsafeSkillMdPath(t *testing.T) {// A SKILL.md whose only candidate path is absolute or traversal must not be// accepted as the primary content; the archive is treated as having none.- for _, name := range []string{"../escape/SKILL.md", "/abs/SKILL.md"} {+ for _, name := range []string{+ "../escape/SKILL.md",+ "/abs/SKILL.md",+ `..\escape\SKILL.md`,+ `\abs\SKILL.md`,+ `C:\abs\SKILL.md`,+ `C:relative\SKILL.md`,+ `.\C:\abs\SKILL.md`,+ `\\server\share\SKILL.md`,+ "unsafe\x00/SKILL.md",+ } {data := buildTestZip(t, map[string]string{name: testSkillMd})if _, err := parseSkillArchive(data, "x.skill"); err == nil {t.Errorf("expected rejection for unsafe SKILL.md path %q", name)@@ -165,6 +214,63 @@ func TestParseSkillArchive_DropsTraversalAndJunk(t *testing.T) {}}+func TestParseSkillArchive_DropsUnsafeWindowsSupportingPaths(t *testing.T) {+ data := buildTestZip(t, map[string]string{+ "SKILL.md": testSkillMd,+ `..\escape.txt`: "outside",+ `\absolute.txt`: "absolute",+ `C:\absolute.txt`: "drive absolute",+ `C:drive-relative.txt`: "drive relative",+ `\\server\share.txt`: "unc",+ "unsafe\x00/file.txt": "nul",+ `safe\nested/file.md`: "keep",+ })+ imported, err := parseSkillArchive(data, "s.skill")+ if err != nil {+ t.Fatalf("parseSkillArchive: %v", err)+ }+ if got := filePaths(imported); len(got) != 1 || got[0] != "safe/nested/file.md" {+ t.Fatalf("files = %v, want only [safe/nested/file.md]", got)+ }+}++func TestParseSkillArchive_DropsDrivePathAfterWrapperRemoval(t *testing.T) {+ data := buildTestZip(t, map[string]string{+ `s\SKILL.md`: testSkillMd,+ `s\C:\absolute.txt`: "drive absolute",+ `s\C:relative.txt`: "drive relative",+ `s\safe.txt`: "keep",+ })+ imported, err := parseSkillArchive(data, "s.skill")+ if err != nil {+ t.Fatalf("parseSkillArchive: %v", err)+ }+ if got := filePaths(imported); len(got) != 1 || got[0] != "safe.txt" {+ t.Fatalf("files = %v, want only [safe.txt]", got)+ }+}++func TestParseSkillArchive_RejectsCanonicalSkillMdCollision(t *testing.T) {+ data := buildTestZip(t, map[string]string{+ "SKILL.md": testSkillMd,+ `.\SKILL.md`: testSkillMd,+ })+ if _, err := parseSkillArchive(data, "s.skill"); err == nil || !strings.Contains(err.Error(), "same path") {+ t.Fatalf("error = %v, want canonical path collision", err)+ }+}++func TestParseSkillArchive_RejectsCanonicalSupportingFileCollision(t *testing.T) {+ data := buildTestZip(t, map[string]string{+ "SKILL.md": testSkillMd,+ `agents\openai.yaml`: "windows",+ "agents/openai.yaml": "slash",+ })+ if _, err := parseSkillArchive(data, "s.skill"); err == nil || !strings.Contains(err.Error(), "same path") {+ t.Fatalf("error = %v, want canonical path collision", err)+ }+}+func TestParseSkillArchive_SkipsBinaryAssets(t *testing.T) {data := buildTestZip(t, map[string]string{"s/SKILL.md": testSkillMd,