Skip to content

Commit

Permalink
libgit2: change knownHostsCallback logic
Browse files Browse the repository at this point in the history
With Go backing the transport, the provided hostname to the callback
now does include the port, and we no longer need to take the `Host`
into account (as this will now result in a mismatch).

Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Oct 25, 2021
1 parent 12e0363 commit b0c28e5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 14 deletions.
16 changes: 3 additions & 13 deletions pkg/git/libgit2/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"crypto/x509"
"fmt"
"hash"
"net"
"strings"
"time"

Expand Down Expand Up @@ -128,26 +127,17 @@ func knownHostsCallback(host string, knownHosts []byte) git2go.CertificateCheckC
return fmt.Errorf("failed to parse known_hosts: %w", err)
}

// First, attempt to split the configured host and port to validate
// the port-less hostname given to the callback.
h, _, 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
}

// Check if the configured host matches the hostname given to
// the callback.
if h != hostname {
return fmt.Errorf("hostname from server '%s' does not match '%s'", hostname, h)
if host != hostname {
return fmt.Errorf("hostname from server '%s' does not match '%s'", hostname, host)
}

// We are now certain that the configured host and the hostname
// 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(hostname)
for _, k := range kh {
if k.matches(h, cert.Hostkey) {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/libgit2/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func Test_knownHostsCallback(t *testing.T) {
},
{
name: "Match with port",
host: "github.com",
host: "github.com:22",
knownHosts: []byte(knownHostsFixture),
hostkey: git2go.HostkeyCertificate{Kind: git2go.HostkeySHA1 | git2go.HostkeyMD5, HashSHA1: sha1Fingerprint("v2toJdKXfFEaR1u++4iq1UqSrHM")},
expectedHost: "github.com:22",
Expand Down

0 comments on commit b0c28e5

Please sign in to comment.