Browse code

Improve validation for volume specs

The current validation only checked for the
number of elements in the volume-spec, however,
did not validate if the elements were empty.

Because of this, an empty volume-spec (""),
or volume spec only containing separators ("::")
would not be invalidated.

This adds a simple check for empty elements in
the volume-spec, and returns an error if
the spec is invalid.

A unit-test is also added to verify the behavior.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2017/01/04 05:26:19
Showing 2 changed files
... ...
@@ -348,6 +348,12 @@ func convertVolumeToMount(
348 348
 	// TODO: split Windows path mappings properly
349 349
 	parts := strings.SplitN(volumeSpec, ":", 3)
350 350
 
351
+	for _, part := range parts {
352
+		if strings.TrimSpace(part) == "" {
353
+			return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
354
+		}
355
+	}
356
+
351 357
 	switch len(parts) {
352 358
 	case 3:
353 359
 		source = parts[0]
... ...
@@ -358,8 +364,6 @@ func convertVolumeToMount(
358 358
 		target = parts[1]
359 359
 	case 1:
360 360
 		target = parts[0]
361
-	default:
362
-		return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
363 361
 	}
364 362
 
365 363
 	if source == "" {
... ...
@@ -10,7 +10,7 @@ import (
10 10
 
11 11
 func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
12 12
 	stackVolumes := map[string]composetypes.VolumeConfig{}
13
-	namespace := namespace{name:"foo"}
13
+	namespace := namespace{name: "foo"}
14 14
 	expected := mount.Mount{
15 15
 		Type:   mount.TypeVolume,
16 16
 		Target: "/foo/bar",
... ...
@@ -19,3 +19,12 @@ func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
19 19
 	assert.NilError(t, err)
20 20
 	assert.DeepEqual(t, mnt, expected)
21 21
 }
22
+
23
+func TestConvertVolumeToMountInvalidFormat(t *testing.T) {
24
+	namespace := namespace{name: "foo"}
25
+	invalids := []string{"::", "::cc", ":bb:", "aa::", "aa::cc", "aa:bb:", " : : ", " : :cc", " :bb: ", "aa: : ", "aa: :cc", "aa:bb: "}
26
+	for _, vol := range invalids {
27
+		_, err := convertVolumeToMount(vol, map[string]composetypes.VolumeConfig{}, namespace)
28
+		assert.Error(t, err, "invalid volume: "+vol)
29
+	}
30
+}