stack/loader: Ignore cmd.exe special env variables

On Windows, ignore all variables that start with "=" when building an
environment variables map for stack.
For MS-DOS compatibility cmd.exe can set some special environment
variables that start with a "=" characters, which breaks the general
assumption that the first encountered "=" separates a variable name from
variable value and causes trouble when parsing.

These variables don't seem to be documented anywhere, but they are
described by some third-party sources and confirmed empirically on my
Windows installation.

Useful sources:
https://devblogs.microsoft.com/oldnewthing/20100506-00/?p=14133
https://ss64.com/nt/syntax-variables.html

Known variables:

- `=ExitCode` stores the exit code returned by external command (in hex
  format)
- `=ExitCodeAscii` - same as above, except the value is the ASCII
  representation of the code (so exit code 65 (0x41) becomes 'A').
- `=::=::\` and friends - store drive specific working directory.
  There is one env variable for each separate drive letter that was
  accessed in the shell session and stores the working directory for that
  specific drive.
  The general format for these is:
    `=<DRIVE_LETTER>:=<CWD>`  (key=`=<DRIVE_LETTER>:`, value=`<CWD>`)
  where <CWD> is a working directory for the drive that is assigned to
  the letter <DRIVE_LETTER>

  A couple of examples:
    `=C:=C:\some\dir`  (key: `=C:`, value: `C:\some\dir`)
    `=D:=D:\some\other\dir`  (key: `=C:`, value: `C:\some\dir`)
    `=Z:=Z:\`  (key: `=Z:`, value: `Z:\`)

  `=::=::\` is the one that seems to be always set and I'm not exactly
  sure what this one is for (what's drive `::`?). Others are set as
  soon as you CD to a path on some drive. Considering that you start a
  cmd.exe also has some working directory, there are 2 of these on start.

All these variables can be safely ignored because they can't be
deliberately set by the user, their meaning is only relevant to the
cmd.exe session and they're all are related to the MS-DOS/Batch feature
that are irrelevant for us.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2023-03-09 15:40:25 +01:00
parent 881c353576
commit a47058bbd5
No known key found for this signature in database
GPG Key ID: B85EFCFE26DEF92A
2 changed files with 46 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"sort" "sort"
"strings" "strings"
@ -104,6 +105,19 @@ 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 %q", s)

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"]))
}