From 5fd543c59c8c211d62b6904e32a4ee3987c9853f Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Wed, 18 Nov 2026 23:09:11 +0800 Subject: [PATCH] fix(tests): reject truncated shared CLI frames at EOF (#12010) Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com> --- .../boundary/cli/cliWorkerCompletion.test.ts | 15 +++++++++++ .../cli/sharedJavaScriptCliSession.test.ts | 26 +++++++++++++++++++ tests/support/cli/cliCommandSession.ts | 7 +++++ .../support/cli/cliCommandSessionFrames.d.mts | 8 ++++-- tests/support/cli/cliCommandSessionFrames.mjs | 8 +++++- tests/support/cli/cliCommandSessionWorker.mjs | 14 +++++++--- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/tests/boundary/cli/cliWorkerCompletion.test.ts b/tests/boundary/cli/cliWorkerCompletion.test.ts index 4756a6ad8e..8132e8cb4e 100644 --- a/tests/boundary/cli/cliWorkerCompletion.test.ts +++ b/tests/boundary/cli/cliWorkerCompletion.test.ts @@ -194,3 +194,18 @@ processTest( ]); }, ); + +processTest( + "reports an incomplete request frame when the input ends", + async ({ processes }) => { + const complete = encodeCliSessionFrame({ id: 1 }); + for (const input of [complete.subarray(0, 2), complete.subarray(0, -1)]) { + const result = await processes.run(process.execPath, [worker], { input }); + expect(result.exitCode, result.stderr).toBe(1); + expect(result.signal).toBeNull(); + expect(result.stderr).toContain( + "CLI session ended with an incomplete frame", + ); + } + }, +); diff --git a/tests/boundary/cli/sharedJavaScriptCliSession.test.ts b/tests/boundary/cli/sharedJavaScriptCliSession.test.ts index c39a5630f8..530301a646 100644 --- a/tests/boundary/cli/sharedJavaScriptCliSession.test.ts +++ b/tests/boundary/cli/sharedJavaScriptCliSession.test.ts @@ -118,6 +118,32 @@ test("reads a CLI session frame delivered one byte at a time", () => { expect(frames).toEqual([{ id: 7, note: "x".repeat(64) }]); }); +test("reports an incomplete response frame when worker stdout ends", async () => { + const session = createCliCommandSession({ + shutdownGraceMs: MOCK_SHUTDOWN_GRACE_MS, + spawnWorker: () => + asCliProcess( + respondingProcess(42, (request, child) => { + const response = encodeCliSessionFrame({ + id: request.id, + exitCode: 0, + }); + child.stdout.end(response.subarray(0, -1)); + child.exitCode = 0; + child.emit("exit", 0, null); + child.stdout.once("end", () => child.emit("close", 0, null)); + }), + ), + }); + try { + await expect( + session.run(command(["--version"], { PATH: "/usr/bin" })), + ).rejects.toThrow(/CLI session ended with an incomplete frame/u); + } finally { + await session.close(); + } +}); + test("stops an unready shared CLI process when the session closes", async () => { let spawned: FakeCliProcess | undefined; const session = createCliCommandSession({ diff --git a/tests/support/cli/cliCommandSession.ts b/tests/support/cli/cliCommandSession.ts index 00a6c7cd12..8149df589a 100644 --- a/tests/support/cli/cliCommandSession.ts +++ b/tests/support/cli/cliCommandSession.ts @@ -414,6 +414,13 @@ class SharedCliProcess implements CliCommandSession { this.#failTransport(started, error); } }); + started.stdout.once("end", () => { + try { + reader.finish(); + } catch (error) { + this.#failTransport(started, error); + } + }); started.stderr.resume(); started.stdin.on("error", (error) => { this.#failTransport(started, error); diff --git a/tests/support/cli/cliCommandSessionFrames.d.mts b/tests/support/cli/cliCommandSessionFrames.d.mts index 85e6ae65f7..34b0d4540e 100644 --- a/tests/support/cli/cliCommandSessionFrames.d.mts +++ b/tests/support/cli/cliCommandSessionFrames.d.mts @@ -6,8 +6,12 @@ export function encodeCliSessionFrame(value: unknown): Buffer; /** * Parse complete frames from a byte stream. An oversized or malformed frame - * fails the reader instead of resynchronizing on a later length. + * fails the reader instead of resynchronizing on a later length. Call finish + * at EOF to require the stream to end on a frame boundary. */ export function createCliSessionFrameReader( onFrame: (value: unknown) => void, -): (chunk: Buffer | string) => void; +): { + (chunk: Buffer | string): void; + finish(): void; +}; diff --git a/tests/support/cli/cliCommandSessionFrames.mjs b/tests/support/cli/cliCommandSessionFrames.mjs index 4f88c70cdd..9b8bf1639c 100644 --- a/tests/support/cli/cliCommandSessionFrames.mjs +++ b/tests/support/cli/cliCommandSessionFrames.mjs @@ -45,7 +45,7 @@ export const createCliSessionFrameReader = (onFrame) => { /** @type {Buffer[]} */ const chunks = []; let buffered = 0; - return (chunk) => { + const read = (chunk) => { const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); chunks.push(bytes); buffered += bytes.length; @@ -74,4 +74,10 @@ export const createCliSessionFrameReader = (onFrame) => { if (rest.length > 0) chunks.push(Buffer.from(rest)); } }; + return Object.assign(read, { + finish() { + if (buffered > 0) + throw new Error("CLI session ended with an incomplete frame"); + }, + }); }; diff --git a/tests/support/cli/cliCommandSessionWorker.mjs b/tests/support/cli/cliCommandSessionWorker.mjs index dec542971c..5b020ceae5 100644 --- a/tests/support/cli/cliCommandSessionWorker.mjs +++ b/tests/support/cli/cliCommandSessionWorker.mjs @@ -71,10 +71,6 @@ const failInput = (cause) => { process.stdin.pause(); resolveInput?.(); }; -process.stdin.once("end", () => { - if (completion.kind === "open") completion = { kind: "ended" }; - resolveInput?.(); -}); const reader = createCliSessionFrameReader((value) => { if (completion.kind !== "open") return; chain = chain @@ -83,6 +79,16 @@ const reader = createCliSessionFrameReader((value) => { }) .catch(failInput); }); +process.stdin.once("end", () => { + if (completion.kind !== "open") return; + try { + reader.finish(); + completion = { kind: "ended" }; + resolveInput?.(); + } catch (cause) { + failInput(cause); + } +}); process.stdin.on("data", (chunk) => { if (completion.kind !== "open") return; try {