github.com/cs3org/reva/v2@v2.27.7/internal/http/services/owncloud/ocdav/validation.go (about) 1 package ocdav 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/config" 9 ) 10 11 // Validator validates strings 12 type Validator func(string) error 13 14 // ValidatorsFromConfig returns the configured Validators 15 func ValidatorsFromConfig(c *config.Config) []Validator { 16 // we always want to exclude empty names 17 vals := []Validator{notEmpty()} 18 19 // forbidden characters 20 vals = append(vals, doesNotContain(c.NameValidation.InvalidChars)) 21 22 // max length 23 vals = append(vals, isShorterThan(c.NameValidation.MaxLength)) 24 25 return vals 26 } 27 28 // ValidateName will validate a file or folder name, returning an error when it is not accepted 29 func ValidateName(name string, validators []Validator) error { 30 return ValidateDestination(name, append(validators, notReserved())) 31 } 32 33 // ValidateDestination will validate a file or folder destination name (which can be . or ..), returning an error when it is not accepted 34 func ValidateDestination(name string, validators []Validator) error { 35 for _, v := range validators { 36 if err := v(name); err != nil { 37 return fmt.Errorf("name validation failed: %w", err) 38 } 39 } 40 return nil 41 } 42 43 func notReserved() Validator { 44 return func(s string) error { 45 if s == ".." || s == "." { 46 return errors.New(". and .. are reserved names") 47 } 48 return nil 49 } 50 } 51 52 func notEmpty() Validator { 53 return func(s string) error { 54 if strings.TrimSpace(s) == "" { 55 return errors.New("must not be empty") 56 } 57 return nil 58 } 59 } 60 61 func doesNotContain(bad []string) Validator { 62 return func(s string) error { 63 for _, b := range bad { 64 if strings.Contains(s, b) { 65 return fmt.Errorf("must not contain %s", b) 66 } 67 } 68 return nil 69 } 70 } 71 72 func isShorterThan(maxLength int) Validator { 73 return func(s string) error { 74 if len(s) > maxLength { 75 return fmt.Errorf("must be shorter than %d", maxLength) 76 } 77 return nil 78 } 79 }