blob: 017641b868f5924c9eea1471e028a51a5e72fbbc (
about) (
plain) (
blame)
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
|
package name
import "strings"
// IsSafe reports whether name is a safe name
// for direct filesystem operations.
func IsSafe(name string) bool {
rest, ok := strings.CutPrefix(name, "refs/")
if ok {
if rest == "" || rest[0] == '/' || rest[len(rest)-1] == '/' {
return false
}
normalized, normOK := normalizeRefPath(rest)
return normOK && normalized == rest
}
if name == "" {
return false
}
for i := range len(name) {
ch := name[i]
if (ch < 'A' || ch > 'Z') && ch != '_' {
return false
}
}
return true
}
func normalizeRefPath(path string) (string, bool) {
components := make([]string, 0, strings.Count(path, "/")+1)
i := 0
for i < len(path) {
for i < len(path) && path[i] == '/' {
i++
}
if i == len(path) {
break
}
j := i
for j < len(path) && path[j] != '/' {
j++
}
component := path[i:j]
switch component {
case ".":
case "..":
if len(components) == 0 {
return "", false
}
components = components[:len(components)-1]
default:
components = append(components, component)
}
i = j
}
return strings.Join(components, "/"), true
}
|