Skip to content

Commit 5eac6f6

Browse files
committed
Fail closed on unrecognized read-only and lockdown boolean values
GITHUB_READ_ONLY and GITHUB_LOCKDOWN_MODE only turned their mode on for strconv.ParseBool spellings (1/true/TRUE/t). Common spellings such as yes, on, and enabled — and typos such as tru — were silently parsed as false, so the server started with write tools exposed. Parse these switches with a stricter helper that accepts the common spellings and returns an error for anything unrecognized, matching the --read-only flag. Fixes #3747
1 parent b209d17 commit 5eac6f6

3 files changed

Lines changed: 89 additions & 5 deletions

File tree

‎cmd/github-mcp-server/list_scopes.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ func runListScopes() error {
9999
}
100100
}
101101

102-
readOnly := viper.GetBool("read-only")
102+
readOnly, err := parseBoolFlag("read-only")
103+
if err != nil {
104+
return err
105+
}
103106
outputFormat := viper.GetString("list-scopes-output")
104107

105108
// Create translation helper

‎cmd/github-mcp-server/main.go‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ var (
115115
}
116116
}
117117

118+
readOnly, err := parseBoolFlag("read-only")
119+
if err != nil {
120+
return err
121+
}
122+
lockdownMode, err := parseBoolFlag("lockdown-mode")
123+
if err != nil {
124+
return err
125+
}
118126
ttl := viper.GetDuration("repo-access-cache-ttl")
119127
stdioServerConfig := ghmcp.StdioServerConfig{
120128
Version: serverVersion,
@@ -123,12 +131,12 @@ var (
123131
EnabledToolsets: enabledToolsets,
124132
EnabledTools: enabledTools,
125133
EnabledFeatures: enabledFeatures,
126-
ReadOnly: viper.GetBool("read-only"),
134+
ReadOnly: readOnly,
127135
ExportTranslations: viper.GetBool("export-translations"),
128136
EnableCommandLogging: viper.GetBool("enable-command-logging"),
129137
LogFilePath: viper.GetString("log-file"),
130138
ContentWindowSize: viper.GetInt("content-window-size"),
131-
LockdownMode: viper.GetBool("lockdown-mode"),
139+
LockdownMode: lockdownMode,
132140
InsidersMode: viper.GetBool("insiders"),
133141
ExcludeTools: excludeTools,
134142
RepoAccessCacheTTL: &ttl,
@@ -202,6 +210,14 @@ var (
202210
}
203211
}
204212

213+
readOnly, err := parseBoolFlag("read-only")
214+
if err != nil {
215+
return err
216+
}
217+
lockdownMode, err := parseBoolFlag("lockdown-mode")
218+
if err != nil {
219+
return err
220+
}
205221
ttl := viper.GetDuration("repo-access-cache-ttl")
206222
httpConfig := ghhttp.ServerConfig{
207223
Version: version,
@@ -215,10 +231,10 @@ var (
215231
EnableCommandLogging: viper.GetBool("enable-command-logging"),
216232
LogFilePath: viper.GetString("log-file"),
217233
ContentWindowSize: viper.GetInt("content-window-size"),
218-
LockdownMode: viper.GetBool("lockdown-mode"),
234+
LockdownMode: lockdownMode,
219235
RepoAccessCacheTTL: &ttl,
220236
ScopeChallenge: viper.GetBool("scope-challenge"),
221-
ReadOnly: viper.GetBool("read-only"),
237+
ReadOnly: readOnly,
222238
EnabledToolsets: enabledToolsets,
223239
EnabledTools: enabledTools,
224240
ExcludeTools: excludeTools,
@@ -373,3 +389,24 @@ func wordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName {
373389
}
374390
return pflag.NormalizedName(name)
375391
}
392+
393+
// parseBoolFlag reads a boolean config value that may come from a flag or an
394+
// environment variable. The pflag Bool flag already fails closed on an invalid
395+
// value, but viper.GetBool silently coerces unrecognized strings (e.g. "yes",
396+
// "on", or the typo "tru") to false. The read-only and lockdown switches keep
397+
// write tools away from an agent, so they must fail closed rather than silently
398+
// turn off.
399+
func parseBoolFlag(key string) (bool, error) {
400+
raw := viper.GetString(key)
401+
switch strings.ToLower(strings.TrimSpace(raw)) {
402+
case "", "0", "f", "false", "no", "n", "off", "disabled":
403+
return false, nil
404+
case "1", "t", "true", "yes", "y", "on", "enabled":
405+
return true, nil
406+
default:
407+
return false, fmt.Errorf(
408+
"invalid boolean value %q for --%s (or GITHUB_%s)",
409+
raw, key, strings.ToUpper(strings.ReplaceAll(key, "-", "_")),
410+
)
411+
}
412+
}

‎cmd/github-mcp-server/main_test.go‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,47 @@ func TestSchemaTypeString(t *testing.T) {
152152
})
153153
}
154154
}
155+
156+
func TestParseBoolFlag(t *testing.T) {
157+
tests := []struct {
158+
name string
159+
value string
160+
want bool
161+
wantErr bool
162+
}{
163+
{name: "empty", value: "", want: false},
164+
{name: "true", value: "true", want: true},
165+
{name: "TRUE", value: "TRUE", want: true},
166+
{name: "one", value: "1", want: true},
167+
{name: "t", value: "t", want: true},
168+
{name: "yes", value: "yes", want: true},
169+
{name: "y", value: "y", want: true},
170+
{name: "on", value: "on", want: true},
171+
{name: "enabled", value: "enabled", want: true},
172+
{name: "false", value: "false", want: false},
173+
{name: "zero", value: "0", want: false},
174+
{name: "no", value: "no", want: false},
175+
{name: "off", value: "off", want: false},
176+
{name: "disabled", value: "disabled", want: false},
177+
{name: "typo", value: "tru", wantErr: true},
178+
{name: "garbage", value: "maybe", wantErr: true},
179+
}
180+
for _, tc := range tests {
181+
t.Run(tc.name, func(t *testing.T) {
182+
viper.Set("read-only", tc.value)
183+
got, err := parseBoolFlag("read-only")
184+
if tc.wantErr {
185+
if err == nil {
186+
t.Fatalf("parseBoolFlag(%q) = %v, want error", tc.value, got)
187+
}
188+
return
189+
}
190+
if err != nil {
191+
t.Fatalf("parseBoolFlag(%q) error: %v", tc.value, err)
192+
}
193+
if got != tc.want {
194+
t.Errorf("parseBoolFlag(%q) = %v, want %v", tc.value, got, tc.want)
195+
}
196+
})
197+
}
198+
}

0 commit comments

Comments
 (0)