From fc07a3c3eac63ebe307d42bcda0fd8470f75f67d Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 29 May 2026 21:03:43 +0800 Subject: [PATCH] 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. --- cmd/skillshare/gitroot.go | 11 +++++++++++ internal/config/config.go | 11 +++++++---- internal/config/config_validate.go | 7 +++++++ internal/config/config_validate_test.go | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/cmd/skillshare/gitroot.go b/cmd/skillshare/gitroot.go index b51573ac4..1e39c462b 100644 --- a/cmd/skillshare/gitroot.go +++ b/cmd/skillshare/gitroot.go @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index 93099e37c..0e2022af3 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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, diff --git a/internal/config/config_validate.go b/internal/config/config_validate.go index d71018ca2..e384f991d 100644 --- a/internal/config/config_validate.go +++ b/internal/config/config_validate.go @@ -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() diff --git a/internal/config/config_validate_test.go b/internal/config/config_validate_test.go index 08c852688..54bfb3577 100644 --- a/internal/config/config_validate_test.go +++ b/internal/config/config_validate_test.go @@ -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")