-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate_test.go
More file actions
139 lines (128 loc) · 3.2 KB
/
Copy pathvalidate_test.go
File metadata and controls
139 lines (128 loc) · 3.2 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package clone
import (
"strings"
"testing"
)
func TestValidateURL(t *testing.T) {
for _, url := range []string{
"https://github.com/git-pkgs/clone",
"https://gitlab.com/example/repo.git",
"https://token@github.com/private/repo",
} {
if err := ValidateURL(url); err != nil {
t.Errorf("ValidateURL(%q): %v", url, err)
}
}
for _, url := range []string{
"http://github.com/example/repo",
"git@github.com:example/repo.git",
"ssh://git@example.com/repo",
"file:///etc/passwd",
"--upload-pack=/bin/sh",
"https://", // no host
"https://\nhost/x", // control byte
"https://host/x\x00", // NUL
"",
} {
if err := ValidateURL(url); err == nil {
t.Errorf("ValidateURL(%q) succeeded", url)
}
if err := ValidateURL(url); err != nil && strings.Contains(err.Error(), "token") {
t.Errorf("ValidateURL(%q) error leaks credential: %v", url, err)
}
}
}
func TestRedactURL(t *testing.T) {
cases := []struct{ in, want string }{
{"https://github.com/x/y", "https://github.com/x/y"},
{"https://token@github.com/x/y", "https://REDACTED@github.com/x/y"},
{"https://user:pass@host/p", "https://REDACTED@host/p"},
{"not a url at all", "not a url at all"},
}
for _, c := range cases {
if got := RedactURL(c.in); got != c.want {
t.Errorf("RedactURL(%q) = %q, want %q", c.in, got, c.want)
}
}
// UnreachableError.Error must not leak the credential.
e := &UnreachableError{URL: "https://secret@host/repo", Err: errFixture}
if strings.Contains(e.Error(), "secret") {
t.Errorf("UnreachableError.Error() leaked credential: %q", e.Error())
}
}
var errFixture = &fixtureErr{}
type fixtureErr struct{}
func (*fixtureErr) Error() string { return "boom" }
func TestValidateRef(t *testing.T) {
for _, ref := range []string{
"",
"main",
"release/1.0",
"v1.2.3",
"feature/abc_def-1",
} {
if err := ValidateRef(ref); err != nil {
t.Errorf("ValidateRef(%q): %v", ref, err)
}
}
for _, ref := range []string{
"--all",
"foo..bar",
"../etc/passwd",
"branch with space",
"branch;rm",
"branch\nmain",
"head@{0}",
"refs/heads/main^",
"branch~1",
} {
if err := ValidateRef(ref); err == nil {
t.Errorf("ValidateRef(%q) succeeded", ref)
}
}
}
func TestValidCommit(t *testing.T) {
cases := []struct {
sha string
want bool
}{
{"abcd", true},
{strings.Repeat("a", 40), true},
{strings.Repeat("f", 64), true},
{"abc", false},
{strings.Repeat("a", 65), false},
{"ABCDEF12", false},
{"abcg", false},
{"", false},
}
for _, test := range cases {
if got := ValidCommit(test.sha); got != test.want {
t.Errorf("ValidCommit(%q) = %v, want %v", test.sha, got, test.want)
}
}
}
func TestSanitizePath(t *testing.T) {
cases := []struct {
input string
want string
ok bool
}{
{"lib/x.rb", "lib/x.rb", true},
{"./a.go", "a.go", true},
{"a/./b", "a/b", true},
{"", "", false},
{"../etc/passwd", "", false},
{"a/../b", "", false},
{"/etc/passwd", "", false},
{"a\x00b", "", false},
{"..", "", false},
{".", "", false},
}
for _, test := range cases {
got, ok := SanitizePath(test.input)
if got != test.want || ok != test.ok {
t.Errorf("SanitizePath(%q) = (%q, %v), want (%q, %v)",
test.input, got, ok, test.want, test.ok)
}
}
}