fix(cli): stream JSON inputs beyond the whole-document string limit (#1360)

Stream large CLI JSON files through a strict UTF-8 decoder and standard JSON parser using the same admitted regular-file handle. Preserve native parsing for small inputs, typed malformed-input/resource/access-denied errors, cancellation, and complete Evidence.

Verified with focused public CLI regressions, a 768 MB whitespace-padded valid request, and the Windows native decoder boundary. Individual strings and the assembled object graph remain subject to native runtime limits. All 27 final-head checks passed.

Refs #1331
This commit is contained in:
Ysopwnable
2026-10-10 04:53:09 +08:00
committed by GitHub
parent 3728c8267b
commit 6ce3288f3b
10 changed files with 555 additions and 197 deletions
+20 -59
View File
@@ -1,13 +1,10 @@
import { constants as bufferConstants } from "node:buffer";
import { resolve } from "node:path";
import { readRegularFile } from "./application/RegularFileRead.js";
import { readCliJsonFile } from "./cliJsonFile.js";
import { NonRegularFileReadError } from "./filesystem/RegularFile.js";
import { parseUtf8Json } from "./application/Utf8JsonInput.js";
import {
AnalysisAccessDeniedError,
AnalysisInputError,
AnalysisResourceConstraintError,
} from "./domain/analysisErrorCore.js";
import { projectAnalysisError } from "./domain/analysisErrorProjection.js";
import type { JsonValue } from "./domain/jsonValue.js";
@@ -23,31 +20,23 @@ export const parseCliJsonInput = async (
> => {
const inline = safeParseJson(value);
if (inline.ok) return { ok: true, value: inline.value };
if (isStringLengthLimit(inline.cause))
return jsonTooLargeError(undefined, operation);
try {
// Read raw bytes so invalid UTF-8 is rejected instead of
// being silently replaced by lossy "utf8" decoding.
const parsed = parseUtf8Json(
await readRegularFile(value),
operation,
value,
"cli-json-input",
);
const parsed = await readCliJsonFile(value, operation);
return parsed.ok
? { ok: true, value: parsed.value }
: jsonFileError(value, operation, "invalid-json");
: {
ok: false,
error: {
error: "Application workflow failed",
...projectAnalysisError(parsed.error),
input_path: value,
input_reason:
parsed.error._tag === "AnalysisInputError"
? "invalid-json"
: "too-large",
},
};
} catch (cause: unknown) {
if (cause instanceof AnalysisResourceConstraintError)
return {
ok: false,
error: {
error: "Application workflow failed",
...projectAnalysisError(cause),
input_path: value,
input_reason: "too-large",
},
};
const systemCode = accessDeniedSystemCode(cause);
if (systemCode !== undefined)
return jsonAccessDeniedError(value, operation, systemCode, cause);
@@ -57,6 +46,12 @@ export const parseCliJsonInput = async (
!hasExplicitJsonFileExtension(value)
)
return { ok: false, error: inputError(operation) };
if (
!(cause instanceof Error) ||
!("code" in cause) ||
typeof cause.code !== "string"
)
throw cause;
return jsonFileError(value, operation, "read-failed", cause);
}
};
@@ -120,13 +115,6 @@ const cannotBeAnExistingFile = (cause: unknown): boolean =>
const hasExplicitJsonFileExtension = (value: string): boolean =>
value.toLowerCase().endsWith(".json");
const isStringLengthLimit = (cause: unknown): boolean =>
cause instanceof Error &&
(("code" in cause && cause.code === "ERR_STRING_TOO_LONG") ||
/cannot create a string longer than|invalid string length/i.test(
cause.message,
));
const inputError = (operation: string): JsonValue => ({
error: "Application workflow failed",
...projectAnalysisError(
@@ -194,30 +182,3 @@ const readFailureIssue = (path: string | undefined, cause: unknown) => {
: `The JSON input file could not be read${code}: ${path ?? ""}`,
};
};
const jsonTooLargeError = (
path: string | undefined,
operation: string,
): { readonly ok: false; readonly error: JsonValue } => ({
ok: false,
error: {
error: "Application workflow failed",
...projectAnalysisError(
new AnalysisResourceConstraintError(
operation,
"memory",
`The JSON input exceeds this Node.js runtime's maximum string length (${bufferConstants.MAX_STRING_LENGTH} UTF-16 code units) and cannot be parsed as one value.`,
{
boundary: "cli-json-input",
max_string_code_units: bufferConstants.MAX_STRING_LENGTH,
},
{
remediationAction:
"Provide a smaller JSON document or rerun its producer on a smaller selection. Splitting JSON text alone does not produce a valid workflow input.",
},
),
),
...(path === undefined ? {} : { input_path: path }),
input_reason: "too-large",
},
});