Repository navigation
fix: fail closed on invalid boolean env settings and support yes/no/on/off - #3801
yunusarda-sudo wants to merge 7 commits into
Conversation
…n/off
When configuring boolean settings via environment variables (such as
GITHUB_READ_ONLY or GITHUB_LOCKDOWN_MODE), Viper binds pflag flags which
use strconv.ParseBool. Any unrecognised value (e.g. 'yes', 'on', 'tru')
causes pflag's Set method to fail silently and Viper defaults to false.
This meant GITHUB_READ_ONLY=yes would silently leave read-only mode off,
exposing write tools without warning.
Introduce parseBoolSetting which accepts common truthy ('1', 't', 'true',
'yes', 'y', 'on', 'enabled') and falsy ('0', 'f', 'false', 'no', 'n',
'off', 'disabled') values, while failing closed with a descriptive error
on unrecognised values. Explicit CLI flags preserve standard precedence.
Fixes github#3747
Co-authored-by: Rafe Walker <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Boolean-setting validation remains incomplete: whitespace-only input can leave write tools enabled, list-scopes bypasses validation, and the parsing test depends on another test initializing Viper.
3 open issues
What changed in this PR
This PR addresses #3747 by adding explicit boolean environment parsing to the GitHub MCP server’s stdio and HTTP startup paths, aiming to prevent invalid settings from silently disabling protections such as read-only mode.
Changes:
- Accepts common boolean spellings, including
yes/no,on/off, andenabled/disabled, while returning errors for unrecognized values. - Preserves explicit CLI flag precedence and defaults for unset or empty settings.
- Adds unit tests for accepted values, invalid input, defaults, and CLI precedence.
| File | Description |
|---|---|
cmd/github-mcp-server/main.go |
Adds the shared boolean parser and uses it for stdio and HTTP settings. |
cmd/github-mcp-server/main_test.go |
Adds boolean parsing and configuration precedence tests. |
Review effort: Max (auto)
|
|
||
| ttl := viper.GetDuration("repo-access-cache-ttl") | ||
|
|
||
| readOnly, err := parseBoolSetting("read-only", cmd.Flag("read-only")) |
There was a problem hiding this comment.
Done — runListScopes now uses parseBoolSetting and propagates its error, so values like GITHUB_READ_ONLY=yes and invalid tokens such as tru are handled the same way as the server command.
| str := strings.TrimSpace(viper.GetString(key)) | ||
| if str == "" { | ||
| if flag != nil { | ||
| return strconv.ParseBool(flag.DefValue) | ||
| } | ||
| return false, nil | ||
| } |
There was a problem hiding this comment.
Done — an actually empty value is still treated as unset, but whitespace-only input such as GITHUB_READ_ONLY=' ' is rejected through the invalid-value branch. The same applies to GITHUB_LOCKDOWN_MODE.
| } | ||
| } | ||
|
|
||
| func TestParseBoolSetting(t *testing.T) { |
There was a problem hiding this comment.
Done — TestParseBoolSetting now calls initConfig() so the GITHUB_* cases work when that test is run in isolation.
…ool settings - list-scopes now reads read-only through parseBoolSetting, so GITHUB_READ_ONLY=yes lists read-only scopes and invalid values fail. - A whitespace-only value is no longer treated as unset; it goes through the invalid-value branch. - TestParseBoolSetting calls initConfig() so it runs on its own.

Description
When configuring boolean settings via environment variables (such as
GITHUB_READ_ONLYorGITHUB_LOCKDOWN_MODE), Viper binds pflag flags which usestrconv.ParseBool. Becausepflag'sboolValue.Setonly accepts"1", "t", "T", "true", "TRUE", "True", "0", "f", "F", "false", "FALSE", "False", any unrecognised value (e.g.yes,on,enabled, or typos liketru) returns a syntax error that Viper silently swallows, falling back tofalse.As reported in #3747, setting
GITHUB_READ_ONLY=yessilently leaves read-only mode off, exposing 46 tools (includingmerge_pull_requestanddelete_file) instead of 27 without warning or error.Changes
parseBoolSetting(key string, flag *pflag.Flag) (bool, error):"1", "t", "true", "yes", "y", "on", "enabled"."0", "f", "false", "no", "n", "off", "disabled".invalid boolean value "tru" for GITHUB_READ_ONLY: expected true, false, 1, 0, yes, no, on, off).stdioandhttpsubcommands (read-only,lockdown-mode,insiders,export-translations,enable-command-logging,scope-challenge,trust-proxy-headers).cmd/github-mcp-server/main_test.gocovering truthy/falsy cases, fail-closed invalid syntax handling, empty/unset defaults, and CLI flag precedence over env vars.Fixes #3747