Skip to content

fix: fail closed on invalid boolean env settings and support yes/no/on/off - #3801

Open
yunusarda-sudo wants to merge 1 commit into
github:mainfrom
yunusarda-sudo:fix/parse-bool-env-fail-closed
Open

yunusarda-sudo wants to merge 1 commit into
github:mainfrom
yunusarda-sudo:fix/parse-bool-env-fail-closed

Conversation

@yunusarda-sudo

Copy link
Copy Markdown

Description

When configuring boolean settings via environment variables (such as GITHUB_READ_ONLY or GITHUB_LOCKDOWN_MODE), Viper binds pflag flags which use strconv.ParseBool. Because pflag's boolValue.Set only accepts "1", "t", "T", "true", "TRUE", "True", "0", "f", "F", "false", "FALSE", "False", any unrecognised value (e.g. yes, on, enabled, or typos like tru) returns a syntax error that Viper silently swallows, falling back to false.

As reported in #3747, setting GITHUB_READ_ONLY=yes silently leaves read-only mode off, exposing 46 tools (including merge_pull_request and delete_file) instead of 27 without warning or error.

Changes

  • Introduce parseBoolSetting(key string, flag *pflag.Flag) (bool, error):
    • Accepts common truthy values: "1", "t", "true", "yes", "y", "on", "enabled".
    • Accepts common falsy values: "0", "f", "false", "no", "n", "off", "disabled".
    • Preserves standard CLI flag precedence: if an explicit CLI flag was passed, it takes precedence.
    • Defaults cleanly to flag default value if the env var is unset or empty.
    • Fails closed on unrecognized syntax (e.g., invalid boolean value "tru" for GITHUB_READ_ONLY: expected true, false, 1, 0, yes, no, on, off).
  • Applied to boolean settings across stdio and http subcommands (read-only, lockdown-mode, insiders, export-translations, enable-command-logging, scope-challenge, trust-proxy-headers).
  • Added comprehensive unit tests in cmd/github-mcp-server/main_test.go covering truthy/falsy cases, fail-closed invalid syntax handling, empty/unset defaults, and CLI flag precedence over env vars.

Fixes #3747

…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 <rafe.walker@users.noreply.github.com>
@yunusarda-sudo
yunusarda-sudo requested a review from a team as a code owner November 10, 2026 12:04
Copilot AI dynamic review requested due to automatic review settings November 10, 2026 12:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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, and enabled/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"))
Comment on lines +440 to +446
str := strings.TrimSpace(viper.GetString(key))
if str == "" {
if flag != nil {
return strconv.ParseBool(flag.DefValue)
}
return false, nil
}
}
}

func TestParseBoolSetting(t *testing.T) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GITHUB_READ_ONLY=yes (or on) silently leaves read-only mode off

2 participants