Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(skills): enforce portable supporting-file paths (#7743)
Soda2a1f393ad2fb
diff --git a/server/internal/daemon/skill_cache.go b/server/internal/daemon/skill_cache.goindex ef598c354a..b9da2ce125 100644--- a/server/internal/daemon/skill_cache.go+++ b/server/internal/daemon/skill_cache.go@@ -6,11 +6,11 @@ import ("fmt""io/fs""os"- "path""path/filepath""strings""sync"+ skillpkg "github.com/multica-ai/multica/server/internal/skill""github.com/multica-ai/multica/server/pkg/skillbundle")@@ -155,14 +155,7 @@ func validateSkillBundle(ref SkillRefData, bundle SkillData) bool {}func safeSkillFilePath(p string) bool {- if p == "" || strings.Contains(p, "\x00") || strings.HasPrefix(p, "/") || strings.Contains(p, "\\") {- return false- }- clean := path.Clean(p)- if clean == "." || clean != p || strings.HasPrefix(clean, "../") || clean == ".." {- return false- }- return true+ return skillpkg.IsSafeFilePath(p)}func safeCacheSegment(s string) string {diff --git a/server/internal/daemon/skill_cache_test.go b/server/internal/daemon/skill_cache_test.goindex a641d281c5..de934b3642 100644--- a/server/internal/daemon/skill_cache_test.go+++ b/server/internal/daemon/skill_cache_test.go@@ -74,6 +74,17 @@ func TestSkillBundleCacheRejectsCorruptBundle(t *testing.T) {}}+// The complete path matrix lives beside skill.IsSafeFilePath. This test only+// verifies that the daemon wrapper uses the shared contract.+func TestSafeSkillFilePathUsesSharedContract(t *testing.T) {+ if !safeSkillFilePath("references/example.md") {+ t.Fatal("expected canonical nested path to be accepted")+ }+ if safeSkillFilePath("C:/outside.txt") {+ t.Fatal("expected Windows drive path to be rejected")+ }+}+func testSkillBundle() SkillData {bundle := SkillData{ID: "skill-1",diff --git a/server/internal/handler/skill.go b/server/internal/handler/skill.goindex 2c2d66fa03..e8fce3c62f 100644--- a/server/internal/handler/skill.go+++ b/server/internal/handler/skill.go@@ -306,19 +306,9 @@ type AddAgentSkillsRequest struct {// --- Helpers ----// validateFilePath checks that a file path is safe (no traversal, no absolute paths).+// validateFilePath checks that a file path uses the shared supporting-file contract.func validateFilePath(p string) bool {- if p == "" {- return false- }- if filepath.IsAbs(p) {- return false- }- cleaned := filepath.Clean(p)- if strings.HasPrefix(cleaned, "..") {- return false- }- return true+ return skillpkg.IsSafeFilePath(p)}func (h *Handler) loadSkillForUser(w http.ResponseWriter, r *http.Request, id string) (db.Skill, bool) {diff --git a/server/internal/handler/skill_path_test.go b/server/internal/handler/skill_path_test.gonew file mode 100644index 0000000000..156b641357--- /dev/null+++ b/server/internal/handler/skill_path_test.go@@ -0,0 +1,54 @@+package handler++import (+ "net/http"+ "testing"++ "github.com/multica-ai/multica/server/internal/testutil"+)++// The complete path matrix lives beside skill.IsSafeFilePath. These checks+// prove that each public write route applies that shared contract.+func TestSkillFileWriteEndpointsRejectNonCanonicalPaths(t *testing.T) {+ if testPool == nil {+ t.Skip("no database available")+ }++ skillID := dbfx.Insert(t, "skill", testutil.Cols{+ "workspace_id": testWorkspaceID,+ "name": "skill-path-validation-fixture",+ "description": "fixture for path validation",+ "content": "# Skill path validation",+ "created_by": testUserID,+ })++ t.Run("create", func(t *testing.T) {+ req := newRequest(http.MethodPost, "/api/workspaces/"+testWorkspaceID+"/skills", CreateSkillRequest{+ Name: "skill-path-validation-create",+ Content: "# Skill path validation",+ Files: []CreateSkillFileRequest{+ {Path: "references//guide.md", Content: "guide"},+ },+ })+ testutil.Call(t, testHandler.CreateSkill, req).Want(http.StatusBadRequest)+ })++ t.Run("update", func(t *testing.T) {+ req := newRequest(http.MethodPut, "/api/skills/"+skillID, UpdateSkillRequest{+ Files: []CreateSkillFileRequest{+ {Path: "references/./guide.md", Content: "guide"},+ },+ })+ req = withURLParam(req, "id", skillID)+ testutil.Call(t, testHandler.UpdateSkill, req).Want(http.StatusBadRequest)+ })++ t.Run("upsert file", func(t *testing.T) {+ req := newRequest(http.MethodPut, "/api/skills/"+skillID+"/files", CreateSkillFileRequest{+ Path: `references\guide.md`,+ Content: "guide",+ })+ req = withURLParam(req, "id", skillID)+ testutil.Call(t, testHandler.UpsertSkillFile, req).Want(http.StatusBadRequest)+ })+}diff --git a/server/internal/skill/file_path.go b/server/internal/skill/file_path.gonew file mode 100644index 0000000000..4b44f260da--- /dev/null+++ b/server/internal/skill/file_path.go@@ -0,0 +1,24 @@+package skill++import (+ "path"+ "strings"+)++// IsSafeFilePath reports whether filePath is a canonical, slash-delimited+// supporting-file path accepted consistently by the server and daemon.+func IsSafeFilePath(filePath string) bool {+ if filePath == "" || strings.Contains(filePath, "\x00") || strings.HasPrefix(filePath, "/") || strings.Contains(filePath, "\\") {+ return false+ }+ if len(filePath) >= 2 && filePath[1] == ':' && isASCIILetter(filePath[0]) {+ return false+ }++ clean := path.Clean(filePath)+ return clean != "." && clean == filePath && clean != ".." && !strings.HasPrefix(clean, "../")+}++func isASCIILetter(value byte) bool {+ return value >= 'a' && value <= 'z' || value >= 'A' && value <= 'Z'+}diff --git a/server/internal/skill/file_path_test.go b/server/internal/skill/file_path_test.gonew file mode 100644index 0000000000..6c2bc8a4b0--- /dev/null+++ b/server/internal/skill/file_path_test.go@@ -0,0 +1,40 @@+package skill++import "testing"++func TestIsSafeFilePath(t *testing.T) {+ tests := []struct {+ name string+ filePath string+ want bool+ }{+ {name: "file", filePath: "reference.md", want: true},+ {name: "nested file", filePath: "references/example.md", want: true},+ {name: "dot-prefixed file", filePath: "..notes.md", want: true},+ {name: "empty", filePath: "", want: false},+ {name: "NUL", filePath: "file\x00.txt", want: false},+ {name: "slash rooted", filePath: "/abs/file.txt", want: false},+ {name: "slash UNC", filePath: "//server/share/file.txt", want: false},+ {name: "relative backslash", filePath: `agents\openai.yaml`, want: false},+ {name: "backslash rooted", filePath: `\abs\file.txt`, want: false},+ {name: "Windows drive absolute with backslashes", filePath: `C:\abs\file.txt`, want: false},+ {name: "Windows drive absolute with slashes", filePath: "C:/abs/file.txt", want: false},+ {name: "Windows drive relative", filePath: `C:relative.txt`, want: false},+ {name: "backslash UNC", filePath: `\\server\share\file.txt`, want: false},+ {name: "parent traversal with slashes", filePath: "../escape.txt", want: false},+ {name: "parent traversal with backslashes", filePath: `..\escape.txt`, want: false},+ {name: "embedded traversal", filePath: "safe/../escape.txt", want: false},+ {name: "duplicate separators", filePath: "safe//file.txt", want: false},+ {name: "dot segment", filePath: "safe/./file.txt", want: false},+ {name: "dot", filePath: ".", want: false},+ {name: "trailing slash", filePath: "safe/", want: false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ if got := IsSafeFilePath(tt.filePath); got != tt.want {+ t.Fatalf("IsSafeFilePath(%q) = %v, want %v", tt.filePath, got, tt.want)+ }+ })+ }+}