Files
Netcatty/electron/bridges/sshBridge/moshStatsConnection.test.cjs
T
陈大猫andClaude Opus 4.8 c6552ddc75 fix: Mosh 模式显示主机信息(stats companion SSH 连接) (#1198) (#1213)
* Show host info for Mosh sessions via a stats companion SSH connection

Mosh sessions run over UDP through a local mosh-client PTY and carry no
ssh2 connection (session.conn), so getServerStats could not open an exec
channel and the terminal's host-info bar (CPU/memory/disk/network) stayed
empty — unlike SSH sessions (issue #1198).

Add a best-effort, non-interactive companion SSH connection that is opened
lazily on the first stats poll for a Mosh session, reusing the credentials
the Mosh handshake already validated, and assign it to session.conn so the
existing stats path works unchanged:

- electron/bridges/sshBridge/moshStatsConnection.cjs: new helper that
  builds the companion connection. It never prompts (only stored password,
  parseable private key, unencrypted/stored-passphrase identity files, or
  ssh-agent), shares one in-flight attempt across concurrent polls, treats
  auth rejection as permanent but transient errors as retryable, and skips
  host-key verification like the existing one-off execCommand path.
- sessionOps.getServerStats: establish the companion connection for Mosh
  sessions that lack session.conn before running the stats command;
  degrades gracefully to the existing "not connected" error otherwise.
- moshSession.swapToMoshClient: stash the handshake credentials and
  algorithm settings on session.moshStatsAuth once the handshake succeeds.
- terminalBridge closeSession / cleanupAllSessions and the mosh-client exit
  handler: tear down the companion connection (it has no session.stream).
- Forward legacyAlgorithms / skipEcdsaHostKey / algorithmOverrides through
  the renderer's Mosh starter and the bridge type so the companion
  negotiates the same algorithms the interactive session would.

Tests cover the helper (auth selection, agent fallback, dedup, permanent vs
transient failure, late-ready discard), the getServerStats integration, and
the moshStatsAuth stash + companion teardown on close.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: target SSH host and settle on mid-handshake close

- moshSession: store options.hostname (the SSH endpoint) on moshStatsAuth
  instead of parsed.host. A `MOSH IP` line advertises the UDP endpoint for
  mosh-client, which can differ from the SSH host on NAT / multi-homed
  setups; the companion is an SSH connection and must target the SSH host.
- moshStatsConnection: resolve the pending attempt from the "close" handler
  when the socket drops mid-handshake without a prior "ready"/"error", so an
  awaiting getServerStats call (and session.moshStatsConnPromise) cannot
  hang indefinitely. Treated as transient so the next poll may retry.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: keyboard-interactive password for stats companion

PAM-backed SSH servers often offer password auth only via
keyboard-interactive, not the plain "password" method. The Mosh handshake's
system ssh handles that through its PTY responder, so without it the
companion stats connection would fail auth on those hosts even with a saved
password, leaving the stats bar empty.

When a saved password is present, enable tryKeyboard and attach a
non-interactive keyboard-interactive handler that auto-fills the password
for a single password prompt (using the existing
isAutoFillablePasswordChallenge predicate) and finishes empty on
2FA/OTP/multi-prompt challenges. It auto-fills at most once so a wrong
password can't drive a retry loop, and it never shows a modal — the
companion stays fully non-interactive.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: verify host key before sending a saved password

A background companion connection that auto-submits a saved password to an
unverified host could disclose it to a spoofed / MITM server (P1). Gate
password auth (plain and keyboard-interactive) behind a silent, trusted-only
host-key check against Netcatty's known-hosts store:

- moshStatsConnection: when the companion would authenticate with a
  password, attach an ssh2 hostVerifier that accepts only a key already
  "trusted" in known-hosts (via hostKeyVerifier.classifyHostKey) and rejects
  unknown/changed keys outright — no prompt, so the password is never sent to
  an unvetted host and no host-key dialog pops for a background poll.
  Public-key / agent auth proves possession via a signature and discloses no
  reusable secret, so it is not gated (matches the existing execCommand
  precedent; the handshake already vetted the host via system ssh).
- Thread knownHosts through the renderer Mosh starter, the bridge type, and
  session.moshStatsAuth.

Note: a password-auth Mosh host that Netcatty has never seen via its own SSH
path (so it is absent from Netcatty's known-hosts) will not get the stats
companion until its key is known — the safe default. Key/agent-auth hosts are
unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: transient pre-handshake polls and agent+password auth

Two functional gaps in the stats companion:

- Missing moshStatsAuth is now transient, not permanent. The renderer can
  mark a Mosh session "connected" (and start polling) from the SSH
  bootstrap's visible PTY output before the swap to mosh-client assigns
  moshStatsAuth. Previously that first poll set moshStatsConnFailed
  permanently, so the companion was never attempted after the handshake
  actually completed. Now it just returns null and a later poll retries.

- A saved password no longer suppresses ssh-agent auth. A public-key host
  that authenticates via the agent may still carry a stored password; the
  companion now offers the agent alongside the password (ssh2 tries agent
  first) instead of attempting password-only and failing permanently. An
  explicit private key still suppresses the agent fallback.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: gate password at authHandler, not whole connection

The previous fix installed a trusted-only hostVerifier whenever a password
was present. Because ssh2 verifies the host before any auth method, that
rejected the entire connection on a host absent from Netcatty's known-hosts
— blocking key/agent auth too, even though those never need to send the
password.

Move the gate from the transport to the auth layer:

- A trust-tracking hostVerifier records whether the live host key is trusted
  (during the transport handshake) and then accepts the transport so
  public-key / agent auth can proceed on any host.
- A function-form authHandler offers none -> agent -> publickey always, and
  appends password + keyboard-interactive only when the host key is trusted.

Result: key/agent auth works on hosts Netcatty hasn't vetted, while a saved
password is still never sent to an untrusted host. Public-key / agent auth
remains ungated (no reusable secret is disclosed).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: isolate Mosh stats connection from session.conn

Storing the stats companion on session.conn made it look like the session's
primary interactive SSH connection. Other bridges key off session.conn —
getSessionPwd assumes its exec channel is a sibling of the interactive shell,
and SFTP / MCP exec run over session.conn — but a Mosh session's shell lives
on the UDP mosh-client, not this background connection. After a stats poll
they could return a bogus cwd or operate over the wrong connection.

Keep the companion strictly on session.moshStatsConn:

- ensureMoshStatsConnection stores/reuses/clears only session.moshStatsConn.
- getServerStats reads session.conn || session.moshStatsConn (real SSH still
  uses conn; Mosh uses the companion) and only opens one when neither exists.
- closeSession / cleanupAllSessions / the mosh-client exit handler tear down
  session.moshStatsConn.

This leaves session.conn untouched for Mosh, so getSessionPwd / SFTP / MCP
exec behave exactly as before (no primary SSH connection for Mosh).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: don't count pre-handshake Mosh polls as failures

useServerStats gives up after 3 consecutive failures. A Mosh session can be
marked "connected" (and start polling) from the SSH bootstrap's visible
output before swapToMoshClient stores moshStatsAuth, during which
ensureMoshStatsConnection returns null. Previously getServerStats reported
that as a normal failure, so a handshake taking ~15s (3 polls) would
permanently disable stats for the session even after credentials became
available.

Introduce a `pending` result:

- getServerStats returns { success: false, pending: true } for a Mosh session
  that has no connection yet and no moshStatsAuth and hasn't permanently
  failed. Once moshStatsAuth is set (or the companion permanently fails), it
  reports a normal failure again.
- useServerStats treats `pending` as neutral: it does not update stats and
  does not increment the consecutive-failure counter, so polling continues
  until the handshake completes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* Address codex review: verify host key for all Mosh stats companion auth

The companion installed a trust-tracking host verifier only when a saved
password was present; key/agent-only connections fell back to ssh2's
default of accepting any host key. A background, user-invisible connection
that authenticated against an unverified host could let a MITM/DNS-spoofed
host feed bogus host-info to the user and enumerate the ssh-agent's public
keys — breaking the host-key guarantee the interactive session enforces.

Attach the host verifier for every auth method and reject an unknown or
changed host key outright (never prompting; stats just stay empty). Treat
an untrusted host as a permanent failure so polling stops reconnecting.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* fix(mosh): trust system known_hosts for the stats companion host-key check

The Mosh stats companion opens a background ssh2 connection and only rides
on a host whose live key is already trusted, rejecting unknown/changed keys
as a permanent failure. Trust was sourced solely from Netcatty's in-app
known-hosts snapshot (options.knownHosts).

But a Mosh session is bootstrapped by the system `ssh`, which vets and
records the host key in the user's OpenSSH known_hosts (~/.ssh/known_hosts,
etc). Netcatty's snapshot is never updated by that handshake, so a host
trusted purely via system ssh was misread as "unknown" and the companion
permanently disabled — Mosh stats never appeared unless the user manually
scanned/imported the host into Netcatty (codex P2).

Add a system-known_hosts trust source (systemKnownHosts.cjs) and consult it
in the companion verifier when the in-app snapshot does not already vouch
for the key. Matching is by the LIVE key's SHA-256 fingerprint, so trust is
granted only for the exact key the user's own OpenSSH already trusts; an
arbitrary or mismatched key is never accepted. Unknown/changed keys stay
rejected and remain a permanent failure.

The parser handles the OpenSSH known_hosts(5) format that the in-app scan
parser does not fully cover for matching: plain hosts, comma lists,
[host]:port, hashed |1|salt|HMAC-SHA1(salt,token) entries (with the
bracketed token for non-default ports, verified against ssh-keygen -H),
multiple key types, @revoked (forces NOT trusted) and @cert-authority
(skipped). Wildcard/negation patterns are deliberately not honored. Paths
mirror localFsBridge.readKnownHosts and are cross-platform (incl. Windows
%PROGRAMDATA%\ssh\known_hosts). All errors fail closed.

ssh2 ships no known_hosts parser, so this is implemented in CommonJS using
node:crypto and covered by unit tests (real ssh-keygen hashed fixtures,
revoked/cert-authority, fingerprint mismatch, fail-closed) plus companion
integration tests (system-only trust accepts; neither source trusts ->
rejected + permanent; key rotation not rescued; optional-dependency safety).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 16:26:27 +08:00

829 lines
30 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const { EventEmitter } = require("node:events");
const { createMoshStatsConnectionApi } = require("./moshStatsConnection.cjs");
// The connection is created inside an async flow (after credentials are
// resolved, which may touch the filesystem), so the fake SSH client appears a
// few microtasks/immediates after ensureMoshStatsConnection() is called.
function tick() {
return new Promise((resolve) => setImmediate(resolve));
}
// Minimal fake ssh2 Client. Records connect() opts and lets the test drive
// the lifecycle via emitReady / emitError.
class FakeSSHClient extends EventEmitter {
constructor() {
super();
FakeSSHClient.instances.push(this);
this.connectOpts = null;
this.ended = false;
}
connect(opts) {
this.connectOpts = opts;
return this;
}
end() {
this.ended = true;
}
emitReady() {
this.emit("ready");
}
emitError(err) {
this.emit("error", err);
}
}
FakeSSHClient.instances = [];
function makeApi(overrides = {}) {
FakeSSHClient.instances = [];
const sessions = overrides.sessions || new Map();
const logs = [];
const api = createMoshStatsConnectionApi({
get sessions() {
return sessions;
},
SSHClient: overrides.SSHClient || FakeSSHClient,
sshUtils: overrides.sshUtils || {
// Default: treat any non-empty string as a parseable key.
parseKey: (key) => (key && key.length > 0 ? { ok: true } : new Error("bad key")),
},
NetcattyAgent: overrides.NetcattyAgent || class {},
buildAlgorithms: overrides.buildAlgorithms || (() => ({ algos: true })),
getSshAgentSocket: overrides.getSshAgentSocket || (() => null),
readFileNoFollow: overrides.readFileNoFollow || (async () => null),
expandIdentityFilePath: overrides.expandIdentityFilePath || ((p) => p),
isAutoFillablePasswordChallenge:
overrides.isAutoFillablePasswordChallenge || (() => false),
hostKeyVerifier: overrides.hostKeyVerifier || {
// Default: classify everything as trusted so password tests connect.
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "fp" }),
classifyHostKey: () => ({ status: "trusted" }),
},
// Default: the system known_hosts vouches for nothing, so trust comes
// solely from the Netcatty classifier above. Individual tests override this
// to exercise the system-known_hosts fallback.
isHostKeyTrustedBySystem:
"isHostKeyTrustedBySystem" in overrides
? overrides.isHostKeyTrustedBySystem
: () => false,
log: (...args) => logs.push(args),
});
return { api, sessions, logs };
}
test("reuses an already-established companion (moshStatsConn) without reconnecting", async () => {
const existing = { exec() {} };
const { api } = makeApi();
const session = { moshStatsConn: existing, moshStatsAuth: { hostname: "h", password: "p" } };
const result = await api.ensureMoshStatsConnection(session, "sid");
assert.equal(result, existing);
assert.equal(FakeSSHClient.instances.length, 0);
});
test("gives up (no connection) when there is no usable non-interactive auth", async () => {
const { api } = makeApi();
// Only an interactively-typed password would have worked; nothing stored.
const session = { moshStatsAuth: { hostname: "h", username: "u" } };
const result = await api.ensureMoshStatsConnection(session, "sid");
assert.equal(result, null);
assert.equal(session.moshStatsConnFailed, true);
assert.equal(FakeSSHClient.instances.length, 0);
});
test("missing moshStatsAuth is transient (handshake not yet swapped), not a permanent failure", async () => {
const { api, sessions } = makeApi();
// Session is connected (renderer polls) but the handshake hasn't swapped to
// mosh-client yet, so moshStatsAuth is not assigned.
const session = {};
sessions.set("sid", session);
const result = await api.ensureMoshStatsConnection(session, "sid");
assert.equal(result, null);
// Must NOT be permanently disabled — a later poll (after the swap sets
// moshStatsAuth) has to be able to establish the companion.
assert.notEqual(session.moshStatsConnFailed, true);
assert.equal(FakeSSHClient.instances.length, 0);
// Once auth becomes available, a subsequent poll connects.
session.moshStatsAuth = { hostname: "h", username: "u", password: "p" };
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(FakeSSHClient.instances.length, 1);
});
test("connects with a stored password and adopts the connection on ready", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "example.com", port: 2222, username: "alice", password: "secret" } };
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
// One connection attempt with the password and host wired in.
assert.equal(FakeSSHClient.instances.length, 1);
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.host, "example.com");
assert.equal(client.connectOpts.port, 2222);
assert.equal(client.connectOpts.username, "alice");
assert.equal(client.connectOpts.password, "secret");
client.emitReady();
const result = await pending;
assert.equal(result, client);
// Stored ONLY on moshStatsConn, never on session.conn (keeps the companion
// invisible to getSessionPwd / SFTP / MCP exec).
assert.equal(session.moshStatsConn, client);
assert.equal(session.conn, undefined);
});
test("uses a parseable private key and passphrase, not password fallback only", async () => {
const { api, sessions } = makeApi();
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
passphrase: "pw",
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.privateKey, session.moshStatsAuth.privateKey);
assert.equal(client.connectOpts.passphrase, "pw");
});
test("skips an unparseable (e.g. encrypted, wrong passphrase) private key", async () => {
const { api, sessions } = makeApi({
sshUtils: { parseKey: () => new Error("encrypted") },
});
const session = {
moshStatsAuth: { hostname: "h", username: "u", privateKey: "enc", password: "fallback" },
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
// Falls back to the stored password instead of offering the bad key.
assert.equal(client.connectOpts.privateKey, undefined);
assert.equal(client.connectOpts.password, "fallback");
});
test("reads identity files non-interactively when no inline key is present", async () => {
const reads = [];
const { api, sessions } = makeApi({
readFileNoFollow: async (p) => {
reads.push(p);
return "FILEKEY";
},
sshUtils: { parseKey: (k) => (k === "FILEKEY" ? { ok: true } : new Error("no")) },
});
const session = {
moshStatsAuth: { hostname: "h", username: "u", identityFilePaths: ["~/.ssh/id_ed25519"] },
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
await tick(); // allow chained async identity read + client creation
assert.deepEqual(reads, ["~/.ssh/id_ed25519"]);
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.privateKey, "FILEKEY");
});
test("falls back to ssh-agent when a socket is available and no inline creds", async () => {
// The system ssh used by the Mosh handshake authenticates via the local
// agent by default, so the companion should too — regardless of the
// agentForwarding (remote forwarding) setting.
for (const agentForwarding of [true, false, undefined]) {
const { api, sessions } = makeApi({
getSshAgentSocket: () => "/tmp/agent.sock",
});
const session = { moshStatsAuth: { hostname: "h", username: "u", agentForwarding } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.agent, "/tmp/agent.sock");
}
});
test("does not attempt a connection when no agent socket and no inline creds", async () => {
const { api } = makeApi({ getSshAgentSocket: () => null });
const session = { moshStatsAuth: { hostname: "h", username: "u" } };
const result = await api.ensureMoshStatsConnection(session, "sid");
assert.equal(result, null);
assert.equal(FakeSSHClient.instances.length, 0);
});
test("enables keyboard-interactive and auto-fills the saved password for a single prompt", async () => {
// Use the real auto-fill predicate so this exercises the actual handler.
const { isAutoFillablePasswordChallenge } = require("../sshAuthHelper.cjs");
const { api, sessions } = makeApi({ isAutoFillablePasswordChallenge });
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "secret" } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.tryKeyboard, true);
let answered = null;
client.emit(
"keyboard-interactive",
"",
"",
"",
[{ prompt: "Password:", echo: false }],
(responses) => { answered = responses; },
);
assert.deepEqual(answered, ["secret"]);
});
test("keyboard-interactive finishes empty on a 2FA / OTP challenge (no hang, no prompt)", async () => {
const { isAutoFillablePasswordChallenge } = require("../sshAuthHelper.cjs");
const { api, sessions } = makeApi({ isAutoFillablePasswordChallenge });
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "secret" } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
let answered = null;
client.emit(
"keyboard-interactive",
"",
"",
"",
[{ prompt: "Verification code:", echo: false }],
(responses) => { answered = responses; },
);
assert.deepEqual(answered, []);
});
test("keyboard-interactive only auto-fills once, then finishes empty to avoid a loop", async () => {
const { isAutoFillablePasswordChallenge } = require("../sshAuthHelper.cjs");
const { api, sessions } = makeApi({ isAutoFillablePasswordChallenge });
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "secret" } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
const prompts = [{ prompt: "Password:", echo: false }];
let first = null;
let second = null;
client.emit("keyboard-interactive", "", "", "", prompts, (r) => { first = r; });
client.emit("keyboard-interactive", "", "", "", prompts, (r) => { second = r; });
assert.deepEqual(first, ["secret"]);
assert.deepEqual(second, []); // retry not re-filled with the same wrong password
});
test("does not enable keyboard-interactive when there is no password", async () => {
const { api, sessions } = makeApi();
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.notEqual(client.connectOpts.tryKeyboard, true);
});
// Drive an ssh2 function-form authHandler to completion, collecting the
// method names it offers. Each call is answered by invoking the callback,
// then we ask for the next method until it yields false (exhausted).
function drainAuthHandler(authHandler) {
const offered = [];
let done = false;
let guard = 0;
while (!done && guard++ < 50) {
let answered = false;
authHandler([], false, (method) => {
answered = true;
if (method === false) {
done = true;
} else {
offered.push(method);
}
});
if (!answered) break;
}
return offered;
}
test("verifier is attached for every auth method; gated authHandler only with a password", async () => {
// Password present -> verifier + gated authHandler attached.
{
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(typeof FakeSSHClient.instances[0].connectOpts.hostVerifier, "function");
assert.equal(typeof FakeSSHClient.instances[0].connectOpts.authHandler, "function");
}
// Key only -> verifier still attached (the host must be vetted); no authHandler.
{
const { api, sessions } = makeApi();
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(typeof FakeSSHClient.instances[0].connectOpts.hostVerifier, "function");
assert.equal(FakeSSHClient.instances[0].connectOpts.authHandler, undefined);
}
// Agent only -> verifier still attached; no authHandler.
{
const { api, sessions } = makeApi({ getSshAgentSocket: () => "/tmp/agent.sock" });
const session = { moshStatsAuth: { hostname: "h", username: "u", agentForwarding: true } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(typeof FakeSSHClient.instances[0].connectOpts.hostVerifier, "function");
assert.equal(FakeSSHClient.instances[0].connectOpts.authHandler, undefined);
}
});
test("the verifier rejects the transport for an untrusted host (key auth included)", async () => {
const hostKeyVerifier = require("../hostKeyVerifier.cjs");
const { api, sessions } = makeApi({ hostKeyVerifier });
// Untrusted host: empty known-hosts, key auth and NO password — must still be
// refused, even though key auth would leak no reusable secret.
const session = {
moshStatsAuth: {
hostname: "unknown.example.com",
port: 22,
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
knownHosts: [],
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const verify = FakeSSHClient.instances[0].connectOpts.hostVerifier;
let accepted = null;
verify(require("node:crypto").randomBytes(32), (ok) => { accepted = ok; });
// Refuses an unvetted host outright — no auth attempted, no stats command run.
assert.equal(accepted, false);
});
test("the gated authHandler offers password ONLY when the host key is trusted", async () => {
const hostKeyVerifier = require("../hostKeyVerifier.cjs");
const rawKey = require("node:crypto").randomBytes(32);
const { keyType, fingerprint } = hostKeyVerifier.describeHostKey(rawKey);
const knownHosts = [
{ id: "k1", hostname: "trusted.example.com", port: 22, keyType, fingerprint, publicKey: "" },
];
// Trusted host: after the verifier runs, password + keyboard-interactive
// are offered.
{
const { api, sessions } = makeApi({ hostKeyVerifier });
const session = {
moshStatsAuth: { hostname: "trusted.example.com", port: 22, username: "u", password: "p", knownHosts },
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const { hostVerifier, authHandler } = FakeSSHClient.instances[0].connectOpts;
hostVerifier(rawKey, () => {}); // verifier runs during transport, sets trust
const offered = drainAuthHandler(authHandler);
assert.ok(offered.includes("password"));
assert.ok(offered.includes("keyboard-interactive"));
}
// Untrusted host: password methods are withheld even though a password is
// saved, so the secret is never sent.
{
const { api, sessions } = makeApi({ hostKeyVerifier });
const session = {
moshStatsAuth: { hostname: "other.example.com", port: 22, username: "u", password: "p", knownHosts },
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const { hostVerifier, authHandler } = FakeSSHClient.instances[0].connectOpts;
hostVerifier(rawKey, () => {});
const offered = drainAuthHandler(authHandler);
assert.ok(!offered.includes("password"));
assert.ok(!offered.includes("keyboard-interactive"));
}
});
test("an explicit private key suppresses the ssh-agent fallback", async () => {
const { api, sessions } = makeApi({ getSshAgentSocket: () => "/tmp/agent.sock" });
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.ok(client.connectOpts.privateKey);
assert.equal(client.connectOpts.agent, undefined);
});
test("a saved password does NOT suppress agent auth (agent offered alongside password)", async () => {
// A public-key host that authenticates via the agent may still carry a
// stored password; the companion must offer both so agent auth (tried
// first by ssh2) can succeed instead of failing on password only.
const { api, sessions } = makeApi({ getSshAgentSocket: () => "/tmp/agent.sock" });
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "pw" } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
assert.equal(client.connectOpts.agent, "/tmp/agent.sock");
assert.equal(client.connectOpts.password, "pw");
});
test("concurrent calls share a single in-flight connection attempt", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
const p1 = api.ensureMoshStatsConnection(session, "sid");
const p2 = api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(FakeSSHClient.instances.length, 1);
FakeSSHClient.instances[0].emitReady();
const [r1, r2] = await Promise.all([p1, p2]);
assert.equal(r1, FakeSSHClient.instances[0]);
assert.equal(r2, FakeSSHClient.instances[0]);
});
test("auth rejection is permanent: no reconnect on the next poll", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
const p1 = api.ensureMoshStatsConnection(session, "sid");
await tick();
const authErr = new Error("All configured authentication methods failed");
authErr.level = "client-authentication";
FakeSSHClient.instances[0].emitError(authErr);
assert.equal(await p1, null);
assert.equal(session.moshStatsConnFailed, true);
// Second poll must not open a new connection.
const r2 = await api.ensureMoshStatsConnection(session, "sid");
assert.equal(r2, null);
assert.equal(FakeSSHClient.instances.length, 1);
});
test("a transient error allows a reconnect on the next poll", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
const p1 = api.ensureMoshStatsConnection(session, "sid");
await tick();
const netErr = new Error("connect ETIMEDOUT");
netErr.level = "client-socket";
FakeSSHClient.instances[0].emitError(netErr);
assert.equal(await p1, null);
assert.notEqual(session.moshStatsConnFailed, true);
// Next poll is allowed to try again.
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(FakeSSHClient.instances.length, 2);
});
test("a socket that closes mid-handshake settles the attempt instead of hanging", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
// Socket drops during the handshake with no prior "ready" or "error".
FakeSSHClient.instances[0].emit("close");
const result = await pending;
assert.equal(result, null);
// Transient — must not permanently disable stats, and the promise must clear.
assert.notEqual(session.moshStatsConnFailed, true);
assert.equal(session.moshStatsConnPromise, null);
// The next poll is allowed to retry.
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(FakeSSHClient.instances.length, 2);
});
test("a connection that becomes ready after the session closed is discarded", async () => {
const { api, sessions } = makeApi();
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p" } };
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
// Session goes away before the handshake completes.
session.closed = true;
sessions.delete("sid");
FakeSSHClient.instances[0].emitReady();
const result = await pending;
assert.equal(result, null);
assert.equal(FakeSSHClient.instances[0].ended, true);
assert.equal(session.conn, undefined);
assert.equal(session.moshStatsConn, undefined);
});
test("honors host algorithm settings via buildAlgorithms", async () => {
const calls = [];
const { api, sessions } = makeApi({
buildAlgorithms: (legacy, opts) => {
calls.push({ legacy, opts });
return { built: true };
},
});
const overrides = { cipher: ["aes128-cbc"] };
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
password: "p",
legacyAlgorithms: true,
skipEcdsaHostKey: true,
algorithmOverrides: overrides,
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
assert.equal(calls.length, 1);
assert.equal(calls[0].legacy, true);
assert.equal(calls[0].opts.skipEcdsaHostKey, true);
assert.equal(calls[0].opts.algorithmOverrides, overrides);
assert.deepEqual(FakeSSHClient.instances[0].connectOpts.algorithms, { built: true });
});
test("installs a host-key verifier even for key-only auth (no password)", async () => {
const { api, sessions } = makeApi();
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
// Regression (#1198 review): a background companion must verify the host key
// for EVERY auth method, not only when a password is present.
assert.equal(typeof client.connectOpts.hostVerifier, "function");
});
test("rejects an untrusted host key for key auth and treats it as permanent", async () => {
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "live-fp" }),
classifyHostKey: () => ({ status: "unknown" }),
},
});
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
},
};
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
// The verifier refuses the transport for an unvetted host, even though the
// companion would have authenticated with a key (no reusable secret leaked).
let verdict;
client.connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, false);
// ssh2 then aborts the handshake; an untrusted host must be a permanent
// failure so we don't reconnect (and re-reject) on every stats poll.
client.emitError(Object.assign(new Error("handshake failed"), { level: "protocol" }));
const result = await pending;
assert.equal(result, null);
assert.equal(session.moshStatsConnFailed, true);
});
test("accepts a trusted host key and adopts the connection", async () => {
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "fp" }),
classifyHostKey: () => ({ status: "trusted" }),
},
});
const session = {
moshStatsAuth: { hostname: "h", username: "u", password: "secret" },
};
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
let verdict;
client.connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, true);
client.emitReady();
assert.equal(await pending, client);
});
test("trusts a host vouched for ONLY by the system known_hosts (Netcatty snapshot empty)", async () => {
// Netcatty's in-app vault has no record (classify -> unknown), but the user's
// system OpenSSH known_hosts already trusts the exact live key — which is
// what the Mosh handshake's system ssh actually used. The companion must
// accept it so Mosh stats appear.
const seen = [];
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "live-fp" }),
classifyHostKey: () => ({ status: "unknown" }),
},
isHostKeyTrustedBySystem: (args) => {
seen.push(args);
return args.hostname === "sys.example.com" && args.fingerprint === "live-fp";
},
});
const session = {
moshStatsAuth: {
hostname: "sys.example.com",
port: 22,
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
knownHosts: [],
},
};
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
let verdict;
client.connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, true);
// The system check was consulted with the live key's fingerprint.
assert.equal(seen.length, 1);
assert.equal(seen[0].fingerprint, "live-fp");
client.emitReady();
assert.equal(await pending, client);
});
test("rejects (permanently) when NEITHER Netcatty nor the system known_hosts trust the key", async () => {
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "live-fp" }),
classifyHostKey: () => ({ status: "unknown" }),
},
isHostKeyTrustedBySystem: () => false,
});
const session = {
moshStatsAuth: {
hostname: "untrusted.example.com",
port: 22,
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
knownHosts: [],
},
};
sessions.set("sid", session);
const pending = api.ensureMoshStatsConnection(session, "sid");
await tick();
const client = FakeSSHClient.instances[0];
let verdict;
client.connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, false);
// ssh2 aborts; an untrusted host is a permanent failure (no re-poll loop).
client.emitError(Object.assign(new Error("handshake failed"), { level: "protocol" }));
assert.equal(await pending, null);
assert.equal(session.moshStatsConnFailed, true);
});
test("the system fallback is NOT consulted when Netcatty already trusts the key", async () => {
// Netcatty says trusted -> accept without even touching the system files.
let consulted = false;
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "fp" }),
classifyHostKey: () => ({ status: "trusted" }),
},
isHostKeyTrustedBySystem: () => {
consulted = true;
return false;
},
});
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p", knownHosts: [] } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
let verdict;
FakeSSHClient.instances[0].connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, true);
assert.equal(consulted, false);
});
test("a Netcatty 'changed' key is NOT rescued by a non-matching system check (key rotation stays rejected)", async () => {
// Netcatty flags a key rotation (changed). The system check is consulted with
// the LIVE fingerprint; since the system does not record this exact new key,
// it returns false and the connection is refused — the mismatch is never
// silently accepted.
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "rotated-fp" }),
classifyHostKey: () => ({ status: "changed", expectedFingerprint: "old-fp" }),
},
isHostKeyTrustedBySystem: () => false,
});
const session = { moshStatsAuth: { hostname: "h", username: "u", password: "p", knownHosts: [] } };
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
let verdict;
FakeSSHClient.instances[0].connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, false);
});
test("works when isHostKeyTrustedBySystem is not wired in (optional dependency)", async () => {
// Backward-compat: an api built without the system-known_hosts dependency
// must not throw; it simply falls back to the Netcatty-only decision.
const { api, sessions } = makeApi({
hostKeyVerifier: {
describeHostKey: () => ({ keyType: "ssh-ed25519", fingerprint: "fp" }),
classifyHostKey: () => ({ status: "unknown" }),
},
isHostKeyTrustedBySystem: undefined,
});
const session = {
moshStatsAuth: {
hostname: "h",
username: "u",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----",
knownHosts: [],
},
};
sessions.set("sid", session);
api.ensureMoshStatsConnection(session, "sid");
await tick();
let verdict;
FakeSSHClient.instances[0].connectOpts.hostVerifier(Buffer.from("rawkey"), (ok) => { verdict = ok; });
assert.equal(verdict, false);
});