This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sanitize_windows.go
74 lines (63 loc) · 2.04 KB
/
sanitize_windows.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package tar
import (
"fmt"
"path/filepath"
"strings"
)
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
var reservedNames = [...]string{"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
const reservedCharsStr = `[<>:"\|?*]` + "\x00" //NOTE: `/` is not included as it is our standard path separator
func isNullDevice(path string) bool {
// This is a case insensitive comparison to NUL
if len(path) != 3 {
return false
}
if path[0]|0x20 != 'n' {
return false
}
if path[1]|0x20 != 'u' {
return false
}
if path[2]|0x20 != 'l' {
return false
}
return true
}
// validatePathComponent returns an error if the given path component is not allowed on the platform
func validatePathComponent(c string) error {
//MSDN: Do not end a file or directory name with a space or a period
if strings.HasSuffix(c, ".") {
return fmt.Errorf("invalid platform path: path components cannot end with '.' : %q", c)
}
if strings.HasSuffix(c, " ") {
return fmt.Errorf("invalid platform path: path components cannot end with ' ' : %q", c)
}
if c == ".." {
return fmt.Errorf("invalid platform path: path component cannot be '..'")
}
// error on reserved characters
if strings.ContainsAny(c, reservedCharsStr) {
return fmt.Errorf("invalid platform path: path components cannot contain any of %s : %q", reservedCharsStr, c)
}
// error on reserved names
for _, rn := range reservedNames {
if c == rn {
return fmt.Errorf("invalid platform path: path component is a reserved name: %s", c)
}
}
return nil
}
func validatePlatformPath(platformPath string) error {
// remove the volume name
p := platformPath[len(filepath.VolumeName(platformPath)):]
// convert to cleaned slash-path
p = filepath.ToSlash(p)
p = strings.Trim(p, "/")
// make sure all components of the path are valid
for _, e := range strings.Split(p, "/") {
if err := validatePathComponent(e); err != nil {
return err
}
}
return nil
}