File size: 10,231 Bytes
fcd8223 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | import fs from "node:fs/promises";
import http from "node:http";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { WebSocketServer } from "ws";
import {
invokeNodeDesktopStream,
invokeNodeWorkerDesktopStream,
} from "./desktop-stream-command.js";
const TICKET = "a".repeat(48);
const cleanups: Array<() => Promise<void>> = [];
async function listenRfbSecurity(securityType: number) {
const peers = new Set<net.Socket>();
const server = net.createServer((socket) => {
peers.add(socket);
socket.once("close", () => peers.delete(socket));
socket.on("error", handleExpectedPeerTeardownError);
socket.write(Buffer.from("RFB 003.008\n", "ascii"));
socket.once("data", () => socket.write(Buffer.from([1, securityType])));
});
await new Promise<void>((resolve) => {
server.listen(0, "127.0.0.1", () => resolve());
});
const address = server.address();
if (!address || typeof address === "string") {
throw new Error("expected RFB test address");
}
cleanups.push(
async () =>
await new Promise<void>((resolve) => {
for (const peer of peers) {
peer.destroy();
}
server.close(() => resolve());
}),
);
return { port: address.port, peers };
}
function handleExpectedPeerTeardownError(error: NodeJS.ErrnoException): void {
if (error.code !== "ECONNRESET" && error.code !== "EPIPE") {
throw error;
}
}
afterEach(async () => {
await Promise.all(cleanups.splice(0).map((cleanup) => cleanup()));
});
describe("node desktop stream command", () => {
it.each([
["caller-selected host", { host: "192.0.2.10" }],
["relative password path", { passwordFilePath: "vnc.password" }],
["invalid RFB port", { port: 65_536 }],
])("rejects worker stream payload with %s", async (_name, override) => {
await expect(
invokeNodeWorkerDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
port: 5900,
...override,
}),
gatewayUrl: "ws://127.0.0.1:1",
signal: new AbortController().signal,
}),
).rejects.toThrow("INVALID_REQUEST");
});
it.each([
[1, "refusing unauthenticated loopback RFB server"],
[19, "loopback RFB server security is unsupported"],
])(
"refuses security type %i and closes its connection before Gateway attach",
async (securityType, message) => {
const rfb = await listenRfbSecurity(securityType);
await expect(
invokeNodeWorkerDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
port: rfb.port,
}),
gatewayUrl: "ws://127.0.0.1:1",
signal: new AbortController().signal,
}),
).rejects.toThrow(message);
await vi.waitFor(() => expect(rfb.peers.size).toBe(0));
},
);
it("bounds the provider-owned VNC password file", async () => {
const rfb = await listenRfbSecurity(2);
const root = await fs.mkdtemp(path.join(await fs.realpath(os.tmpdir()), "desktop-password-"));
const oversized = path.join(root, "oversized");
await fs.writeFile(oversized, "x".repeat(4 * 1024 + 1));
cleanups.push(async () => fs.rm(root, { recursive: true, force: true }));
for (const [passwordFilePath, message] of [
[root, "must be a regular file"],
[oversized, "is too large"],
] as const) {
await expect(
invokeNodeWorkerDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
port: rfb.port,
passwordFilePath,
}),
gatewayUrl: "ws://127.0.0.1:1",
signal: new AbortController().signal,
}),
).rejects.toThrow(message);
await vi.waitFor(() => expect(rfb.peers.size).toBe(0));
}
});
it("honors cancellation before reading a VNC password file", async () => {
const rfb = await listenRfbSecurity(2);
const root = await fs.mkdtemp(path.join(await fs.realpath(os.tmpdir()), "desktop-password-"));
const passwordFilePath = path.join(root, "password");
await fs.writeFile(passwordFilePath, "secret");
cleanups.push(async () => fs.rm(root, { recursive: true, force: true }));
const controller = new AbortController();
controller.abort(new Error("desktop owner closed"));
await expect(
invokeNodeWorkerDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
port: rfb.port,
passwordFilePath,
}),
gatewayUrl: "ws://127.0.0.1:1",
signal: controller.signal,
}),
).rejects.toThrow("desktop owner closed");
});
it("refuses a caller-selected RFB target before dialing", async () => {
await expect(
invokeNodeDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
target: { host: "192.0.2.10", port: 5900 },
}),
gatewayUrl: "ws://127.0.0.1:1",
config: { enabled: true },
signal: new AbortController().signal,
}),
).rejects.toThrow("unsupported fields");
});
it("refuses an attach path that changes the connected gateway origin", async () => {
await expect(
invokeNodeDesktopStream({
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `//attacker.example/node-desktop/attach?ticket=${TICKET}`,
}),
gatewayUrl: "ws://127.0.0.1:1",
config: { enabled: true },
signal: new AbortController().signal,
}),
).rejects.toThrow("ticket and attachPath required");
});
it.each(["", "/openclaw-gw", "/openclaw-gw/"])(
"authenticates public and worker attaches through Gateway context %j and tears down on cancellation",
async (contextPath) => {
const rfbPeers = new Set<net.Socket>();
const rfbServer = net.createServer((socket) => {
rfbPeers.add(socket);
socket.once("close", () => rfbPeers.delete(socket));
// Cancellation destroys the client socket; the synthetic server owns the matching reset.
socket.on("error", handleExpectedPeerTeardownError);
socket.write(Buffer.from("RFB 003.008\n", "ascii"));
socket.once("data", () => socket.write(Buffer.from([1, 2])));
});
await new Promise<void>((resolve) => {
rfbServer.listen(0, "127.0.0.1", resolve);
});
const rfbAddress = rfbServer.address();
if (!rfbAddress || typeof rfbAddress === "string") {
throw new Error("expected RFB test address");
}
cleanups.push(
async () =>
await new Promise<void>((resolve) => {
for (const peer of rfbPeers) {
peer.destroy();
}
rfbServer.close(() => resolve());
}),
);
const httpServer = http.createServer();
const wss = new WebSocketServer({
server: httpServer,
path: `${contextPath.replace(/\/$/u, "")}/node-desktop/attach`,
});
const streams: Array<{
accessHeaders: [string | undefined, string | undefined];
closed: boolean;
}> = [];
wss.on("connection", (ws, request) => {
const stream = {
accessHeaders: [
request.headers["cf-access-client-id"],
request.headers["cf-access-client-secret"],
] as [string | undefined, string | undefined],
closed: false,
};
streams.push(stream);
ws.once("close", () => {
stream.closed = true;
});
});
await new Promise<void>((resolve) => {
httpServer.listen(0, "127.0.0.1", resolve);
});
const gatewayAddress = httpServer.address();
if (!gatewayAddress || typeof gatewayAddress === "string") {
throw new Error("expected Gateway test address");
}
cleanups.push(
async () =>
await new Promise<void>((resolve) => {
wss.close(() => httpServer.close(() => resolve()));
}),
);
for (const kind of ["public", "worker"] as const) {
const controller = new AbortController();
const emitStatus = vi.fn(async () => undefined);
const connection = {
paramsJSON: JSON.stringify({
ticket: TICKET,
attachPath: `/node-desktop/attach?ticket=${TICKET}`,
...(kind === "worker" ? { port: rfbAddress.port } : {}),
}),
gatewayUrl: `ws://127.0.0.1:${gatewayAddress.port}${contextPath}`,
gatewayCloudflareAccess: {
clientId: "desktop-client-id",
clientSecret: "desktop-client-secret",
},
signal: controller.signal,
};
const running =
kind === "worker"
? invokeNodeWorkerDesktopStream(connection)
: invokeNodeDesktopStream({
...connection,
config: { enabled: true, port: rfbAddress.port },
emitStatus,
});
void running.catch(() => undefined);
cleanups.push(async () => {
controller.abort();
await running.catch(() => undefined);
});
await vi.waitFor(() => expect(streams).toHaveLength(kind === "public" ? 1 : 2));
const stream = streams.at(-1);
if (!stream) {
throw new Error("expected desktop stream attachment");
}
expect(stream.accessHeaders).toEqual(["desktop-client-id", "desktop-client-secret"]);
if (kind === "public") {
await vi.waitFor(() =>
expect(emitStatus).toHaveBeenCalledWith("desktop stream attached\n"),
);
}
controller.abort();
await expect(running).resolves.toBeUndefined();
await vi.waitFor(() => expect(stream.closed).toBe(true));
await vi.waitFor(() => expect(rfbPeers.size).toBe(0));
}
},
);
});
|