Skip to content

Commit a3adbd6

Browse files
committed
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 <[email protected]>
1 parent c80370b commit a3adbd6

File tree

2 files changed

+9
-22
lines changed

2 files changed

+9
-22
lines changed

frontend/dockerfile/parser/directives.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,14 @@ func (d *DirectiveParser) ParseAll(data []byte) ([]*Directive, error) {
112112
// This allows for a flexible range of input formats, and appropriate syntax
113113
// selection.
114114
func DetectSyntax(dt []byte) (string, string, []Range, bool) {
115-
return ParseDirective(keySyntax, dt)
115+
return parseDirective(keySyntax, dt, true)
116116
}
117117

118118
func ParseDirective(key string, dt []byte) (string, string, []Range, bool) {
119+
return parseDirective(key, dt, false)
120+
}
121+
122+
func parseDirective(key string, dt []byte, anyFormat bool) (string, string, []Range, bool) {
119123
dt = discardBOM(dt)
120124
dt, hadShebang, err := discardShebang(dt)
121125
if err != nil {
@@ -132,6 +136,10 @@ func ParseDirective(key string, dt []byte) (string, string, []Range, bool) {
132136
return syntax, cmdline, loc, true
133137
}
134138

139+
if !anyFormat {
140+
return "", "", nil, false
141+
}
142+
135143
// use directive with different comment prefix, and search for //key=
136144
directiveParser = DirectiveParser{line: line}
137145
directiveParser.setComment("//")

frontend/dockerfile/parser/directives_test.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,6 @@ RUN ls
158158

159159
dt = `//check=skip=all
160160
//key=value`
161-
ref, _, _, ok = ParseDirective("check", []byte(dt))
162-
require.True(t, ok)
163-
require.Equal(t, "skip=all", ref)
164-
165-
dt = `#!/bin/sh
166-
//check=skip=all`
167-
ref, _, _, ok = ParseDirective("check", []byte(dt))
168-
require.True(t, ok)
169-
require.Equal(t, "skip=all", ref)
170-
171-
dt = `{"check": "skip=all"}`
172-
ref, _, _, ok = ParseDirective("check", []byte(dt))
173-
require.True(t, ok)
174-
require.Equal(t, "skip=all", ref)
175-
176-
dt = `{"check": "foo"`
177-
_, _, _, ok = ParseDirective("check", []byte(dt))
178-
require.False(t, ok)
179-
180-
dt = `{"check": "foo"}
181-
# syntax=bar`
182161
_, _, _, ok = ParseDirective("check", []byte(dt))
183162
require.False(t, ok)
184163
}

0 commit comments

Comments
 (0)