From a3adbd6efc56d61756061150960f5daef8fb211b Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 10 Jan 2025 16:49:39 -0800 Subject: [PATCH] dockerfile: don't allow non-Dockerfile syntax for directives ParseDirectives code was changed when "check" directive was added and copied over logic from DetectSyntax function. This does not look correct as the only allowed formatting for Dockerfile directives is with a Dockerfile comment. "#syntax" is a special case because we want to allow frontend forwarding capability also in non-Dockerfile sources that use different style of comments or using config files with JSON as frontend entrypoints. Signed-off-by: Tonis Tiigi --- frontend/dockerfile/parser/directives.go | 10 ++++++++- frontend/dockerfile/parser/directives_test.go | 21 ------------------- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/frontend/dockerfile/parser/directives.go b/frontend/dockerfile/parser/directives.go index c29d18b8f11d..4f1e1300ec4e 100644 --- a/frontend/dockerfile/parser/directives.go +++ b/frontend/dockerfile/parser/directives.go @@ -112,10 +112,14 @@ func (d *DirectiveParser) ParseAll(data []byte) ([]*Directive, error) { // This allows for a flexible range of input formats, and appropriate syntax // selection. func DetectSyntax(dt []byte) (string, string, []Range, bool) { - return ParseDirective(keySyntax, dt) + return parseDirective(keySyntax, dt, true) } func ParseDirective(key string, dt []byte) (string, string, []Range, bool) { + return parseDirective(key, dt, false) +} + +func parseDirective(key string, dt []byte, anyFormat bool) (string, string, []Range, bool) { dt = discardBOM(dt) dt, hadShebang, err := discardShebang(dt) if err != nil { @@ -132,6 +136,10 @@ func ParseDirective(key string, dt []byte) (string, string, []Range, bool) { return syntax, cmdline, loc, true } + if !anyFormat { + return "", "", nil, false + } + // use directive with different comment prefix, and search for //key= directiveParser = DirectiveParser{line: line} directiveParser.setComment("//") diff --git a/frontend/dockerfile/parser/directives_test.go b/frontend/dockerfile/parser/directives_test.go index 651120b29118..dd633dddc2fb 100644 --- a/frontend/dockerfile/parser/directives_test.go +++ b/frontend/dockerfile/parser/directives_test.go @@ -158,27 +158,6 @@ RUN ls dt = `//check=skip=all //key=value` - ref, _, _, ok = ParseDirective("check", []byte(dt)) - require.True(t, ok) - require.Equal(t, "skip=all", ref) - - dt = `#!/bin/sh -//check=skip=all` - ref, _, _, ok = ParseDirective("check", []byte(dt)) - require.True(t, ok) - require.Equal(t, "skip=all", ref) - - dt = `{"check": "skip=all"}` - ref, _, _, ok = ParseDirective("check", []byte(dt)) - require.True(t, ok) - require.Equal(t, "skip=all", ref) - - dt = `{"check": "foo"` - _, _, _, ok = ParseDirective("check", []byte(dt)) - require.False(t, ok) - - dt = `{"check": "foo"} -# syntax=bar` _, _, _, ok = ParseDirective("check", []byte(dt)) require.False(t, ok) }