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 <[email protected]>
  • Loading branch information
tonistiigi committed Jan 11, 2025
1 parent fe09265 commit 525856e
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, hadShebang, err := discardShebang(dt)
if err != nil {
return "", "", nil, false
Expand All @@ -131,6 +135,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 @@ -145,26 +145,5 @@ 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 525856e

Please sign in to comment.