ssh: don't log health checks and port probes as failed sign-ins
CI / test (push) Successful in 1m26s
Release / release (push) Successful in 4m36s

A connection that never sends a byte is a probe (Hammerhead health-checks every
TCP resource that way), so it is no longer logged every 30 seconds.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
VeillaxandClaude Sonnet 5.5 committed 2026-10-31 00:32:50 -06:00
1 parent 6142ae6262
commit 1fba65fd3e
2 files changed
+70 -2

No files matched your search

+50
View File
@@ -4,9 +4,12 @@ import (
"context"
"crypto/ed25519"
"crypto/rand"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
@@ -148,3 +151,50 @@ func TestNoForwarding(t *testing.T) {
t.Fatal("port forwarding was allowed")
}
}
// Hammerhead health-checks a TCP resource by connecting and hanging up; that must not look like
// a failed sign-in in the log, while a real failure still does.
func TestProbesAreNotLogged(t *testing.T) {
good := newSigner(t)
dir := t.TempDir()
keys := filepath.Join(dir, "authorized_keys")
if err := os.WriteFile(keys, []byte(authLine(good)), 0o600); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var logs []string
srv, err := New(Config{AuthorizedKeys: keys, HostKey: filepath.Join(dir, "host_key"), Logf: func(f string, a ...any) {
mu.Lock()
logs = append(logs, fmt.Sprintf(f, a...))
mu.Unlock()
}})
if err != nil {
t.Fatal(err)
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { srv.Serve(ctx, ln); close(done) }()
t.Cleanup(func() { cancel(); <-done })
for i := 0; i < 3; i++ {
c, err := net.Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
c.Close()
}
time.Sleep(200 * time.Millisecond)
if _, err := dial(ln.Addr().String(), ssh.PublicKeys(newSigner(t))); err == nil {
t.Fatal("an unknown key signed in")
}
time.Sleep(200 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
if len(logs) != 1 || !strings.Contains(logs[0], "failed") {
t.Fatalf("want exactly the one real failure logged, got %q", logs)
}
}