Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dockerfile: don't allow non-Dockerfile syntax for directives #5646

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading