-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate_filename.go
32 lines (28 loc) · 950 Bytes
/
validate_filename.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
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/spf13/viper"
)
func validateUploadFilename(cleanedPath string) error {
forbiddenNames := viper.GetStringSlice("upload.forbiddenNames")
forbiddenPrefixes := viper.GetStringSlice("upload.forbiddenPrefixes")
extension := filepath.Ext(strings.ToLower(cleanedPath))
if extension == "._infocache" || extension == "._infolock" {
return fmt.Errorf("forbidden filename extension (._infocache)")
}
for _, n := range forbiddenNames {
lowercase := strings.ToLower(cleanedPath)
if strings.ToLower(n) == lowercase || strings.ToLower("/"+n) == lowercase {
return fmt.Errorf("forbidden filename")
}
}
for _, n := range forbiddenPrefixes {
lowercase := strings.ToLower(cleanedPath)
if strings.HasPrefix(lowercase, strings.ToLower(n)) || strings.HasPrefix(lowercase, strings.ToLower("/"+n)) {
return fmt.Errorf("forbidden filename prefix: %v", n)
}
}
return nil
}