Files
skillshare/internal/server/source_name.go
T
Willie c61f48865e fix(server): reject skill names that escape the skills source (#408)
GET /api/audit/{name} joined the client-supplied name to the source
without checking it, so a name such as ../../x scanned a directory
outside the skills source and returned a clean result for it. The
single-skill update, the update stream, and the batch uninstall built
paths the same way; the tracked-repo uninstall route already refused
such names inline.

One helper, validSourceName, now holds that rule: non-empty, relative,
not starting with "..", and no Windows separators or volume prefixes on
any host, as sourcewalk does for declared names. The audit and update
routes answer 400, the update stream reports the name as an error
result, the batch uninstall fails that item, and the repo uninstall
route uses the same helper. Nested names such as group/skill still
resolve.

Closes #402
2026-10-05 11:03:39 +08:00

27 lines
967 B
Go

package server
import (
"path/filepath"
"strings"
)
// validSourceName reports whether a client-supplied skill or repository name
// stays inside the skills source once joined to it: it must be non-empty,
// relative, and must not start with "..". Nested names such as "group/skill"
// are accepted; the path itself is not checked on disk. Windows separators and
// volume prefixes are rejected on every host, as sourcewalk does for declared
// names, so a name cannot change meaning between platforms.
func validSourceName(name string) bool {
if strings.TrimSpace(name) == "" || strings.Contains(name, `\`) {
return false
}
if len(name) >= 2 && name[1] == ':' && ((name[0] >= 'a' && name[0] <= 'z') || (name[0] >= 'A' && name[0] <= 'Z')) {
return false
}
clean := filepath.Clean(filepath.FromSlash(name))
if clean == "." || clean == ".." || filepath.IsAbs(clean) {
return false
}
return !strings.HasPrefix(clean, ".."+string(filepath.Separator))
}