|
1 | 1 | import net from "node:net"; |
2 | | -import { afterEach, describe, expect, it } from "vitest"; |
3 | | -import { classifyRfbSecurity, probeRfbServer } from "./rfb-probe.js"; |
| 2 | +import { finished } from "node:stream/promises"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createDeferred } from "../../../test/helpers/promise.js"; |
| 5 | +import { classifyRfbSecurity, connectRfbServer, probeRfbServer } from "./rfb-probe.js"; |
4 | 6 |
|
5 | 7 | const cleanups: Array<() => Promise<void>> = []; |
6 | 8 |
|
@@ -159,3 +161,271 @@ describe("RFB security classification", () => { |
159 | 161 | expect(classifyRfbSecurity([30, 33, 36, 35])).toBe("ard-account"); |
160 | 162 | }); |
161 | 163 | }); |
| 164 | + |
| 165 | +describe("retained RFB connection", () => { |
| 166 | + it.each([ |
| 167 | + ["3.3", "RFB 003.003\n", "RFB 003.003\n", Buffer.from([0, 0, 0, 2])], |
| 168 | + ["3.7", "RFB 003.007\n", "RFB 003.007\n", Buffer.from([1, 2])], |
| 169 | + ["3.8", "RFB 003.008\n", "RFB 003.008\n", Buffer.from([1, 2])], |
| 170 | + ["macOS", "RFB 003.889\n", "RFB 003.008\n", Buffer.from([2, 30, 2])], |
| 171 | + ])( |
| 172 | + "retains one %s connection across split replies and coalesced server data", |
| 173 | + async (_name, banner, reply, offer) => { |
| 174 | + const challenge = Buffer.alloc(16, 7); |
| 175 | + const clientData = Buffer.from([2, 8, 9]); |
| 176 | + const received: Buffer[] = []; |
| 177 | + let connections = 0; |
| 178 | + const port = await listenScriptedRfb((socket) => { |
| 179 | + connections++; |
| 180 | + socket.on("data", (data) => received.push(Buffer.from(data))); |
| 181 | + socket.write(Buffer.from(banner.slice(0, 5))); |
| 182 | + setImmediate(() => socket.write(Buffer.from(banner.slice(5)))); |
| 183 | + socket.once("data", (version) => { |
| 184 | + expect(version).toEqual(Buffer.from(reply)); |
| 185 | + socket.write(Buffer.concat([offer, challenge])); |
| 186 | + }); |
| 187 | + }); |
| 188 | + const result = await connectRfbServer({ host: "127.0.0.1", port, timeoutMs: 2_000 }); |
| 189 | + expect(result.kind).toBe("rfb"); |
| 190 | + if (result.kind !== "rfb") { |
| 191 | + throw new Error("expected retained connection"); |
| 192 | + } |
| 193 | + cleanups.push(async () => { |
| 194 | + result.stream.destroy(); |
| 195 | + }); |
| 196 | + const output: Buffer[] = []; |
| 197 | + result.stream.on("data", (chunk: Buffer) => output.push(chunk)); |
| 198 | + await vi.waitFor(() => |
| 199 | + expect(Buffer.concat(output)).toEqual( |
| 200 | + Buffer.concat([Buffer.from(banner), offer, challenge]), |
| 201 | + ), |
| 202 | + ); |
| 203 | + result.stream.write(Buffer.from(reply.slice(0, 4))); |
| 204 | + result.stream.write(Buffer.from(reply.slice(4, 9))); |
| 205 | + result.stream.write(Buffer.concat([Buffer.from(reply.slice(9)), clientData])); |
| 206 | + await vi.waitFor(() => |
| 207 | + expect(Buffer.concat(received)).toEqual(Buffer.concat([Buffer.from(reply), clientData])), |
| 208 | + ); |
| 209 | + expect(connections).toBe(1); |
| 210 | + }, |
| 211 | + ); |
| 212 | + |
| 213 | + it("rejects a changed downstream version without forwarding authentication bytes", async () => { |
| 214 | + const received: Buffer[] = []; |
| 215 | + let closed = false; |
| 216 | + const port = await listenScriptedRfb((socket) => { |
| 217 | + socket.once("close", () => { |
| 218 | + closed = true; |
| 219 | + }); |
| 220 | + socket.on("data", (data) => received.push(Buffer.from(data))); |
| 221 | + socket.write(Buffer.from("RFB 003.008\n")); |
| 222 | + socket.once("data", () => socket.write(Buffer.from([1, 2]))); |
| 223 | + }); |
| 224 | + const result = await connectRfbServer({ host: "127.0.0.1", port, timeoutMs: 2_000 }); |
| 225 | + if (result.kind !== "rfb") { |
| 226 | + throw new Error("expected retained connection"); |
| 227 | + } |
| 228 | + const failure = new Promise<Error>((resolve) => { |
| 229 | + result.stream.once("error", resolve); |
| 230 | + }); |
| 231 | + result.stream.write(Buffer.from("RFB 003.003\nsecret-response")); |
| 232 | + await expect(failure).resolves.toMatchObject({ |
| 233 | + message: "RFB client changed the inspected protocol version", |
| 234 | + }); |
| 235 | + await vi.waitFor(() => expect(closed).toBe(true)); |
| 236 | + expect(Buffer.concat(received)).toEqual(Buffer.from("RFB 003.008\n")); |
| 237 | + }); |
| 238 | + |
| 239 | + it.each(["banner", "security", "handoff"])( |
| 240 | + "cancels the owned socket during %s", |
| 241 | + async (phase) => { |
| 242 | + let connected = false; |
| 243 | + let closed = false; |
| 244 | + const port = await listenScriptedRfb((socket) => { |
| 245 | + connected = true; |
| 246 | + socket.once("close", () => { |
| 247 | + closed = true; |
| 248 | + }); |
| 249 | + if (phase === "banner") { |
| 250 | + return; |
| 251 | + } |
| 252 | + socket.write(Buffer.from("RFB 003.008\n")); |
| 253 | + socket.once("data", () => { |
| 254 | + if (phase === "handoff") { |
| 255 | + socket.write(Buffer.from([1, 2])); |
| 256 | + } |
| 257 | + }); |
| 258 | + }); |
| 259 | + const controller = new AbortController(); |
| 260 | + const pending = connectRfbServer({ |
| 261 | + host: "127.0.0.1", |
| 262 | + port, |
| 263 | + timeoutMs: 2_000, |
| 264 | + signal: controller.signal, |
| 265 | + }); |
| 266 | + if (phase === "handoff") { |
| 267 | + const result = await pending; |
| 268 | + if (result.kind !== "rfb") { |
| 269 | + throw new Error("expected retained connection"); |
| 270 | + } |
| 271 | + controller.abort(new Error("desktop owner stopped")); |
| 272 | + await vi.waitFor(() => expect(result.stream.destroyed).toBe(true)); |
| 273 | + } else { |
| 274 | + const rejected = expect(pending).rejects.toThrow("desktop owner stopped"); |
| 275 | + await vi.waitFor(() => expect(connected).toBe(true)); |
| 276 | + controller.abort(new Error("desktop owner stopped")); |
| 277 | + await rejected; |
| 278 | + } |
| 279 | + await vi.waitFor(() => expect(closed).toBe(true)); |
| 280 | + }, |
| 281 | + ); |
| 282 | + it("ends writes and preserves terminal response bytes while a consumer remains paused", async () => { |
| 283 | + const response = Buffer.alloc(32 * 1024, 7); |
| 284 | + const peerClosed = createDeferred(); |
| 285 | + const port = await listenScriptedRfb((socket) => { |
| 286 | + socket.once("close", () => peerClosed.resolve()); |
| 287 | + socket.write(Buffer.from("RFB 003.008\n")); |
| 288 | + socket.once("data", () => { |
| 289 | + socket.write(Buffer.from([1, 2])); |
| 290 | + socket.once("data", (selection) => { |
| 291 | + expect(selection).toEqual(Buffer.from([2])); |
| 292 | + socket.end(response); |
| 293 | + }); |
| 294 | + }); |
| 295 | + }); |
| 296 | + const result = await connectRfbServer({ host: "127.0.0.1", port, timeoutMs: 2_000 }); |
| 297 | + if (result.kind !== "rfb") { |
| 298 | + throw new Error("expected retained connection"); |
| 299 | + } |
| 300 | + cleanups.push(async () => { |
| 301 | + result.stream.destroy(); |
| 302 | + }); |
| 303 | + const prefix = await new Promise<Buffer>((resolve) => { |
| 304 | + result.stream.once("data", (chunk: Buffer) => { |
| 305 | + result.stream.pause(); |
| 306 | + resolve(chunk); |
| 307 | + }); |
| 308 | + }); |
| 309 | + expect(prefix).toEqual(Buffer.concat([Buffer.from("RFB 003.008\n"), Buffer.from([1, 2])])); |
| 310 | + result.stream.write(Buffer.concat([Buffer.from("RFB 003.008\n"), Buffer.from([2])])); |
| 311 | + // The peer has finished and closed while the caller has consumed no response. |
| 312 | + await peerClosed.promise; |
| 313 | + expect(result.stream.destroyed).toBe(false); |
| 314 | + expect(result.stream.writableEnded).toBe(true); |
| 315 | + const settled = finished(result.stream); |
| 316 | + void settled.catch(() => undefined); |
| 317 | + const received: Buffer[] = []; |
| 318 | + for await (const chunk of result.stream) { |
| 319 | + received.push(Buffer.from(chunk)); |
| 320 | + } |
| 321 | + expect(Buffer.concat(received)).toEqual(response); |
| 322 | + await expect(settled).resolves.toBeUndefined(); |
| 323 | + }); |
| 324 | + |
| 325 | + it.each(["peer-fin", "owner-abort"])("settles queued client writes on %s", async (ending) => { |
| 326 | + const ready = createDeferred<net.Socket>(); |
| 327 | + const received: Buffer[] = []; |
| 328 | + let receivedBytes = 0; |
| 329 | + const response = Buffer.alloc(32 * 1024, 9); |
| 330 | + const port = await listenScriptedRfb((peer) => { |
| 331 | + peer.on("error", (error: NodeJS.ErrnoException) => { |
| 332 | + if (error.code !== "ECONNRESET" && error.code !== "EPIPE") { |
| 333 | + throw error; |
| 334 | + } |
| 335 | + }); |
| 336 | + peer.write(Buffer.from("RFB 003.008\n")); |
| 337 | + peer.once("data", () => { |
| 338 | + peer.write(Buffer.from([1, 2])); |
| 339 | + peer.once("data", () => { |
| 340 | + peer.pause(); |
| 341 | + peer.on("data", (chunk) => { |
| 342 | + const bytes = Buffer.from(chunk); |
| 343 | + received.push(bytes); |
| 344 | + receivedBytes += bytes.length; |
| 345 | + }); |
| 346 | + ready.resolve(peer); |
| 347 | + }); |
| 348 | + }); |
| 349 | + }); |
| 350 | + const controller = new AbortController(); |
| 351 | + const result = await connectRfbServer({ |
| 352 | + host: "127.0.0.1", |
| 353 | + port, |
| 354 | + timeoutMs: 2_000, |
| 355 | + signal: controller.signal, |
| 356 | + }); |
| 357 | + if (result.kind !== "rfb") { |
| 358 | + throw new Error("expected retained connection"); |
| 359 | + } |
| 360 | + cleanups.push(async () => { |
| 361 | + result.stream.destroy(); |
| 362 | + }); |
| 363 | + const settled = finished(result.stream); |
| 364 | + void settled.catch(() => undefined); |
| 365 | + await new Promise<void>((resolve) => { |
| 366 | + result.stream.once("data", () => { |
| 367 | + result.stream.pause(); |
| 368 | + resolve(); |
| 369 | + }); |
| 370 | + }); |
| 371 | + result.stream.write(Buffer.concat([Buffer.from("RFB 003.008\n"), Buffer.from([2])])); |
| 372 | + const peer = await ready.promise; |
| 373 | + const first = Buffer.alloc(8 * 1024 * 1024, 7); |
| 374 | + const last = Buffer.from("queued-client-tail"); |
| 375 | + const callbacks: Array<Error | null> = []; |
| 376 | + result.stream.write(first, (error) => callbacks.push(error ?? null)); |
| 377 | + result.stream.write(last, (error) => callbacks.push(error ?? null)); |
| 378 | + // Keep the first native write pending and the second queued in the wrapper |
| 379 | + // when FIN arrives. The same owner must drain both before ending TCP writes. |
| 380 | + expect(result.stream.writableLength).toBe(first.length + last.length); |
| 381 | + if (ending === "owner-abort") { |
| 382 | + controller.abort(new Error("desktop owner stopped")); |
| 383 | + await expect(settled).rejects.toThrow("desktop owner stopped"); |
| 384 | + await vi.waitFor(() => expect(callbacks).toHaveLength(2)); |
| 385 | + expect(callbacks.every((error) => error instanceof Error)).toBe(true); |
| 386 | + expect(result.stream.destroyed).toBe(true); |
| 387 | + return; |
| 388 | + } |
| 389 | + peer.end(response); |
| 390 | + await vi.waitFor(() => expect(result.stream.writableEnded).toBe(true)); |
| 391 | + peer.resume(); |
| 392 | + const output: Buffer[] = []; |
| 393 | + result.stream.on("data", (chunk: Buffer) => output.push(chunk)); |
| 394 | + result.stream.resume(); |
| 395 | + await expect(settled).resolves.toBeUndefined(); |
| 396 | + expect(Buffer.concat(output)).toEqual(response); |
| 397 | + expect(callbacks).toEqual([null, null]); |
| 398 | + await vi.waitFor(() => expect(receivedBytes).toBe(first.length + last.length)); |
| 399 | + expect(Buffer.concat(received).equals(Buffer.concat([first, last]))).toBe(true); |
| 400 | + }); |
| 401 | + |
| 402 | + it.each(["", "final-pre-attach-response"])( |
| 403 | + "retains an offer and FIN before attachment (tail: %j)", |
| 404 | + async (tail) => { |
| 405 | + const finSent = createDeferred(); |
| 406 | + const wire = Buffer.concat([ |
| 407 | + Buffer.from("RFB 003.008\n"), |
| 408 | + Buffer.from([1, 2]), |
| 409 | + Buffer.from(tail), |
| 410 | + ]); |
| 411 | + const port = await listenScriptedRfb((peer) => { |
| 412 | + peer.write(wire.subarray(0, 12)); |
| 413 | + peer.once("data", () => peer.end(wire.subarray(12), () => finSent.resolve())); |
| 414 | + }); |
| 415 | + const result = await connectRfbServer({ host: "127.0.0.1", port, timeoutMs: 2_000 }); |
| 416 | + if (result.kind !== "rfb") { |
| 417 | + throw new Error("expected retained connection"); |
| 418 | + } |
| 419 | + cleanups.push(async () => { |
| 420 | + result.stream.destroy(); |
| 421 | + }); |
| 422 | + await finSent.promise; |
| 423 | + const settled = finished(result.stream); |
| 424 | + const received: Buffer[] = []; |
| 425 | + result.stream.on("data", (chunk: Buffer) => received.push(chunk)); |
| 426 | + await settled; |
| 427 | + expect(Buffer.concat(received)).toEqual(wire); |
| 428 | + expect(result.stream.writableFinished).toBe(true); |
| 429 | + }, |
| 430 | + ); |
| 431 | +}); |
0 commit comments