fix(desktop): reject malformed build command JSON output (#12177)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-18 23:51:22 -05:00
1 parent fe784e7f03
commit b0ef28a77f
3 files changed
+53 -7

No files matched your search

@@ -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,
@@ -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(
@@ -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 — 正在运行",
});
});
});