Skip to content

Commit 8a4e508

Browse files
committed
fixes: no name check in docker container command
Signed-off-by: Lifubang <lifubang@acmcoder.com>
1 parent 3ea56aa commit 8a4e508

5 files changed

Lines changed: 45 additions & 0 deletions

File tree

cli/command/container/kill.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
2626
Short: "Kill one or more running containers",
2727
Args: cli.RequiresMinArgs(1),
2828
RunE: func(cmd *cobra.Command, args []string) error {
29+
for _, name := range args {
30+
if !cli.CheckContainerName(name) {
31+
return fmt.Errorf("container name %s is invalid", name)
32+
}
33+
}
2934
opts.containers = args
3035
return runKill(dockerCli, &opts)
3136
},

cli/command/container/rm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
2929
Short: "Remove one or more containers",
3030
Args: cli.RequiresMinArgs(1),
3131
RunE: func(cmd *cobra.Command, args []string) error {
32+
for _, name := range args {
33+
if !cli.CheckContainerName(name) {
34+
return fmt.Errorf("container name %s is invalid", name)
35+
}
36+
}
3237
opts.containers = args
3338
return runRm(dockerCli, &opts)
3439
},

cli/command/container/stop.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
2828
Short: "Stop one or more running containers",
2929
Args: cli.RequiresMinArgs(1),
3030
RunE: func(cmd *cobra.Command, args []string) error {
31+
for _, name := range args {
32+
if !cli.CheckContainerName(name) {
33+
return fmt.Errorf("container name %s is invalid", name)
34+
}
35+
}
3136
opts.containers = args
3237
opts.timeChanged = cmd.Flags().Changed("time")
3338
return runStop(dockerCli, &opts)

cli/names.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cli
2+
3+
import (
4+
"strings"
5+
6+
"github.com/docker/cli/cli/names"
7+
)
8+
9+
var (
10+
validContainerNamePattern = names.RestrictedNamePattern
11+
)
12+
13+
// CheckContainerName check container's name is valid or not
14+
func CheckContainerName(name string) bool {
15+
if len(name) == 0 {
16+
return false
17+
}
18+
return validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/"))
19+
}

cli/names/names.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package names
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
// RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
8+
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
9+
10+
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
11+
var RestrictedNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)

0 commit comments

Comments
 (0)