mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 00:20:45 +00:00
Add a host and group toggle for bastions that allow one session channel per TCP connection. After the handshake, BHostSSH, TERM-SSHD, and CLOUDBILITY take that path automatically. File transfer uses a separate SFTP login, and the interactive terminal is not given another exec, SFTP, copy/split, or port-forward channel. The manual toggle remains for bastions that still advertise OpenSSH.
83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
|
|
const { createFileOpsApi } = require("./fileOps.cjs");
|
|
|
|
function createExtractApi({ execImpl, clients } = {}) {
|
|
const commands = [];
|
|
const optionsLog = [];
|
|
const api = createFileOpsApi({
|
|
sftpClients: clients || new Map([
|
|
["sftp-1", { client: { exec() {} } }],
|
|
]),
|
|
resolveEncodingForRequest: () => "utf-8",
|
|
throwIfAborted: () => {},
|
|
encodePath: (value) => value,
|
|
requireSftpChannel: async () => ({}),
|
|
lstatAsync: async () => ({ size: 1024 }),
|
|
isScpModeClient: () => false,
|
|
execRemoteShellCommand: async (_ssh, command, options) => {
|
|
commands.push(command);
|
|
optionsLog.push(options);
|
|
if (typeof execImpl === "function") return execImpl(command);
|
|
return { stdout: "", stderr: "", code: 0 };
|
|
},
|
|
});
|
|
return { api, commands, optionsLog };
|
|
}
|
|
|
|
test("extractSftpArchive runs a quoted remote tar command", async () => {
|
|
const { api, commands, optionsLog } = createExtractApi();
|
|
const result = await api.extractSftpArchive(null, {
|
|
sftpId: "sftp-1",
|
|
path: "/home/app/backup.tar.gz",
|
|
});
|
|
assert.deepEqual(result, { success: true });
|
|
assert.equal(commands.length, 1);
|
|
assert.match(commands[0], /tar -xzf '\/home\/app\/backup\.tar\.gz' -C '\/home\/app'/);
|
|
assert.equal(optionsLog[0].discardStdout, true);
|
|
});
|
|
|
|
test("extractSftpArchive rejects unsupported files", async () => {
|
|
const { api, commands } = createExtractApi();
|
|
await assert.rejects(
|
|
() => api.extractSftpArchive(null, { sftpId: "sftp-1", path: "/home/app/notes.txt" }),
|
|
/Unsupported archive type/,
|
|
);
|
|
assert.equal(commands.length, 0);
|
|
});
|
|
|
|
test("extractSftpArchive requires an open SFTP session", async () => {
|
|
const { api } = createExtractApi({ clients: new Map() });
|
|
await assert.rejects(
|
|
() => api.extractSftpArchive(null, { sftpId: "missing", path: "/tmp/a.zip" }),
|
|
/SFTP session not found/,
|
|
);
|
|
});
|
|
|
|
|
|
test("extractSftpArchive refuses a second channel on single-channel SSH", async () => {
|
|
let execCalls = 0;
|
|
const { api, commands } = createExtractApi({
|
|
clients: new Map([[
|
|
"sftp-1",
|
|
{
|
|
__netcattySingleChannelSsh: true,
|
|
__netcattyEndpointKey: "ep-extract",
|
|
client: { exec() { execCalls += 1; } },
|
|
},
|
|
]]),
|
|
});
|
|
await assert.rejects(
|
|
() => api.extractSftpArchive(null, {
|
|
sftpId: "sftp-1",
|
|
path: "/home/app/backup.tar.gz",
|
|
}),
|
|
(err) => err && err.code === "ERR_SFTP_SINGLE_CHANNEL_EXEC",
|
|
);
|
|
assert.equal(commands.length, 0);
|
|
assert.equal(execCalls, 0);
|
|
});
|