mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
Bind provider credentials to canonical or explicitly configured HTTPS origins and preserve that policy across remote consumers. Reject cross-site credentialed reads, linked fallback directories, and discovered-agent writes outside the source; audit staged content before rooted atomic publication. Add boundary regressions, private Git update coverage, stable cross-platform fixtures, and aligned documentation. Verified the integrated main baseline with all executed CI checks passing.
221 lines
6.9 KiB
Go
221 lines
6.9 KiB
Go
// Package sourcewalk owns traversal of the skills source. The source root is
|
|
// resolved; links inside the source are not followed, except first-level
|
|
// directory links when the operation's Options carry a Follow policy.
|
|
package sourcewalk
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// TargetDotDirs is the set of hidden directory names (e.g. ".claude", ".cursor")
|
|
// that hold target-synced copies rather than source skills. Set by the CLI
|
|
// entrypoint from config data to avoid a circular import.
|
|
var TargetDotDirs map[string]bool
|
|
|
|
// SkipTargetDotDir reports whether a directory found while walking the source
|
|
// should be skipped as a target dotdir. Only dotdirs nested below the source
|
|
// root are skipped: a skill installed from a repo that ships its own
|
|
// .claude/skills/ must not surface those as nested skills. Dotdirs directly
|
|
// under the root (e.g. ".cursor/skills/*") stay visible because host-style
|
|
// paths there are used to infer the skill's targets. The root itself is never
|
|
// skipped, even when its name matches (e.g. a source named ".skillshare").
|
|
func SkipTargetDotDir(walkRoot, path string, info os.FileInfo) bool {
|
|
if path == walkRoot || !info.IsDir() || !TargetDotDirs[info.Name()] {
|
|
return false
|
|
}
|
|
return filepath.Dir(path) != walkRoot
|
|
}
|
|
|
|
var errSkillFound = errors.New("skill found")
|
|
|
|
// AgentsSkillsDir returns repoRoot/.agents/skills when the repository authors
|
|
// its skills there. .agents is usually a target copy of skills/, so it counts
|
|
// only when no SKILL.md exists elsewhere in the repository outside .git and
|
|
// the target dot dirs in dotDirs. Ignore rules are not applied: a repository
|
|
// whose authored skills are all ignored must not fall back to its copies.
|
|
// The repository root may be linked, but both fallback components must be real
|
|
// directories so the fallback cannot follow a nested link out of the repository.
|
|
func AgentsSkillsDir(repoRoot string, dotDirs map[string]bool) (string, bool) {
|
|
if !dotDirs[".agents"] {
|
|
return "", false
|
|
}
|
|
// Resolve only the intentionally allowed repository root, then inspect each
|
|
// fallback component without following links.
|
|
root := utils.ResolveSymlink(repoRoot)
|
|
dir := root
|
|
for _, component := range [...]string{".agents", "skills"} {
|
|
dir = filepath.Join(dir, component)
|
|
info, err := os.Lstat(dir)
|
|
if err != nil || !info.IsDir() || utils.IsLinkMode(dir, info.Mode()) {
|
|
return "", false
|
|
}
|
|
}
|
|
// Scan the resolved root: filepath.Walk does not descend into a root that
|
|
// is a link, which would hide the authored skills of a linked repository.
|
|
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if info.IsDir() && path != root && (info.Name() == ".git" || dotDirs[info.Name()]) {
|
|
return filepath.SkipDir
|
|
}
|
|
if !info.IsDir() && info.Name() == "SKILL.md" {
|
|
return errSkillFound
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
return filepath.Join(repoRoot, ".agents", "skills"), true
|
|
}
|
|
|
|
// Options is the operation's traversal policy. The zero value follows no links.
|
|
type Options struct {
|
|
// Follow, when set, follows directory links directly under the source root.
|
|
Follow *Follow
|
|
}
|
|
|
|
// Walk walks the resolved source root with filepath.Walk's callback, ordering,
|
|
// SkipDir, and error semantics. Callback paths use the resolved root as base.
|
|
// A followed link is reported under its own name as a directory and its
|
|
// target's contents are walked under that logical path.
|
|
func Walk(root string, opts Options, fn filepath.WalkFunc) error {
|
|
resolved := utils.ResolveSymlink(root)
|
|
f := opts.Follow
|
|
if f == nil {
|
|
return filepath.Walk(resolved, fn)
|
|
}
|
|
return filepath.Walk(resolved, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil || !utils.IsLinkMode(path, info.Mode()) || !f.firstLevel(path) {
|
|
return fn(path, info, err)
|
|
}
|
|
target, dirInfo, ok := f.check(path)
|
|
if !ok {
|
|
return fn(path, info, err)
|
|
}
|
|
if err := fn(path, dirInfo, nil); err != nil {
|
|
if err == filepath.SkipDir {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
var stop error
|
|
err = filepath.Walk(target, func(p string, i os.FileInfo, e error) error {
|
|
f.recordWalkError(path, target, p, e)
|
|
if p == target {
|
|
if e == nil {
|
|
return nil // already reported under the link's name
|
|
}
|
|
i = dirInfo
|
|
}
|
|
r := fn(path+p[len(target):], i, e)
|
|
if r == filepath.SkipAll {
|
|
stop = r
|
|
}
|
|
return r
|
|
})
|
|
if stop != nil {
|
|
return stop
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
// WalkDir is Walk's fs.DirEntry counterpart, with filepath.WalkDir semantics.
|
|
func WalkDir(root string, opts Options, fn fs.WalkDirFunc) error {
|
|
resolved := utils.ResolveSymlink(root)
|
|
f := opts.Follow
|
|
if f == nil {
|
|
return filepath.WalkDir(resolved, fn)
|
|
}
|
|
return filepath.WalkDir(resolved, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil || !utils.IsLinkMode(path, d.Type()) || !f.firstLevel(path) {
|
|
return fn(path, d, err)
|
|
}
|
|
target, dirInfo, ok := f.check(path)
|
|
if !ok {
|
|
return fn(path, d, err)
|
|
}
|
|
dirEntry := fs.FileInfoToDirEntry(dirInfo)
|
|
if err := fn(path, dirEntry, nil); err != nil {
|
|
if err == filepath.SkipDir {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
var stop error
|
|
err = filepath.WalkDir(target, func(p string, e fs.DirEntry, walkErr error) error {
|
|
f.recordWalkError(path, target, p, walkErr)
|
|
if p == target {
|
|
if walkErr == nil {
|
|
return nil // already reported under the link's name
|
|
}
|
|
e = dirEntry
|
|
}
|
|
r := fn(path+p[len(target):], e, walkErr)
|
|
if r == filepath.SkipAll {
|
|
stop = r
|
|
}
|
|
return r
|
|
})
|
|
if stop != nil {
|
|
return stop
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
// ReadDir reads the resolved source root in filename order. Child links remain
|
|
// links, just as with os.ReadDir, unless opts follows them: a followed link is
|
|
// reported under its own name as a directory.
|
|
func ReadDir(root string, opts Options) ([]os.DirEntry, error) {
|
|
resolved := utils.ResolveSymlink(root)
|
|
entries, err := os.ReadDir(resolved)
|
|
f := opts.Follow
|
|
if f != nil {
|
|
for i, entry := range entries {
|
|
path := filepath.Join(resolved, entry.Name())
|
|
if !utils.IsLinkMode(path, entry.Type()) || !f.firstLevel(path) {
|
|
continue
|
|
}
|
|
if _, dirInfo, ok := f.check(path); ok {
|
|
entries[i] = fs.FileInfoToDirEntry(dirInfo)
|
|
}
|
|
}
|
|
}
|
|
if resolved == root {
|
|
return entries, err
|
|
}
|
|
// ReadDir already follows a root link. Preserve its logical error paths,
|
|
// including errors from a later DirEntry.Info call, for existing callers.
|
|
for i, entry := range entries {
|
|
entries[i] = sourceEntry{DirEntry: entry, path: filepath.Join(root, entry.Name())}
|
|
}
|
|
return entries, readErrorPath(err, root)
|
|
}
|
|
|
|
type sourceEntry struct {
|
|
os.DirEntry
|
|
path string
|
|
}
|
|
|
|
func (e sourceEntry) Info() (os.FileInfo, error) {
|
|
info, err := e.DirEntry.Info()
|
|
return info, readErrorPath(err, e.path)
|
|
}
|
|
|
|
func readErrorPath(err error, path string) error {
|
|
if pathErr, ok := err.(*os.PathError); ok {
|
|
copy := *pathErr
|
|
copy.Path = path
|
|
return ©
|
|
}
|
|
return err
|
|
}
|