-
Notifications
You must be signed in to change notification settings - Fork 5
/
main_test.go
145 lines (137 loc) · 3.41 KB
/
main_test.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
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
140
141
142
143
144
145
package main
import (
"fmt"
"net/url"
"testing"
)
func TestIsRewriteAllowed(t *testing.T) {
var cases = []struct {
input []string
expected bool
}{
{
input: []string{"clone", "[email protected]:org/repo"},
expected: true,
},
{
input: []string{"fetch", ""},
expected: true,
},
{
input: []string{"ls-remote", ""},
expected: true,
},
{
input: []string{"--work-tree=/work", "clone"},
expected: true,
},
{
input: []string{"config", "--global", "url.\"https://github.com/\".insteadOf", "[email protected]:"},
expected: false,
},
}
for _, test := range cases {
t.Run(fmt.Sprintln(test.input), func(t *testing.T) {
if v := IsRewriteAllowed(test.input); v != test.expected {
t.Errorf("Input: %v\tExpected: %v\tGot: %v\n", test.input, test.expected, v)
}
})
}
}
func TestScrub(t *testing.T) {
var cases = []struct {
input string
expected string
}{
{
input: "git://github.com/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "[email protected]:dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "git+ssh://[email protected]/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "git+ssh://user:[email protected]/dependabot/git-https-shim",
expected: "https://user:[email protected]/dependabot/git-https-shim",
},
{
input: "ssh://user:[email protected]/dependabot/git-https-shim",
expected: "https://user:[email protected]/dependabot/git-https-shim",
},
{
input: "ssh://[email protected]/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "ssh://github.com/dependabot/git-https-shim",
expected: "https://github.com/dependabot/git-https-shim",
},
{
input: "file://github.com/dependabot/git-https-shim",
expected: "file://github.com/dependabot/git-https-shim",
},
{
input: "HEAD~1",
expected: "HEAD~1",
},
}
for _, test := range cases {
t.Run(fmt.Sprintln(test.input), func(t *testing.T) {
if v := Scrub(test.input); v != test.expected {
t.Errorf("Input: %v\tExpected: %v\tGot: %v\n", test.input, test.expected, v)
}
})
}
}
func TestFindGit(t *testing.T) {
t.Run("finds the second git", func(t *testing.T) {
if v := FindGit("test/bin1:test/bin2:test/bin3"); v != "test/bin2/git" {
t.Errorf(v)
}
})
t.Run("finds nothing", func(t *testing.T) {
if v := FindGit("test/bin1"); v != "" {
t.Errorf(v)
}
})
}
func FuzzScrub(f *testing.F) {
testcases := []string{
"https://github.com/org/repo",
"ssh://[email protected]/org/repo",
"[email protected]:org/repo",
"git://github.com/repo",
"HEAD~1",
"+/refs/HEAD",
}
for _, tc := range testcases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, orig string) {
result := Scrub(orig)
if result != orig {
if extractHost(orig, true) != extractHost(result, false) {
// transformed a nonURL into a URL or changed what the URL was, which could be an attack
t.Errorf("Before: %q (%q), after: %q (%q)", orig, extractHost(orig, true), result, extractHost(result, false))
}
}
})
}
func extractHost(input string, orig bool) string {
u, err := url.ParseRequestURI(input)
if err == nil {
return u.Hostname()
}
if !orig {
return ""
}
if scpUrl.MatchString(input) {
return scpUrl.FindStringSubmatch(input)[2]
}
return ""
}