From 2d517b687b0832a6240a165e8aacef2876b9228a Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 8 Dec 2021 14:35:12 +0100 Subject: generate systemd: support entrypoint JSON strings Make sure to preserve the quoting of entrypoint JSON strings. Fixes: #12477 Signed-off-by: Valentin Rothberg --- pkg/systemd/generate/common.go | 2 +- pkg/systemd/generate/common_test.go | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/systemd/generate/common.go b/pkg/systemd/generate/common.go index 24c85a27e..8689e084c 100644 --- a/pkg/systemd/generate/common.go +++ b/pkg/systemd/generate/common.go @@ -101,7 +101,7 @@ func escapeSystemdArguments(command []string) []string { func escapeSystemdArg(arg string) string { arg = strings.ReplaceAll(arg, "$", "$$") arg = strings.ReplaceAll(arg, "%", "%%") - if strings.ContainsAny(arg, " \t") { + if strings.ContainsAny(arg, " \t\"") { arg = strconv.Quote(arg) } else if strings.Contains(arg, `\`) { // strconv.Quote also escapes backslashes so diff --git a/pkg/systemd/generate/common_test.go b/pkg/systemd/generate/common_test.go index 45004ecb0..b9a43df8c 100644 --- a/pkg/systemd/generate/common_test.go +++ b/pkg/systemd/generate/common_test.go @@ -146,7 +146,7 @@ func TestEscapeSystemdArguments(t *testing.T) { }{ { []string{"foo", "bar=\"arg\""}, - []string{"foo", "bar=\"arg\""}, + []string{"foo", "\"bar=\\\"arg\\\"\""}, }, { []string{"foo", "bar=\"arg with space\""}, @@ -192,6 +192,22 @@ func TestEscapeSystemdArguments(t *testing.T) { []string{"foo", `command with two backslashes \\`}, []string{"foo", `"command with two backslashes \\\\"`}, }, + { + []string{"podman", "create", "--entrypoint", "foo"}, + []string{"podman", "create", "--entrypoint", "foo"}, + }, + { + []string{"podman", "create", "--entrypoint=foo"}, + []string{"podman", "create", "--entrypoint=foo"}, + }, + { + []string{"podman", "create", "--entrypoint", "[\"foo\"]"}, + []string{"podman", "create", "--entrypoint", "\"[\\\"foo\\\"]\""}, + }, + { + []string{"podman", "create", "--entrypoint", "[\"sh\", \"-c\", \"date '+%s'\"]"}, + []string{"podman", "create", "--entrypoint", "\"[\\\"sh\\\", \\\"-c\\\", \\\"date '+%%s'\\\"]\""}, + }, } for _, test := range tests { -- cgit v1.2.3-54-g00ecf