From 645d514d11a35b4a74603f00f6a53d8e035a08a3 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 15 Nov 2026 22:20:05 +1100 Subject: [PATCH] fix(host): pin the host key's algorithm, so a host holding several keys matches its pin --- connectors/host/ssh.go | 24 ++++++-- connectors/host/ssh_test.go | 111 ++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 6 deletions(-) create mode 100644 connectors/host/ssh_test.go diff --git a/connectors/host/ssh.go b/connectors/host/ssh.go index d05467d..bb300b2 100644 --- a/connectors/host/ssh.go +++ b/connectors/host/ssh.go @@ -34,12 +34,7 @@ func (h *host) run(ctx context.Context, target Target, command string) ([]byte, } stop := context.AfterFunc(ctx, func() { _ = conn.Close() }) defer stop() - cc, chans, reqs, err := ssh.NewClientConn(conn, target.Address, &ssh.ClientConfig{ - User: h.username, - Auth: []ssh.AuthMethod{ssh.PublicKeys(h.signer)}, - HostKeyCallback: ssh.FixedHostKey(target.key), - Timeout: dialTimeout, - }) + cc, chans, reqs, err := ssh.NewClientConn(conn, target.Address, h.clientConfig(target)) if err != nil { _ = conn.Close() return nil, fmt.Errorf("ssh to %s: %w", target.Name, err) @@ -63,6 +58,23 @@ func (h *host) run(ctx context.Context, target Target, command string) ([]byte, return nil, nil } +// clientConfig pins the target's host key. It asks only for that key's +// algorithm: a host holding several keys would otherwise present its +// preferred one, and a correct pin would read as a mismatch. +func (h *host) clientConfig(target Target) *ssh.ClientConfig { + algorithms := []string{target.key.Type()} + if target.key.Type() == ssh.KeyAlgoRSA { + algorithms = []string{ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256} + } + return &ssh.ClientConfig{ + User: h.username, + Auth: []ssh.AuthMethod{ssh.PublicKeys(h.signer)}, + HostKeyCallback: ssh.FixedHostKey(target.key), + HostKeyAlgorithms: algorithms, + Timeout: dialTimeout, + } +} + // limited keeps the first maxOutput bytes and drops the rest. type limited struct{ bytes.Buffer } diff --git a/connectors/host/ssh_test.go b/connectors/host/ssh_test.go new file mode 100644 index 0000000..1be2242 --- /dev/null +++ b/connectors/host/ssh_test.go @@ -0,0 +1,111 @@ +package main + +import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "net" + "testing" + + "golang.org/x/crypto/ssh" +) + +// TestHostKeyPinHoldsOnAHostWithSeveralKeys reproduces the pin failing on a +// host that also holds a key the client prefers (carina offers ECDSA before +// ed25519): the pinned key must be accepted, and a key that isn't pinned +// must still be refused. +func TestHostKeyPinHoldsOnAHostWithSeveralKeys(t *testing.T) { + edHost := signer(t, newEd25519(t)) + ecHost := signer(t, newECDSA(t)) + stranger := signer(t, newEd25519(t)) + client := signer(t, newEd25519(t)) + + tests := []struct { + name string + pin ssh.PublicKey + wantErr bool + }{ + {name: "pinned ed25519 key among several", pin: edHost.PublicKey()}, + {name: "pinned ecdsa key among several", pin: ecHost.PublicKey()}, + {name: "a key the host doesn't hold", pin: stranger.PublicKey(), wantErr: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + addr := serve(t, client.PublicKey(), ecHost, edHost) + h := &host{username: "archie", signer: client} + conn, err := (&net.Dialer{}).DialContext(t.Context(), "tcp", addr) + if err != nil { + t.Fatal(err) + } + defer func() { _ = conn.Close() }() + cc, _, _, err := ssh.NewClientConn(conn, addr, h.clientConfig(Target{Name: "box", Address: addr, key: tt.pin})) + if tt.wantErr { + if err == nil { + t.Fatal("handshake accepted a host key that wasn't pinned") + } + return + } + if err != nil { + t.Fatalf("handshake: %v", err) + } + _ = cc.Close() + }) + } +} + +// serve runs a one-connection SSH server holding hostKeys that accepts only +// the client key. +func serve(t *testing.T, clientKey ssh.PublicKey, hostKeys ...ssh.Signer) string { + t.Helper() + cfg := &ssh.ServerConfig{PublicKeyCallback: func(_ ssh.ConnMetadata, k ssh.PublicKey) (*ssh.Permissions, error) { + if string(k.Marshal()) == string(clientKey.Marshal()) { + return &ssh.Permissions{}, nil + } + return nil, net.UnknownNetworkError("unknown client key") + }} + for _, k := range hostKeys { + cfg.AddHostKey(k) + } + ln, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = ln.Close() }) + go func() { + c, err := ln.Accept() + if err != nil { + return + } + defer func() { _ = c.Close() }() + _, _, _, _ = ssh.NewServerConn(c, cfg) + }() + return ln.Addr().String() +} + +func newEd25519(t *testing.T) ed25519.PrivateKey { + t.Helper() + _, k, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + return k +} + +func newECDSA(t *testing.T) *ecdsa.PrivateKey { + t.Helper() + k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + return k +} + +func signer(t *testing.T, key any) ssh.Signer { + t.Helper() + s, err := ssh.NewSignerFromKey(key) + if err != nil { + t.Fatal(err) + } + return s +}