-
Notifications
You must be signed in to change notification settings - Fork 568
/
paths.go
37 lines (32 loc) · 894 Bytes
/
paths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package storage
import (
"regexp"
"strings"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
)
var (
validRangeRegex = regexp.MustCompile("^[ -~]+$")
globRegex = regexp.MustCompile(`[*?[\]{}!()@+^]`)
)
func ValidateFilename(p string) error {
pBytes := []byte(p)
if !validRangeRegex.Match(pBytes) {
return errors.Errorf("path (%v) invalid: only printable ASCII characters allowed", p)
}
if globRegex.Match(pBytes) {
return errors.Errorf("path (%v) invalid: globbing character (%v) not allowed in path", p, globRegex.FindString(p))
}
for _, elem := range strings.Split(p, "/") {
if elem == "." || elem == ".." {
return errors.Errorf("path (%v) invalid: relative file paths are not allowed", p)
}
}
return nil
}
func GlobLiteralPrefix(glob string) string {
idx := globRegex.FindStringIndex(glob)
if idx == nil {
return glob
}
return glob[:idx[0]]
}