From 2c68691b95ef2af4325cebdbbac3f55bcecf349d Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Fri, 3 Jun 2016 05:32:37 -0700 Subject: [PATCH 1/2] Skip UTF-8 BOM bytes from Dockerfile if exists This fix tries to address issues in #23221 where Dockerfile may consists of UTF-8 BOM. This likely happens when Notepad tries to save a file as UTF-8 in Windows. This fix skips the UTF-8 BOM bytes from the beginning of the Dockerfile if exists. Additional tests has been added to cover the changes in this fix. This fix fixes #23221. Signed-off-by: Yong Tang Upstream-commit: 678c80f9256021ce74184fdd6b612d9dea377fba Component: engine --- .../engine/builder/dockerfile/parser/parser.go | 9 ++++++++- .../integration-cli/docker_cli_build_test.go | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/components/engine/builder/dockerfile/parser/parser.go b/components/engine/builder/dockerfile/parser/parser.go index 683f30f68a..48aee99103 100644 --- a/components/engine/builder/dockerfile/parser/parser.go +++ b/components/engine/builder/dockerfile/parser/parser.go @@ -3,6 +3,7 @@ package parser import ( "bufio" + "bytes" "fmt" "io" "regexp" @@ -152,8 +153,14 @@ func Parse(rwc io.Reader) (*Node, error) { root.StartLine = -1 scanner := bufio.NewScanner(rwc) + utf8bom := []byte{0xEF, 0xBB, 0xBF} for scanner.Scan() { - scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace) + scannedBytes := scanner.Bytes() + // We trim UTF8 BOM + if currentLine == 0 { + scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom) + } + scannedLine := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace) currentLine++ line, child, err := ParseLine(scannedLine) if err != nil { diff --git a/components/engine/integration-cli/docker_cli_build_test.go b/components/engine/integration-cli/docker_cli_build_test.go index 8b74b4d6cb..d8c5a31014 100644 --- a/components/engine/integration-cli/docker_cli_build_test.go +++ b/components/engine/integration-cli/docker_cli_build_test.go @@ -6791,3 +6791,17 @@ foo2 c.Fatal(err) } } + +// Test case for #23221 +func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) { + name := "test-with-utf8-bom" + dockerfile := []byte(`FROM busybox`) + bomDockerfile := append([]byte{0xEF, 0xBB, 0xBF}, dockerfile...) + ctx, err := fakeContextFromNewTempDir() + c.Assert(err, check.IsNil) + defer ctx.Close() + err = ctx.addFile("Dockerfile", bomDockerfile) + c.Assert(err, check.IsNil) + _, err = buildImageFromContext(name, ctx, true) + c.Assert(err, check.IsNil) +} From ab25cb22b0361b96756da40d8d361b6ca825358e Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Fri, 3 Jun 2016 07:26:36 -0700 Subject: [PATCH 2/2] Skip UTF-8 BOM bytes from Dockerignore if exist This fix tries to address issues related to #23221 where Dockerignore may consists of UTF-8 BOM. This likely happens when Notepad tries to save a file as UTF-8 in Windows. This fix skips the UTF-8 BOM bytes from the beginning of the Dockerignore if exists. Additional tests has been added to cover the changes in this fix. This fix is related to #23221 (UTF-8 BOM in Dockerfile). Signed-off-by: Yong Tang Upstream-commit: ea86320fcc61ea9ded3eef7ba01699c4e3ccf3ce Component: engine --- .../builder/dockerignore/dockerignore.go | 11 ++++++++- .../integration-cli/docker_cli_build_test.go | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/components/engine/builder/dockerignore/dockerignore.go b/components/engine/builder/dockerignore/dockerignore.go index 4e09b05b0a..9ddf5dd51e 100644 --- a/components/engine/builder/dockerignore/dockerignore.go +++ b/components/engine/builder/dockerignore/dockerignore.go @@ -2,6 +2,7 @@ package dockerignore import ( "bufio" + "bytes" "fmt" "io" "path/filepath" @@ -18,10 +19,18 @@ func ReadAll(reader io.ReadCloser) ([]string, error) { defer reader.Close() scanner := bufio.NewScanner(reader) var excludes []string + currentLine := 0 + utf8bom := []byte{0xEF, 0xBB, 0xBF} for scanner.Scan() { + scannedBytes := scanner.Bytes() + // We trim UTF8 BOM + if currentLine == 0 { + scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom) + } + pattern := string(scannedBytes) + currentLine++ // Lines starting with # (comments) are ignored before processing - pattern := scanner.Text() if strings.HasPrefix(pattern, "#") { continue } diff --git a/components/engine/integration-cli/docker_cli_build_test.go b/components/engine/integration-cli/docker_cli_build_test.go index d8c5a31014..981791daf7 100644 --- a/components/engine/integration-cli/docker_cli_build_test.go +++ b/components/engine/integration-cli/docker_cli_build_test.go @@ -6805,3 +6805,27 @@ func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) { _, err = buildImageFromContext(name, ctx, true) c.Assert(err, check.IsNil) } + +// Test case for UTF-8 BOM in .dockerignore, related to #23221 +func (s *DockerSuite) TestBuildWithUTF8BOMDockerignore(c *check.C) { + name := "test-with-utf8-bom-dockerignore" + dockerfile := ` + FROM busybox + ADD . /tmp/ + RUN ls -la /tmp + RUN sh -c "! ls /tmp/Dockerfile" + RUN ls /tmp/.dockerignore` + dockerignore := []byte("./Dockerfile\n") + bomDockerignore := append([]byte{0xEF, 0xBB, 0xBF}, dockerignore...) + ctx, err := fakeContext(dockerfile, map[string]string{ + "Dockerfile": dockerfile, + }) + c.Assert(err, check.IsNil) + defer ctx.Close() + err = ctx.addFile(".dockerignore", bomDockerignore) + c.Assert(err, check.IsNil) + _, err = buildImageFromContext(name, ctx, true) + if err != nil { + c.Fatal(err) + } +}