Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(httpapi): extract stripUploadTopDir shared by skillFiles and mcpFiles (#536) (#537)

fx-kurumid5a5b7f8fde9
diff --git a/internal/httpapi/mcp_files.go b/internal/httpapi/mcp_files.go
index 757f1d7d..05802e89 100644
--- a/internal/httpapi/mcp_files.go
+++ b/internal/httpapi/mcp_files.go
@@ -87,15 +87,13 @@ func (h *mcpHandlers) mcpFiles(c *gin.Context) {
for i, p := range plain {
names[i] = p.path
}
- if _, nested := sharedTopDir(names); nested {
- for i := range plain {
- clean, err := cleanUploadPath(strings.Join(strings.Split(plain[i].path, "/")[1:], "/"))
- if err != nil {
- c.JSON(400, map[string]string{"error": err.Error()})
- return
- }
- plain[i].path = clean
- }
+ names, err := stripUploadTopDir(names)
+ if err != nil {
+ c.JSON(400, map[string]string{"error": err.Error()})
+ return
+ }
+ for i := range plain {
+ plain[i].path = names[i]
}
for _, part := range plain {
if seen[part.path] {
diff --git a/internal/httpapi/skill_files.go b/internal/httpapi/skill_files.go
index e5bb13ea..85b3b660 100644
--- a/internal/httpapi/skill_files.go
+++ b/internal/httpapi/skill_files.go
@@ -111,16 +111,13 @@ func (h *catalogHandlers) skillFiles(c *gin.Context) {
for i, part := range plain {
plainNames[i] = part.path
}
- if _, nested := sharedTopDir(plainNames); nested {
- for i := range plain {
- stripped := strings.Join(strings.Split(plain[i].path, "/")[1:], "/")
- clean, err := cleanUploadPath(stripped)
- if err != nil {
- reject(err)
- return
- }
- plain[i].path = clean
- }
+ plainNames, err := stripUploadTopDir(plainNames)
+ if err != nil {
+ reject(err)
+ return
+ }
+ for i := range plain {
+ plain[i].path = plainNames[i]
}
for _, part := range plain {
@@ -342,6 +339,24 @@ func skillZipEntries(reader *zip.Reader, label string) ([]string, map[string]*zi
return names, files, nil
}
+// stripUploadTopDir removes the shared top-level directory from plain
+// (non-zip) upload paths, re-validating each shortened path. Paths without a
+// common top-level directory are returned unchanged.
+func stripUploadTopDir(paths []string) ([]string, error) {
+ if _, nested := sharedTopDir(paths); !nested {
+ return paths, nil
+ }
+ out := make([]string, len(paths))
+ for i, name := range paths {
+ clean, err := cleanUploadPath(strings.Join(strings.Split(name, "/")[1:], "/"))
+ if err != nil {
+ return nil, err
+ }
+ out[i] = clean
+ }
+ return out, nil
+}
+
func top(name string) string {
if idx := strings.Index(name, "/"); idx > 0 {
return name[:idx]
diff --git a/internal/httpapi/upload_top_dir_test.go b/internal/httpapi/upload_top_dir_test.go
new file mode 100644
index 00000000..47c3a7b6
--- /dev/null
+++ b/internal/httpapi/upload_top_dir_test.go
@@ -0,0 +1,34 @@
+package httpapi
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestStripUploadTopDirKeepsPathsWithoutSharedTopDir(t *testing.T) {
+ in := []string{"a/one.txt", "b/two.txt"}
+ got, err := stripUploadTopDir(in)
+ if err != nil || !reflect.DeepEqual(got, in) {
+ t.Fatalf("stripUploadTopDir(%v) = %v, %v", in, got, err)
+ }
+ flat := []string{"one.txt"}
+ if got, err := stripUploadTopDir(flat); err != nil || !reflect.DeepEqual(got, flat) {
+ t.Fatalf("stripUploadTopDir(%v) = %v, %v", flat, got, err)
+ }
+}
+
+func TestStripUploadTopDirRemovesSharedTopDir(t *testing.T) {
+ got, err := stripUploadTopDir([]string{"pkg/SKILL.md", "pkg/scripts/run.sh"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := []string{"SKILL.md", "scripts/run.sh"}; !reflect.DeepEqual(got, want) {
+ t.Fatalf("got %v want %v", got, want)
+ }
+}
+
+func TestStripUploadTopDirRejectsInvalidShortenedPath(t *testing.T) {
+ if _, err := stripUploadTopDir([]string{"pkg/../x", "pkg/ok.txt"}); err == nil {
+ t.Fatal("expected invalid shortened path to be rejected")
+ }
+}