Merge pull request #4083 from thaJeztah/23.0_backport_windows_drive_cwd_env

[23.0 backport] stack/loader: Ignore cmd.exe special env variables
This commit is contained in:
Sebastiaan van Stijn 2023-03-10 13:04:29 +01:00 committed by GitHub
commit 288b6c79fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strings" "strings"
@ -104,9 +105,22 @@ func GetConfigDetails(composefiles []string, stdin io.Reader) (composetypes.Conf
func buildEnvironment(env []string) (map[string]string, error) { func buildEnvironment(env []string) (map[string]string, error) {
result := make(map[string]string, len(env)) result := make(map[string]string, len(env))
for _, s := range env { for _, s := range env {
if runtime.GOOS == "windows" && len(s) > 0 {
// cmd.exe can have special environment variables which names start with "=".
// They are only there for MS-DOS compatibility and we should ignore them.
// See TestBuildEnvironment for examples.
//
// https://ss64.com/nt/syntax-variables.html
// https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133
// https://github.com/docker/cli/issues/4078
if s[0] == '=' {
continue
}
}
k, v, ok := strings.Cut(s, "=") k, v, ok := strings.Cut(s, "=")
if !ok || k == "" { if !ok || k == "" {
return result, errors.Errorf("unexpected environment %q", s) return result, errors.Errorf("unexpected environment variable '%s'", s)
} }
// value may be set, but empty if "s" is like "K=", not "K". // value may be set, but empty if "s" is like "K=", not "K".
result[k] = v result[k] = v

View File

@ -3,6 +3,7 @@ package loader
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
@ -45,3 +46,34 @@ services:
assert.Check(t, is.Equal("3.0", details.ConfigFiles[0].Config["version"])) assert.Check(t, is.Equal("3.0", details.ConfigFiles[0].Config["version"]))
assert.Check(t, is.Len(details.Environment, len(os.Environ()))) assert.Check(t, is.Len(details.Environment, len(os.Environ())))
} }
func TestBuildEnvironment(t *testing.T) {
inputEnv := []string{
"LEGIT_VAR=LEGIT_VALUE",
"EMPTY_VARIABLE=",
}
if runtime.GOOS == "windows" {
inputEnv = []string{
"LEGIT_VAR=LEGIT_VALUE",
// cmd.exe has some special environment variables which start with "=".
// These should be ignored as they're only there for MS-DOS compatibility.
"=ExitCode=00000041",
"=ExitCodeAscii=A",
`=C:=C:\some\dir`,
`=D:=D:\some\different\dir`,
`=X:=X:\`,
`=::=::\`,
"EMPTY_VARIABLE=",
}
}
env, err := buildEnvironment(inputEnv)
assert.NilError(t, err)
assert.Check(t, is.Len(env, 2))
assert.Check(t, is.Equal("LEGIT_VALUE", env["LEGIT_VAR"]))
assert.Check(t, is.Equal("", env["EMPTY_VARIABLE"]))
}