Files
Kelx/cmd/sshterm/tofu_hostkey_test.go
antonn 78193254ad Security: fix ws-upgrade crash, per-user TOFU eviction, token log leaks; rebuild sshterm
Panel (index.js, client, views):
- upgrade handler: error guard on every upgraded socket (any path) plus a 3s
  zero-byte orphan reaper; engine.io destroyUpgrade disabled — closes the
  unauthenticated ECONNRESET crash of the whole panel and stops engine.io's
  1s timer from killing slow ssh-terminal handshakes
- /api/ssh-terminal/session: per-session token reuse cache in front of the
  rate limiter — floating-terminal auto-restore no longer burns the 10/min
  mint budget (tokens are re-served only to the same panel session + IP)
- global error middleware redacts credential-bearing query params via
  utils/url-sanitize.js — ssh-terminal ?token= values no longer reach logs
- floating-terminal restore consumes the persisted live flag: after the
  frame reported the SSH session dead, restore drops the stored session id
  and starts a clean connect; script cache busters bumped

sshterm (cmd/sshterm/main.go, rebuilt and deployed):
- TOFU store eviction is per-user and offender-pays (per-user caps: 64 pins
  and 256 contributed advisory hosts; total caps remain as backstops that
  evict the largest contributor first) — one user flooding first-use pins
  to hosts they control can no longer evict another user's pinned key or
  suppress their divergence warning
- the store file is reloaded on mtime/size change: admin edits to
  data/ssh_known_hosts.json now take effect on the next connection without
  a restart; corrupt files keep the last good state, deleted files clear
  the store, other read errors fail closed
- regression tests for flood/eviction/reload/healing + negative controls
- also lands the previously uncommitted IP-binding, trusted-proxy,
  allowed-hosts and metadata-blocking hardening; binary rebuilt through
  initialize.sh --repair-sshterm and verified live at
  /usr/local/bin/adpanel-sshterm (sha256 6bc0363479b91e6c49af9b153c8e552fd4ad8f9a5f787d73bbdc238ce0f7579e)
2026-09-23 17:50:40 +00:00

109 lines
3.5 KiB
Go

package main
import (
"crypto/ed25519"
"net"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/crypto/ssh"
)
func mkKey(t *testing.T) ssh.PublicKey {
t.Helper()
pub, _, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
k, err := ssh.NewPublicKey(pub)
if err != nil {
t.Fatal(err)
}
return k
}
func TestTOFUPerUserPinning(t *testing.T) {
dir := t.TempDir()
store := filepath.Join(dir, "data", "ssh_known_hosts.json")
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
real := mkKey(t)
trap := mkKey(t)
userA := tofuHostKeyCallback(store, "userA", false)
// userA first use pins real key.
if err := userA("srv.example:22", addr, real); err != nil {
t.Fatalf("userA first-use should pin, got: %v", err)
}
// userA reconnect with same key is fine.
if err := userA("srv.example:22", addr, real); err != nil {
t.Fatalf("userA same key should be allowed, got: %v", err)
}
// userA presented a DIFFERENT key on reconnect => MITM, rejected.
if err := userA("srv.example:22", addr, trap); err == nil {
t.Fatal("userA mismatched key MUST be rejected")
}
}
// Core of the reported attack: a malicious user pre-pins a TRAP key; the victim
// must NOT inherit it. The victim either pins the real key for themselves, or
// (if the attacker also intercepts the victim) sees a divergence rejection.
func TestTOFUPrePinningDoesNotPoisonVictim(t *testing.T) {
dir := t.TempDir()
store := filepath.Join(dir, "data", "ssh_known_hosts.json")
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
real := mkKey(t)
trap := mkKey(t)
attacker := tofuHostKeyCallback(store, "attacker", false)
victim := tofuHostKeyCallback(store, "victim", false)
// Attacker pre-pins a trap key for the host (their own store only).
if err := attacker("newsrv:22", addr, trap); err != nil {
t.Fatalf("attacker pre-pin setup failed: %v", err)
}
// Victim connects to the REAL server (sees the real key). Because the
// attacker recorded a different key globally, the victim must be WARNED
// (divergence) rather than silently trusting anything.
if err := victim("newsrv:22", addr, real); err == nil {
t.Fatal("victim must be warned about divergence, not silently allowed")
} else if !strings.Contains(strings.ToLower(err.Error()), "divergence") {
t.Fatalf("expected divergence error, got: %v", err)
}
// Critically: the victim is NEVER bound to the attacker's trap key.
st, _ := loadKnownHosts(store)
if got := st.Pins[userHostKey("victim", "newsrv:22")]; got == hostKeyFingerprint(trap) {
t.Fatal("victim must NOT inherit the attacker's pinned key")
}
// After explicit, informed acceptance, the victim pins the REAL key only.
victimAccept := tofuHostKeyCallback(store, "victim", true)
if err := victimAccept("newsrv:22", addr, real); err != nil {
t.Fatalf("victim explicit-accept of real key failed: %v", err)
}
st, _ = loadKnownHosts(store)
if got := st.Pins[userHostKey("victim", "newsrv:22")]; got != hostKeyFingerprint(real) {
t.Fatalf("victim should be pinned to the REAL key, got %q", got)
}
}
func TestTOFUFailsClosedOnUnwritableStore(t *testing.T) {
dir := t.TempDir()
blocker := filepath.Join(dir, "blocker")
if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
store := filepath.Join(blocker, "data", "known.json")
cb := tofuHostKeyCallback(store, "u", false)
addr := &net.TCPAddr{IP: net.IPv4(10, 0, 0, 9), Port: 22}
if err := cb("h:22", addr, mkKey(t)); err == nil {
t.Fatal("must fail closed when the key store is unavailable")
}
}