Skip to content

Commit 5de99e6

Browse files
committed
opts: MountOpt: improve validation for whitespace in values
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 9620e41 commit 5de99e6

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

opts/mount.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,24 @@ func (m *MountOpt) Set(value string) error {
7878
mount.Type = mounttypes.TypeVolume // default to volume mounts
7979

8080
for _, field := range fields {
81-
key, val, ok := strings.Cut(field, "=")
81+
key, val, hasValue := strings.Cut(field, "=")
8282
if k := strings.TrimSpace(key); k != key {
8383
return fmt.Errorf("invalid option '%s' in '%s': option should not have whitespace", k, field)
8484
}
85+
if hasValue {
86+
v := strings.TrimSpace(val)
87+
if v == "" {
88+
return fmt.Errorf("invalid value for '%s': value is empty", key)
89+
}
90+
if v != val {
91+
return fmt.Errorf("invalid value for '%s' in '%s': value should not have whitespace", key, field)
92+
}
93+
}
8594

8695
// TODO(thaJeztah): these options should not be case-insensitive.
8796
key = strings.ToLower(key)
8897

89-
if !ok {
98+
if !hasValue {
9099
switch key {
91100
case "readonly", "ro":
92101
mount.ReadOnly = true

opts/mount_test.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ func TestMountOptErrors(t *testing.T) {
128128
value: "type=volume,src =/foo,target=/foo",
129129
expErr: "invalid option 'src' in 'src =/foo': option should not have whitespace",
130130
},
131+
{
132+
doc: "invalid value is empty",
133+
value: "type=volume,src=,target=/foo",
134+
expErr: "invalid value for 'src': value is empty",
135+
},
136+
{
137+
doc: "invalid value with leading whitespace",
138+
value: "type=volume,src= /foo,target=/foo",
139+
expErr: "invalid value for 'src' in 'src= /foo': value should not have whitespace",
140+
},
141+
{
142+
doc: "invalid value with trailing whitespace",
143+
value: "type=volume,src=/foo ,target=/foo",
144+
expErr: "invalid value for 'src' in 'src=/foo ': value should not have whitespace",
145+
},
131146
{
132147
doc: "missing value",
133148
value: "type=volume,target=/foo,bogus",
@@ -171,8 +186,8 @@ func TestMountOptReadOnly(t *testing.T) {
171186
}{
172187
{value: "", exp: false},
173188
{value: "readonly", exp: true},
174-
{value: "readonly=", expErr: `invalid value for readonly: `},
175-
{value: "readonly= true", expErr: `invalid value for readonly: true`},
189+
{value: "readonly=", expErr: `invalid value for 'readonly': value is empty`},
190+
{value: "readonly= true", expErr: `invalid value for 'readonly' in 'readonly= true': value should not have whitespace`},
176191
{value: "readonly=no", expErr: `invalid value for readonly: no`},
177192
{value: "readonly=1", exp: true},
178193
{value: "readonly=true", exp: true},
@@ -215,8 +230,8 @@ func TestMountOptVolumeNoCopy(t *testing.T) {
215230
}{
216231
{value: "", exp: false},
217232
{value: "volume-nocopy", exp: true},
218-
{value: "volume-nocopy=", expErr: `invalid value for volume-nocopy: `},
219-
{value: "volume-nocopy= true", expErr: `invalid value for volume-nocopy: true`},
233+
{value: "volume-nocopy=", expErr: `invalid value for 'volume-nocopy': value is empty`},
234+
{value: "volume-nocopy= true", expErr: `invalid value for 'volume-nocopy' in 'volume-nocopy= true': value should not have whitespace`},
220235
{value: "volume-nocopy=no", expErr: `invalid value for volume-nocopy: no`},
221236
{value: "volume-nocopy=1", exp: true},
222237
{value: "volume-nocopy=true", exp: true},

0 commit comments

Comments
 (0)