Skip to content

Modern Docker Management, Designed for Everyone

fix: restrict ssh host key algorithms to known_hosts entries (#4403)

Kyle Mendell2ab83fe310d7
diff --git a/backend/pkg/gitutil/git.go b/backend/pkg/gitutil/git.go
index 5dc2dbba2..6d63716ba 100644
--- a/backend/pkg/gitutil/git.go
+++ b/backend/pkg/gitutil/git.go
@@ -15,6 +15,7 @@ import (
"path/filepath"
"regexp"
"slices"
+ "strconv"
"strings"
"time"
@@ -149,6 +150,20 @@ func (c *Client) getAuth(ctx context.Context, url string, localConfig AuthConfig
publicKeys.HostKeyCallbackHelper = ssh.HostKeyCallbackHelper{
HostKeyCallback: hostKeyCallback,
}
+ // go-git skips algorithm negotiation for custom callbacks, so a server offering a
+ // different key type than known_hosts records would fail as a mismatch.
+ if localConfig.SSHHostKeyVerification != SSHHostKeyVerificationSkip {
+ knownHostsPaths, pathErr := getKnownHostsPaths(c.workDir, os.Getenv, os.Stat, os.UserHomeDir)
+ if pathErr != nil {
+ return nil, fmt.Errorf("failed to configure SSH host key verification: %w", pathErr)
+ }
+ knownHosts, dbErr := ssh.NewKnownHostsDb(knownHostsPaths...)
+ if dbErr != nil {
+ return nil, fmt.Errorf("failed to read known_hosts: %w", dbErr)
+ }
+ hostWithPort := net.JoinHostPort(endpoint.Host, strconv.Itoa(cmp.Or(endpoint.Port, ssh.DefaultPort)))
+ publicKeys.HostKeyAlgorithms = knownHosts.HostKeyAlgorithms(hostWithPort)
+ }
return publicKeys, nil
}
diff --git a/backend/pkg/gitutil/git_test.go b/backend/pkg/gitutil/git_test.go
index 545a16902..82fa7a9ab 100644
--- a/backend/pkg/gitutil/git_test.go
+++ b/backend/pkg/gitutil/git_test.go
@@ -1,6 +1,9 @@
package git
import (
+ "crypto/ed25519"
+ "crypto/rand"
+ "encoding/pem"
"errors"
"net"
"os"
@@ -13,9 +16,11 @@ import (
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/transport"
+ "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gossh "golang.org/x/crypto/ssh"
+ "golang.org/x/crypto/ssh/knownhosts"
)
// Azure DevOps requires multi_ack/multi_ack_detailed in pack negotiation;
@@ -209,6 +214,58 @@ func TestGetSSHHostKeyCallback(t *testing.T) {
})
}
+func TestGetAuthSSHHostKeyAlgorithms(t *testing.T) {
+ const repoURL = "[email protected]:org/repo.git"
+
+ getSSHAuth := func(t *testing.T, mode string) *ssh.PublicKeys {
+ t.Helper()
+ auth, err := NewClient("").getAuth(t.Context(), repoURL, AuthConfig{
+ AuthType: "ssh",
+ SSHKey: generateTestPrivateKeyPEM(t),
+ SSHHostKeyVerification: mode,
+ })
+ require.NoError(t, err)
+ publicKeys, ok := auth.(*ssh.PublicKeys)
+ require.True(t, ok, "expected *ssh.PublicKeys, got %T", auth)
+ return publicKeys
+ }
+
+ t.Run("saved host keys select matching algorithms", func(t *testing.T) {
+ knownHostsPath := filepath.Join(t.TempDir(), "known_hosts")
+ t.Setenv("SSH_KNOWN_HOSTS", knownHostsPath)
+ key := generateTestPublicKey(t)
+ require.NoError(t, os.WriteFile(knownHostsPath, []byte(knownhosts.Line([]string{"example.com"}, key)+"\n"), 0o600))
+
+ for _, mode := range []string{SSHHostKeyVerificationStrict, SSHHostKeyVerificationAcceptNew} {
+ publicKeys := getSSHAuth(t, mode)
+ assert.Equal(t, []string{gossh.KeyAlgoED25519}, publicKeys.HostKeyAlgorithms, "mode %q", mode)
+ assert.NoError(t, publicKeys.HostKeyCallback("example.com:22", &net.TCPAddr{}, key), "mode %q", mode)
+ }
+ })
+
+ t.Run("accept_new still trusts unknown hosts", func(t *testing.T) {
+ knownHostsPath := filepath.Join(t.TempDir(), "known_hosts")
+ t.Setenv("SSH_KNOWN_HOSTS", knownHostsPath)
+
+ publicKeys := getSSHAuth(t, SSHHostKeyVerificationAcceptNew)
+ assert.Empty(t, publicKeys.HostKeyAlgorithms)
+ require.NoError(t, publicKeys.HostKeyCallback("example.com:22", &net.TCPAddr{}, generateTestPublicKey(t)))
+
+ content, err := os.ReadFile(knownHostsPath)
+ require.NoError(t, err)
+ assert.NotEmpty(t, content, "expected unknown host key to be saved")
+ })
+
+ t.Run("skip does not require a trust file", func(t *testing.T) {
+ knownHostsPath := filepath.Join(t.TempDir(), "missing", "known_hosts")
+ t.Setenv("SSH_KNOWN_HOSTS", knownHostsPath)
+
+ publicKeys := getSSHAuth(t, SSHHostKeyVerificationSkip)
+ assert.Empty(t, publicKeys.HostKeyAlgorithms)
+ assert.NoFileExists(t, knownHostsPath)
+ })
+}
+
func TestAddHostKey(t *testing.T) {
t.Run("adds host key to file", func(t *testing.T) {
tmpDir := t.TempDir()
@@ -744,6 +801,18 @@ func generateTestPublicKey(t *testing.T) gossh.PublicKey {
return key
}
+// generateTestPrivateKeyPEM creates an OpenSSH-encoded ED25519 client key.
+func generateTestPrivateKeyPEM(t *testing.T) string {
+ t.Helper()
+
+ _, privateKey, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ block, err := gossh.MarshalPrivateKey(privateKey, "")
+ require.NoError(t, err)
+
+ return string(pem.EncodeToMemory(block))
+}
+
// generateTestPublicKeyVariant creates a different test ED25519 public key
func generateTestPublicKeyVariant(t *testing.T) gossh.PublicKey {
t.Helper()