From 1ed932d22b7f602d3aa0a4a5ab12ff479a2c1dce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 13 Oct 2019 22:33:18 +0100 Subject: [PATCH] cmd/go: refuse -w with an invalid GO111MODULE MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was possible to get 'go env' to break itself: $ go env -w GO111MODULE=bad $ go env go: unknown environment setting GO111MODULE=bad We already check if the variable name is known. In some cases like GO111MODULE, we also know what the variable's valid values are. Enforce it when writing the variable, not just when fetching it. Fixes #34880. Change-Id: I10d682087c69f3445f314fd4473644f694e255f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/200867 Run-TryBot: Daniel Martí TryBot-Result: Gobot Gobot Reviewed-by: Jay Conrod Reviewed-by: Bryan C. Mills --- src/cmd/go/internal/envcmd/env.go | 12 ++++++++++++ src/cmd/go/testdata/script/env_write.txt | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index 17852deed1..b80b181642 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -343,6 +343,18 @@ func checkEnvWrite(key, val string, env []cfg.EnvVar) error { return fmt.Errorf("unknown go command variable %s", key) } + // Some variables can only have one of a few valid values. If set to an + // invalid value, the next cmd/go invocation might fail immediately, + // even 'go env -w' itself. + switch key { + case "GO111MODULE": + switch val { + case "", "auto", "on", "off": + default: + return fmt.Errorf("invalid %s value %q", key, val) + } + } + if !utf8.ValidString(val) { return fmt.Errorf("invalid UTF-8 in %s=... value", key) } diff --git a/src/cmd/go/testdata/script/env_write.txt b/src/cmd/go/testdata/script/env_write.txt index 695cc83f3d..7dbb1d9028 100644 --- a/src/cmd/go/testdata/script/env_write.txt +++ b/src/cmd/go/testdata/script/env_write.txt @@ -85,3 +85,7 @@ stderr 'multiple values for key: GOOS' # go env -w rejects missing variables ! go env -w GOOS stderr 'arguments must be KEY=VALUE: invalid argument: GOOS' + +# go env -w rejects invalid GO111MODULE values, as otherwise cmd/go would break +! go env -w GO111MODULE=badvalue +stderr 'invalid GO111MODULE value "badvalue"'