From 71b0e47bee6674f2571303a3663f645368479c75 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 26 Jul 2021 12:59:19 +0200 Subject: [PATCH] volume/mounts: add destination validation to "validators" This way, there's no need to pass down the regular expression, and the validation is "just another" validator (which we already pass). Signed-off-by: Sebastiaan van Stijn --- volume/mounts/lcow_parser.go | 11 +++++++---- volume/mounts/windows_parser.go | 34 ++++++++++++++++----------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/volume/mounts/lcow_parser.go b/volume/mounts/lcow_parser.go index 260c5ed270..c572349c8a 100644 --- a/volume/mounts/lcow_parser.go +++ b/volume/mounts/lcow_parser.go @@ -30,13 +30,16 @@ var ( lcowSplitRawSpec = regexp.MustCompile(`^` + rxSource + rxLCOWDestination + rxMode + `$`) ) -var lcowSpecificValidators mountValidator = func(m *mount.Mount) error { +var lcowValidators mountValidator = func(m *mount.Mount) error { if path.Clean(m.Target) == "/" { return ErrVolumeTargetIsRoot } if m.Type == mount.TypeNamedPipe { return errors.New("Linux containers on Windows do not support named pipe mounts") } + if err := windowsValidateRegex(m.Target, lcowMountDestinationRegex); err != nil { + return err + } return nil } @@ -45,7 +48,7 @@ type lcowParser struct { } func (p *lcowParser) ValidateMountConfig(mnt *mount.Mount) error { - return p.validateMountConfigReg(mnt, lcowMountDestinationRegex, lcowSpecificValidators) + return p.validateMountConfigReg(mnt, lcowValidators) } func (p *lcowParser) ParseMountRaw(raw, volumeDriver string) (*MountPoint, error) { @@ -53,9 +56,9 @@ func (p *lcowParser) ParseMountRaw(raw, volumeDriver string) (*MountPoint, error if err != nil { return nil, err } - return p.parseMount(arr, raw, volumeDriver, lcowMountDestinationRegex, false, lcowSpecificValidators) + return p.parseMount(arr, raw, volumeDriver, false, lcowValidators) } func (p *lcowParser) ParseMountSpec(cfg mount.Mount) (*MountPoint, error) { - return p.parseMountSpec(cfg, lcowMountDestinationRegex, false, lcowSpecificValidators) + return p.parseMountSpec(cfg, false, lcowValidators) } diff --git a/volume/mounts/windows_parser.go b/volume/mounts/windows_parser.go index 6be9ae9be9..a3157105f8 100644 --- a/volume/mounts/windows_parser.go +++ b/volume/mounts/windows_parser.go @@ -151,8 +151,11 @@ func windowsValidateNotRoot(p string) error { return nil } -var windowsSpecificValidators mountValidator = func(mnt *mount.Mount) error { - return windowsValidateNotRoot(mnt.Target) +var windowsValidators mountValidator = func(m *mount.Mount) error { + if err := windowsValidateNotRoot(m.Target); err != nil { + return err + } + return windowsValidateRegex(m.Target, mountDestinationRegexp) } func windowsValidateRegex(p string, r *regexp.Regexp) error { @@ -193,7 +196,7 @@ func (p *windowsParser) ValidateVolumeName(name string) error { return nil } func (p *windowsParser) ValidateMountConfig(mnt *mount.Mount) error { - return p.validateMountConfigReg(mnt, mountDestinationRegexp, windowsSpecificValidators) + return p.validateMountConfigReg(mnt, windowsValidators) } type fileInfoProvider interface { @@ -214,19 +217,15 @@ func (defaultFileInfoProvider) fileInfo(path string) (exist, isDir bool, err err return true, fi.IsDir(), nil } -func (p *windowsParser) validateMountConfigReg(mnt *mount.Mount, destRegex *regexp.Regexp, additionalValidators ...mountValidator) error { +func (p *windowsParser) validateMountConfigReg(mnt *mount.Mount, additionalValidators ...mountValidator) error { + if len(mnt.Target) == 0 { + return &errMountConfig{mnt, errMissingField("Target")} + } for _, v := range additionalValidators { if err := v(mnt); err != nil { return &errMountConfig{mnt, err} } } - if len(mnt.Target) == 0 { - return &errMountConfig{mnt, errMissingField("Target")} - } - - if err := windowsValidateRegex(mnt.Target, destRegex); err != nil { - return &errMountConfig{mnt, err} - } switch mnt.Type { case mount.TypeBind: @@ -303,10 +302,10 @@ func (p *windowsParser) ParseMountRaw(raw, volumeDriver string) (*MountPoint, er if err != nil { return nil, err } - return p.parseMount(arr, raw, volumeDriver, mountDestinationRegexp, true, windowsSpecificValidators) + return p.parseMount(arr, raw, volumeDriver, true, windowsValidators) } -func (p *windowsParser) parseMount(arr []string, raw, volumeDriver string, destRegex *regexp.Regexp, convertTargetToBackslash bool, additionalValidators ...mountValidator) (*MountPoint, error) { +func (p *windowsParser) parseMount(arr []string, raw, volumeDriver string, convertTargetToBackslash bool, additionalValidators ...mountValidator) (*MountPoint, error) { var spec mount.Mount var mode string switch len(arr) { @@ -355,7 +354,7 @@ func (p *windowsParser) parseMount(arr []string, raw, volumeDriver string, destR spec.VolumeOptions.NoCopy = !copyData } - mp, err := p.parseMountSpec(spec, destRegex, convertTargetToBackslash, additionalValidators...) + mp, err := p.parseMountSpec(spec, convertTargetToBackslash, additionalValidators...) if mp != nil { mp.Mode = mode } @@ -366,10 +365,11 @@ func (p *windowsParser) parseMount(arr []string, raw, volumeDriver string, destR } func (p *windowsParser) ParseMountSpec(cfg mount.Mount) (*MountPoint, error) { - return p.parseMountSpec(cfg, mountDestinationRegexp, true, windowsSpecificValidators) + return p.parseMountSpec(cfg, true, windowsValidators) } -func (p *windowsParser) parseMountSpec(cfg mount.Mount, destRegex *regexp.Regexp, convertTargetToBackslash bool, additionalValidators ...mountValidator) (*MountPoint, error) { - if err := p.validateMountConfigReg(&cfg, destRegex, additionalValidators...); err != nil { + +func (p *windowsParser) parseMountSpec(cfg mount.Mount, convertTargetToBackslash bool, additionalValidators ...mountValidator) (*MountPoint, error) { + if err := p.validateMountConfigReg(&cfg, additionalValidators...); err != nil { return nil, err } mp := &MountPoint{