Files
NetMesh/electron/bridges/sshAuthHelper.pkcs8.test.cjs
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

129 lines
4.3 KiB
JavaScript

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");
});