vcom/internal/fs/remote/knownhosts_test.go
vrubelroman d426752b6e fix(remote): skip unparseable known_hosts lines instead of failing every connection
A single known_hosts line with a key type x/crypto/ssh does not implement
(OpenSSH 10 ssh-mldsa44-ed25519) made knownhosts.New fail and every SSH
connection abort (QA-11). Such lines are now skipped with a warning in the
log. A host recorded only with an unsupported key type is refused instead
of being re-trusted on first use.

Host key algorithms of keys already recorded for the host are offered
first (like OpenSSH), so a host known only by its ed25519 key is not
reported as changed because x/crypto prefers ECDSA. Algorithms of skipped
lines are never offered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:39:02 +03:00

146 lines
4.4 KiB
Go

package remote
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
"vcom/internal/testutil/sshtest"
)
// Unsupported OpenSSH 10 post-quantum key (QA-11). The blob only has to look
// like base64; x/crypto rejects it by its algorithm name.
const mldsaKey = "ssh-mldsa44-ed25519 AAAAFHNzaC1tbGRzYTQ0LWVkMjU1MTkAAAAA"
func writeKnownHosts(t *testing.T, lines ...string) string {
t.Helper()
p := filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts")
if err := os.WriteFile(p, []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
t.Fatal(err)
}
return p
}
func connectTo(srv *sshtest.Server) (*SSHClient, error) {
return Connect(SSHHost{Name: "h", HostName: "127.0.0.1", Port: srv.Port(), User: "u", Password: srv.Password})
}
func hostPattern(srv *sshtest.Server) string {
return knownhosts.Normalize(fmt.Sprintf("127.0.0.1:%s", srv.Port()))
}
// Unparseable lines for other hosts (unknown key type, garbage, bad hash)
// are skipped; the connection proceeds and the new key is recorded.
func TestKnownHostsSkipsUnparseableLines(t *testing.T) {
sshtest.IsolatedHome(t)
srv := sshtest.Start(t, sshtest.Options{})
kh := writeKnownHosts(t,
"# comment",
"otherhost "+mldsaKey,
"garbage-without-key",
"|2|bad|hash ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl",
"@revoked * "+mldsaKey,
)
c, err := connectTo(srv)
if err != nil {
t.Fatalf("connect: %v", err)
}
c.Close()
data, _ := os.ReadFile(kh)
if !strings.Contains(string(data), "otherhost "+mldsaKey) {
t.Fatal("skipped lines must stay in known_hosts untouched")
}
if !strings.Contains(string(data), hostPattern(srv)+" ") {
t.Fatalf("new host key was not recorded:\n%s", data)
}
// Second connection verifies against the recorded key.
c, err = connectTo(srv)
if err != nil {
t.Fatalf("reconnect: %v", err)
}
c.Close()
}
// The maintainer's case: the host is recorded with an unsupported PQ key and
// with its ed25519 key only. The server also has an ECDSA key, which
// x/crypto would prefer by default and then report as a changed key; the
// recorded algorithm must be negotiated instead.
func TestKnownHostsPrefersRecordedAlgorithm(t *testing.T) {
sshtest.IsolatedHome(t)
ed, ec := sshtest.GenKeys(t)
srv := sshtest.Start(t, sshtest.Options{HostKeys: []ssh.Signer{ed, ec}})
writeKnownHosts(t,
hostPattern(srv)+" "+mldsaKey,
knownhosts.Line([]string{hostPattern(srv)}, ed.PublicKey()),
)
c, err := connectTo(srv)
if err != nil {
t.Fatalf("connect: %v", err)
}
c.Close()
}
// A recorded key of a type we cannot check must not be replaced silently
// by trust-on-first-use of another key.
func TestKnownHostsUnsupportedOnlyRefuses(t *testing.T) {
sshtest.IsolatedHome(t)
srv := sshtest.Start(t, sshtest.Options{})
kh := writeKnownHosts(t, hostPattern(srv)+" "+mldsaKey)
if c, err := connectTo(srv); err == nil {
c.Close()
t.Fatal("connected although the only recorded key is of an unsupported type")
} else if !strings.Contains(err.Error(), "does not support") {
t.Fatalf("unexpected error: %v", err)
}
data, _ := os.ReadFile(kh)
if strings.Count(string(data), "\n") != 1 {
t.Fatalf("known_hosts modified:\n%s", data)
}
}
// A changed key is still refused when the file also has unsupported lines.
func TestKnownHostsMismatchStillRefused(t *testing.T) {
sshtest.IsolatedHome(t)
srv := sshtest.Start(t, sshtest.Options{})
other, _ := sshtest.GenKeys(t)
writeKnownHosts(t,
"otherhost "+mldsaKey,
knownhosts.Line([]string{hostPattern(srv)}, other.PublicKey()),
)
if c, err := connectTo(srv); err == nil {
c.Close()
t.Fatal("connected with a mismatching recorded key")
} else if !strings.Contains(err.Error(), "HAS CHANGED") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestHostKeyAlgorithmsNeverUnsupported(t *testing.T) {
sshtest.IsolatedHome(t)
ed, _ := sshtest.GenKeys(t)
kh := writeKnownHosts(t,
"h.example "+mldsaKey,
knownhosts.Line([]string{"h.example"}, ed.PublicKey()),
)
db, err := loadKnownHosts(kh)
if err != nil {
t.Fatal(err)
}
algos := db.hostKeyAlgorithms("h.example:22")
if len(algos) == 0 || algos[0] != ssh.KeyAlgoED25519 {
t.Fatalf("recorded algorithm not first: %v", algos)
}
for _, a := range algos {
if strings.Contains(a, "mldsa") {
t.Fatalf("unsupported algorithm offered: %v", algos)
}
}
if got := db.hostKeyAlgorithms("unknown.example:22"); got != nil {
t.Fatalf("unknown host: want defaults, got %v", got)
}
}