Skip to content

Commit

Permalink
Merge pull request #5646 from tonistiigi/directive-parse-format-fix
Browse files Browse the repository at this point in the history
dockerfile: don't allow non-Dockerfile syntax for directives
  • Loading branch information
crazy-max authored Jan 14, 2025
2 parents c18d237 + a3adbd6 commit fa5aead
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 fa5aead

Please sign in to comment.