fix(cli): classify oversized JSON inputs

This commit is contained in:
morluto
2026-10-10 02:49:46 +08:00
parent db171be6e5
commit f7cf4c890d
3 changed files with 162 additions and 12 deletions
+99 -1
View File
@@ -1,11 +1,20 @@
import { execFile } from "node:child_process";
import { chmod, symlink, writeFile } from "node:fs/promises";
import { constants as bufferConstants } from "node:buffer";
import {
chmod,
open,
symlink,
type FileHandle,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { describe, expect, it, onTestFinished } from "vitest";
import { isCliOperationFailure } from "../../../src/cliLogging.js";
import { parseCliJsonInput } from "../../../src/cliJsonInput.js";
import { analysisCliErrorEnvelopeSchema } from "../../../src/contracts/errorSchemas.js";
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
@@ -85,6 +94,51 @@ describe("CLI JSON file selection", () => {
});
describe("CLI JSON input", () => {
it("reports a valid file beyond the runtime string limit as too large", async () => {
const root = await createTestTempDirectory("rea-json-input-too-large-");
const path = join(root, "input.json");
const size = bufferConstants.MAX_STRING_LENGTH + 1;
await writeValidOversizedObject(path, size);
const result = await parseCliJsonInput(path, "test-input");
if (result.ok) throw new Error("Expected oversized JSON to be rejected");
expect(analysisCliErrorEnvelopeSchema.parse(result.error)).toEqual(
result.error,
);
expect(result).toMatchObject({
ok: false,
error: {
code: "resource_constraint",
input_path: path,
input_reason: "too-large",
details: {
resource: "memory",
reported_limits: {
boundary: "cli-json-input",
max_string_code_units: bufferConstants.MAX_STRING_LENGTH,
},
},
remediation: {
action:
"Split the JSON into smaller inputs or rerun its producer on a smaller subset, then retry.",
},
},
});
expect(isCliOperationFailure(result.error)).toBe(true);
const characterCount =
Math.floor(bufferConstants.MAX_STRING_LENGTH / 2) + 2;
await writeValidMultibyteString(path, characterCount);
const multibyteResult = await parseCliJsonInput(path, "test-input");
if (!multibyteResult.ok)
throw new Error("Expected large multibyte JSON to fit the string limit");
if (typeof multibyteResult.value !== "string")
throw new Error("Expected parsed JSON string");
expect(multibyteResult.value.length).toBe(characterCount);
}, 20_000);
it("distinguishes malformed inline text from files and preserves bracket-prefixed paths", async () => {
expect(await parseCliJsonInput("[", "test-input")).toMatchObject({
ok: false,
@@ -177,3 +231,47 @@ describe("CLI JSON input", () => {
},
);
});
const writeRepeatedBytes = async (
handle: FileHandle,
bytes: Buffer,
length: number,
): Promise<void> => {
let remaining = length;
while (remaining > 0) {
const chunkLength = Math.min(bytes.length, remaining);
await handle.writeFile(bytes.subarray(0, chunkLength));
remaining -= chunkLength;
}
};
const writeValidOversizedObject = async (
path: string,
size: number,
): Promise<void> => {
const handle = await open(path, "w");
const whitespace = Buffer.alloc(1024 * 1024, 0x20);
try {
await handle.writeFile("{");
await writeRepeatedBytes(handle, whitespace, size - 2);
await handle.writeFile("}");
} finally {
await handle.close();
}
};
const writeValidMultibyteString = async (
path: string,
characterCount: number,
): Promise<void> => {
const encodedCharacters = Buffer.from("é".repeat(512 * 1024));
const byteLength = characterCount * 2 + 2;
const handle = await open(path, "w");
try {
await handle.writeFile('"');
await writeRepeatedBytes(handle, encodedCharacters, byteLength - 2);
await handle.writeFile('"');
} finally {
await handle.close();
}
};