diff --git a/cmd/github-mcp-server/list_scopes.go b/cmd/github-mcp-server/list_scopes.go index 25e6e87b5c..a344c13301 100644 --- a/cmd/github-mcp-server/list_scopes.go +++ b/cmd/github-mcp-server/list_scopes.go @@ -99,7 +99,10 @@ func runListScopes() error { } } - readOnly := viper.GetBool("read-only") + readOnly, err := parseBoolFlag("read-only") + if err != nil { + return err + } outputFormat := viper.GetString("list-scopes-output") // Create translation helper diff --git a/cmd/github-mcp-server/main.go b/cmd/github-mcp-server/main.go index 04ba68e226..345a8726b3 100644 --- a/cmd/github-mcp-server/main.go +++ b/cmd/github-mcp-server/main.go @@ -115,6 +115,14 @@ var ( } } + readOnly, err := parseBoolFlag("read-only") + if err != nil { + return err + } + lockdownMode, err := parseBoolFlag("lockdown-mode") + if err != nil { + return err + } ttl := viper.GetDuration("repo-access-cache-ttl") stdioServerConfig := ghmcp.StdioServerConfig{ Version: serverVersion, @@ -123,12 +131,12 @@ var ( EnabledToolsets: enabledToolsets, EnabledTools: enabledTools, EnabledFeatures: enabledFeatures, - ReadOnly: viper.GetBool("read-only"), + ReadOnly: readOnly, ExportTranslations: viper.GetBool("export-translations"), EnableCommandLogging: viper.GetBool("enable-command-logging"), LogFilePath: viper.GetString("log-file"), ContentWindowSize: viper.GetInt("content-window-size"), - LockdownMode: viper.GetBool("lockdown-mode"), + LockdownMode: lockdownMode, InsidersMode: viper.GetBool("insiders"), ExcludeTools: excludeTools, RepoAccessCacheTTL: &ttl, @@ -202,6 +210,14 @@ var ( } } + readOnly, err := parseBoolFlag("read-only") + if err != nil { + return err + } + lockdownMode, err := parseBoolFlag("lockdown-mode") + if err != nil { + return err + } ttl := viper.GetDuration("repo-access-cache-ttl") httpConfig := ghhttp.ServerConfig{ Version: version, @@ -215,10 +231,10 @@ var ( EnableCommandLogging: viper.GetBool("enable-command-logging"), LogFilePath: viper.GetString("log-file"), ContentWindowSize: viper.GetInt("content-window-size"), - LockdownMode: viper.GetBool("lockdown-mode"), + LockdownMode: lockdownMode, RepoAccessCacheTTL: &ttl, ScopeChallenge: viper.GetBool("scope-challenge"), - ReadOnly: viper.GetBool("read-only"), + ReadOnly: readOnly, EnabledToolsets: enabledToolsets, EnabledTools: enabledTools, ExcludeTools: excludeTools, @@ -373,3 +389,29 @@ func wordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName { } return pflag.NormalizedName(name) } + +// parseBoolFlag reads a boolean config value that may come from a flag or an +// environment variable. The pflag Bool flag already fails closed on an invalid +// value, but viper.GetBool silently coerces unrecognized strings (e.g. "yes", +// "on", or the typo "tru") to false. The read-only and lockdown switches keep +// write tools away from an agent, so they must fail closed rather than silently +// turn off. +func parseBoolFlag(key string) (bool, error) { + raw := viper.GetString(key) + if raw == "" { + return false, nil + } + // A whitespace-only value is not a valid setting, so it falls through to + // the error below instead of trimming to the unset default. + switch strings.ToLower(strings.TrimSpace(raw)) { + case "0", "f", "false", "no", "n", "off", "disabled": + return false, nil + case "1", "t", "true", "yes", "y", "on", "enabled": + return true, nil + default: + return false, fmt.Errorf( + "invalid boolean value %q for --%s (or GITHUB_%s)", + raw, key, strings.ToUpper(strings.ReplaceAll(key, "-", "_")), + ) + } +} diff --git a/cmd/github-mcp-server/main_test.go b/cmd/github-mcp-server/main_test.go index e064236582..692ff34be8 100644 --- a/cmd/github-mcp-server/main_test.go +++ b/cmd/github-mcp-server/main_test.go @@ -152,3 +152,49 @@ func TestSchemaTypeString(t *testing.T) { }) } } + +func TestParseBoolFlag(t *testing.T) { + tests := []struct { + name string + value string + want bool + wantErr bool + }{ + {name: "empty", value: "", want: false}, + {name: "true", value: "true", want: true}, + {name: "TRUE", value: "TRUE", want: true}, + {name: "one", value: "1", want: true}, + {name: "t", value: "t", want: true}, + {name: "yes", value: "yes", want: true}, + {name: "y", value: "y", want: true}, + {name: "on", value: "on", want: true}, + {name: "enabled", value: "enabled", want: true}, + {name: "false", value: "false", want: false}, + {name: "zero", value: "0", want: false}, + {name: "no", value: "no", want: false}, + {name: "off", value: "off", want: false}, + {name: "disabled", value: "disabled", want: false}, + {name: "typo", value: "tru", wantErr: true}, + {name: "garbage", value: "maybe", wantErr: true}, + {name: "whitespace only", value: " ", wantErr: true}, + {name: "padded true", value: " true ", want: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + viper.Set("read-only", tc.value) + got, err := parseBoolFlag("read-only") + if tc.wantErr { + if err == nil { + t.Fatalf("parseBoolFlag(%q) = %v, want error", tc.value, got) + } + return + } + if err != nil { + t.Fatalf("parseBoolFlag(%q) error: %v", tc.value, err) + } + if got != tc.want { + t.Errorf("parseBoolFlag(%q) = %v, want %v", tc.value, got, tc.want) + } + }) + } +}