From b0ef28a77f20677b90f262910a1fcb9fb2dcd8f3 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 12:51:22 +0800 Subject: [PATCH] fix(desktop): reject malformed build command JSON output (#12177) --- .../desktop/DesktopBuildCapture.ts | 12 ++++--- .../desktop/DesktopBuildCommands.ts | 13 ++++++- .../desktop/DesktopBuildOutputRead.test.ts | 35 +++++++++++++++++-- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/application/desktop/DesktopBuildCapture.ts b/src/application/desktop/DesktopBuildCapture.ts index 7a57f4bb12..369b4d395b 100644 --- a/src/application/desktop/DesktopBuildCapture.ts +++ b/src/application/desktop/DesktopBuildCapture.ts @@ -39,7 +39,13 @@ import { notAnswered, type CaptureStep, } from "./DesktopBuildCaptureFailure.js"; -import { adapterError, refused, runRea, WORK } from "./DesktopBuildCommands.js"; +import { + adapterError, + parseBuildComputerJson, + refused, + runRea, + WORK, +} from "./DesktopBuildCommands.js"; import { exportBuild, type BuildExportRequest, @@ -177,10 +183,8 @@ const checkExportRuntime = async ( ); let rawSchema: unknown; try { - rawSchema = JSON.parse(content.toString("utf8")); + rawSchema = parseBuildComputerJson(content); } catch { - // Parser errors can quote only a fragment of a credential, which cannot - // be removed reliably by exact-token redaction. return err( adapterError( START, diff --git a/src/application/desktop/DesktopBuildCommands.ts b/src/application/desktop/DesktopBuildCommands.ts index 9ae6129920..22fb150f2a 100644 --- a/src/application/desktop/DesktopBuildCommands.ts +++ b/src/application/desktop/DesktopBuildCommands.ts @@ -12,6 +12,7 @@ import { type JsonValue, } from "../../domain/jsonValue.js"; import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; +import { decodeStrictUtf8 } from "../../domain/strictUtf8.js"; import { guestFileAnswerCeiling, guestFileAnswerRefusal, @@ -138,6 +139,16 @@ const redactedJson = ( ); }; +/** Decode structured REA output without forwarding parser excerpts of guest data. */ +export const parseBuildComputerJson = (bytes: Uint8Array): unknown => { + try { + return JSON.parse(decodeStrictUtf8(bytes, "preserve")) as unknown; + } catch { + // Parser excerpts may retain only part of a control token or typed secret. + throw new Error("The build computer's REA output is not valid UTF-8 JSON."); + } +}; + /** * The Evidence result, or the reported failure, a REA command wrote to a file * of `bytes` in the computer, with the control token replaced in every @@ -154,7 +165,7 @@ export const readOutput = async ( if (refusal !== undefined) throw refusal; const content = (await computer.homeFiles([path], bytes, signal)).get(path); if (content === undefined) throw new Error(`The computer has no ${path}.`); - const value: unknown = JSON.parse(content.toString("utf8")); + const value = parseBuildComputerJson(content); const redact = (text: string) => computer.redacted(text); if (cliFailureSchema.safeParse(value).success) { const reported = jsonObjectSchema.parse( diff --git a/src/application/desktop/DesktopBuildOutputRead.test.ts b/src/application/desktop/DesktopBuildOutputRead.test.ts index d51d6a9172..b04c9fb18c 100644 --- a/src/application/desktop/DesktopBuildOutputRead.test.ts +++ b/src/application/desktop/DesktopBuildOutputRead.test.ts @@ -40,9 +40,40 @@ describe("readOutput", () => { }); }); + it.each([ + [ + "invalid UTF-8", + Buffer.concat([ + Buffer.from('{"code":"busy","message":"'), + Buffer.from([0xff]), + Buffer.from('"}'), + ]), + ], + ["invalid JSON", Buffer.from("incomplete output")], + ] as const)( + "refuses %s in guest command output without quoting the bytes", + async (_name, body) => { + const computer = { + homeFiles: () => Promise.resolve(new Map([["out.json", body]])), + redacted: (text: string) => text, + exec: () => Promise.reject(new Error("unused")), + }; + await expect( + readOutput( + computer, + "out.json", + body.length, + new AbortController().signal, + ), + ).rejects.toThrow( + /^The build computer's REA output is not valid UTF-8 JSON\.$/u, + ); + }, + ); + it("reads a reported failure that fits the ceiling", async () => { const body = Buffer.from( - JSON.stringify({ code: "busy", message: "the app is busy" }), + JSON.stringify({ code: "busy", message: "the app is busy — 正在运行" }), ); let reported = 0; const output = await readOutput( @@ -63,7 +94,7 @@ describe("readOutput", () => { if (output.kind !== "failure") return; expect(output.failure).toMatchObject({ code: "busy", - message: "the app is busy", + message: "the app is busy — 正在运行", }); }); });