Merge pull request #2365 from weaveworks/issues/2283-validate-cidrs

Validate CIDRs in proxy before attaching a container

Fixes #2283
This commit is contained in:
Matthias Radestock authored and GitHub committed 2016-06-13 15:15:48 +01:00
commit aca34191da
1 file changed
+22
+22
View File
@@ -20,6 +20,7 @@ import (
"github.com/weaveworks/weave/common"
weavedocker "github.com/weaveworks/weave/common/docker"
weavenet "github.com/weaveworks/weave/net"
"github.com/weaveworks/weave/net/address"
)
const (
@@ -488,6 +489,10 @@ func (proxy *Proxy) attach(containerID string, orDie, killProcess bool) error {
return nil
}
Log.Infof("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " "))
if err := validateCIDRs(cidrs); err != nil {
return err
}
args := []string{"attach"}
args = append(args, cidrs...)
if !proxy.NoRewriteHosts {
@@ -523,6 +528,23 @@ func callWeaveAttach(container *docker.Container, args []string) error {
return nil
}
func validateCIDRs(cidrs []string) error {
for _, cidr := range cidrs {
if cidr == "net:default" {
continue
}
for _, prefix := range []string{"ip:", "net:", ""} {
if strings.HasPrefix(cidr, prefix) {
if _, err := address.ParseCIDR(strings.TrimPrefix(cidr, prefix)); err == nil {
break
}
return fmt.Errorf("invalid WEAVE_CIDR: %s", cidr)
}
}
}
return nil
}
func (proxy *Proxy) weaveCIDRs(networkMode string, env []string) ([]string, error) {
if networkMode == "host" || strings.HasPrefix(networkMode, "container:") {
return nil, fmt.Errorf("the container has '--net=%s'", networkMode)