mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-16 20:11:08 +00:00
* 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>
394 lines
12 KiB
JavaScript
394 lines
12 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const crypto = require("node:crypto");
|
|
const path = require("node:path");
|
|
const os = require("node:os");
|
|
|
|
const { createSystemKnownHostsApi } = require("./systemKnownHosts.cjs");
|
|
|
|
// Build an api whose fs.readFileSync returns the given content for the FIRST
|
|
// system known_hosts path and throws (ENOENT-like) for the rest, mirroring the
|
|
// common case of only `~/.ssh/known_hosts` existing.
|
|
function makeApi(fileContents = {}) {
|
|
const reads = [];
|
|
const fs = {
|
|
readFileSync(filePath) {
|
|
reads.push(filePath);
|
|
if (Object.prototype.hasOwnProperty.call(fileContents, filePath)) {
|
|
return fileContents[filePath];
|
|
}
|
|
const err = new Error(`ENOENT: ${filePath}`);
|
|
err.code = "ENOENT";
|
|
throw err;
|
|
},
|
|
};
|
|
const logs = [];
|
|
const api = createSystemKnownHostsApi({
|
|
fs,
|
|
path,
|
|
os,
|
|
crypto,
|
|
log: (...args) => logs.push(args),
|
|
});
|
|
return { api, reads, logs };
|
|
}
|
|
|
|
// SHA-256 base64 (no padding) fingerprint of an OpenSSH public-key blob.
|
|
function fingerprintOf(base64Key) {
|
|
return crypto
|
|
.createHash("sha256")
|
|
.update(Buffer.from(base64Key, "base64"))
|
|
.digest("base64")
|
|
.replace(/=+$/g, "");
|
|
}
|
|
|
|
// A valid-looking ed25519 key blob seeded deterministically.
|
|
function keyBlob(seed) {
|
|
return (
|
|
"AAAAC3NzaC1lZDI1NTE5AAAAI" +
|
|
crypto.createHash("sha256").update(seed).digest("base64").slice(0, 27)
|
|
);
|
|
}
|
|
|
|
// Produce a hashed host field `|1|salt|HMAC-SHA1(salt, token)` for `token`,
|
|
// exactly as `ssh-keygen -H` would (verified empirically against ssh-keygen).
|
|
function hashedHostField(token, salt = crypto.randomBytes(20)) {
|
|
const hash = crypto.createHmac("sha1", salt).update(token).digest("base64");
|
|
return `|1|${salt.toString("base64")}|${hash}`;
|
|
}
|
|
|
|
const HOME_KH = path.join(os.homedir(), ".ssh", "known_hosts");
|
|
|
|
test("system known_hosts paths include the OpenSSH defaults for the platform", () => {
|
|
const { api } = makeApi();
|
|
const paths = api.getSystemKnownHostsPaths();
|
|
assert.ok(paths.includes(HOME_KH), "must include ~/.ssh/known_hosts");
|
|
if (process.platform === "win32") {
|
|
assert.ok(
|
|
paths.some((p) => /ssh[\\/]known_hosts$/i.test(p) && /ProgramData/i.test(p)),
|
|
"Windows must include %PROGRAMDATA%/ssh/known_hosts",
|
|
);
|
|
} else {
|
|
assert.ok(paths.includes("/etc/ssh/ssh_known_hosts"));
|
|
}
|
|
});
|
|
|
|
test("trusts a plain entry whose fingerprint matches (default port)", () => {
|
|
const blob = keyBlob("plain");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `example.com ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
port: 22,
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("matches hostnames case-insensitively", () => {
|
|
const blob = keyBlob("case");
|
|
const { api } = makeApi({ [HOME_KH]: `Example.COM ssh-ed25519 ${blob}\n` });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("matches a comma-separated host list", () => {
|
|
const blob = keyBlob("list");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `alias.example.com,192.0.2.5 ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "192.0.2.5",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("matches a [host]:port entry only on the right non-default port", () => {
|
|
const blob = keyBlob("port");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `[example.com]:2222 ssh-ed25519 ${blob}\n`,
|
|
});
|
|
const fingerprint = fingerprintOf(blob);
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", port: 2222, fingerprint }),
|
|
true,
|
|
);
|
|
// Same host, wrong port -> not trusted.
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", port: 22, fingerprint }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("does NOT trust a fingerprint mismatch (different key for the same host)", () => {
|
|
const stored = keyBlob("stored");
|
|
const live = keyBlob("live-different");
|
|
const { api } = makeApi({ [HOME_KH]: `example.com ssh-ed25519 ${stored}\n` });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
fingerprint: fingerprintOf(live),
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("does NOT trust when the host does not appear at all", () => {
|
|
const blob = keyBlob("other");
|
|
const { api } = makeApi({ [HOME_KH]: `other.example.com ssh-ed25519 ${blob}\n` });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("trusts a hashed entry whose token + fingerprint match (default port)", () => {
|
|
const blob = keyBlob("hashed-default");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `${hashedHostField("example.com")} ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
port: 22,
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("trusts a hashed entry for a non-default port ([host]:port token)", () => {
|
|
const blob = keyBlob("hashed-port");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `${hashedHostField("[h.example.com]:2022")} ssh-ed25519 ${blob}\n`,
|
|
});
|
|
const fingerprint = fingerprintOf(blob);
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "h.example.com", port: 2022, fingerprint }),
|
|
true,
|
|
);
|
|
// The same hashed entry must NOT match the default-port (bare-host) token.
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "h.example.com", port: 22, fingerprint }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("hashed entry does not match a different hostname (HMAC differs)", () => {
|
|
const blob = keyBlob("hashed-wrong-host");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `${hashedHostField("example.com")} ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "evil.example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("matches against ssh-keygen-generated hashed entries (real fixtures)", () => {
|
|
// These two lines were produced by `ssh-keygen -H` from:
|
|
// example.com ssh-ed25519 …KEYDATA0000…
|
|
// [example.com]:2222 ssh-ed25519 …KEYDATA1111…
|
|
// and pin the exact HMAC-SHA1 hashing OpenSSH uses (incl. the bracketed
|
|
// token for the non-default port).
|
|
const blobA = "AAAAC3NzaC1lZDI1NTE5AAAAITESTKEYDATA0000000000000000000000000";
|
|
const blobB = "AAAAC3NzaC1lZDI1NTE5AAAAITESTKEYDATA1111111111111111111111111";
|
|
const content =
|
|
`|1|GjfxyrxES8V34vZje/1Lt1hHg/Y=|ZEnB8OFqAbq3mcme43V+dukJ51I= ssh-ed25519 ${blobA}\n` +
|
|
`|1|uZT6RsKBJirh9q9ycDnQUhVSmqI=|auufYDNOuFA17oSrmJwneIyl9po= ssh-ed25519 ${blobB}\n`;
|
|
const { api } = makeApi({ [HOME_KH]: content });
|
|
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
port: 22,
|
|
fingerprint: fingerprintOf(blobA),
|
|
}),
|
|
true,
|
|
"default-port hashed entry must match",
|
|
);
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
port: 2222,
|
|
fingerprint: fingerprintOf(blobB),
|
|
}),
|
|
true,
|
|
"non-default-port hashed entry must match",
|
|
);
|
|
// The 2222 key must NOT be accepted for port 22 (token differs).
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
port: 22,
|
|
fingerprint: fingerprintOf(blobB),
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a @revoked entry with a matching fingerprint forces NOT trusted", () => {
|
|
const blob = keyBlob("revoked");
|
|
const fingerprint = fingerprintOf(blob);
|
|
// Even if a non-revoked entry would also match, the revoked one wins.
|
|
const content =
|
|
`example.com ssh-ed25519 ${blob}\n` +
|
|
`@revoked example.com ssh-ed25519 ${blob}\n`;
|
|
const { api } = makeApi({ [HOME_KH]: content });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", fingerprint }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a @revoked hashed entry also forces NOT trusted", () => {
|
|
const blob = keyBlob("revoked-hashed");
|
|
const fingerprint = fingerprintOf(blob);
|
|
const content = `@revoked ${hashedHostField("example.com")} ssh-ed25519 ${blob}\n`;
|
|
const { api } = makeApi({ [HOME_KH]: content });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", fingerprint }),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("a @cert-authority line is skipped (not a literal host-key match)", () => {
|
|
const blob = keyBlob("ca");
|
|
const { api } = makeApi({
|
|
[HOME_KH]: `@cert-authority *.example.com ssh-ed25519 ${blob}\n`,
|
|
});
|
|
// Fingerprint matches the CA key, but CA delegation is not modeled -> not
|
|
// trusted via this path.
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "host.example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
false,
|
|
);
|
|
});
|
|
|
|
test("comments, blank lines, and malformed lines are ignored", () => {
|
|
const blob = keyBlob("with-comments");
|
|
const content = [
|
|
"# a comment",
|
|
"",
|
|
" ",
|
|
"garbage-without-enough-fields",
|
|
`example.com ssh-ed25519 ${blob}`,
|
|
"# trailing comment",
|
|
].join("\n");
|
|
const { api } = makeApi({ [HOME_KH]: content });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("wildcard / negation host patterns are not honored for trust", () => {
|
|
const blob = keyBlob("wild");
|
|
const fingerprint = fingerprintOf(blob);
|
|
const wildcard = makeApi({ [HOME_KH]: `*.example.com ssh-ed25519 ${blob}\n` });
|
|
assert.equal(
|
|
wildcard.api.isHostKeyTrustedBySystem({ hostname: "host.example.com", fingerprint }),
|
|
false,
|
|
"a wildcard entry must not vouch for a specific host's key we never saw",
|
|
);
|
|
const negated = makeApi({
|
|
[HOME_KH]: `!example.com,example.com ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
negated.api.isHostKeyTrustedBySystem({ hostname: "example.com", fingerprint }),
|
|
true,
|
|
"the non-negated token in the list still matches",
|
|
);
|
|
});
|
|
|
|
test("combines multiple system files (home + /etc) into the trust set", () => {
|
|
const blob = keyBlob("etc");
|
|
const etcPath = process.platform === "win32"
|
|
? path.join(process.env.PROGRAMDATA || "C:\\ProgramData", "ssh", "known_hosts")
|
|
: "/etc/ssh/ssh_known_hosts";
|
|
const { api } = makeApi({
|
|
[HOME_KH]: "# only comments here\n",
|
|
[etcPath]: `shared.example.com ssh-ed25519 ${blob}\n`,
|
|
});
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "shared.example.com",
|
|
fingerprint: fingerprintOf(blob),
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("returns false (fail-closed) when no system files exist", () => {
|
|
const { api, reads } = makeApi(); // every read throws ENOENT
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({
|
|
hostname: "example.com",
|
|
fingerprint: "anything",
|
|
}),
|
|
false,
|
|
);
|
|
assert.ok(reads.length >= 1, "should have attempted to read at least one path");
|
|
});
|
|
|
|
test("returns false on empty/whitespace fingerprint or hostname", () => {
|
|
const blob = keyBlob("guard");
|
|
const { api } = makeApi({ [HOME_KH]: `example.com ssh-ed25519 ${blob}\n` });
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "", fingerprint: fingerprintOf(blob) }),
|
|
false,
|
|
);
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", fingerprint: "" }),
|
|
false,
|
|
);
|
|
assert.equal(api.isHostKeyTrustedBySystem({}), false);
|
|
});
|
|
|
|
test("fingerprint comparison ignores base64 padding differences", () => {
|
|
const blob = keyBlob("padding");
|
|
const { api } = makeApi({ [HOME_KH]: `example.com ssh-ed25519 ${blob}\n` });
|
|
const padded = `${fingerprintOf(blob)}==`;
|
|
assert.equal(
|
|
api.isHostKeyTrustedBySystem({ hostname: "example.com", fingerprint: padded }),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("parseKnownHostsLine extracts markers and fingerprint", () => {
|
|
const blob = keyBlob("parse-line");
|
|
const { api } = makeApi();
|
|
const entry = api.parseKnownHostsLine(`@revoked example.com ssh-rsa ${blob}`);
|
|
assert.equal(entry.revoked, true);
|
|
assert.equal(entry.certAuthority, false);
|
|
assert.equal(entry.hostField, "example.com");
|
|
assert.equal(entry.keyType, "ssh-rsa");
|
|
assert.equal(entry.fingerprint, fingerprintOf(blob));
|
|
assert.equal(api.parseKnownHostsLine("# comment"), null);
|
|
assert.equal(api.parseKnownHostsLine(""), null);
|
|
assert.equal(api.parseKnownHostsLine("too few"), null);
|
|
});
|