fix: respect quoting in procfile commands
Procfile commands for the `docker-local` scheduler were split differently on deploy and on `dokku run`. Deploys failed on escaped quotes, and `dokku run` ignored quotes entirely. Both now split commands using shell quoting rules, and commands containing bare shell operators fail with a clear error, matching the `k3s` scheduler.
This commit is contained in:
13 files changed
+317
-92
No files matched your search
@@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
"github.com/dokku/dokku/plugins/common/shellwords"
|
||||
)
|
||||
|
||||
// DefaultProcessType is the sentinel process-type key used for options that
|
||||
@@ -32,7 +32,7 @@ func SplitOptionString(input string) (options []string, processes []string, err
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
fields, err := literalFields(input)
|
||||
fields, err := shellwords.Split(input)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Unable to parse docker option: %s", err.Error())
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func groupOptionTokens(fields []string) [][]string {
|
||||
// stops before consuming all of it - a line whose tail begins with an unquoted
|
||||
// `#` reads as a shell comment, and dropping it would lose stored data.
|
||||
func canonicalOptionsFromLine(line string) []string {
|
||||
fields, end, err := literalFieldsWithEnd(line)
|
||||
fields, end, err := shellwords.SplitWithEnd(line)
|
||||
if err != nil || end < len(strings.TrimRight(line, " \t")) {
|
||||
return []string{line}
|
||||
}
|
||||
@@ -118,12 +118,12 @@ func optionsEqual(stored string, option string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
storedFields, err := literalFields(stored)
|
||||
storedFields, err := shellwords.Split(stored)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
optionFields, err := literalFields(option)
|
||||
optionFields, err := shellwords.Split(option)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -131,90 +131,6 @@ func optionsEqual(stored string, option string) bool {
|
||||
return slices.Equal(storedFields, optionFields)
|
||||
}
|
||||
|
||||
// literalFields splits input into shell words using the parser directly, so
|
||||
// quotes delimit words and are stripped from the stored value, but nothing is
|
||||
// expanded. Parameter expansions, command substitutions, and other
|
||||
// metacharacters are preserved verbatim. Malformed input, such as an unbalanced
|
||||
// quote, returns the parser error.
|
||||
func literalFields(input string) ([]string, error) {
|
||||
fields, _, err := literalFieldsWithEnd(input)
|
||||
return fields, err
|
||||
}
|
||||
|
||||
// literalFieldsWithEnd is literalFields plus the byte offset just past the last
|
||||
// word the parser consumed. Callers that must not lose data compare the offset
|
||||
// against the length of the input to detect a tail the parser skipped, such as
|
||||
// a shell comment.
|
||||
func literalFieldsWithEnd(input string) ([]string, int, error) {
|
||||
parser := syntax.NewParser()
|
||||
var fields []string
|
||||
end := 0
|
||||
for word, err := range parser.WordsSeq(strings.NewReader(input)) {
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
fields = append(fields, literalWordValue(input, word.Parts))
|
||||
end = int(word.End().Offset())
|
||||
}
|
||||
return fields, end, nil
|
||||
}
|
||||
|
||||
// literalWordValue reconstructs the unquoted, unexpanded value of a shell word.
|
||||
// Literal and single-quoted parts contribute their literal text; double-quoted
|
||||
// parts have their surrounding quotes dropped while their contents stay
|
||||
// literal; every other part - parameter expansions, command substitutions,
|
||||
// arithmetic expansions - contributes its original source text unchanged.
|
||||
// Backslash escapes are removed the same way the shell removes them during
|
||||
// quote removal so a value such as `Host(\`app\`)` round-trips to `Host(`app`)`.
|
||||
func literalWordValue(input string, parts []syntax.WordPart) string {
|
||||
var sb strings.Builder
|
||||
for _, part := range parts {
|
||||
switch p := part.(type) {
|
||||
case *syntax.Lit:
|
||||
sb.WriteString(unquoteBackslashes(p.Value, false))
|
||||
case *syntax.SglQuoted:
|
||||
sb.WriteString(p.Value)
|
||||
case *syntax.DblQuoted:
|
||||
for _, inner := range p.Parts {
|
||||
if lit, ok := inner.(*syntax.Lit); ok {
|
||||
sb.WriteString(unquoteBackslashes(lit.Value, true))
|
||||
continue
|
||||
}
|
||||
sb.WriteString(input[inner.Pos().Offset():inner.End().Offset()])
|
||||
}
|
||||
default:
|
||||
sb.WriteString(input[part.Pos().Offset():part.End().Offset()])
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// unquoteBackslashes removes backslashes the way the shell does during quote
|
||||
// removal. Inside double quotes only \$, \`, \", \\, and an escaped newline
|
||||
// lose their backslash; unquoted, a backslash escapes any following character.
|
||||
func unquoteBackslashes(s string, inDblQuotes bool) string {
|
||||
if !strings.Contains(s, "\\") {
|
||||
return s
|
||||
}
|
||||
var sb strings.Builder
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == '\\' && i+1 < len(s) {
|
||||
next := s[i+1]
|
||||
if next == '\n' {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if !inDblQuotes || next == '$' || next == '`' || next == '"' || next == '\\' {
|
||||
sb.WriteByte(next)
|
||||
i++
|
||||
continue
|
||||
}
|
||||
}
|
||||
sb.WriteByte(s[i])
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// isFlagToken reports whether tok looks like a CLI flag (long or short) rather
|
||||
// than a value. Treating any token that begins with `-` and has more than one
|
||||
// character as a flag matches docker's flag conventions and avoids the need
|
||||
|
||||
@@ -5,7 +5,6 @@ go 1.26.2
|
||||
require (
|
||||
github.com/dokku/dokku/plugins/common v0.0.0-00010101000000-000000000000
|
||||
github.com/spf13/pflag v1.0.10
|
||||
mvdan.cc/sh/v3 v3.14.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -25,6 +24,7 @@ require (
|
||||
golang.org/x/crypto v0.57.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
mvdan.cc/sh/v3 v3.14.1 // indirect
|
||||
)
|
||||
|
||||
replace github.com/dokku/dokku/plugins/common => ../common
|
||||
Reference in new issue
Block a user