File tree 2 files changed +14
-10
lines changed
2 files changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -26,23 +26,28 @@ import (
26
26
)
27
27
28
28
var sanitizePathRegexp = regexp .MustCompile ("[#%&{}<>*\\ $:!'\" +@\x60 |=]" )
29
- var slashConsolidateRegexp = regexp .MustCompile ("/{2,}" )
30
29
31
30
// SanitizePath removes certain problematic characters from path names
32
- func SanitizePath (path string ) string {
31
+ func SanitizePath (str string ) string {
33
32
// Unicode normalization
34
- path = norm .NFKC .String (path )
33
+ str = norm .NFKC .String (str )
35
34
36
35
// Replace all back slashes with a forward slash
37
- path = strings .ReplaceAll (path , "\\ " , "/" )
36
+ str = strings .ReplaceAll (str , "\\ " , "/" )
38
37
39
- // Sanitize the string
40
- path = sanitizePathRegexp .ReplaceAllString (path , "" )
38
+ // Remove invalid characters
39
+ str = sanitizePathRegexp .ReplaceAllString (str , "" )
41
40
42
- // Replace all consecutive slashes with a single one
43
- path = slashConsolidateRegexp .ReplaceAllString (path , "/" )
41
+ // Clean the path
42
+ str = path .Clean (str )
43
+ if str == "/" || str == "." {
44
+ return ""
45
+ }
46
+
47
+ // Trim the ending slash if present
48
+ str = strings .TrimSuffix (str , "/" )
44
49
45
- return path
50
+ return str
46
51
}
47
52
48
53
var mimeTypeRegex = regexp .MustCompile ("^(application|audio|font|image|model|text|video)\\ /([a-z0-9-+*.]+)" )
Original file line number Diff line number Diff line change @@ -45,7 +45,6 @@ func TestSanitizePath(t *testing.T) {
45
45
{"foo\\ bar" , "foo/bar" },
46
46
{"foo\\ bar\\ 2" , "foo/bar/2" },
47
47
// Replace multiple slashes with a single one
48
- {"//" , "/" },
49
48
{"//aa" , "/aa" },
50
49
{"hello////world" , "hello/world" },
51
50
{"hello////world//aa" , "hello/world/aa" },
You can’t perform that action at this time.
0 commit comments