From a8edbc715f45f308a90fe93d5033d6fe563dd05b Mon Sep 17 00:00:00 2001 From: Willie Date: Tue, 6 Oct 2026 15:57:02 +0800 Subject: [PATCH] fix(mcp): follow the reference validator for names and bound SKILL.md reads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Agent Skills reference validator accepts lowercase Unicode letters and digits after NFKC normalization and counts the 64-character limit in characters, so names such as 技能-審查 or café were skipped although they are valid. ValidateName now applies the same rules. sync's target naming shares it and accepts those names too. golang.org/x/text becomes a direct dependency at the version already in the module graph. SKILL.md was size-checked and then read whole, so a file that grew in between was still read into memory; the read itself now stops past 16 MiB. --- go.mod | 2 +- internal/skillpkg/skillpkg.go | 46 +++++++++++++++++++++-------- internal/skillserve/catalog_test.go | 18 +++++++++++ 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 635b4f01e..c455a99c9 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/yuin/goldmark v1.7.8 golang.org/x/sys v0.41.0 golang.org/x/term v0.32.0 + golang.org/x/text v0.26.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -67,6 +68,5 @@ require ( golang.org/x/net v0.33.0 // indirect golang.org/x/oauth2 v0.35.0 // indirect golang.org/x/sync v0.20.0 // indirect - golang.org/x/text v0.26.0 // indirect golang.org/x/time v0.15.0 // indirect ) diff --git a/internal/skillpkg/skillpkg.go b/internal/skillpkg/skillpkg.go index 120492b11..915007918 100644 --- a/internal/skillpkg/skillpkg.go +++ b/internal/skillpkg/skillpkg.go @@ -5,11 +5,15 @@ package skillpkg import ( "fmt" + "io" "os" "path/filepath" "strings" + "unicode" "unicode/utf8" + "golang.org/x/text/unicode/norm" + "skillshare/internal/sourcewalk" "skillshare/internal/utils" ) @@ -42,8 +46,7 @@ type Package struct { func Load(dir string) (*Package, error) { dirName := filepath.Base(filepath.Clean(dir)) // a followed link's name, not its target's dir = utils.ResolveSymlink(dir) - // Lstat first: a linked SKILL.md would be missing from the manifest, which lists no - // links, and an oversized one is skipped without reading it into memory. + // A linked SKILL.md would be missing from the manifest, which lists no links. info, err := os.Lstat(filepath.Join(dir, "SKILL.md")) if err != nil { return nil, err @@ -51,10 +54,8 @@ func Load(dir string) (*Package, error) { if !info.Mode().IsRegular() { return nil, fmt.Errorf("SKILL.md is not a regular file") } - if info.Size() > MaxBytes { - return nil, fmt.Errorf("SKILL.md is over %d MiB", MaxBytes>>20) - } - content, err := os.ReadFile(filepath.Join(dir, "SKILL.md")) + // The read itself is bounded: the file can grow after the Lstat. + content, err := readAtMost(filepath.Join(dir, "SKILL.md"), MaxBytes) if err != nil { return nil, err } @@ -117,13 +118,33 @@ func Load(dir string) (*Package, error) { return p, nil } +// readAtMost reads the file, failing once it holds more than limit bytes. +func readAtMost(path string, limit int64) ([]byte, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + data, err := io.ReadAll(io.LimitReader(f, limit+1)) + if err != nil { + return nil, err + } + if int64(len(data)) > limit { + return nil, fmt.Errorf("SKILL.md is over %d MiB", limit>>20) + } + return data, nil +} + // ValidateName returns why a SKILL.md name breaks the Agent Skills naming rules -// (including name == directory name), or "" when it follows them. +// (including name == directory name), or "" when it follows them. Like the +// reference validator, it takes lowercase Unicode letters and digits after NFKC +// normalization and counts the limit in characters. func ValidateName(name, dirName string) string { + name = norm.NFKC.String(strings.TrimSpace(name)) if name == "" { return "SKILL.md is missing a name" } - if len(name) > 64 { + if utf8.RuneCountInString(name) > 64 { return fmt.Sprintf("SKILL.md name %q is longer than 64 characters", name) } if strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") { @@ -132,13 +153,12 @@ func ValidateName(name, dirName string) string { if strings.Contains(name, "--") { return fmt.Sprintf("SKILL.md name %q cannot contain consecutive hyphens", name) } - for _, r := range name { - if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' { - continue - } + if name != strings.ToLower(name) || strings.ContainsFunc(name, func(r rune) bool { + return r != '-' && !unicode.IsLetter(r) && !unicode.IsNumber(r) + }) { return fmt.Sprintf("SKILL.md name %q must use only lowercase letters, numbers, and hyphens", name) } - if name != dirName { + if name != norm.NFKC.String(dirName) { return fmt.Sprintf("SKILL.md name %q does not match directory name %q", name, dirName) } return "" diff --git a/internal/skillserve/catalog_test.go b/internal/skillserve/catalog_test.go index e2d9e6a52..263d1605e 100644 --- a/internal/skillserve/catalog_test.go +++ b/internal/skillserve/catalog_test.go @@ -194,6 +194,24 @@ func TestHashFile_StopsPastTheSizeLimit(t *testing.T) { } } +// The Agent Skills reference validator takes lowercase Unicode letters and digits, +// after NFKC normalization, and counts the 64-character limit in characters. +func TestBuild_AcceptsLowercaseUnicodeNames(t *testing.T) { + src := t.TempDir() + writeFile(t, filepath.Join(src, "技能-審查/SKILL.md"), skillMD("技能-審查")) + writeFile(t, filepath.Join(src, "café/SKILL.md"), skillMD("café")) + writeFile(t, filepath.Join(src, "Été/SKILL.md"), skillMD("Été")) + writeFile(t, filepath.Join(src, "a_b/SKILL.md"), skillMD("a_b")) + + c, err := (&Builder{Source: src}).Build() + if err != nil { + t.Fatal(err) + } + if len(c.Skills) != 2 || !hasWarning(c, "skipped Été") || !hasWarning(c, "skipped a_b") { + t.Errorf("skills=%v warnings=%v, want 技能-審查 and café served, Été and a_b skipped", c.Skills, c.Skipped) + } +} + func TestBuild_SkipsSkillOverFileLimit(t *testing.T) { src := t.TempDir() writeFile(t, filepath.Join(src, "big/SKILL.md"), skillMD("big"))