Skip to content

Commit afa425d

Browse files
committed
Fix healthcheck args with spaces split in Docker API
Signed-off-by: MayorFaj <[email protected]>
1 parent 2fbecb4 commit afa425d

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

pkg/api/handlers/compat/containers_create.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,13 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C
604604
cliOpts.OOMKillDisable = *cc.HostConfig.OomKillDisable
605605
}
606606
if cc.Config.Healthcheck != nil {
607-
finCmd := ""
608-
for _, str := range cc.Config.Healthcheck.Test {
609-
finCmd = finCmd + str + " "
610-
}
611-
if len(finCmd) > 1 {
612-
finCmd = finCmd[:len(finCmd)-1]
607+
// Encode healthcheck test as JSON to preserve arguments with spaces.
608+
// MakeHealthCheckFromCli will unmarshal this back to the original array.
609+
cmdJSON, err := json.Marshal(cc.Config.Healthcheck.Test)
610+
if err != nil {
611+
return nil, nil, err
613612
}
614-
cliOpts.HealthCmd = finCmd
613+
cliOpts.HealthCmd = string(cmdJSON)
615614
if cc.Config.Healthcheck.Interval > 0 {
616615
cliOpts.HealthInterval = cc.Config.Healthcheck.Interval.String()
617616
}

pkg/specgenutil/specgen.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,10 @@ func MakeHealthCheckFromCli(inCmd, interval string, retries uint, timeout, start
986986

987987
var concat string
988988
if strings.ToUpper(cmdArr[0]) == define.HealthConfigTestCmd || strings.ToUpper(cmdArr[0]) == define.HealthConfigTestNone { // this is for compat, we are already split properly for most compat cases
989-
cmdArr = strings.Fields(inCmd)
989+
// Only re-split if the input was not already a JSON array (isArr == false); otherwise preserve the unmarshaled array structure
990+
if !isArr {
991+
cmdArr = strings.Fields(inCmd)
992+
}
990993
} else if strings.ToUpper(cmdArr[0]) != define.HealthConfigTestCmdShell { // this is for podman side of things, won't contain the keywords
991994
if isArr && len(cmdArr) > 1 { // an array of consecutive commands
992995
cmdArr = append([]string{define.HealthConfigTestCmd}, cmdArr...)

test/apiv2/20-containers.at

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,24 @@ t GET containers/$cid/json 200 \
593593
.Config.Healthcheck.Timeout=30000000000 \
594594
.Config.Healthcheck.Retries=3
595595

596+
t DELETE containers/$cid?v=true 204
597+
598+
# Test Compat Create with healthcheck preserving arguments with spaces (issue #26519)
599+
t POST containers/create \
600+
Image=$IMAGE \
601+
Cmd='["top"]' \
602+
Healthcheck='{"Test":["CMD","/usr/bin/test","--arg=value with spaces","another arg"]}' \
603+
201 \
604+
.Id~[0-9a-f]\\{64\\}
605+
cid=$(jq -r '.Id' <<<"$output")
606+
t GET containers/$cid/json 200 \
607+
.Config.Healthcheck.Test[0]="CMD" \
608+
.Config.Healthcheck.Test[1]="/usr/bin/test" \
609+
.Config.Healthcheck.Test[2]="--arg=value with spaces" \
610+
.Config.Healthcheck.Test[3]="another arg"
611+
612+
t DELETE containers/$cid?v=true 204
613+
596614
# compat api: Test for mount options support
597615
# Sigh, JSON can't handle octal. 0755(octal) = 493(decimal)
598616
payload='{"Mounts":[{"Type":"tmpfs","Target":"/mnt/scratch","TmpfsOptions":{"SizeBytes":1024,"Mode":493}}]}'

0 commit comments

Comments
 (0)