Browse code

add proto validation at parse

Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)

Victor Vieux authored on 2014/06/03 08:03:10
Showing 1 changed files
... ...
@@ -5,9 +5,10 @@ package nat
5 5
 
6 6
 import (
7 7
 	"fmt"
8
-	"github.com/dotcloud/docker/utils"
9 8
 	"strconv"
10 9
 	"strings"
10
+
11
+	"github.com/dotcloud/docker/utils"
11 12
 )
12 13
 
13 14
 const (
... ...
@@ -72,6 +73,15 @@ func SplitProtoPort(rawPort string) (string, string) {
72 72
 	return parts[1], parts[0]
73 73
 }
74 74
 
75
+func validateProto(proto string) bool {
76
+	for _, availableProto := range []string{"tcp", "udp"} {
77
+		if availableProto == proto {
78
+			return true
79
+		}
80
+	}
81
+	return false
82
+}
83
+
75 84
 // We will receive port specs in the format of ip:public:private/proto and these need to be
76 85
 // parsed in the internal types
77 86
 func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
... ...
@@ -113,6 +123,9 @@ func ParsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
113 113
 		if _, err := strconv.ParseUint(hostPort, 10, 16); hostPort != "" && err != nil {
114 114
 			return nil, nil, fmt.Errorf("Invalid hostPort: %s", hostPort)
115 115
 		}
116
+		if !validateProto(proto) {
117
+			return nil, nil, fmt.Errorf("Invalid proto: %s", proto)
118
+		}
116 119
 
117 120
 		port := NewPort(proto, containerPort)
118 121
 		if _, exists := exposedPorts[port]; !exists {