Skip to content

Commit cd24506

Browse files
authored
Merge pull request #475 from darkowlzz/libgit2-parseKnownHosts-fix
2 parents 5ccb914 + 10d2620 commit cd24506

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

pkg/git/libgit2/transport.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"crypto/x509"
2626
"fmt"
2727
"hash"
28+
"io"
2829
"net"
2930
"strings"
3031
"time"
@@ -168,6 +169,11 @@ func parseKnownHosts(s string) ([]knownKey, error) {
168169
for scanner.Scan() {
169170
_, hosts, pubKey, _, _, err := ssh.ParseKnownHosts(scanner.Bytes())
170171
if err != nil {
172+
// Lines that aren't host public key result in EOF, like a comment
173+
// line. Continue parsing the other lines.
174+
if err == io.EOF {
175+
continue
176+
}
171177
return []knownKey{}, err
172178
}
173179

pkg/git/libgit2/transport_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func Test_knownHostsCallback(t *testing.T) {
251251
}
252252
}
253253

254-
func Test_parseKnownHosts(t *testing.T) {
254+
func Test_parseKnownHosts_matches(t *testing.T) {
255255
tests := []struct {
256256
name string
257257
hostkey git2go.HostkeyCertificate
@@ -280,6 +280,72 @@ func Test_parseKnownHosts(t *testing.T) {
280280
}
281281
}
282282

283+
func Test_parseKnownHosts(t *testing.T) {
284+
tests := []struct {
285+
name string
286+
fixture string
287+
wantErr bool
288+
}{
289+
{
290+
name: "empty file",
291+
fixture: "",
292+
wantErr: false,
293+
},
294+
{
295+
name: "single host",
296+
fixture: `github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==`,
297+
wantErr: false,
298+
},
299+
{
300+
name: "single host with comment",
301+
fixture: `# github.com
302+
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==`,
303+
wantErr: false,
304+
},
305+
{
306+
name: "multiple hosts with comments",
307+
fixture: `# github.com
308+
github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
309+
# gitlab.com
310+
gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf`,
311+
},
312+
{
313+
name: "no host key, only comments",
314+
fixture: `# example.com
315+
#github.com
316+
# gitlab.com`,
317+
wantErr: false,
318+
},
319+
{
320+
name: "invalid host entry",
321+
fixture: `github.com ssh-rsa`,
322+
wantErr: true,
323+
},
324+
{
325+
name: "invalid content",
326+
fixture: `some random text`,
327+
wantErr: true,
328+
},
329+
{
330+
name: "invalid line with valid host key",
331+
fixture: `some random text
332+
gitlab.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf`,
333+
wantErr: true,
334+
},
335+
}
336+
for _, tt := range tests {
337+
t.Run(tt.name, func(t *testing.T) {
338+
g := NewWithT(t)
339+
_, err := parseKnownHosts(tt.fixture)
340+
if tt.wantErr {
341+
g.Expect(err).To(HaveOccurred())
342+
} else {
343+
g.Expect(err).ToNot(HaveOccurred())
344+
}
345+
})
346+
}
347+
}
348+
283349
func md5Fingerprint(in string) [16]byte {
284350
var out [16]byte
285351
copy(out[:], in)

0 commit comments

Comments
 (0)