Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-6356: fix(skills): skip binary supporting files in runtime-local skill sync (#7175)
Danila Katalshovec6a7b1285a9
diff --git a/server/internal/daemon/local_skills.go b/server/internal/daemon/local_skills.goindex 4ccb91c8a6..6fddf23e25 100644--- a/server/internal/daemon/local_skills.go+++ b/server/internal/daemon/local_skills.go@@ -1,12 +1,15 @@package daemonimport (+ "bytes""fmt""io/fs"+ "log/slog""os""path/filepath""sort""strings"+ "unicode/utf8""github.com/multica-ai/multica/server/internal/skill""github.com/multica-ai/multica/server/pkg/agent"@@ -372,6 +375,57 @@ func collectLocalSkillFiles(skillDir string, includeContent bool) ([]SkillFileDaif err != nil || info.Size() > maxLocalSkillFileSize {return nil}+ // A binary supporting file cannot survive SkillFileData.Content: the+ // bytes go out as a Go string and encoding/json rewrites every invalid+ // UTF-8 byte to U+FFFD, so writeSkillFiles later recreates a file that+ // differs from the original and will not open.+ //+ // IsLikelyBinaryFilePath is the cheap first pass on the extension — the+ // same heuristic the archive/URL importer uses — checked before any+ // read so a known-binary file never pays the I/O. On its own it misses+ // a binary file with an unlisted or missing extension (a .safetensors,+ // a .parquet, a stray no-extension blob) and a text file in a+ // non-UTF-8 encoding, both of which corrupt exactly the same way.+ if skill.IsLikelyBinaryFilePath(rel) {+ slog.Info("local skill: skipping binary file",+ "skill_dir", skillDir,+ "path", filepath.ToSlash(rel),+ "size", info.Size(),+ "reason", "binary_extension",+ )+ return nil+ }+ // The read below is the actual guarantee: skip whenever the bytes+ // themselves are not safely round-trippable, regardless of what the+ // extension suggested. This runs on both the includeContent=false+ // (discovery) and includeContent=true (sync) passes so they agree on+ // which files make up the bundle — skipping the read on the false pass+ // would let a discovery listing promise a file that sync then silently+ // drops.+ //+ // Valid UTF-8 alone is not enough: a NUL byte is legal UTF-8 but the+ // server-side import path strips every 0x00 via sanitizeNullBytes+ // (server/internal/handler/skill_create.go), so a file that is valid+ // UTF-8 but contains NUL — UTF-16LE text made of ASCII characters is a+ // realistic example — still comes back different from what went in.+ // Require both: valid UTF-8 AND NUL-free.+ content, err := os.ReadFile(path)+ if err != nil {+ return nil+ }+ if !utf8.Valid(content) || bytes.IndexByte(content, 0) >= 0 {+ reason := "invalid_utf8"+ if utf8.Valid(content) {+ reason = "embedded_nul"+ }+ slog.Info("local skill: skipping binary file",+ "skill_dir", skillDir,+ "path", filepath.ToSlash(rel),+ "size", info.Size(),+ "reason", reason,+ )+ return nil+ }if len(files) >= maxLocalSkillFileCount {return fmt.Errorf("local skill exceeds %d files", maxLocalSkillFileCount)}@@ -382,10 +436,6 @@ func collectLocalSkillFiles(skillDir string, includeContent bool) ([]SkillFileDafile := SkillFileData{Path: filepath.ToSlash(rel)}if includeContent {- content, err := os.ReadFile(path)- if err != nil {- return nil- }file.Content = string(content)}files = append(files, file)diff --git a/server/internal/daemon/local_skills_test.go b/server/internal/daemon/local_skills_test.goindex 1ec349b1a8..18e170a846 100644--- a/server/internal/daemon/local_skills_test.go+++ b/server/internal/daemon/local_skills_test.go@@ -1100,3 +1100,221 @@ func TestLoadRuntimeLocalSkillBundle_ProviderNonDirFallsThrough(t *testing.T) {}}++// invalidUTF8Docx is a .docx prefix (PK zip magic) followed by bytes that are+// not valid UTF-8. Carrying this through SkillFileData.Content is exactly the+// corruption in #7143: encoding/json rewrites each invalid byte to U+FFFD, so+// the file written back into the task environment no longer opens.+const invalidUTF8Docx = "PK\x03\x04\x14\x00\x06\x00\xff\xfe\x80\x81payload"++func TestCollectLocalSkillFiles_SkipsBinarySupportingFiles(t *testing.T) {+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "references/report.docx": invalidUTF8Docx,+ "assets/logo.png": "\x89PNG\r\n\x1a\n\x00\x00\x00",+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }++ paths := make([]string, 0, len(files))+ for _, f := range files {+ paths = append(paths, f.Path)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(paths, want) {+ t.Fatalf("collected paths = %v, want %v", paths, want)+ }++ // The text file that survives must survive byte-for-byte.+ if files[0].Content != "# notes\n" {+ t.Errorf("text content = %q, want %q", files[0].Content, "# notes\n")+ }+}++func TestCollectLocalSkillFiles_BinarySkipIsConsistentAcrossPasses(t *testing.T) {+ // Discovery reports the bundle with includeContent=false and sync uploads+ // it with includeContent=true. If only one pass skipped binary files the+ // two would disagree on what the bundle contains. This must hold both for+ // a listed binary extension and for one caught only by the content-level+ // UTF-8 check — the discovery pass has to actually read the file to make+ // the same call the sync pass makes.+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "references/report.docx": invalidUTF8Docx,+ "weights/model.safetensors": invalidUTF8Bytes,+ })++ listed, err := collectLocalSkillFiles(skillDir, false)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles(includeContent=false): %v", err)+ }+ synced, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles(includeContent=true): %v", err)+ }++ listedPaths := make([]string, 0, len(listed))+ for _, f := range listed {+ listedPaths = append(listedPaths, f.Path)+ }+ syncedPaths := make([]string, 0, len(synced))+ for _, f := range synced {+ syncedPaths = append(syncedPaths, f.Path)+ }+ if !reflect.DeepEqual(listedPaths, syncedPaths) {+ t.Fatalf("discovery pass = %v, sync pass = %v; passes must agree", listedPaths, syncedPaths)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(listedPaths, want) {+ t.Fatalf("collected paths = %v, want %v", listedPaths, want)+ }+}++// invalidUTF8Bytes carries no listed binary extension's magic — it exists to+// prove the content-level check, not the extension list, is what catches it.+const invalidUTF8Bytes = "\xff\xfe\x00\x01payload\x80\x81"++// Bohan-J's review on #7175: the extension blacklist alone doesn't close the+// root cause. A binary file with an unlisted extension must still be caught+// by the content-level utf8.Valid check.+func TestCollectLocalSkillFiles_SkipsUnlistedBinaryExtension(t *testing.T) {+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "weights/model.safetensors": invalidUTF8Bytes,+ "data/table.parquet": invalidUTF8Bytes,+ "blob.bin": invalidUTF8Bytes,+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }++ paths := make([]string, 0, len(files))+ for _, f := range files {+ paths = append(paths, f.Path)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(paths, want) {+ t.Fatalf("collected paths = %v, want %v — an unlisted binary extension must still be caught by the content check", paths, want)+ }+}++// A binary file with no extension at all must be caught the same way.+func TestCollectLocalSkillFiles_SkipsInvalidUTF8WithNoExtension(t *testing.T) {+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "references/README": invalidUTF8Bytes,+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }++ paths := make([]string, 0, len(files))+ for _, f := range files {+ paths = append(paths, f.Path)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(paths, want) {+ t.Fatalf("collected paths = %v, want %v — a no-extension binary file must still be caught by the content check", paths, want)+ }+}++// validUTF8WithNUL is UTF-16LE encoding of "Hello": every byte is a valid+// single-byte UTF-8 sequence (printable ASCII or NUL), so utf8.Valid alone+// would accept it — multica-eve's review on #7175 flagged this as a+// realistic byte-integrity hole: the server-side import path+// (server/internal/handler/skill_create.go's sanitizeNullBytes) strips every+// 0x00, so a file like this still comes back different from what went in.+const validUTF8WithNUL = "H\x00e\x00l\x00l\x00o\x00"++// A file with an unlisted extension that is valid UTF-8 but contains an+// embedded NUL must still be caught — utf8.Valid alone is not sufficient.+func TestCollectLocalSkillFiles_SkipsUnlistedExtensionWithEmbeddedNUL(t *testing.T) {+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "weights/model.dat": validUTF8WithNUL,+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }++ paths := make([]string, 0, len(files))+ for _, f := range files {+ paths = append(paths, f.Path)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(paths, want) {+ t.Fatalf("collected paths = %v, want %v — valid UTF-8 with an embedded NUL must still be caught", paths, want)+ }+}++// A no-extension file with the same valid-UTF-8-but-NUL-containing content+// must be caught the same way.+func TestCollectLocalSkillFiles_SkipsNoExtensionWithEmbeddedNUL(t *testing.T) {+ root := t.TempDir()+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "references/notes.md": "# notes\n",+ "references/UTF16NAME": validUTF8WithNUL,+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }++ paths := make([]string, 0, len(files))+ for _, f := range files {+ paths = append(paths, f.Path)+ }+ want := []string{"references/notes.md"}+ if !reflect.DeepEqual(paths, want) {+ t.Fatalf("collected paths = %v, want %v — a no-extension file with an embedded NUL must still be caught", paths, want)+ }+}++// A valid-UTF-8 text file — including one with an extension the blacklist+// doesn't recognize, and one with no extension at all — must still pass+// through untouched. The content check must not over-trigger on ordinary+// text.+func TestCollectLocalSkillFiles_ValidUTF8TextPassesThroughUntouched(t *testing.T) {+ root := t.TempDir()+ body := "# Notes\n\nContém acentos e emoji 🎉 — não é binário.\n"+ skillDir := writeTestLocalSkill(t, root, "docs", map[string]string{+ "SKILL.md": "---\nname: docs\n---\nbody\n",+ "notes.txt": body,+ "data.csv": "a,b,c\n1,2,3\n",+ "config": "key=value\n",+ })++ files, err := collectLocalSkillFiles(skillDir, true)+ if err != nil {+ t.Fatalf("collectLocalSkillFiles: %v", err)+ }+ if len(files) != 3 {+ t.Fatalf("expected all 3 valid-UTF-8 supporting files to pass through, got %d: %+v", len(files), files)+ }+ for _, f := range files {+ if f.Path == "notes.txt" && f.Content != body {+ t.Errorf("notes.txt content = %q, want %q", f.Content, body)+ }+ }+}diff --git a/server/internal/handler/skill.go b/server/internal/handler/skill.goindex 2f3e7ae4d4..3f2a75d208 100644--- a/server/internal/handler/skill.go+++ b/server/internal/handler/skill.go@@ -644,7 +644,7 @@ func isCapError(err error) bool {// assets the agent never reads as text anyway. Logging the skip leaves a// breadcrumb if a user expected one of these to import.func (s *importedSkill) addFile(path, content string) error {- if isLikelyBinaryFilePath(path) {+ if skillpkg.IsLikelyBinaryFilePath(path) {slog.Info("skill import: skipping binary file", "path", path, "size", len(content))return nil}@@ -659,34 +659,6 @@ func (s *importedSkill) addFile(path, content string) error {return nil}-// isLikelyBinaryFilePath reports whether the file's extension indicates a-// non-text payload. Conservative blacklist — extensions not on the list-// are assumed text and pass through. `sanitizeNullBytes` (called at PG-// insert time) is the second-line defence against any text file that-// turns out to have stray invalid-UTF-8 bytes.-func isLikelyBinaryFilePath(path string) bool {- ext := strings.ToLower(filepath.Ext(path))- switch ext {- case- // images- ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".ico", ".heic",- // fonts- ".ttf", ".otf", ".woff", ".woff2", ".eot",- // archives- ".zip", ".gz", ".tar", ".bz2", ".7z", ".rar",- // documents (binary office)- ".pdf", ".docx", ".xlsx", ".pptx", ".doc", ".xls", ".ppt",- // media- ".mp3", ".mp4", ".wav", ".avi", ".mov", ".webm", ".m4a", ".flac",- // compiled / executable- ".exe", ".dll", ".so", ".dylib", ".class", ".jar", ".wasm",- // db / cache- ".db", ".sqlite", ".sqlite3", ".pyc":- return true- }- return false-}-// --- ClawHub types ---var clawHubAPIBase = "https://clawhub.ai/api/v1"@@ -1368,7 +1340,7 @@ func addSupportingFilesFromTree(ctx context.Context, httpClient *http.Client, reif lowerBase == "skill.md" || lowerBase == "license" || lowerBase == "license.txt" || lowerBase == "license.md" {continue}- if isLikelyBinaryFilePath(relPath) {+ if skillpkg.IsLikelyBinaryFilePath(relPath) {continue}eligible = append(eligible, treeFile{repoPath: entry.Path, relPath: relPath, size: entry.size()})diff --git a/server/internal/skill/binary.go b/server/internal/skill/binary.gonew file mode 100644index 0000000000..fb8b667aba--- /dev/null+++ b/server/internal/skill/binary.go@@ -0,0 +1,40 @@+package skill++import (+ "path/filepath"+ "strings"+)++// IsLikelyBinaryFilePath reports whether the file's extension indicates a+// non-text payload. Conservative blacklist — extensions not on the list are+// assumed text and pass through.+//+// Both skill ingestion paths need this, for the same reason from two+// directions: an archive/URL import cannot put these bytes in a PG TEXT+// column (SQLSTATE 22021), and runtime-local sync cannot carry them through+// SkillFileData.Content, because encoding/json rewrites every invalid UTF-8+// byte to U+FFFD on the daemon/server hop. Either way the bytes that come+// back out are not the bytes that went in, so both paths skip the file+// instead of storing a payload they would later hand back corrupted.+func IsLikelyBinaryFilePath(path string) bool {+ ext := strings.ToLower(filepath.Ext(path))+ switch ext {+ case+ // images+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".ico", ".heic",+ // fonts+ ".ttf", ".otf", ".woff", ".woff2", ".eot",+ // archives+ ".zip", ".gz", ".tar", ".bz2", ".7z", ".rar",+ // documents (binary office)+ ".pdf", ".docx", ".xlsx", ".pptx", ".doc", ".xls", ".ppt",+ // media+ ".mp3", ".mp4", ".wav", ".avi", ".mov", ".webm", ".m4a", ".flac",+ // compiled / executable+ ".exe", ".dll", ".so", ".dylib", ".class", ".jar", ".wasm",+ // db / cache+ ".db", ".sqlite", ".sqlite3", ".pyc":+ return true+ }+ return false+}diff --git a/server/internal/skill/binary_test.go b/server/internal/skill/binary_test.gonew file mode 100644index 0000000000..05840c128e--- /dev/null+++ b/server/internal/skill/binary_test.go@@ -0,0 +1,49 @@+package skill++import "testing"++func TestIsLikelyBinaryFilePath(t *testing.T) {+ tests := []struct {+ path string+ want bool+ }{+ // The Office formats runtime-local skill bundles actually carry.+ {"references/report.docx", true},+ {"references/data.xlsx", true},+ {"deck.pptx", true},+ {"legacy.doc", true},+ {"spec.pdf", true},+ // Other non-text payloads.+ {"assets/logo.png", true},+ {"fonts/Inter.woff2", true},+ {"bundle.zip", true},+ {"clip.mp4", true},+ {"helper.so", true},+ {"cache.sqlite3", true},+ // Extension matching is case-insensitive: Windows skill dirs routinely+ // carry upper-cased extensions.+ {"references/REPORT.DOCX", true},+ {"Assets/Logo.PNG", true},+ // Text payloads must pass through — skipping these would silently drop+ // the reference material a skill exists to provide.+ {"SKILL.md", false},+ {"references/notes.md", false},+ {"scripts/run.sh", false},+ {"config.json", false},+ {"data.csv", false},+ {"template.html", false},+ {"main.py", false},+ // No extension, and a dotfile whose name is not an extension.+ {"Makefile", false},+ {".gitignore", false},+ {"", false},+ // A directory-looking path with a binary extension is still binary.+ {"nested/dir/archive.tar", true},+ }++ for _, tt := range tests {+ if got := IsLikelyBinaryFilePath(tt.path); got != tt.want {+ t.Errorf("IsLikelyBinaryFilePath(%q) = %v, want %v", tt.path, got, tt.want)+ }+ }+}