Debian Codegraph @

plumbing/transport/ssh/common_test.go

raw
1 package ssh
2
3 import (
4 "testing"
5
6 "github.com/go-git/go-git/v5/plumbing/transport"
7
8 "github.com/gliderlabs/ssh"
9 "github.com/kevinburke/ssh_config"
10 "github.com/stretchr/testify/assert"
11 stdssh "golang.org/x/crypto/ssh"
12 "golang.org/x/crypto/ssh/testdata"
13 . "gopkg.in/check.v1"
14 )
15
16 func Test(t *testing.T) { TestingT(t) }
17
18 func (s *SuiteCommon) TestOverrideConfig(c *C) {
19 config := &stdssh.ClientConfig{
20 User: "foo",
21 Auth: []stdssh.AuthMethod{
22 stdssh.Password("yourpassword"),
23 },
24 HostKeyCallback: stdssh.FixedHostKey(nil),
25 }
26
27 target := &stdssh.ClientConfig{}
28 overrideConfig(config, target)
29
30 c.Assert(target.User, Equals, "foo")
31 c.Assert(target.Auth, HasLen, 1)
32 c.Assert(target.HostKeyCallback, NotNil)
33 }
34
35 func (s *SuiteCommon) TestOverrideConfigKeep(c *C) {
36 config := &stdssh.ClientConfig{
37 User: "foo",
38 }
39
40 target := &stdssh.ClientConfig{
41 User: "bar",
42 }
43
44 overrideConfig(config, target)
45 c.Assert(target.User, Equals, "foo")
46 }
47
48 func (s *SuiteCommon) TestDefaultSSHConfig(c *C) {
49 defer func() {
50 DefaultSSHConfig = ssh_config.DefaultUserSettings
51 }()
52
53 DefaultSSHConfig = &mockSSHConfig{map[string]map[string]string{
54 "github.com": {
55 "Hostname": "foo.local",
56 "Port": "42",
57 },
58 }}
59
60 ep, err := transport.NewEndpoint("[email protected]:foo/bar.git")
61 c.Assert(err, IsNil)
62
63 cmd := &command{endpoint: ep}
64 c.Assert(cmd.getHostWithPort(), Equals, "foo.local:42")
65 }
66
67 func (s *SuiteCommon) TestDefaultSSHConfigNil(c *C) {
68 defer func() {
69 DefaultSSHConfig = ssh_config.DefaultUserSettings
70 }()
71
72 DefaultSSHConfig = nil
73
74 ep, err := transport.NewEndpoint("[email protected]:foo/bar.git")
75 c.Assert(err, IsNil)
76
77 cmd := &command{endpoint: ep}
78 c.Assert(cmd.getHostWithPort(), Equals, "github.com:22")
79 }
80
81 func (s *SuiteCommon) TestDefaultSSHConfigWildcard(c *C) {
82 defer func() {
83 DefaultSSHConfig = ssh_config.DefaultUserSettings
84 }()
85
86 DefaultSSHConfig = &mockSSHConfig{Values: map[string]map[string]string{
87 "*": {
88 "Port": "42",
89 },
90 }}
91
92 ep, err := transport.NewEndpoint("[email protected]:foo/bar.git")
93 c.Assert(err, IsNil)
94
95 cmd := &command{endpoint: ep}
96 c.Assert(cmd.getHostWithPort(), Equals, "github.com:22")
97 }
98
99 func (s *SuiteCommon) TestIgnoreHostKeyCallback(c *C) {
100 uploadPack := &UploadPackSuite{
101 opts: []ssh.Option{
102 ssh.HostKeyPEM(testdata.PEMBytes["ed25519"]),
103 },
104 }
105 uploadPack.SetUpSuite(c)
106 // Use the default client, which does not have a host key callback
107 uploadPack.Client = DefaultClient
108 auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
109 c.Assert(err, IsNil)
110 c.Assert(auth, NotNil)
111 auth.HostKeyCallback = stdssh.InsecureIgnoreHostKey()
112 ep := uploadPack.newEndpoint(c, "bar.git")
113 ps, err := uploadPack.Client.NewUploadPackSession(ep, auth)
114 c.Assert(err, IsNil)
115 c.Assert(ps, NotNil)
116 }
117
118 func (s *SuiteCommon) TestFixedHostKeyCallback(c *C) {
119 hostKey, err := stdssh.ParsePrivateKey(testdata.PEMBytes["ed25519"])
120 c.Assert(err, IsNil)
121 uploadPack := &UploadPackSuite{
122 opts: []ssh.Option{
123 ssh.HostKeyPEM(testdata.PEMBytes["ed25519"]),
124 },
125 }
126 uploadPack.SetUpSuite(c)
127 // Use the default client, which does not have a host key callback
128 uploadPack.Client = DefaultClient
129 auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
130 c.Assert(err, IsNil)
131 c.Assert(auth, NotNil)
132 auth.HostKeyCallback = stdssh.FixedHostKey(hostKey.PublicKey())
133 auth.HostKeyAlgorithms = []string{"ssh-ed25519"}
134 ep := uploadPack.newEndpoint(c, "bar.git")
135 ps, err := uploadPack.Client.NewUploadPackSession(ep, auth)
136 c.Assert(err, IsNil)
137 c.Assert(ps, NotNil)
138 }
139
140 func (s *SuiteCommon) TestFixedHostKeyCallbackUnexpectedAlgorithm(c *C) {
141 hostKey, err := stdssh.ParsePrivateKey(testdata.PEMBytes["ed25519"])
142 c.Assert(err, IsNil)
143 uploadPack := &UploadPackSuite{
144 opts: []ssh.Option{
145 ssh.HostKeyPEM(testdata.PEMBytes["rsa"]),
146 },
147 }
148 uploadPack.SetUpSuite(c)
149 // Use the default client, which does not have a host key callback
150 uploadPack.Client = DefaultClient
151 auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
152 c.Assert(err, IsNil)
153 c.Assert(auth, NotNil)
154 auth.HostKeyCallback = stdssh.FixedHostKey(hostKey.PublicKey())
155 auth.HostKeyAlgorithms = []string{"ssh-ed25519"}
156 ep := uploadPack.newEndpoint(c, "bar.git")
157 ps, err := uploadPack.Client.NewUploadPackSession(ep, auth)
158 c.Assert(err, NotNil)
159 c.Assert(ps, IsNil)
160 }
161
162 func (s *SuiteCommon) TestFailHostKeyCallback(c *C) {
163 uploadPack := &UploadPackSuite{
164 opts: []ssh.Option{
165 ssh.HostKeyPEM(testdata.PEMBytes["ed25519"]),
166 },
167 }
168 uploadPack.SetUpSuite(c)
169 // Use the default client, which does not have a host key callback
170 uploadPack.Client = DefaultClient
171 auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
172 c.Assert(err, IsNil)
173 c.Assert(auth, NotNil)
174 ep := uploadPack.newEndpoint(c, "bar.git")
175 _, err = uploadPack.Client.NewUploadPackSession(ep, auth)
176 c.Assert(err, NotNil)
177 }
178
179 func (s *SuiteCommon) TestIssue70(c *C) {
180 uploadPack := &UploadPackSuite{}
181 uploadPack.SetUpSuite(c)
182
183 config := &stdssh.ClientConfig{
184 HostKeyCallback: stdssh.InsecureIgnoreHostKey(),
185 }
186 r := &runner{
187 config: config,
188 }
189
190 cmd, err := r.Command("command", uploadPack.newEndpoint(c, "endpoint"), uploadPack.EmptyAuth)
191 c.Assert(err, IsNil)
192
193 c.Assert(cmd.(*command).client.Close(), IsNil)
194
195 err = cmd.Close()
196 c.Assert(err, IsNil)
197 }
198
199 /*
200 Given, an endpoint to a git server with a socks5 proxy URL,
201 When, the socks5 proxy server is not reachable,
202 Then, there should not be any panic and an error with appropriate message should be returned.
203 Related issue : https://github.com/go-git/go-git/pull/900
204 */
205 func (s *SuiteCommon) TestInvalidSocks5Proxy(c *C) {
206 ep, err := transport.NewEndpoint("[email protected]:foo/bar.git")
207 c.Assert(err, IsNil)
208 ep.Proxy.URL = "socks5://127.0.0.1:1080"
209
210 auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
211 c.Assert(err, IsNil)
212 c.Assert(auth, NotNil)
213
214 ps, err := DefaultClient.NewUploadPackSession(ep, auth)
215 //Since the proxy server is not running, we expect an error.
216 c.Assert(ps, IsNil)
217 c.Assert(err, NotNil)
218 c.Assert(err, ErrorMatches, "socks connect .* dial tcp 127.0.0.1:1080: .*")
219 }
220
221 type mockSSHConfig struct {
222 Values map[string]map[string]string
223 }
224
225 func (c *mockSSHConfig) Get(alias, key string) string {
226 a, ok := c.Values[alias]
227 if !ok {
228 return c.Values["*"][key]
229 }
230
231 return a[key]
232 }
233
234 type invalidAuthMethod struct {
235 }
236
237 func (a *invalidAuthMethod) Name() string {
238 return "invalid"
239 }
240
241 func (a *invalidAuthMethod) String() string {
242 return "invalid"
243 }
244
245 func (s *SuiteCommon) TestCommandWithInvalidAuthMethod(c *C) {
246 uploadPack := &UploadPackSuite{}
247 uploadPack.SetUpSuite(c)
248 r := &runner{}
249 auth := &invalidAuthMethod{}
250
251 _, err := r.Command("command", uploadPack.newEndpoint(c, "endpoint"), auth)
252
253 c.Assert(err, NotNil)
254 c.Assert(err, ErrorMatches, "invalid auth method")
255 }
256
257 func TestEndpointToCommand(t *testing.T) {
258 t.Parallel()
259
260 for _, tc := range []struct {
261 name string
262 cmd string
263 path string
264 want string
265 }{
266 {
267 name: "plain path",
268 cmd: "git-upload-pack",
269 path: "/repo.git",
270 want: "git-upload-pack '/repo.git'",
271 },
272 {
273 name: "path with single-quote injection payload",
274 cmd: "git-upload-pack",
275 path: "/repo.git'; touch /tmp/x ; #",
276 want: `git-upload-pack '/repo.git'\''; touch /tmp/x ; #'`,
277 },
278 {
279 name: "bang is escaped for csh history expansion",
280 cmd: "git-upload-pack",
281 path: "/repo!.git",
282 want: `git-upload-pack '/repo'\!'.git'`,
283 },
284 {
285 name: "mixed quote and bang",
286 cmd: "git-upload-pack",
287 path: "/a'b!c",
288 want: `git-upload-pack '/a'\''b'\!'c'`,
289 },
290 {
291 name: "inert shell metacharacters pass through",
292 cmd: "git-upload-pack",
293 path: "/a\\b\"c$d`e",
294 want: "git-upload-pack '/a\\b\"c$d`e'",
295 },
296 } {
297 t.Run(tc.name, func(t *testing.T) {
298 t.Parallel()
299 ep := &transport.Endpoint{Path: tc.path}
300 assert.Equal(t, tc.want, endpointToCommand(tc.cmd, ep))
301 })
302 }
303 }