mirror of
https://github.com/morluto/rea.git
synced 2026-11-17 04:04:25 +00:00
refactor(parsers): state duplicate-member policies and bring parser-json-parse to 0 (#11789)
* refactor(parsers): state duplicate-member policies and bring parser-json-parse to 0 * fix(domain): return duplicate members as a parseJsonWithDuplicatePolicy failure * refactor(evm): name the worker request byte limit constant
This commit is contained in:
1 parent
30d5ad2a48
commit
4a6bee6cdb
20 files changed
+275
-118
No files matched your search
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"src/android/JadxProtocol.ts": 2,
|
||||
"src/android/JadxWorkspaceRoot.ts": 1,
|
||||
"src/artifacts/ReleaseExposureReader.ts": 1,
|
||||
"src/artifacts/apple/AppleAssetCatalogAnalysis.ts": 1,
|
||||
"src/artifacts/deno/DenoSectionLayout.ts": 1,
|
||||
"src/domain/android/proguardMapping.ts": 1,
|
||||
"src/domain/apple/interfaceBuilderKeyedArchive.ts": 1,
|
||||
"src/domain/pcap/tsharkJsonPackets.ts": 1,
|
||||
"src/evm/EvmoleInterfaceWorker.ts": 2,
|
||||
"src/firmware/FirmwareReports.ts": 1,
|
||||
"src/model/SafetensorsHeader.ts": 1,
|
||||
"src/native/LldbRetainedObservations.ts": 1,
|
||||
"src/native/apple/losslessJson.ts": 1,
|
||||
"src/native/parsers/plist.ts": 1,
|
||||
"src/resources/AsarHeader.ts": 1,
|
||||
"src/rust/RustAuditableReader.ts": 1
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
|
||||
import type { AndroidOperation } from "../domain/android/androidAnalysis.js";
|
||||
import {
|
||||
@@ -53,13 +54,9 @@ export const jadxHeapExhausted = (text: string): boolean =>
|
||||
export const parseJadxFailure = (
|
||||
text: string,
|
||||
): z.infer<typeof bridgeFailureSchema> | undefined => {
|
||||
let candidate: unknown;
|
||||
try {
|
||||
candidate = JSON.parse(text);
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
const parsed = bridgeFailureSchema.safeParse(candidate);
|
||||
const decoded = parseJsonWithDuplicatePolicy(text, "reject");
|
||||
if (!decoded.ok) return undefined;
|
||||
const parsed = bridgeFailureSchema.safeParse(decoded.value);
|
||||
return parsed.success ? parsed.data : undefined;
|
||||
};
|
||||
/** Values observed inside the exact JVM producing the analysis. */
|
||||
@@ -219,8 +216,21 @@ export const parseJadxJson = (
|
||||
text: string,
|
||||
operation: AndroidOperation,
|
||||
): z.infer<typeof jsonValueSchema> => {
|
||||
const decoded = parseJsonWithDuplicatePolicy(text, "reject");
|
||||
if (!decoded.ok) {
|
||||
if (decoded.duplicate !== undefined)
|
||||
throw new AnalysisOutputError(
|
||||
operation,
|
||||
`JADX returned JSON with duplicate member ${JSON.stringify(decoded.duplicate.name)}`,
|
||||
);
|
||||
throw new AnalysisOutputError(
|
||||
operation,
|
||||
"JADX returned malformed JSON text",
|
||||
{ cause: decoded.cause },
|
||||
);
|
||||
}
|
||||
try {
|
||||
return jsonValueSchema.parse(JSON.parse(text));
|
||||
return jsonValueSchema.parse(decoded.value);
|
||||
} catch (cause) {
|
||||
throw new AnalysisOutputError(
|
||||
operation,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { fileURLToPath } from "node:url";
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
||||
|
||||
/** Match the native owner's ordinary spelling only for extended local drive paths. */
|
||||
@@ -144,7 +145,8 @@ class WindowsJadxWorkspaceRoot implements JadxWorkspaceRoot {
|
||||
15000,
|
||||
"Android workspace guardian startup timed out",
|
||||
);
|
||||
const observation = readySchema.parse(JSON.parse(line));
|
||||
const decoded = parseJsonWithDuplicatePolicy(line, "reject");
|
||||
const observation = readySchema.parse(decoded.ok ? decoded.value : null);
|
||||
if (
|
||||
win32.dirname(localDrivePath(observation.path)).toLowerCase() !==
|
||||
localDrivePath(win32.resolve(parent)).toLowerCase() ||
|
||||
|
||||
@@ -113,6 +113,7 @@ import {
|
||||
type StableRegularFileDescriptor,
|
||||
} from "../filesystem/RegularFile.js";
|
||||
import { sourceMapJsonText } from "../domain/sourceMapEnvelope.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import {
|
||||
inspectSourceMap,
|
||||
inspectSourceMapSourceContent,
|
||||
@@ -1528,12 +1529,12 @@ const scriptHoldsMap = (
|
||||
!isScriptTarget(state, member.location)
|
||||
)
|
||||
return false;
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(sourceMapJsonText(text));
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const decoded = parseJsonWithDuplicatePolicy(
|
||||
sourceMapJsonText(text),
|
||||
"last-wins",
|
||||
);
|
||||
if (!decoded.ok) return false;
|
||||
const parsed = decoded.value;
|
||||
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
|
||||
return false;
|
||||
return (
|
||||
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from "../ArtifactTraversalObservation.js";
|
||||
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
|
||||
import { analyzeInterfaceBuilderBundle } from "./InterfaceBuilderAnalysis.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../../domain/jsonDuplicateMember.js";
|
||||
import {
|
||||
appleAssetCatalogInputSchema,
|
||||
appleResourceKeyFields,
|
||||
@@ -396,16 +397,21 @@ const inspectAssetCatalogEntry = async (input: {
|
||||
signal: null,
|
||||
};
|
||||
const diagnostics = { catalog_path: entry.path };
|
||||
let parsed: unknown;
|
||||
try {
|
||||
parsed = JSON.parse(output);
|
||||
} catch (cause: unknown) {
|
||||
const decoded = parseJsonWithDuplicatePolicy(output, "reject");
|
||||
if (!decoded.ok) {
|
||||
if (decoded.duplicate !== undefined)
|
||||
throw new AnalysisOutputError(
|
||||
"inspect_asset_catalog",
|
||||
`assetutil returned duplicate JSON member ${JSON.stringify(decoded.duplicate.name)} for ${entry.path}`,
|
||||
{ capturedOutput, diagnostics },
|
||||
);
|
||||
throw new AnalysisOutputError(
|
||||
"inspect_asset_catalog",
|
||||
`assetutil returned invalid JSON for ${entry.path}`,
|
||||
{ cause, capturedOutput, diagnostics },
|
||||
{ cause: decoded.cause, capturedOutput, diagnostics },
|
||||
);
|
||||
}
|
||||
const parsed = decoded.value;
|
||||
try {
|
||||
return {
|
||||
catalog: {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
artifactFixedLimitFailure,
|
||||
} from "../../resources/index.js";
|
||||
import { walkDenoVirtualFs, type DenoVfsWalk } from "./DenoVirtualFs.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../../domain/jsonDuplicateMember.js";
|
||||
import { strictUtf8Decoder } from "../../domain/strictUtf8.js";
|
||||
|
||||
/** Opening and closing marker of a Deno compile data section. */
|
||||
@@ -402,11 +403,15 @@ const decodeStrict = (
|
||||
const decodeJson = (bytes: Buffer, scope: string): unknown => {
|
||||
const text = decodeStrict(bytes, jsonDecoder);
|
||||
if (text === undefined) throw denoFormatFailure(`${scope} is invalid`);
|
||||
try {
|
||||
return JSON.parse(text) as unknown;
|
||||
} catch {
|
||||
const parsed = parseJsonWithDuplicatePolicy(text, "reject");
|
||||
if (!parsed.ok) {
|
||||
if (parsed.duplicate !== undefined)
|
||||
throw denoFormatFailure(
|
||||
`${scope} declares member ${JSON.stringify(parsed.duplicate.name)} more than once`,
|
||||
);
|
||||
throw denoFormatFailure(`${scope} is invalid`);
|
||||
}
|
||||
return parsed.value;
|
||||
};
|
||||
|
||||
const observeMetadata = (value: unknown): DenoObservedMetadata => {
|
||||
|
||||
@@ -1100,3 +1100,14 @@ it("does not charge repeated identical residual metadata as new retained strings
|
||||
}),
|
||||
).toMatchObject({ status: "mapped" });
|
||||
});
|
||||
|
||||
it("reports duplicate metadata members even when a repeated id overwrites its string value", () => {
|
||||
const metadata = parse(
|
||||
[
|
||||
'# {"id":"com.android.tools.r8.mapping","id":null}',
|
||||
"com.example.Meta -> a.m:",
|
||||
" int count -> a",
|
||||
].join("\n"),
|
||||
);
|
||||
expect(metadata.malformed).toEqual([{ line: 1, reason: "metadata" }]);
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import { dexDescriptorSchema, dexTypeSchema } from "./dexTypes.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../jsonDuplicateMember.js";
|
||||
import { jsonStringByteLength } from "../jsonStringByteLength.js";
|
||||
|
||||
/** Retained representation and per-residual-class claim budgets, not file-size limits. */
|
||||
@@ -550,21 +551,26 @@ const parseMember = (state: ParseState, line: string, number: number) => {
|
||||
malformed(state, number, "member-line");
|
||||
};
|
||||
|
||||
const metadataObject = (body: string): Record<string, unknown> | undefined => {
|
||||
const metadataObject = (
|
||||
body: string,
|
||||
):
|
||||
| { readonly kind: "valid"; readonly record: Record<string, unknown> }
|
||||
| { readonly kind: "duplicate" }
|
||||
| undefined => {
|
||||
if (!body.startsWith("{")) return undefined;
|
||||
let value: unknown;
|
||||
try {
|
||||
value = JSON.parse(body);
|
||||
} catch {
|
||||
const parsed = parseJsonWithDuplicatePolicy(body, "reject");
|
||||
if (!parsed.ok)
|
||||
return parsed.duplicate === undefined ? undefined : { kind: "duplicate" };
|
||||
const value = parsed.value;
|
||||
if (
|
||||
value === null ||
|
||||
typeof value !== "object" ||
|
||||
Array.isArray(value) ||
|
||||
!("id" in value) ||
|
||||
typeof value.id !== "string"
|
||||
)
|
||||
return undefined;
|
||||
}
|
||||
return value !== null &&
|
||||
typeof value === "object" &&
|
||||
!Array.isArray(value) &&
|
||||
"id" in value &&
|
||||
typeof value.id === "string"
|
||||
? (value as Record<string, unknown>)
|
||||
: undefined;
|
||||
return { kind: "valid", record: value as Record<string, unknown> };
|
||||
};
|
||||
|
||||
const setReported = (
|
||||
@@ -647,7 +653,8 @@ const parseComment = (state: ParseState, line: string, number: number) => {
|
||||
}
|
||||
const metadata = metadataObject(body);
|
||||
if (metadata !== undefined) {
|
||||
parseMetadata(state, metadata, number);
|
||||
if (metadata.kind === "duplicate") malformed(state, number, "metadata");
|
||||
else parseMetadata(state, metadata.record, number);
|
||||
return;
|
||||
}
|
||||
const header = state.current === undefined ? HEADER.exec(line) : null;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { plistUidValue } from "./plistUid.js";
|
||||
import { z } from "zod";
|
||||
|
||||
import { jsonObjectSchema, jsonValueSchema } from "../jsonValue.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../jsonDuplicateMember.js";
|
||||
import { malformedInput } from "../parseFailure.js";
|
||||
|
||||
const objectNode = z.strictObject({
|
||||
@@ -648,7 +649,9 @@ const jsonSafeRecord = (value: Record<string, unknown>) =>
|
||||
try {
|
||||
const serialized = JSON.stringify(item);
|
||||
if (serialized === undefined) return [];
|
||||
const parsed = jsonValueSchema.safeParse(JSON.parse(serialized));
|
||||
const decoded = parseJsonWithDuplicatePolicy(serialized, "reject");
|
||||
if (!decoded.ok) return [];
|
||||
const parsed = jsonValueSchema.safeParse(decoded.value);
|
||||
return parsed.success ? [[key, parsed.data]] : [];
|
||||
} catch (cause: unknown) {
|
||||
// Unserializable entries are omitted from the record.
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { findDuplicateJsonMember } from "./jsonDuplicateMember.js";
|
||||
import {
|
||||
findDuplicateJsonMember,
|
||||
parseJsonStringLiteral,
|
||||
parseJsonWithDuplicatePolicy,
|
||||
parseLastWinsJson,
|
||||
} from "./jsonDuplicateMember.js";
|
||||
|
||||
it.each([
|
||||
['{"a":1,"a":2}', { name: "a", offset: 7 }],
|
||||
@@ -27,3 +32,20 @@ it("scans nesting deeper than the call stack allows", () => {
|
||||
const text = `${'{"d":'.repeat(depth)}{"a":1,"a":2}${"}".repeat(depth)}`;
|
||||
expect(findDuplicateJsonMember(text)).toMatchObject({ name: "a" });
|
||||
});
|
||||
|
||||
it("parses JSON with an explicit duplicate-member policy and string-literal helper", () => {
|
||||
expect(parseJsonWithDuplicatePolicy('{"a":1,"a":2}', "reject")).toEqual({
|
||||
ok: false,
|
||||
duplicate: { name: "a", offset: 7 },
|
||||
});
|
||||
expect(parseJsonWithDuplicatePolicy('{"a":1,"a":2}', "last-wins")).toEqual({
|
||||
ok: true,
|
||||
value: { a: 2 },
|
||||
});
|
||||
expect(parseJsonWithDuplicatePolicy("{", "reject")).toMatchObject({
|
||||
ok: false,
|
||||
cause: expect.any(SyntaxError),
|
||||
});
|
||||
expect(parseLastWinsJson('{"a":1,"a":2}')).toEqual({ a: 2 });
|
||||
expect(parseJsonStringLiteral('"\\u0061"')).toBe("a");
|
||||
});
|
||||
@@ -1,3 +1,32 @@
|
||||
/**
|
||||
* What a JSON parser in `src/` does when an object declares the same member
|
||||
* name more than once:
|
||||
*
|
||||
* - `reject`: refuse the input as malformed when any object repeats a member
|
||||
* name (`findDuplicateJsonMember`).
|
||||
* - `last-wins`: keep `JSON.parse`'s last value for a repeated name, when a
|
||||
* format or protocol specifies last-value precedence or when an upstream tool
|
||||
* already deduplicated or emitted the JSON object.
|
||||
*/
|
||||
export type JsonDuplicateMemberPolicy = "reject" | "last-wins";
|
||||
|
||||
/** Result of parsing JSON with an explicit duplicate-member policy. */
|
||||
export type ParsedJsonWithPolicy =
|
||||
| {
|
||||
readonly ok: true;
|
||||
readonly value: unknown;
|
||||
}
|
||||
| {
|
||||
readonly ok: false;
|
||||
readonly duplicate: JsonDuplicateMember;
|
||||
readonly cause?: undefined;
|
||||
}
|
||||
| {
|
||||
readonly ok: false;
|
||||
readonly cause: unknown;
|
||||
readonly duplicate?: undefined;
|
||||
};
|
||||
|
||||
/** A member name that occurs more than once in one JSON object. */
|
||||
export interface JsonDuplicateMember {
|
||||
readonly name: string;
|
||||
@@ -8,6 +37,56 @@ export interface JsonDuplicateMember {
|
||||
const QUOTE = 0x22;
|
||||
const BACKSLASH = 0x5c;
|
||||
|
||||
/**
|
||||
* Decode a JSON string literal (`"..."`). A string literal has no object
|
||||
* members, so `JSON.parse` has no duplicate-member ambiguity to resolve.
|
||||
*/
|
||||
export const parseJsonStringLiteral = (raw: string): unknown =>
|
||||
JSON.parse(raw) as unknown;
|
||||
|
||||
/**
|
||||
* Parse JSON text under an explicit duplicate-member policy, with an optional
|
||||
* `JSON.parse` reviver.
|
||||
*
|
||||
* When `policy` is `"reject"`, `findDuplicateJsonMember` scans the validated
|
||||
* JSON text and attaches the first repeated member name as `duplicate` on the
|
||||
* returned result so the caller can raise its format-specific error.
|
||||
*/
|
||||
export const parseJsonWithDuplicatePolicy = (
|
||||
text: string,
|
||||
policy: JsonDuplicateMemberPolicy,
|
||||
reviver?: (
|
||||
key: string,
|
||||
value: unknown,
|
||||
context?: { readonly source?: string },
|
||||
) => unknown,
|
||||
): ParsedJsonWithPolicy => {
|
||||
let value: unknown;
|
||||
try {
|
||||
value = JSON.parse(text, reviver) as unknown;
|
||||
} catch (cause: unknown) {
|
||||
return { ok: false, cause };
|
||||
}
|
||||
const duplicate =
|
||||
policy === "reject" ? findDuplicateJsonMember(text) : undefined;
|
||||
return duplicate === undefined
|
||||
? { ok: true, value }
|
||||
: { ok: false, duplicate };
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse JSON text whose duplicate-member policy is `"last-wins"`, throwing the
|
||||
* native `SyntaxError` on invalid JSON.
|
||||
*/
|
||||
export const parseLastWinsJson = (
|
||||
text: string,
|
||||
reviver?: (
|
||||
key: string,
|
||||
value: unknown,
|
||||
context?: { readonly source?: string },
|
||||
) => unknown,
|
||||
): unknown => JSON.parse(text, reviver) as unknown;
|
||||
|
||||
/**
|
||||
* Find the first object member name that repeats within its object.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { setImmediate as yieldToEventLoop } from "node:timers/promises";
|
||||
import { isRecord } from "../../kernel/isRecord.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../jsonDuplicateMember.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
|
||||
/**
|
||||
@@ -80,12 +81,11 @@ const chargeExpansion = async (
|
||||
export const parseTsharkJsonPackets = (
|
||||
text: string,
|
||||
): Result<TsharkPacketLayers[], TsharkJsonEnvelopeFailure> => {
|
||||
let decoded: unknown;
|
||||
try {
|
||||
decoded = JSON.parse(text) as unknown;
|
||||
} catch (cause) {
|
||||
return err({ kind: "malformed-json", cause });
|
||||
}
|
||||
// Both tshark callers pass `--no-duplicate-keys`, so tshark merges repeated
|
||||
// protocol and field names into arrays before REA parses the JSON document.
|
||||
const parsed = parseJsonWithDuplicatePolicy(text, "last-wins");
|
||||
if (!parsed.ok) return err({ kind: "malformed-json", cause: parsed.cause });
|
||||
const decoded = parsed.value;
|
||||
if (!Array.isArray(decoded))
|
||||
return err({
|
||||
kind: "malformed-packet",
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
evmProducerLimitsSchema,
|
||||
selectedEvmProducerLimits,
|
||||
} from "../domain/evm/evmInterface.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import { readStableArtifact } from "../resources/index.js";
|
||||
import { errorMessage } from "../kernel/errorMessage.js";
|
||||
import { sha256Hex } from "../kernel/sha256Hex.js";
|
||||
@@ -23,14 +24,24 @@ const requestSchema = z.strictObject({
|
||||
encoding: z.enum(["raw", "hex"]),
|
||||
producer_limits: evmProducerLimitsSchema,
|
||||
});
|
||||
/** Maximum UTF-8 byte size of the REA-authored worker request JSON file. */
|
||||
const MAX_EVM_WORKER_REQUEST_BYTES = 64 * 1024;
|
||||
class UnsupportedContainer extends Error {}
|
||||
|
||||
const main = async (): Promise<void> => {
|
||||
const requestPath = process.argv[2];
|
||||
if (requestPath === undefined)
|
||||
throw new EvmAdmissionError("request_path_missing");
|
||||
const requestJson = parseJsonWithDuplicatePolicy(
|
||||
(
|
||||
await readStableArtifact(requestPath, MAX_EVM_WORKER_REQUEST_BYTES)
|
||||
).bytes.toString("utf8"),
|
||||
"reject",
|
||||
);
|
||||
if (!requestJson.ok && requestJson.cause !== undefined)
|
||||
throw requestJson.cause;
|
||||
const request = requestSchema.parse(
|
||||
JSON.parse(await readFile(requestPath, "utf8")),
|
||||
requestJson.ok ? requestJson.value : null,
|
||||
);
|
||||
const limits = selectedEvmProducerLimits(request.producer_limits);
|
||||
let reply: unknown;
|
||||
@@ -45,13 +56,14 @@ const main = async (): Promise<void> => {
|
||||
"Container marker EF00 is outside the verified flat-bytecode interface profile.",
|
||||
);
|
||||
const modulePath = import.meta.resolve("evmole/no_tla");
|
||||
const packageJson = parseJsonWithDuplicatePolicy(
|
||||
await readFile(new URL("../package.json", modulePath), "utf8"),
|
||||
"last-wins",
|
||||
);
|
||||
if (!packageJson.ok) throw packageJson.cause;
|
||||
const metadata = z
|
||||
.object({ version: z.literal("0.9.3") })
|
||||
.parse(
|
||||
JSON.parse(
|
||||
await readFile(new URL("../package.json", modulePath), "utf8"),
|
||||
),
|
||||
);
|
||||
.parse(packageJson.value);
|
||||
const engine = await import("evmole/no_tla");
|
||||
engine.initSync({
|
||||
module: await readFile(
|
||||
|
||||
@@ -302,7 +302,14 @@ export const normalizeUnblobReport = (
|
||||
const taskFailures = new Set<string>();
|
||||
const missingDependencies = new Map<string, DependencyFacts>();
|
||||
/** Dependencies reported by one chunk's extraction, keyed with its chunk id. */
|
||||
const chunkMissingDependencies = new Map<string, DependencyFacts>();
|
||||
const chunkMissingDependencies = new Map<
|
||||
string,
|
||||
{
|
||||
readonly input_path: string;
|
||||
readonly provider_id: string;
|
||||
readonly facts: DependencyFacts;
|
||||
}
|
||||
>();
|
||||
const dependencyRows = (
|
||||
facts: DependencyFacts,
|
||||
): readonly { dependencies: readonly string[] }[] => [
|
||||
@@ -409,11 +416,21 @@ export const normalizeUnblobReport = (
|
||||
detail?.extraction_reports ?? [],
|
||||
);
|
||||
// Chunk-nested reports retain their owning chunk, not the whole task.
|
||||
accumulateDependencies(
|
||||
chunkMissingDependencies,
|
||||
JSON.stringify([path, parsed.id]),
|
||||
problems.dependencies,
|
||||
);
|
||||
if (problems.dependencies !== null) {
|
||||
const chunkKey = JSON.stringify([path, parsed.id]);
|
||||
const knownChunk = chunkMissingDependencies.get(chunkKey);
|
||||
if (knownChunk === undefined) {
|
||||
chunkMissingDependencies.set(chunkKey, {
|
||||
input_path: path,
|
||||
provider_id: parsed.id,
|
||||
facts: problems.dependencies,
|
||||
});
|
||||
} else {
|
||||
for (const name of problems.dependencies.names)
|
||||
knownChunk.facts.names.add(name);
|
||||
knownChunk.facts.unnamed ||= problems.dependencies.unnamed;
|
||||
}
|
||||
}
|
||||
if (detail !== null)
|
||||
for (const problem of detail.extraction_reports) {
|
||||
diagnostics.push(
|
||||
@@ -611,15 +628,14 @@ export const normalizeUnblobReport = (
|
||||
? 1
|
||||
: 0,
|
||||
),
|
||||
chunk_missing_dependencies: [...chunkMissingDependencies.entries()]
|
||||
.flatMap(([key, facts]) => {
|
||||
const [input_path, provider_id] = JSON.parse(key) as [string, string];
|
||||
return dependencyRows(facts).map(({ dependencies }) => ({
|
||||
chunk_missing_dependencies: [...chunkMissingDependencies.values()]
|
||||
.flatMap(({ input_path, provider_id, facts }) =>
|
||||
dependencyRows(facts).map(({ dependencies }) => ({
|
||||
input_path,
|
||||
provider_id,
|
||||
dependencies,
|
||||
}));
|
||||
})
|
||||
})),
|
||||
)
|
||||
.sort((left, right) =>
|
||||
`${left.input_path}\u0000${left.provider_id}` <
|
||||
`${right.input_path}\u0000${right.provider_id}`
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
|
||||
import { parseJsonStringLiteral } from "../domain/jsonDuplicateMember.js";
|
||||
import { escapeJsonPointerToken } from "../domain/jsonPointer.js";
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
import { assertArtifactReadActive } from "../resources/index.js";
|
||||
@@ -50,7 +51,7 @@ class HeaderJson {
|
||||
const raw = this.text.slice(start, this.#position);
|
||||
this.budget.charge(6 * raw.length + 2);
|
||||
try {
|
||||
const value: unknown = JSON.parse(raw);
|
||||
const value: unknown = parseJsonStringLiteral(raw);
|
||||
if (typeof value !== "string")
|
||||
return rejectModel("Invalid JSON string");
|
||||
for (const unit of value) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { strictUtf8Decoder } from "../domain/strictUtf8.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import {
|
||||
NATIVE_CALL_CAPTURE_DEFAULTS,
|
||||
type NativeCallCaptureLimits,
|
||||
@@ -126,13 +127,19 @@ const parseLldbObservationJournal = (bytes: Uint8Array): ParsedLldbJournal => {
|
||||
malformedCount += 1;
|
||||
firstMalformedRow ??= rowNumber;
|
||||
};
|
||||
let row: unknown;
|
||||
let text: string;
|
||||
try {
|
||||
row = JSON.parse(decoder.decode(line));
|
||||
text = decoder.decode(line);
|
||||
} catch {
|
||||
markMalformed();
|
||||
continue;
|
||||
}
|
||||
const parsed = parseJsonWithDuplicatePolicy(text, "reject");
|
||||
if (!parsed.ok) {
|
||||
markMalformed();
|
||||
continue;
|
||||
}
|
||||
const row = parsed.value;
|
||||
if (
|
||||
typeof row !== "object" ||
|
||||
row === null ||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { parseJsonStringLiteral } from "../../domain/jsonDuplicateMember.js";
|
||||
|
||||
/** JSON container type a projection declined to materialize. */
|
||||
export type JsonContainerType = "array" | "object";
|
||||
|
||||
@@ -312,7 +314,7 @@ class Parser {
|
||||
const decoded: unknown = escaped
|
||||
? // A decoded JSON string cannot contain more code units than its literal.
|
||||
// The input's byte allowance bounds this allocation, including dense escapes.
|
||||
JSON.parse(this.text.slice(start, this.position))
|
||||
parseJsonStringLiteral(this.text.slice(start, this.position))
|
||||
: this.text.slice(start + 1, this.position - 1);
|
||||
if (typeof decoded !== "string")
|
||||
throw new LosslessJsonFailure("Expected a JSON string");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { decodeBinaryPropertyList } from "../../domain/apple/binaryPlistMaterializer.js";
|
||||
import { parseLastWinsJson } from "../../domain/jsonDuplicateMember.js";
|
||||
import {
|
||||
plistDuplicateKeyLimitations,
|
||||
type PlistDuplicateKeys,
|
||||
@@ -157,7 +158,7 @@ export const readPlistJson = (output: string): PlistJsonParseAttempt => {
|
||||
// plutil output can carry a UTF-8 BOM, which JSON.parse rejects outright.
|
||||
const text = output.replace(/^\uFEFF/u, "");
|
||||
decoded = ok(
|
||||
JSON.parse(
|
||||
parseLastWinsJson(
|
||||
text,
|
||||
(
|
||||
_key: string,
|
||||
|
||||
+11
-19
@@ -1,7 +1,7 @@
|
||||
import type { Stats } from "node:fs";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
|
||||
import { findDuplicateJsonMember } from "../domain/jsonDuplicateMember.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
|
||||
import { assertArtifactReadActive } from "./ArtifactFileOperation.js";
|
||||
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||
@@ -445,22 +445,14 @@ export const readAsarHeader = async (
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
const header = JSON.parse(json) as unknown;
|
||||
rejectDuplicateAsarHeaderMembers(json);
|
||||
return header;
|
||||
};
|
||||
|
||||
/**
|
||||
* `JSON.parse` keeps only the last of repeated member names, so a second
|
||||
* declaration of a path would hide the first from the listing while coverage
|
||||
* still read as complete. Treat a repeated name as a malformed index, like the
|
||||
* unsafe path components the listing already refuses.
|
||||
*/
|
||||
const rejectDuplicateAsarHeaderMembers = (json: string): void => {
|
||||
const duplicate = findDuplicateJsonMember(json);
|
||||
if (duplicate !== undefined)
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
`ASAR JSON header declares member ${JSON.stringify(duplicate.name)} more than once in one object (offset ${String(duplicate.offset)})`,
|
||||
);
|
||||
const parsed = parseJsonWithDuplicatePolicy(json, "reject");
|
||||
if (!parsed.ok) {
|
||||
if (parsed.duplicate !== undefined)
|
||||
throw new ArtifactReaderFailure(
|
||||
"format",
|
||||
`ASAR JSON header declares member ${JSON.stringify(parsed.duplicate.name)} more than once in one object (offset ${String(parsed.duplicate.offset)})`,
|
||||
);
|
||||
throw parsed.cause;
|
||||
}
|
||||
return parsed.value;
|
||||
};
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from "../domain/rust/rustBinary.js";
|
||||
import { jsonValueByteLength } from "../domain/jsonValueByteLength.js";
|
||||
import { malformedInput, ParseFailureError } from "../domain/parseFailure.js";
|
||||
import { findDuplicateJsonMember } from "../domain/jsonDuplicateMember.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
|
||||
|
||||
/** Compressed zlib stream limit. */
|
||||
@@ -231,19 +231,17 @@ const parseDocument = (
|
||||
throw malformedSection("Dependency JSON is not valid UTF-8", cause);
|
||||
}
|
||||
assertDepth(text);
|
||||
let value: unknown;
|
||||
try {
|
||||
value = JSON.parse(text);
|
||||
} catch (cause) {
|
||||
throw malformedSection("Dependency section JSON is invalid", cause);
|
||||
}
|
||||
// JSON.parse keeps the last of a repeated field; the format allows one.
|
||||
const duplicate = findDuplicateJsonMember(text);
|
||||
if (duplicate !== undefined)
|
||||
throw malformedInput(
|
||||
RUST_AUDITABLE_FORMAT,
|
||||
`Dependency JSON contains duplicate field ${JSON.stringify(duplicate.name)}`,
|
||||
);
|
||||
const decoded = parseJsonWithDuplicatePolicy(text, "reject");
|
||||
if (!decoded.ok) {
|
||||
if (decoded.duplicate !== undefined)
|
||||
throw malformedInput(
|
||||
RUST_AUDITABLE_FORMAT,
|
||||
`Dependency JSON contains duplicate field ${JSON.stringify(decoded.duplicate.name)}`,
|
||||
);
|
||||
throw malformedSection("Dependency section JSON is invalid", decoded.cause);
|
||||
}
|
||||
const value = decoded.value;
|
||||
const version = documentVersionSchema.safeParse(value);
|
||||
if (!version.success)
|
||||
throw malformedSection(
|
||||
|
||||
Reference in new issue
Block a user