fix(config): reject invalid git_root scope keyword

An unknown git_root value silently fell back to the skills scope via ScopeDir's default case, so a typo like "agnets" made commit/push/pull operate on the wrong repository without any warning. Validate the scope at the commit/push/pull entry point (resolveGitRoot) and in ValidateConfig, reusing the existing ValidGitRoot check backed by a new ValidGitRoots list.
This commit is contained in:
Willie
2026-05-30 19:23:12 +08:00
parent 53e4a02a7b
commit fc07a3c3ea
4 changed files with 41 additions and 4 deletions
+11
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"skillshare/internal/config"
gitops "skillshare/internal/git"
@@ -26,6 +27,16 @@ func hasGitDir(dir string) bool {
// guidance (this also preserves the legacy case of a skills source nested inside
// an unrelated parent git repo).
func resolveGitRoot(cfg *config.Config, spinner *ui.Spinner) (string, bool) {
// Reject an unknown git_root before resolving: ScopeDir silently falls back
// to the skills scope for any unrecognized value, which would make
// commit/push/pull operate on the wrong repository without warning.
if !config.ValidGitRoot(cfg.GitRoot) {
spinner.Fail("Invalid git_root")
ui.Info(" git_root %q is not a valid scope", cfg.GitRoot)
ui.Info(" valid values: %s (or leave empty for skills)", strings.Join(config.ValidGitRoots, ", "))
return "", false
}
root := cfg.EffectiveGitRoot()
if scope, dir, mismatch := cfg.GitRootMismatch(); mismatch {
configured := cfg.GitRoot
+7 -4
View File
@@ -30,6 +30,10 @@ var ValidSyncModes = []string{"merge", "symlink", "copy"}
// ValidTargetNamings lists all valid target naming values.
var ValidTargetNamings = []string{"flat", "standard"}
// ValidGitRoots lists all valid git_root scope keywords. Empty is also accepted
// (meaning "skills"); see ValidGitRoot.
var ValidGitRoots = []string{"skills", "agents", "extras", "root"}
// IsValidSyncMode reports whether mode is a valid sync mode (or empty, meaning inherit).
func IsValidSyncMode(mode string) bool {
if mode == "" {
@@ -388,11 +392,10 @@ func (c *Config) GitRootMismatch() (scope, dir string, mismatch bool) {
// ValidGitRoot reports whether s is an accepted git_root scope keyword.
// Empty is accepted (means "skills").
func ValidGitRoot(s string) bool {
switch s {
case "", "skills", "agents", "extras", "root":
return true
if s == "" {
return true // empty = skills
}
return false
return slices.Contains(ValidGitRoots, s)
}
// HasAgentTarget reports whether any configured target has an agents path,
+7
View File
@@ -40,6 +40,13 @@ func ValidateConfig(cfg *Config) (warnings []string, err error) {
errs = append(errs, fmt.Sprintf("invalid global target naming %q (valid: %s)", cfg.TargetNaming, strings.Join(ValidTargetNamings, ", ")))
}
// git_root scope keyword. An unknown value silently falls back to the skills
// scope in ScopeDir, so commit/push/pull would operate on the wrong repo
// without warning — reject it here instead.
if !ValidGitRoot(cfg.GitRoot) {
errs = append(errs, fmt.Sprintf("invalid git_root %q (valid: %s)", cfg.GitRoot, strings.Join(ValidGitRoots, ", ")))
}
// Per-target validation
for name, target := range cfg.Targets {
sc := target.SkillsConfig()
+16
View File
@@ -85,6 +85,22 @@ func TestValidateConfig_InvalidGlobalTargetNaming(t *testing.T) {
}
}
func TestValidateConfig_InvalidGitRoot(t *testing.T) {
tmpDir := t.TempDir()
cfg := &Config{
Source: tmpDir,
GitRoot: "agnets", // typo for "agents"
Targets: map[string]TargetConfig{},
}
_, err := ValidateConfig(cfg)
if err == nil {
t.Fatal("expected error for invalid git_root")
}
if !strings.Contains(err.Error(), "invalid git_root") {
t.Errorf("unexpected error: %v", err)
}
}
func TestValidateConfig_InvalidTargetMode(t *testing.T) {
tmpDir := t.TempDir()
targetDir := filepath.Join(tmpDir, "target")