1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-07-26 12:59:19 +02:00
parent cff0276aaf
commit 71b0e47bee
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 24 additions and 21 deletions

View file

@ -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)
}

View file

@ -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{