From 9479d04779ccb7fc44b972cde23cb9a6c052f445 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Wed, 26 Jan 2022 17:25:18 +0000 Subject: [PATCH] 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 --- pkg/git/libgit2/transport.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/git/libgit2/transport.go b/pkg/git/libgit2/transport.go index ab36130b6..d8d120a24 100644 --- a/pkg/git/libgit2/transport.go +++ b/pkg/git/libgit2/transport.go @@ -185,16 +185,21 @@ func knownHostsCallback(host string, knownHosts []byte) git2go.CertificateCheckC // First, attempt to split the configured host and port to validate // the port-less hostname given to the callback. - h, _, err := net.SplitHostPort(host) + hostWithoutPort, _, err := net.SplitHostPort(host) if err != nil { // SplitHostPort returns an error if the host is missing // a port, assume the host has no port. - h = host + hostWithoutPort = host } - // Check if the configured host matches the hostname given to - // the callback. - if h != hostname { + // Different versions of libgit handle this differently. + // This fixes the case in which ports may be sent back. + hostnameWithoutPort, _, err := net.SplitHostPort(hostname) + if err != nil { + hostnameWithoutPort = hostname + } + + if hostnameWithoutPort != hostWithoutPort { return git2go.ErrorCodeUser } @@ -202,7 +207,7 @@ func knownHostsCallback(host string, knownHosts []byte) git2go.CertificateCheckC // given to the callback match. Use the configured host (that // includes the port), and normalize it, so we can check if there // is an entry for the hostname _and_ port. - h = knownhosts.Normalize(host) + h := knownhosts.Normalize(host) for _, k := range kh { if k.matches(h, cert.Hostkey) { return git2go.ErrorCodeOK