From 5ed6db9dfe347ecb21422c93ad72204aa82b53e9 Mon Sep 17 00:00:00 2001 From: chrislu Date: Tue, 2 Dec 2025 23:46:28 -0800 Subject: [PATCH] security: use proper SSH host key verification in tests Replace ssh.InsecureIgnoreHostKey() with ssh.FixedHostKey() that verifies the server's host key matches the known test key we generated. This addresses CodeQL warning go/insecure-hostkeycallback. Also updates go.mod to specify go 1.24.0 explicitly. --- test/sftp/framework.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/sftp/framework.go b/test/sftp/framework.go index 26c9f2abc..6d0a39880 100644 --- a/test/sftp/framework.go +++ b/test/sftp/framework.go @@ -189,12 +189,18 @@ func (f *SftpTestFramework) GetFilerAddr() string { // ConnectSFTP creates an SFTP client connection with the given credentials func (f *SftpTestFramework) ConnectSFTP(username, password string) (*sftp.Client, *ssh.Client, error) { + // Load the known host public key for verification + hostKeyCallback, err := f.getHostKeyCallback() + if err != nil { + return nil, nil, fmt.Errorf("failed to get host key callback: %v", err) + } + config := &ssh.ClientConfig{ User: username, Auth: []ssh.AuthMethod{ ssh.Password(password), }, - HostKeyCallback: ssh.InsecureIgnoreHostKey(), + HostKeyCallback: hostKeyCallback, Timeout: 5 * time.Second, } @@ -212,6 +218,26 @@ func (f *SftpTestFramework) ConnectSFTP(username, password string) (*sftp.Client return sftpClient, sshConn, nil } +// getHostKeyCallback returns a callback that verifies the server's host key +// matches the known test server key we generated +func (f *SftpTestFramework) getHostKeyCallback() (ssh.HostKeyCallback, error) { + // Read the public key file generated alongside the private key + pubKeyFile := f.hostKeyFile + ".pub" + pubKeyBytes, err := os.ReadFile(pubKeyFile) + if err != nil { + return nil, fmt.Errorf("failed to read host public key: %v", err) + } + + // Parse the public key + pubKey, _, _, _, err := ssh.ParseAuthorizedKey(pubKeyBytes) + if err != nil { + return nil, fmt.Errorf("failed to parse host public key: %v", err) + } + + // Return a callback that verifies the server key matches our known key + return ssh.FixedHostKey(pubKey), nil +} + // startMaster starts the SeaweedFS master server func (f *SftpTestFramework) startMaster(config *TestConfig) error { args := []string{