68 lines
4.0 KiB
TypeScript
68 lines
4.0 KiB
TypeScript
import { generateKeyPairSync } from "node:crypto";
|
|
import { Server, utils, type Connection } from "ssh2";
|
|
import type { HostInfo } from "@shellbook/core";
|
|
import type { ServerConfig } from "../src/config";
|
|
import { fingerprint } from "../src/ssh";
|
|
export async function createSshFixture(config: ServerConfig, cleanup: (() => Promise<unknown>)[], authenticate: boolean | ((ctx: import("ssh2").AuthContext) => void) = true) {
|
|
const key = generateKeyPairSync("rsa", { modulusLength: 2048 }).privateKey.export({ type: "pkcs1", format: "pem" }).toString();
|
|
const parsed = utils.parseKey(key);
|
|
if (parsed instanceof Error) throw parsed;
|
|
const fp = fingerprint(parsed.getPublicSSH());
|
|
const clients = new Set<Connection>();
|
|
const received: { command: string; input: string }[] = [];
|
|
const files = new Map<string, Buffer>([["/tmp/shellbook-test/out.txt", Buffer.from("artifact bytes")]]);
|
|
const server = new Server({ hostKeys: [key] }, client => {
|
|
clients.add(client);
|
|
client.on("error", () => {});
|
|
client.on("close", () => clients.delete(client));
|
|
client.on("authentication", ctx => typeof authenticate === "function" ? authenticate(ctx) : authenticate ? ctx.accept() : ctx.reject());
|
|
client.on("ready", () => client.on("session", accept => {
|
|
const session = accept();
|
|
session.on("exec", (acceptExec, _reject, info) => {
|
|
const stream = acceptExec();
|
|
const record = { command: info.command, input: "" };
|
|
received.push(record);
|
|
stream.on("data", (data: Buffer) => { record.input += data.toString(); });
|
|
stream.on("end", () => {
|
|
if (record.input.includes("HANG")) return;
|
|
if (info.command === "mktemp -d") stream.write("/tmp/shellbook-test\n");
|
|
else if (info.command.startsWith("rm -rf")) { /* deterministic cleanup */ }
|
|
else { stream.write("hello "); stream.stderr.write("warning\n"); stream.write("world\n"); }
|
|
stream.exit(record.input.includes("FAIL") ? 7 : 0);
|
|
stream.end();
|
|
});
|
|
});
|
|
session.on("sftp", acceptSftp => {
|
|
const session = acceptSftp();
|
|
const { OK, EOF, NO_SUCH_FILE } = utils.sftp.STATUS_CODE;
|
|
const attrs = (name: string) => ({ mode: 0o100600, uid: 0, gid: 0, size: files.get(name)?.length ?? 0, atime: 0, mtime: 0 });
|
|
session.on("REALPATH", (id, name) => session.name(id, [{ filename: name, longname: name, attrs: attrs(name) }]));
|
|
session.on("STAT", (id, name) => files.has(name) ? session.attrs(id, attrs(name)) : session.status(id, NO_SUCH_FILE));
|
|
session.on("OPEN", (id, name, flags) => {
|
|
if (flags & 8) files.set(name, Buffer.alloc(0));
|
|
if (!files.has(name)) return session.status(id, NO_SUCH_FILE);
|
|
session.handle(id, Buffer.from(name));
|
|
});
|
|
session.on("FSTAT", (id, handle) => session.attrs(id, attrs(handle.toString())));
|
|
session.on("READ", (id, handle, offset, length) => {
|
|
const data = files.get(handle.toString())!;
|
|
if (offset >= data.length) session.status(id, EOF);
|
|
else session.data(id, data.subarray(offset, offset + length));
|
|
});
|
|
session.on("WRITE", (id, handle, offset, data) => {
|
|
const name = handle.toString(); const old = files.get(name)!;
|
|
const next = Buffer.alloc(Math.max(old.length, offset + data.length)); old.copy(next); data.copy(next, offset); files.set(name, next);
|
|
session.status(id, OK);
|
|
});
|
|
session.on("CLOSE", id => session.status(id, OK));
|
|
});
|
|
}));
|
|
});
|
|
await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));
|
|
cleanup.push(async () => { for (const client of clients) client.end(); await new Promise<void>(resolve => server.close(() => resolve())); });
|
|
const address = server.address();
|
|
if (typeof address === "string" || !address) throw new Error("Missing test address");
|
|
const host: HostInfo = { name: "test", address: "127.0.0.1", port: address.port, user: "admin", tags: [] };
|
|
return { config, host, fp, received, files };
|
|
}
|