mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 18:16:32 +00:00
* refactor: streamline codebase, unify duplicated modules, and consolidate tests * fix(test): batch discovered test files into Windows-safe command lines
486 lines
15 KiB
JavaScript
486 lines
15 KiB
JavaScript
"use strict";
|
|
|
|
// --- merged from sshAuthHelper.identityFile.test.cjs ---
|
|
{
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { spawnSync } = require("node:child_process");
|
|
|
|
const {
|
|
loadIdentityFileForAuth,
|
|
preparePrivateKeyForAuth,
|
|
isPassphraseCancelledError,
|
|
} = require("./sshAuthHelper.cjs");
|
|
const passphraseHandler = require("./passphraseHandler.cjs");
|
|
|
|
function createKey(t, passphrase) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-identity-file-"));
|
|
t.after(() => {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
|
|
const keyPath = path.join(dir, "id_ed25519");
|
|
const result = spawnSync("ssh-keygen", [
|
|
"-q",
|
|
"-t",
|
|
"ed25519",
|
|
"-N",
|
|
passphrase,
|
|
"-f",
|
|
keyPath,
|
|
"-C",
|
|
"netcatty-test",
|
|
], { encoding: "utf8" });
|
|
|
|
if (result.status !== 0) {
|
|
t.skip("ssh-keygen is unavailable");
|
|
return null;
|
|
}
|
|
|
|
return keyPath;
|
|
}
|
|
|
|
function createEncryptedKey(t) {
|
|
return createKey(t, "secret");
|
|
}
|
|
|
|
function createSender() {
|
|
const events = [];
|
|
return {
|
|
events,
|
|
sender: {
|
|
isDestroyed: () => false,
|
|
send: (channel, payload) => {
|
|
events.push({ channel, payload });
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
test("loadIdentityFileForAuth accepts an unencrypted Ed25519 private key", async (t) => {
|
|
const keyPath = createKey(t, "");
|
|
if (!keyPath) return;
|
|
|
|
const identityFile = await loadIdentityFileForAuth({
|
|
keyPath,
|
|
hostname: "example.test",
|
|
});
|
|
|
|
assert.equal(identityFile.keyPath, keyPath);
|
|
assert.match(identityFile.privateKey, /BEGIN OPENSSH PRIVATE KEY/);
|
|
});
|
|
|
|
test("loadIdentityFileForAuth rejects a public-only identity file", async (t) => {
|
|
const keyPath = createKey(t, "");
|
|
if (!keyPath) return;
|
|
|
|
const publicKeyPath = `${keyPath}.pub`;
|
|
const publicKey = fs.readFileSync(publicKeyPath, "utf8");
|
|
|
|
await assert.rejects(
|
|
() => loadIdentityFileForAuth({
|
|
keyPath: publicKeyPath,
|
|
hostname: "example.test",
|
|
}),
|
|
(err) => {
|
|
assert.equal(err.code, "ERR_PRIVATE_KEY_INVALID");
|
|
assert.ok(err.message.includes(publicKeyPath));
|
|
assert.equal(err.message.includes(publicKey.trim()), false);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth rejects a public-only inline key", async (t) => {
|
|
const keyPath = createKey(t, "");
|
|
if (!keyPath) return;
|
|
|
|
const publicKey = fs.readFileSync(`${keyPath}.pub`, "utf8");
|
|
|
|
await assert.rejects(
|
|
() => preparePrivateKeyForAuth({
|
|
privateKey: publicKey,
|
|
keyName: "public-only",
|
|
hostname: "example.test",
|
|
}),
|
|
(err) => {
|
|
assert.equal(err.code, "ERR_PRIVATE_KEY_INVALID");
|
|
assert.equal(err.message.includes(publicKey.trim()), false);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test("loadIdentityFileForAuth rejects malformed key content without a raw fallback", async (t) => {
|
|
const keyPath = createKey(t, "");
|
|
if (!keyPath) return;
|
|
fs.writeFileSync(keyPath, "not a private key");
|
|
|
|
await assert.rejects(
|
|
() => loadIdentityFileForAuth({
|
|
keyPath,
|
|
hostname: "example.test",
|
|
}),
|
|
(err) => {
|
|
assert.equal(err.code, "ERR_PRIVATE_KEY_INVALID");
|
|
assert.ok(err.message.includes(keyPath));
|
|
assert.equal(err.message.includes("not a private key"), false);
|
|
return true;
|
|
},
|
|
);
|
|
});
|
|
|
|
test("loadIdentityFileForAuth uses a valid saved passphrase without prompting", async (t) => {
|
|
const keyPath = createEncryptedKey(t);
|
|
if (!keyPath) return;
|
|
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
passphraseHandler.requestPassphrase = async () => {
|
|
throw new Error("Unexpected passphrase prompt");
|
|
};
|
|
|
|
const { sender, events } = createSender();
|
|
const identityFile = await loadIdentityFileForAuth({
|
|
sender,
|
|
keyPath,
|
|
hostname: "example.test",
|
|
initialPassphrase: "secret",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.equal(identityFile.passphrase, "secret");
|
|
assert.match(identityFile.privateKey, /BEGIN OPENSSH PRIVATE KEY/);
|
|
assert.deepEqual(events, []);
|
|
});
|
|
|
|
test("loadIdentityFileForAuth clears an invalid saved passphrase before prompting", async (t) => {
|
|
const keyPath = createEncryptedKey(t);
|
|
if (!keyPath) return;
|
|
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
|
|
let promptCount = 0;
|
|
passphraseHandler.requestPassphrase = async (_sender, promptedPath, keyName, hostname, passphraseInvalid) => {
|
|
promptCount += 1;
|
|
assert.equal(promptedPath, keyPath);
|
|
assert.equal(keyName, "id_ed25519");
|
|
assert.equal(hostname, "example.test");
|
|
assert.equal(passphraseInvalid, true);
|
|
return { passphrase: "secret" };
|
|
};
|
|
|
|
const { sender, events } = createSender();
|
|
const identityFile = await loadIdentityFileForAuth({
|
|
sender,
|
|
keyPath,
|
|
hostname: "example.test",
|
|
initialPassphrase: "wrong",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.equal(promptCount, 1);
|
|
assert.equal(identityFile.passphrase, "secret");
|
|
assert.deepEqual(events, [
|
|
{
|
|
channel: "netcatty:passphrase-auth-failed",
|
|
payload: { keyPaths: [keyPath] },
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth prompts for encrypted inline private keys", async (t) => {
|
|
const keyPath = createEncryptedKey(t);
|
|
if (!keyPath) return;
|
|
const privateKey = fs.readFileSync(keyPath, "utf8");
|
|
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
|
|
let promptCount = 0;
|
|
const promptEvents = [];
|
|
passphraseHandler.requestPassphrase = async (_sender, promptedPath, keyName, hostname, passphraseInvalid) => {
|
|
promptCount += 1;
|
|
promptEvents.push("request");
|
|
assert.equal(promptedPath, "SSH key for export-key");
|
|
assert.equal(keyName, "export-key");
|
|
assert.equal(hostname, "example.test");
|
|
assert.equal(passphraseInvalid, false);
|
|
return { passphrase: "secret" };
|
|
};
|
|
|
|
const { sender, events } = createSender();
|
|
const prepared = await preparePrivateKeyForAuth({
|
|
sender,
|
|
privateKey,
|
|
keyId: "key-1",
|
|
keyName: "export-key",
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
onPassphrasePromptShown: () => promptEvents.push("shown"),
|
|
onPassphrasePromptResolved: () => promptEvents.push("resolved"),
|
|
});
|
|
|
|
assert.equal(promptCount, 1);
|
|
assert.deepEqual(promptEvents, ["shown", "request", "resolved"]);
|
|
assert.equal(prepared.passphrase, "secret");
|
|
assert.equal(prepared.privateKey, privateKey);
|
|
assert.deepEqual(events, []);
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth clears invalid saved inline private key passphrases", async (t) => {
|
|
const keyPath = createEncryptedKey(t);
|
|
if (!keyPath) return;
|
|
const privateKey = fs.readFileSync(keyPath, "utf8");
|
|
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
|
|
passphraseHandler.requestPassphrase = async (_sender, promptedPath, keyName, hostname, passphraseInvalid) => {
|
|
assert.equal(promptedPath, "SSH key for export-key");
|
|
assert.equal(keyName, "export-key");
|
|
assert.equal(hostname, "example.test");
|
|
assert.equal(passphraseInvalid, true);
|
|
return { passphrase: "secret" };
|
|
};
|
|
|
|
const { sender, events } = createSender();
|
|
const prepared = await preparePrivateKeyForAuth({
|
|
sender,
|
|
privateKey,
|
|
keyId: "key-1",
|
|
keyName: "export-key",
|
|
hostname: "example.test",
|
|
initialPassphrase: "wrong",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.equal(prepared.passphrase, "secret");
|
|
assert.deepEqual(events, [
|
|
{
|
|
channel: "netcatty:passphrase-auth-failed",
|
|
payload: { keyPaths: ["SSH key for export-key"], keyIds: ["key-1"] },
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth throws when the passphrase prompt is cancelled", async (t) => {
|
|
const keyPath = createEncryptedKey(t);
|
|
if (!keyPath) return;
|
|
const privateKey = fs.readFileSync(keyPath, "utf8");
|
|
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
|
|
passphraseHandler.requestPassphrase = async () => ({ cancelled: true });
|
|
|
|
await assert.rejects(
|
|
() => preparePrivateKeyForAuth({
|
|
sender: createSender().sender,
|
|
privateKey,
|
|
keyId: "key-1",
|
|
keyName: "export-key",
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
}),
|
|
(err) => isPassphraseCancelledError(err),
|
|
);
|
|
});
|
|
}
|
|
|
|
// --- merged from sshAuthHelper.pkcs8.test.cjs ---
|
|
{
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const crypto = require("node:crypto");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { spawnSync } = require("node:child_process");
|
|
const { utils: sshUtils } = require("ssh2");
|
|
|
|
const {
|
|
preparePrivateKeyForAuth,
|
|
loadIdentityFileForAuth,
|
|
} = require("./sshAuthHelper.cjs");
|
|
const passphraseHandler = require("./passphraseHandler.cjs");
|
|
|
|
function genRsaPkcs8(passphrase) {
|
|
return crypto.generateKeyPairSync("rsa", {
|
|
modulusLength: 2048,
|
|
privateKeyEncoding: passphrase
|
|
? { type: "pkcs8", format: "pem", cipher: "aes-256-cbc", passphrase }
|
|
: { type: "pkcs8", format: "pem" },
|
|
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
}).privateKey;
|
|
}
|
|
|
|
function isParseable(key) {
|
|
const parsed = sshUtils.parseKey(key);
|
|
return !!parsed && !(parsed instanceof Error);
|
|
}
|
|
|
|
const sender = { isDestroyed: () => false, send: () => {} };
|
|
|
|
test("preparePrivateKeyForAuth converts an unencrypted PKCS#8 key for ssh2", async () => {
|
|
const result = await preparePrivateKeyForAuth({
|
|
sender,
|
|
privateKey: genRsaPkcs8(),
|
|
keyName: "oracle.key",
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.ok(result, "expected a prepared key");
|
|
assert.ok(isParseable(result.privateKey), "prepared key should be parseable by ssh2");
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth decrypts and converts an encrypted PKCS#8 key", async (t) => {
|
|
const original = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = original;
|
|
});
|
|
|
|
let calls = 0;
|
|
passphraseHandler.requestPassphrase = async () => {
|
|
calls += 1;
|
|
return calls === 1 ? { passphrase: "secret" } : { passphrase: null };
|
|
};
|
|
|
|
const result = await preparePrivateKeyForAuth({
|
|
sender,
|
|
privateKey: genRsaPkcs8("secret"),
|
|
keyName: "oracle.key",
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.ok(result, "expected a prepared key");
|
|
assert.equal(calls, 1, "correct passphrase should not be re-prompted");
|
|
assert.ok(isParseable(result.privateKey), "prepared key should be parseable by ssh2");
|
|
assert.equal(result.passphrase, undefined, "passphrase is unnecessary after conversion");
|
|
});
|
|
|
|
test("loadIdentityFileForAuth converts an unencrypted PKCS#8 identity file", async (t) => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-pkcs8-"));
|
|
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
const keyPath = path.join(dir, "oracle.key");
|
|
fs.writeFileSync(keyPath, genRsaPkcs8(), "utf8");
|
|
|
|
const result = await loadIdentityFileForAuth({
|
|
sender,
|
|
keyPath,
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.ok(result, "expected a loaded identity file");
|
|
assert.ok(isParseable(result.privateKey), "prepared key should be parseable by ssh2");
|
|
});
|
|
|
|
test("preparePrivateKeyForAuth recovers a mangled encrypted OpenSSH key via passphrase prompt", async (t) => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-mangled-openssh-"));
|
|
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
const keyPath = path.join(dir, "id_ed25519");
|
|
const gen = spawnSync(
|
|
"ssh-keygen",
|
|
["-q", "-t", "ed25519", "-N", "secret", "-f", keyPath, "-C", "netcatty-test"],
|
|
{ encoding: "utf8" },
|
|
);
|
|
if (gen.status !== 0) {
|
|
t.skip("ssh-keygen is unavailable");
|
|
return;
|
|
}
|
|
// Simulate a key whose line breaks were flattened into literal "\n" on paste.
|
|
const mangled = fs.readFileSync(keyPath, "utf8").replace(/\n/g, "\\n");
|
|
|
|
const originalRequest = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequest;
|
|
});
|
|
let prompts = 0;
|
|
passphraseHandler.requestPassphrase = async () => {
|
|
prompts += 1;
|
|
return { passphrase: "secret" };
|
|
};
|
|
|
|
const result = await preparePrivateKeyForAuth({
|
|
sender,
|
|
privateKey: mangled,
|
|
keyName: "id_ed25519",
|
|
hostname: "example.test",
|
|
logPrefix: "[Test]",
|
|
});
|
|
|
|
assert.ok(result, "expected a prepared key");
|
|
assert.equal(prompts, 1, "the encrypted key should trigger exactly one passphrase prompt");
|
|
assert.equal(result.passphrase, "secret");
|
|
const parsed = sshUtils.parseKey(result.privateKey, result.passphrase);
|
|
assert.ok(parsed && !(parsed instanceof Error), "prepared key + passphrase should parse in ssh2");
|
|
});
|
|
}
|
|
|
|
// --- merged from sshAuthHelper.userConfiguredKey.test.cjs ---
|
|
{
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
|
|
const { hasUserConfiguredKey } = require("./sshAuthHelper.cjs");
|
|
|
|
test("hasUserConfiguredKey is true for inline private key material", () => {
|
|
assert.equal(hasUserConfiguredKey({ privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----" }), true);
|
|
});
|
|
|
|
test("hasUserConfiguredKey is false for empty or whitespace inline keys", () => {
|
|
assert.equal(hasUserConfiguredKey({ privateKey: "" }), false);
|
|
assert.equal(hasUserConfiguredKey({ privateKey: " " }), false);
|
|
assert.equal(hasUserConfiguredKey({}), false);
|
|
});
|
|
|
|
test("hasUserConfiguredKey is true for Keychain reference identity file paths", () => {
|
|
assert.equal(
|
|
hasUserConfiguredKey({ identityFilePaths: ["/Users/alice/.ssh/id_ed25519"] }),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("hasUserConfiguredKey is false for empty identity file path lists", () => {
|
|
assert.equal(hasUserConfiguredKey({ identityFilePaths: [] }), false);
|
|
assert.equal(hasUserConfiguredKey({ identityFilePaths: null }), false);
|
|
});
|
|
|
|
test("hasUserConfiguredKey prefers explicit inline key over empty paths", () => {
|
|
assert.equal(
|
|
hasUserConfiguredKey({
|
|
privateKey: "-----BEGIN OPENSSH PRIVATE KEY-----",
|
|
identityFilePaths: [],
|
|
}),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("isPasswordProvided accepts whitespace-only passwords (#2036)", () => {
|
|
const { isPasswordProvided } = require("./sshAuthHelper.cjs");
|
|
assert.equal(isPasswordProvided(""), false);
|
|
assert.equal(isPasswordProvided(" "), true);
|
|
assert.equal(isPasswordProvided(" "), true);
|
|
assert.equal(isPasswordProvided("secret"), true);
|
|
assert.equal(isPasswordProvided(null), false);
|
|
assert.equal(isPasswordProvided(undefined), false);
|
|
});
|
|
}
|