Skip to content

Commit

Permalink
dockerfile: don't allow non-Dockerfile syntax for directives
Browse files Browse the repository at this point in the history
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 <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Jan 13, 2025
1 parent c80370b commit a3adbd6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
10 changes: 9 additions & 1 deletion frontend/dockerfile/parser/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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("//")
Expand Down
21 changes: 0 additions & 21 deletions frontend/dockerfile/parser/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a3adbd6

Please sign in to comment.