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
@@ -0,0 +1,91 @@
|
||||
package shellwords
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"mvdan.cc/sh/v3/syntax"
|
||||
)
|
||||
|
||||
// Split splits input into shell words using the parser directly, so quotes
|
||||
// delimit words and are stripped from the returned 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 Split(input string) ([]string, error) {
|
||||
fields, _, err := SplitWithEnd(input)
|
||||
return fields, err
|
||||
}
|
||||
|
||||
// SplitWithEnd is Split 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 SplitWithEnd(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()
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package shellwords
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want []string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty input",
|
||||
input: "",
|
||||
},
|
||||
{
|
||||
name: "plain words",
|
||||
input: "python -m http.server 5000",
|
||||
want: []string{"python", "-m", "http.server", "5000"},
|
||||
},
|
||||
{
|
||||
name: "double quoted word",
|
||||
input: `sh -c "echo hi"`,
|
||||
want: []string{"sh", "-c", "echo hi"},
|
||||
},
|
||||
{
|
||||
name: "single quoted word",
|
||||
input: `sh -c 'echo hi'`,
|
||||
want: []string{"sh", "-c", "echo hi"},
|
||||
},
|
||||
{
|
||||
name: "escaped quotes inside double quotes",
|
||||
input: `sh -c "echo \"hi\""`,
|
||||
want: []string{"sh", "-c", `echo "hi"`},
|
||||
},
|
||||
{
|
||||
name: "escaped single quote outside quotes",
|
||||
input: `echo it\'s`,
|
||||
want: []string{"echo", "it's"},
|
||||
},
|
||||
{
|
||||
name: "parameter expansion kept verbatim",
|
||||
input: `sh -c "echo $PORT"`,
|
||||
want: []string{"sh", "-c", "echo $PORT"},
|
||||
},
|
||||
{
|
||||
name: "command substitution kept verbatim",
|
||||
input: `echo $(date)`,
|
||||
want: []string{"echo", "$(date)"},
|
||||
},
|
||||
{
|
||||
name: "backticks kept verbatim",
|
||||
input: "echo `date`",
|
||||
want: []string{"echo", "`date`"},
|
||||
},
|
||||
{
|
||||
name: "escaped backticks inside double quotes",
|
||||
input: "echo \"\\`date\\`\"",
|
||||
want: []string{"echo", "`date`"},
|
||||
},
|
||||
{
|
||||
name: "unterminated quote",
|
||||
input: `sh -c "echo hi`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "bare operator",
|
||||
input: `a && b`,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := Split(tc.input)
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("Split(%q) expected error, got %q", tc.input, got)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Split(%q) unexpected error: %v", tc.input, err)
|
||||
}
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Fatalf("Split(%q) = %q, want %q", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitWithEndReportsSkippedComment(t *testing.T) {
|
||||
input := "a # comment"
|
||||
fields, end, err := SplitWithEnd(input)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if !slices.Equal(fields, []string{"a"}) {
|
||||
t.Fatalf("fields = %q, want [a]", fields)
|
||||
}
|
||||
if end != 1 {
|
||||
t.Fatalf("end = %d, want 1", end)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user