mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Return error if env file contains non-ascii or utf8 bytes (for windows)
This fix tries to address the issue raised in 26179 where an env file with non-ascii or utf8 bytes will crash on windows platform. The issue is two-fold: - Windows will adds a BOM mark at the begining with Notepad as the editor - Non-utf8 bytes can not be handled by env file parser. This fix removes utf8 BOM marker if exists so that utf8 encoded env file could be processed. This fix also returns an error (instead of a runtime CreateProcess crash) if env file contains non-utf8 bytes, thus giving users better experiences. Additional test cases has been added in unit tests. This fix fixes 26179. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
parent
0797e3f61a
commit
01d3a16f58
5 changed files with 45 additions and 1 deletions
|
@ -2,9 +2,12 @@ package opts
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ParseEnvFile reads a file with environment variables enumerated by lines
|
||||
|
@ -29,9 +32,20 @@ func ParseEnvFile(filename string) ([]string, error) {
|
|||
|
||||
lines := []string{}
|
||||
scanner := bufio.NewScanner(fh)
|
||||
currentLine := 0
|
||||
utf8bom := []byte{0xEF, 0xBB, 0xBF}
|
||||
for scanner.Scan() {
|
||||
scannedBytes := scanner.Bytes()
|
||||
if !utf8.Valid(scannedBytes) {
|
||||
return []string{}, fmt.Errorf("env file %s contains invalid utf8 bytes at line %d: %v", filename, currentLine+1, scannedBytes)
|
||||
}
|
||||
// We trim UTF8 BOM
|
||||
if currentLine == 0 {
|
||||
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
|
||||
}
|
||||
// trim the line from all leading whitespace first
|
||||
line := strings.TrimLeft(scanner.Text(), whiteSpaces)
|
||||
line := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace)
|
||||
currentLine++
|
||||
// line is not empty, and not starting with '#'
|
||||
if len(line) > 0 && !strings.HasPrefix(line, "#") {
|
||||
data := strings.SplitN(line, "=", 2)
|
||||
|
|
BIN
runconfig/opts/fixtures/utf16.env
Executable file
BIN
runconfig/opts/fixtures/utf16.env
Executable file
Binary file not shown.
BIN
runconfig/opts/fixtures/utf16be.env
Executable file
BIN
runconfig/opts/fixtures/utf16be.env
Executable file
Binary file not shown.
3
runconfig/opts/fixtures/utf8.env
Executable file
3
runconfig/opts/fixtures/utf8.env
Executable file
|
@ -0,0 +1,3 @@
|
|||
FOO=BAR
|
||||
HELLO=您好
|
||||
BAR=FOO
|
|
@ -666,6 +666,33 @@ func TestParseEnvfileVariables(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestParseEnvfileVariablesWithBOMUnicode(t *testing.T) {
|
||||
// UTF8 with BOM
|
||||
config, _, _, err := parseRun([]string{"--env-file=fixtures/utf8.env", "img", "cmd"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
env := []string{"FOO=BAR", "HELLO=" + string([]byte{0xe6, 0x82, 0xa8, 0xe5, 0xa5, 0xbd}), "BAR=FOO"}
|
||||
if len(config.Env) != len(env) {
|
||||
t.Fatalf("Expected a config with %d env variables, got %v: %v", len(env), len(config.Env), config.Env)
|
||||
}
|
||||
for i, v := range env {
|
||||
if config.Env[i] != v {
|
||||
t.Fatalf("Expected a config with [%s], got %v", v, []byte(config.Env[i]))
|
||||
}
|
||||
}
|
||||
|
||||
// UTF16 with BOM
|
||||
e := "contains invalid utf8 bytes at line"
|
||||
if _, _, _, err := parseRun([]string{"--env-file=fixtures/utf16.env", "img", "cmd"}); err == nil || !strings.Contains(err.Error(), e) {
|
||||
t.Fatalf("Expected an error with message '%s', got %v", e, err)
|
||||
}
|
||||
// UTF16BE with BOM
|
||||
if _, _, _, err := parseRun([]string{"--env-file=fixtures/utf16be.env", "img", "cmd"}); err == nil || !strings.Contains(err.Error(), e) {
|
||||
t.Fatalf("Expected an error with message '%s', got %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLabelfileVariables(t *testing.T) {
|
||||
e := "open nonexistent: no such file or directory"
|
||||
if runtime.GOOS == "windows" {
|
||||
|
|
Loading…
Reference in a new issue