From d66fb36f32fec957add9568c9f62db588f73f568 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 10 Jan 2025 16:42:50 -0800 Subject: [PATCH] dockerfile: fix syntax forwarding for files with BOM Signed-off-by: Tonis Tiigi --- frontend/dockerfile/parser/directives.go | 5 +++++ frontend/dockerfile/parser/directives_test.go | 14 ++++++++++++++ frontend/dockerfile/parser/parser.go | 4 +--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/frontend/dockerfile/parser/directives.go b/frontend/dockerfile/parser/directives.go index 651b13148c3a..c29d18b8f11d 100644 --- a/frontend/dockerfile/parser/directives.go +++ b/frontend/dockerfile/parser/directives.go @@ -116,6 +116,7 @@ func DetectSyntax(dt []byte) (string, string, []Range, bool) { } func ParseDirective(key string, dt []byte) (string, string, []Range, bool) { + dt = discardBOM(dt) dt, hadShebang, err := discardShebang(dt) if err != nil { return "", "", nil, false @@ -171,3 +172,7 @@ func discardShebang(dt []byte) ([]byte, bool, error) { } return dt, false, nil } + +func discardBOM(dt []byte) []byte { + return bytes.TrimPrefix(dt, []byte{0xEF, 0xBB, 0xBF}) +} diff --git a/frontend/dockerfile/parser/directives_test.go b/frontend/dockerfile/parser/directives_test.go index 61ede0ac407f..651120b29118 100644 --- a/frontend/dockerfile/parser/directives_test.go +++ b/frontend/dockerfile/parser/directives_test.go @@ -104,6 +104,20 @@ RUN ls require.False(t, ok) } +func TestDetectSyntaxBOM(t *testing.T) { + t.Parallel() + + dt := append([]byte{0xEF, 0xBB, 0xBF}, []byte(`# syntax = myfrontend +FROM busybox +`)...) + ref, cmdline, loc, ok := DetectSyntax(dt) + require.True(t, ok) + require.Equal(t, "myfrontend", ref) + require.Equal(t, "myfrontend", cmdline) + require.Equal(t, 1, loc[0].Start.Line) + require.Equal(t, 1, loc[0].End.Line) +} + func TestParseDirective(t *testing.T) { t.Parallel() diff --git a/frontend/dockerfile/parser/parser.go b/frontend/dockerfile/parser/parser.go index 740f03b708f3..1b0a96228656 100644 --- a/frontend/dockerfile/parser/parser.go +++ b/frontend/dockerfile/parser/parser.go @@ -291,7 +291,7 @@ func Parse(rwc io.Reader) (*Result, error) { bytesRead := scanner.Bytes() if currentLine == 0 { // First line, strip the byte-order-marker if present - bytesRead = bytes.TrimPrefix(bytesRead, utf8bom) + bytesRead = discardBOM(bytesRead) } if isComment(bytesRead) { comment := strings.TrimSpace(string(bytesRead[1:])) @@ -522,8 +522,6 @@ func isEmptyContinuationLine(line []byte) bool { return len(trimLeadingWhitespace(trimNewline(line))) == 0 } -var utf8bom = []byte{0xEF, 0xBB, 0xBF} - func trimContinuationCharacter(line []byte, d *directives) ([]byte, bool) { if d.lineContinuationRegex.Match(line) { line = d.lineContinuationRegex.ReplaceAll(line, []byte("$1"))