Skip to content

Commit

Permalink
Merge pull request #4188 from Mrigank11/validate_network_name
Browse files Browse the repository at this point in the history
podman network create: validate user input
  • Loading branch information
openshift-merge-robot authored Oct 4, 2019
2 parents 7af4074 + c5e26f8 commit 1fe9556
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
6 changes: 5 additions & 1 deletion cmd/podman/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ package main

import (
"fmt"
"github.com/containers/libpod/pkg/network"
"net"

"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/adapter"
"github.com/containers/libpod/pkg/network"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -58,6 +59,9 @@ func networkcreateCmd(c *cliconfig.NetworkCreateValues) error {
if len(c.InputArgs) > 1 {
return errors.Errorf("only one network can be created at a time")
}
if len(c.InputArgs) > 0 && !libpod.NameRegex.MatchString(c.InputArgs[0]) {
return libpod.RegexError
}
runtime, err := adapter.GetRuntimeNoStore(getContext(), &c.PodmanCommand)
if err != nil {
return err
Expand Down
20 changes: 10 additions & 10 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
)

var (
nameRegex = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")
regexError = errors.Wrapf(define.ErrInvalidArg, "names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*")
NameRegex = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$")
RegexError = errors.Wrapf(define.ErrInvalidArg, "names must match [a-zA-Z0-9][a-zA-Z0-9_.-]*")
)

// Runtime Creation Options
Expand Down Expand Up @@ -648,8 +648,8 @@ func WithName(name string) CtrCreateOption {
}

// Check the name against a regex
if !nameRegex.MatchString(name) {
return regexError
if !NameRegex.MatchString(name) {
return RegexError
}

ctr.config.Name = name
Expand Down Expand Up @@ -1426,8 +1426,8 @@ func WithVolumeName(name string) VolumeCreateOption {
}

// Check the name against a regex
if !nameRegex.MatchString(name) {
return regexError
if !NameRegex.MatchString(name) {
return RegexError
}
volume.config.Name = name

Expand Down Expand Up @@ -1532,8 +1532,8 @@ func WithPodName(name string) PodCreateOption {
}

// Check the name against a regex
if !nameRegex.MatchString(name) {
return regexError
if !NameRegex.MatchString(name) {
return RegexError
}

pod.config.Name = name
Expand All @@ -1550,8 +1550,8 @@ func WithPodHostname(hostname string) PodCreateOption {
}

// Check the hostname against a regex
if !nameRegex.MatchString(hostname) {
return regexError
if !NameRegex.MatchString(hostname) {
return RegexError
}

pod.config.Hostname = hostname
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/network_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,10 @@ var _ = Describe("Podman network create", func() {
Expect(ncFail.ExitCode()).ToNot(BeZero())
})

It("podman network create with invalid network name", func() {
nc := podmanTest.Podman([]string{"network", "create", "foo "})
nc.WaitWithDefaultTimeout()
Expect(nc.ExitCode()).ToNot(BeZero())
})

})

0 comments on commit 1fe9556

Please sign in to comment.