mirror of https://github.com/docker/cli.git
fixes: no name check in docker container command
Signed-off-by: Lifubang <lifubang@acmcoder.com>
This commit is contained in:
parent
3ea56aa0ca
commit
8a4e508830
|
@ -26,6 +26,11 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
|
|||
Short: "Kill one or more running containers",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
for _, name := range args {
|
||||
if !cli.CheckContainerName(name) {
|
||||
return fmt.Errorf("container name %s is invalid", name)
|
||||
}
|
||||
}
|
||||
opts.containers = args
|
||||
return runKill(dockerCli, &opts)
|
||||
},
|
||||
|
|
|
@ -29,6 +29,11 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
|
|||
Short: "Remove one or more containers",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
for _, name := range args {
|
||||
if !cli.CheckContainerName(name) {
|
||||
return fmt.Errorf("container name %s is invalid", name)
|
||||
}
|
||||
}
|
||||
opts.containers = args
|
||||
return runRm(dockerCli, &opts)
|
||||
},
|
||||
|
|
|
@ -28,6 +28,11 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
|
|||
Short: "Stop one or more running containers",
|
||||
Args: cli.RequiresMinArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
for _, name := range args {
|
||||
if !cli.CheckContainerName(name) {
|
||||
return fmt.Errorf("container name %s is invalid", name)
|
||||
}
|
||||
}
|
||||
opts.containers = args
|
||||
opts.timeChanged = cmd.Flags().Changed("time")
|
||||
return runStop(dockerCli, &opts)
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/docker/cli/cli/names"
|
||||
)
|
||||
|
||||
var (
|
||||
validContainerNamePattern = names.RestrictedNamePattern
|
||||
)
|
||||
|
||||
// CheckContainerName check container's name is valid or not
|
||||
func CheckContainerName(name string) bool {
|
||||
if len(name) == 0 {
|
||||
return false
|
||||
}
|
||||
return validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/"))
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package names
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
|
||||
const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
|
||||
|
||||
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
|
||||
var RestrictedNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)
|
Loading…
Reference in New Issue