Files
Netcatty/electron/bridges/sshAuthHelper.passwordOnly.test.cjs
T
bincxz 94749e92ef fix(ssh): do not fall back to default keys for password-only hosts
Direct terminal sessions still tried ~/.ssh default keys after a saved
password failed, while jump-host and SFTP paths (buildAuthHandler) only
tried password. That made a wrong/stale host password look fine on direct
login but fail ProxyJump and SFTP until credentials were re-saved (e.g.
via Keychain).

Align startSession with #266 password-only policy and pin buildAuthHandler
behavior with regression tests.

Fixes #2079
2026-07-13 13:18:37 +08:00

128 lines
4.3 KiB
JavaScript

"use strict";
/**
* Password-only auth must not probe default ~/.ssh keys (issues #266 / #2079).
* Jump hosts and SFTP share buildAuthHandler; a wrong host password used to
* look fine on direct terminal (startSession still fell back to id_rsa) while
* ProxyJump / SFTP failed after password rejection alone.
*/
const test = require("node:test");
const assert = require("node:assert/strict");
const { buildAuthHandler } = require("./sshAuthHelper.cjs");
const DEFAULT_KEYS = [
{
keyName: "id_ed25519",
keyPath: "/tmp/id_ed25519",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\ned\n-----END OPENSSH PRIVATE KEY-----\n",
},
{
keyName: "id_rsa",
keyPath: "/tmp/id_rsa",
privateKey: "-----BEGIN RSA PRIVATE KEY-----\nrsa\n-----END RSA PRIVATE KEY-----\n",
},
];
/**
* Walk the ssh2-style authHandler and collect method labels.
* Object methods expose type (and sometimes username/key); string methods
* come from the simple ordered-list path (e.g. password-only).
*/
function collectAuthMethods(authHandler, maxSteps = 16) {
const labels = [];
let methodsLeft = null;
for (let i = 0; i < maxSteps; i += 1) {
let offered = null;
authHandler(methodsLeft, false, (method) => {
offered = method;
});
if (offered == null || offered === false) break;
if (typeof offered === "string") {
labels.push(offered);
} else if (offered && typeof offered === "object") {
labels.push(offered.type || "unknown");
} else {
break;
}
// Keep all common methods available so the handler can walk its full list.
methodsLeft = ["publickey", "password", "keyboard-interactive", "agent"];
}
return labels;
}
test("buildAuthHandler password-only does not offer default SSH keys (#2079)", () => {
const auth = buildAuthHandler({
password: "wrong-or-stale-secret",
username: "root",
defaultKeys: DEFAULT_KEYS,
allowAgentFallback: false,
});
const labels = collectAuthMethods(auth.authHandler);
assert.ok(labels.includes("password"), `expected password; got ${labels.join(",")}`);
assert.ok(labels.includes("keyboard-interactive"), `expected KI; got ${labels.join(",")}`);
assert.equal(
labels.includes("publickey"),
false,
`password-only must not probe default keys; offered=${labels.join(",")}`,
);
assert.equal(labels.includes("agent"), false);
});
test("buildAuthHandler with no credentials still offers default keys", () => {
const auth = buildAuthHandler({
username: "root",
defaultKeys: DEFAULT_KEYS,
allowAgentFallback: false,
});
const labels = collectAuthMethods(auth.authHandler);
assert.ok(
labels.includes("publickey"),
`expected default-key fallback when no explicit auth; offered=${labels.join(",")}`,
);
});
test("buildAuthHandler key+password still allows default key fallback after user key", () => {
const auth = buildAuthHandler({
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nuser\n-----END OPENSSH PRIVATE KEY-----\n",
password: "also-have-password",
username: "root",
defaultKeys: DEFAULT_KEYS,
allowAgentFallback: false,
});
const labels = collectAuthMethods(auth.authHandler);
assert.ok(labels.includes("publickey"), `expected publickey; offered=${labels.join(",")}`);
assert.ok(labels.includes("password"), `expected password; offered=${labels.join(",")}`);
// user key + at least one default key => multiple publickey offers
const publickeyCount = labels.filter((l) => l === "publickey").length;
assert.ok(
publickeyCount >= 2,
`key auth may still fall back to default keys; offered=${labels.join(",")}`,
);
});
test("buildAuthHandler password-only may still attach unlocked encrypted keys for jump retry", () => {
const auth = buildAuthHandler({
password: "host-password",
username: "root",
defaultKeys: DEFAULT_KEYS,
allowAgentFallback: false,
unlockedEncryptedKeys: [{
keyName: "id_encrypted",
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----\nenc\n-----END OPENSSH PRIVATE KEY-----\n",
passphrase: "key-pass",
}],
});
const labels = collectAuthMethods(auth.authHandler);
assert.ok(labels.includes("password"), `offered=${labels.join(",")}`);
assert.ok(
labels.includes("publickey"),
`jump-chain retry may still offer unlocked keys; offered=${labels.join(",")}`,
);
});