Browse code

Don't allow empty env names

Closes: #25281

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2016/08/02 01:38:05
Showing 2 changed files
... ...
@@ -66,6 +66,11 @@ func env(b *Builder, args []string, attributes map[string]bool, original string)
66 66
 	for j := 0; j < len(args); j++ {
67 67
 		// name  ==> args[j]
68 68
 		// value ==> args[j+1]
69
+
70
+		if len(args[j]) == 0 {
71
+			return fmt.Errorf("ENV names can not be blank")
72
+		}
73
+
69 74
 		newVar := args[j] + "=" + args[j+1] + ""
70 75
 		commitStr += " " + newVar
71 76
 
... ...
@@ -129,6 +134,11 @@ func label(b *Builder, args []string, attributes map[string]bool, original strin
129 129
 	for j := 0; j < len(args); j++ {
130 130
 		// name  ==> args[j]
131 131
 		// value ==> args[j+1]
132
+
133
+		if len(args[j]) == 0 {
134
+			return fmt.Errorf("LABEL names can not be blank")
135
+		}
136
+
132 137
 		newVar := args[j] + "=" + args[j+1] + ""
133 138
 		commitStr += " " + newVar
134 139
 
... ...
@@ -696,6 +706,10 @@ func arg(b *Builder, args []string, attributes map[string]bool, original string)
696 696
 	// name-value pair). If possible, it will be good to harmonize the two.
697 697
 	if strings.Contains(arg, "=") {
698 698
 		parts := strings.SplitN(arg, "=", 2)
699
+		if len(parts[0]) == 0 {
700
+			return fmt.Errorf("ARG names can not be blank")
701
+		}
702
+
699 703
 		name = parts[0]
700 704
 		value = parts[1]
701 705
 		hasDefault = true
... ...
@@ -2020,6 +2020,42 @@ func (s *DockerSuite) TestBuildRelativeCopy(c *check.C) {
2020 2020
 	}
2021 2021
 }
2022 2022
 
2023
+func (s *DockerSuite) TestBuildBlankName(c *check.C) {
2024
+	name := "testbuildblankname"
2025
+	_, _, stderr, err := buildImageWithStdoutStderr(name,
2026
+		`FROM busybox
2027
+		ENV =`,
2028
+		true)
2029
+	if err == nil {
2030
+		c.Fatal("Build was supposed to fail but didn't")
2031
+	}
2032
+	if !strings.Contains(stderr, "ENV names can not be blank") {
2033
+		c.Fatalf("Missing error message, got: %s", stderr)
2034
+	}
2035
+
2036
+	_, _, stderr, err = buildImageWithStdoutStderr(name,
2037
+		`FROM busybox
2038
+		LABEL =`,
2039
+		true)
2040
+	if err == nil {
2041
+		c.Fatal("Build was supposed to fail but didn't")
2042
+	}
2043
+	if !strings.Contains(stderr, "LABEL names can not be blank") {
2044
+		c.Fatalf("Missing error message, got: %s", stderr)
2045
+	}
2046
+
2047
+	_, _, stderr, err = buildImageWithStdoutStderr(name,
2048
+		`FROM busybox
2049
+		ARG =foo`,
2050
+		true)
2051
+	if err == nil {
2052
+		c.Fatal("Build was supposed to fail but didn't")
2053
+	}
2054
+	if !strings.Contains(stderr, "ARG names can not be blank") {
2055
+		c.Fatalf("Missing error message, got: %s", stderr)
2056
+	}
2057
+}
2058
+
2023 2059
 func (s *DockerSuite) TestBuildEnv(c *check.C) {
2024 2060
 	testRequires(c, DaemonIsLinux) // ENV expansion is different in Windows
2025 2061
 	name := "testbuildenv"