Skip to content

security: reject -toolexec in GOFLAGS environment variable#1618

Open
mohammadmseet-hue wants to merge 2 commits intoko-build:mainfrom
mohammadmseet-hue:fix/goflags-toolexec-bypass
Open

security: reject -toolexec in GOFLAGS environment variable#1618
mohammadmseet-hue wants to merge 2 commits intoko-build:mainfrom
mohammadmseet-hue:fix/goflags-toolexec-bypass

Conversation

@mohammadmseet-hue
Copy link
Copy Markdown

Summary

The existing -toolexec blocklist in createBuildArgs() only inspects the args slice (flags/ldflags from .ko.yaml). However, a malicious .ko.yaml can set GOFLAGS=-toolexec=<cmd> in the env or defaultEnv field, which flows through buildEnv() to cmd.Env without any -toolexec check.

This allows arbitrary command execution during go build, bypassing the security control.

Changes

  • Added -toolexec detection in buildEnv() for GOFLAGS environment variables
  • Added 5 test cases covering the new validation (positive and negative)

Reproduction

# .ko.yaml
defaultEnv:
  - "GOFLAGS=-toolexec=id"

Running ko build with this config executes id as the toolexec program.

Test results

--- PASS: TestBuildEnvRejectsToolexecInGOFLAGS (0.00s)
    --- PASS: GOFLAGS_with_-toolexec_should_be_rejected (0.00s)
    --- PASS: GOFLAGS_with_--toolexec_should_be_rejected (0.00s)
    --- PASS: GOFLAGS_with_-toolexec_and_other_flags_should_be_rejected (0.00s)
    --- PASS: GOFLAGS_without_toolexec_should_be_allowed (0.00s)
    --- PASS: non-GOFLAGS_env_vars_should_be_allowed (0.00s)

The existing -toolexec blocklist in createBuildArgs only checks the
args slice (flags/ldflags from .ko.yaml). A malicious .ko.yaml can
bypass this by setting GOFLAGS=-toolexec=<cmd> in the env or
defaultEnv field, which flows through buildEnv() to cmd.Env without
any toolexec check.

This adds a check in buildEnv() to reject any GOFLAGS environment
variable containing -toolexec or --toolexec, closing the bypass.

Includes test cases for the new validation.
Copilot AI review requested due to automatic review settings March 25, 2026 23:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR closes a security bypass where -toolexec could be injected via GOFLAGS set in .ko.yaml (env / defaultEnv) and reach go build without being blocked.

Changes:

  • Added validation in buildEnv() to reject GOFLAGS values that include -toolexec / --toolexec.
  • Added new unit tests covering acceptance/rejection of GOFLAGS values.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/build/gobuild.go Adds GOFLAGS scanning in buildEnv() to block -toolexec via environment variables.
pkg/build/gobuild_test.go Adds table-driven tests for the new GOFLAGS validation behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Author

@mohammadmseet-hue mohammadmseet-hue left a comment

Choose a reason for hiding this comment

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

Nice

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.

3 participants