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
121 lines
3.8 KiB
JavaScript
121 lines
3.8 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { EventEmitter } = require("node:events");
|
|
const Module = require("node:module");
|
|
|
|
function makeExecStream() {
|
|
const stream = new EventEmitter();
|
|
stream.stderr = new EventEmitter();
|
|
stream.write = () => {};
|
|
stream.close = () => {};
|
|
stream.end = () => {};
|
|
stream.destroy = () => {};
|
|
return stream;
|
|
}
|
|
|
|
function loadSftpBridgeWithFailingFreshSudo(t) {
|
|
const bridgePath = require.resolve("../sftpBridge.cjs");
|
|
const openConnectionPath = require.resolve("./openConnection.cjs");
|
|
const authHelperPath = require.resolve("../sshAuthHelper.cjs");
|
|
const realAuthHelper = require(authHelperPath);
|
|
const originalLoad = Module._load;
|
|
|
|
class MockSftpClient extends EventEmitter {
|
|
constructor() {
|
|
super();
|
|
MockSftpClient.instances.push(this);
|
|
this.sftp = null;
|
|
this.client = new EventEmitter();
|
|
this.client._sock = { setTimeout() {} };
|
|
this.client.setMaxListeners = () => {};
|
|
this.client.connect = () => {
|
|
setImmediate(() => {
|
|
this.client.emit("connect");
|
|
this.client.emit("handshake");
|
|
this.client.emit("ready");
|
|
});
|
|
};
|
|
this.client.exec = (command, execOptions, callback) => {
|
|
const done = typeof execOptions === "function" ? execOptions : callback;
|
|
this.execCommands.push(command);
|
|
const stream = makeExecStream();
|
|
done(null, stream);
|
|
setImmediate(() => {
|
|
if (command.startsWith("test -x ")) stream.emit("close", 1);
|
|
else stream.emit("exit", 127);
|
|
});
|
|
};
|
|
this.client.sftp = (callback) => {
|
|
this.standardSftpCalls += 1;
|
|
callback(null, new EventEmitter());
|
|
};
|
|
this.client.end = () => { this.sshEnded = true; };
|
|
this.client.destroy = () => { this.sshDestroyed = true; };
|
|
this.execCommands = [];
|
|
this.standardSftpCalls = 0;
|
|
this.sshEnded = false;
|
|
this.sshDestroyed = false;
|
|
}
|
|
|
|
end() {
|
|
this.highLevelEnded = true;
|
|
}
|
|
}
|
|
MockSftpClient.instances = [];
|
|
|
|
Module._load = function patchedLoad(request, parent, isMain) {
|
|
if (request === "ssh2-sftp-client") return MockSftpClient;
|
|
if (request === "./sshAuthHelper.cjs") {
|
|
return {
|
|
...realAuthHelper,
|
|
findAllDefaultPrivateKeys: async () => [],
|
|
getAvailableAgentSocket: async () => null,
|
|
prepareSystemSshAgentForAuth: async () => null,
|
|
};
|
|
}
|
|
return originalLoad.call(this, request, parent, isMain);
|
|
};
|
|
|
|
delete require.cache[bridgePath];
|
|
delete require.cache[openConnectionPath];
|
|
const bridge = require("../sftpBridge.cjs");
|
|
t.after(() => {
|
|
delete require.cache[bridgePath];
|
|
delete require.cache[openConnectionPath];
|
|
Module._load = originalLoad;
|
|
});
|
|
return { bridge, MockSftpClient };
|
|
}
|
|
|
|
test("fresh sudo SFTP failure rejects without ordinary SFTP or SCP downgrade", async (t) => {
|
|
const { bridge, MockSftpClient } = loadSftpBridgeWithFailingFreshSudo(t);
|
|
const sftpClients = new Map();
|
|
bridge.init({ sftpClients, sessions: new Map(), electronModule: {} });
|
|
|
|
await assert.rejects(
|
|
bridge.openSftp(
|
|
{ sender: { id: 1, isDestroyed: () => false, send: () => {} } },
|
|
{
|
|
sessionId: "fresh-sudo-failure",
|
|
hostname: "target.example",
|
|
port: 22,
|
|
username: "alice",
|
|
password: "secret",
|
|
authMethod: "password",
|
|
useSshAgent: false,
|
|
verifyHostKeys: false,
|
|
fileProtocol: "auto",
|
|
sudo: true,
|
|
},
|
|
),
|
|
/SFTP sudo failed with exit code 127/,
|
|
);
|
|
|
|
const client = MockSftpClient.instances[0];
|
|
assert.ok(client.execCommands.some((command) => command.startsWith("sudo -S")));
|
|
assert.equal(client.standardSftpCalls, 0);
|
|
assert.equal(sftpClients.size, 0);
|
|
assert.equal(client.sshEnded, true);
|
|
assert.equal(client.sshDestroyed, true);
|
|
});
|