Merge pull request #23234 from yongtang/23221-utf8-bom

Skip UTF-8 BOM bytes from Dockerfile and .dockerignore if exist
This commit is contained in:
Sebastiaan van Stijn 2016-06-03 22:28:48 +02:00
commit 2d40e36af8
3 changed files with 56 additions and 2 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -6791,3 +6791,41 @@ 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)
}
// 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)
}
}