Merge pull request #19339 from dfr/validate-device

pkg/specgen: Don't crash for device spec with empty destination path
This commit is contained in:
OpenShift Merge Robot
2023-07-25 11:45:13 +02:00
committed by GitHub
2 changed files with 23 additions and 1 deletions

View File

@ -193,7 +193,7 @@ func ParseDevice(device string) (string, string, string, error) {
if IsValidDeviceMode(arr[1]) { if IsValidDeviceMode(arr[1]) {
permissions = arr[1] permissions = arr[1]
} else { } else {
if arr[1][0] != '/' { if len(arr[1]) > 0 && arr[1][0] != '/' {
return "", "", "", fmt.Errorf("invalid device mode: %s", arr[1]) return "", "", "", fmt.Errorf("invalid device mode: %s", arr[1])
} }
dst = arr[1] dst = arr[1]

View File

@ -26,3 +26,25 @@ func TestShouldMask(t *testing.T) {
assert.Equal(t, val, test.shouldMask) assert.Equal(t, val, test.shouldMask)
} }
} }
func TestParseDevice(t *testing.T) {
tests := []struct {
device string
src string
dst string
perm string
}{
{"/dev/foo", "/dev/foo", "/dev/foo", "rwm"},
{"/dev/foo:/dev/bar", "/dev/foo", "/dev/bar", "rwm"},
{"/dev/foo:/dev/bar:rw", "/dev/foo", "/dev/bar", "rw"},
{"/dev/foo:rw", "/dev/foo", "/dev/foo", "rw"},
{"/dev/foo::rw", "/dev/foo", "/dev/foo", "rw"},
}
for _, test := range tests {
src, dst, perm, err := ParseDevice(test.device)
assert.NoError(t, err)
assert.Equal(t, src, test.src)
assert.Equal(t, dst, test.dst)
assert.Equal(t, perm, test.perm)
}
}