Skip to content

Commit 9479d04

Browse files
author
Paulo Gomes
committed
Fix host mismatch in libgit2
Depending on libgit2 version or from its dependencies, the hostname may or may not contain ports Signed-off-by: Paulo Gomes <[email protected]>
1 parent 4aad174 commit 9479d04

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

pkg/git/libgit2/transport.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,29 @@ func knownHostsCallback(host string, knownHosts []byte) git2go.CertificateCheckC
185185

186186
// First, attempt to split the configured host and port to validate
187187
// the port-less hostname given to the callback.
188-
h, _, err := net.SplitHostPort(host)
188+
hostWithoutPort, _, err := net.SplitHostPort(host)
189189
if err != nil {
190190
// SplitHostPort returns an error if the host is missing
191191
// a port, assume the host has no port.
192-
h = host
192+
hostWithoutPort = host
193193
}
194194

195-
// Check if the configured host matches the hostname given to
196-
// the callback.
197-
if h != hostname {
195+
// Different versions of libgit handle this differently.
196+
// This fixes the case in which ports may be sent back.
197+
hostnameWithoutPort, _, err := net.SplitHostPort(hostname)
198+
if err != nil {
199+
hostnameWithoutPort = hostname
200+
}
201+
202+
if hostnameWithoutPort != hostWithoutPort {
198203
return git2go.ErrorCodeUser
199204
}
200205

201206
// We are now certain that the configured host and the hostname
202207
// given to the callback match. Use the configured host (that
203208
// includes the port), and normalize it, so we can check if there
204209
// is an entry for the hostname _and_ port.
205-
h = knownhosts.Normalize(host)
210+
h := knownhosts.Normalize(host)
206211
for _, k := range kh {
207212
if k.matches(h, cert.Hostkey) {
208213
return git2go.ErrorCodeOK

0 commit comments

Comments
 (0)