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
168 lines
4.4 KiB
JavaScript
168 lines
4.4 KiB
JavaScript
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 { EventEmitter } = require("node:events");
|
|
const Module = require("node:module");
|
|
|
|
const passphraseHandler = require("./passphraseHandler.cjs");
|
|
|
|
function loadBridgeWithMockedSsh2(t, ClientClass) {
|
|
const bridgePath = require.resolve("./sshBridge.cjs");
|
|
const authHelperPath = require.resolve("./sshAuthHelper.cjs");
|
|
const originalLoad = Module._load;
|
|
let connectCount = 0;
|
|
|
|
class MockSSHClient extends EventEmitter {
|
|
connect() {
|
|
connectCount += 1;
|
|
this.emit("error", new Error("unexpected connect"));
|
|
}
|
|
|
|
end() {}
|
|
|
|
exec() {}
|
|
}
|
|
|
|
Module._load = function patchedLoad(request, parent, isMain) {
|
|
if (request === "ssh2") {
|
|
return {
|
|
Client: ClientClass || MockSSHClient,
|
|
utils: {
|
|
parseKey: () => new Error("bad passphrase"),
|
|
},
|
|
};
|
|
}
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
|
|
delete require.cache[bridgePath];
|
|
delete require.cache[authHelperPath];
|
|
const bridge = require("./sshBridge.cjs");
|
|
|
|
t.after(() => {
|
|
delete require.cache[bridgePath];
|
|
delete require.cache[authHelperPath];
|
|
Module._load = originalLoad;
|
|
});
|
|
|
|
return {
|
|
bridge,
|
|
getConnectCount: () => connectCount,
|
|
};
|
|
}
|
|
|
|
class SuccessfulSshClient extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
this.socketTimeouts = [];
|
|
this._sock = { setTimeout: (value) => this.socketTimeouts.push(value) };
|
|
SuccessfulSshClient.instances.push(this);
|
|
}
|
|
|
|
connect(options) {
|
|
this.connectOptions = options;
|
|
queueMicrotask(() => {
|
|
this.emit("connect");
|
|
this.emit("ready");
|
|
});
|
|
}
|
|
|
|
exec(_command, callback) {
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
callback(null, stream);
|
|
queueMicrotask(() => stream.emit("close", 0));
|
|
}
|
|
|
|
end() {}
|
|
}
|
|
SuccessfulSshClient.instances = [];
|
|
|
|
function createEncryptedIdentityFile(t) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-ssh-exec-"));
|
|
t.after(() => {
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
const keyPath = path.join(dir, "id_ed25519");
|
|
fs.writeFileSync(
|
|
keyPath,
|
|
"-----BEGIN ENCRYPTED PRIVATE KEY-----\nabc\n-----END ENCRYPTED PRIVATE KEY-----\n",
|
|
"utf8",
|
|
);
|
|
return keyPath;
|
|
}
|
|
|
|
test("execCommand stops when an identity file passphrase prompt is cancelled", async (t) => {
|
|
const keyPath = createEncryptedIdentityFile(t);
|
|
const originalRequestPassphrase = passphraseHandler.requestPassphrase;
|
|
t.after(() => {
|
|
passphraseHandler.requestPassphrase = originalRequestPassphrase;
|
|
});
|
|
passphraseHandler.requestPassphrase = async () => ({ cancelled: true });
|
|
|
|
const { bridge, getConnectCount } = loadBridgeWithMockedSsh2(t);
|
|
const ipcMain = {
|
|
handlers: new Map(),
|
|
handle(channel, handler) {
|
|
this.handlers.set(channel, handler);
|
|
},
|
|
on() {},
|
|
};
|
|
bridge.registerHandlers(ipcMain);
|
|
const execHandler = ipcMain.handlers.get("netcatty:ssh:exec");
|
|
|
|
await assert.rejects(
|
|
() => execHandler(
|
|
{
|
|
sender: {
|
|
isDestroyed: () => false,
|
|
send: () => {},
|
|
},
|
|
},
|
|
{
|
|
hostname: "example.test",
|
|
username: "alice",
|
|
command: "true",
|
|
identityFilePaths: [keyPath],
|
|
timeout: 100,
|
|
},
|
|
),
|
|
/Passphrase entry cancelled/,
|
|
);
|
|
assert.equal(getConnectCount(), 0);
|
|
});
|
|
|
|
test("execCommand applies separate host connection timeouts", async (t) => {
|
|
SuccessfulSshClient.instances = [];
|
|
const { bridge } = loadBridgeWithMockedSsh2(t, SuccessfulSshClient);
|
|
const ipcMain = {
|
|
handlers: new Map(),
|
|
handle(channel, handler) {
|
|
this.handlers.set(channel, handler);
|
|
},
|
|
on() {},
|
|
};
|
|
bridge.registerHandlers(ipcMain);
|
|
|
|
const execHandler = ipcMain.handlers.get("netcatty:ssh:exec");
|
|
const result = await execHandler(
|
|
{ sender: { isDestroyed: () => false, send: () => {} } },
|
|
{
|
|
hostname: "example.test",
|
|
username: "alice",
|
|
command: "true",
|
|
timeout: 30_000,
|
|
sshTcpConnectTimeoutMs: 45_000,
|
|
sshAuthReadyTimeoutMs: 300_000,
|
|
},
|
|
);
|
|
|
|
const client = SuccessfulSshClient.instances[0];
|
|
assert.equal(client.connectOptions.timeout, 45_000);
|
|
assert.equal(client.connectOptions.readyTimeout, 0);
|
|
assert.deepEqual(client.socketTimeouts, [0]);
|
|
assert.equal(result.code, 0);
|
|
});
|