Files
reverse-engineering-anything/src/domain/jsonValue.ts
T
Aniket Rawatandmorluto dd1bb43db3 feat(elf): decode FDO packaging and dlopen note metadata (#7237)
* feat(elf): decode FDO packaging and dlopen note metadata

Name FDO notes within their owner's namespace: type 0xcafe1a7e is
fdo_packaging_metadata and 0x407c0c0a is fdo_dlopen_metadata. One
elfNoteTypeName(owner, type) replaces the GNU-only gnuNoteTypeName.

Each entry gains an fdo field next to gnu. metadata holds the descriptor's
JSON object (packaging) or array (dlopen) when it is NUL-terminated UTF-8
JSON followed only by NUL padding; otherwise metadata is null and issue
names the broken rule, without failing the inspection. Each descriptor
range is decoded once and its retained JSON text is charged to the shared
note text budget per repeated entry. A limitation marks the metadata as a
producer declaration.

Fixes #7235

* fix(elf): key FDO note decoding by note type

The FDO decode cache was keyed by descriptor range only, but the note
type selects the required JSON shape. Overlapping PT_NOTE and SHT_NOTE
tables can give one descriptor range a packaging note and a dlopen note,
and the second reused the first note's verdict. Include the type name in
the key.

Refs #7235

* fix(elf): bound FDO note decoding before parsing JSON

The FDO decoder charged only descriptor source bytes before JSON.parse,
but a dense JSON value such as [{},{},...] decodes to about 50 times its
source size in heap before the service and Evidence copy it again. A
16 MiB descriptor took about 790 MiB of heap; one near the default
64 MiB file budget could exhaust the Node heap. Leave descriptors above
64 KiB undecoded with an issue naming the limit; real FDO notes are a
few hundred bytes.

Refs #7235

* fix(elf): share one FDO decode allowance across the report

The 64 KiB FDO decode bound applied to each descriptor, so raising
max_entries to 256 admitted 256 distinct dense 64 KiB notes: a 16 MiB
file added about 700 MiB of heap before Evidence copied it again.
Charge each distinct descriptor's size to one 64 KiB allowance per
report; a range repeated by another table reuses its decode, and notes
past the allowance keep their entry with an issue.

Refs #7235

* test(desktop): exercise the shared JSON depth guard in the heap suite

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-19 06:01:39 +00:00

233 lines
7.6 KiB
TypeScript

import { types } from "node:util";
import { z } from "zod";
import { isImmutableJsonSnapshot } from "./immutableJson.js";
import { isRecord } from "../kernel/isRecord.js";
/** Maximum JSON nesting accepted before recursive result cloning. */
export const MAX_JSON_DEPTH = 1000;
/** JSON-safe value shared by domain, application, and adapter boundaries. */
export type JsonValue =
| string
| number
| boolean
| null
| JsonValue[]
| { [key: string]: JsonValue };
const immutableValidation = new WeakMap<object, string | undefined>();
/** Validate JSON meaning and nesting without cloning a trusted immutable value. */
export const jsonValueValidationIssue = (root: unknown): string | undefined => {
const steps = jsonValueValidationSteps(root);
for (;;) {
const step = steps.next();
if (step.done) return step.value;
}
};
function* ownEnumerableKeys(value: Record<string, unknown>): Generator<string> {
for (const key in value) if (Object.hasOwn(value, key)) yield key;
}
function* jsonObjectValues(
record: Record<string, JsonValue>,
): Generator<JsonValue> {
for (const key of ownEnumerableKeys(record)) yield record[key]!;
}
/** Check a validated value's nesting without queuing whole containers. */
export const jsonValueExceedsDepth = (
root: JsonValue,
limit: number,
): boolean => {
const levels: Iterator<JsonValue>[] = [[root].values()];
for (let level = levels.at(-1); level !== undefined; level = levels.at(-1)) {
const next = level.next();
if (next.done === true) levels.pop();
else if (levels.length - 1 > limit) return true;
else if (Array.isArray(next.value)) levels.push(next.value.values());
else if (isRecord(next.value)) levels.push(jsonObjectValues(next.value));
}
return false;
};
/** Validate JSON in bounded steps; cache only authenticated immutable roots. */
export function* jsonValueValidationSteps(
root: unknown,
): Generator<void, string | undefined> {
const immutable =
typeof root === "object" && root !== null && isImmutableJsonSnapshot(root)
? root
: undefined;
if (immutable !== undefined && immutableValidation.has(immutable))
return immutableValidation.get(immutable);
const finish = (issue: string | undefined): string | undefined => {
if (immutable !== undefined) immutableValidation.set(immutable, issue);
return issue;
};
type Frame =
| {
readonly kind: "array";
readonly value: unknown[];
readonly depth: number;
index: number;
}
| {
readonly kind: "object";
readonly value: Record<string, unknown>;
readonly depth: number;
readonly keys: Generator<string>;
};
const pending: Frame[] = [];
let value = root;
let depth = 0;
let inspect = true;
let examined = 0;
for (;;) {
if (inspect) {
if (examined++ === 4096) {
examined = 0;
yield;
}
if (depth > MAX_JSON_DEPTH)
return finish(
`JSON value exceeds maximum nesting depth of ${MAX_JSON_DEPTH}. This release has no runtime override.`,
);
if (typeof value === "object" && value !== null && types.isProxy(value))
return finish("JSON values cannot contain proxies");
if (
value === null ||
typeof value === "string" ||
typeof value === "boolean"
) {
inspect = false;
} else if (typeof value === "number") {
if (!Number.isFinite(value))
return finish("JSON numbers must be finite");
inspect = false;
} else if (Array.isArray(value)) {
pending.push({
kind: "array",
value,
depth,
index: 0,
});
inspect = false;
} else if (isRecord(value)) {
const prototype: unknown = Object.getPrototypeOf(value);
if (prototype !== Object.prototype && prototype !== null)
return finish("JSON objects require ordinary data properties");
for (const key of Object.getOwnPropertySymbols(value))
if (Object.getOwnPropertyDescriptor(value, key)?.enumerable === true)
return finish("JSON object keys must be strings");
pending.push({
kind: "object",
value,
depth,
keys: ownEnumerableKeys(value),
});
inspect = false;
} else return finish("Invalid JSON value");
continue;
}
const frame = pending[pending.length - 1];
if (frame === undefined) return finish(undefined);
if (frame.kind === "array") {
if (frame.index >= frame.value.length) {
pending.pop();
continue;
}
const index = frame.index++;
const descriptor = Object.getOwnPropertyDescriptor(frame.value, index);
if (descriptor === undefined || !Object.hasOwn(descriptor, "value"))
return finish(
"JSON arrays require dense data properties without accessors",
);
value = descriptor.value;
depth = frame.depth + 1;
inspect = true;
continue;
}
const next = frame.keys.next();
if (next.done) {
pending.pop();
continue;
}
const descriptor = Object.getOwnPropertyDescriptor(frame.value, next.value);
if (descriptor?.enumerable !== true) continue;
if (!Object.hasOwn(descriptor, "value"))
return finish("JSON objects cannot contain accessors");
value = descriptor.value;
depth = frame.depth + 1;
inspect = true;
}
}
const preserveJsonValue = (value: JsonValue): JsonValue => {
const immutableCandidate: unknown = value;
if (isImmutableJsonSnapshot(immutableCandidate)) return value;
if (Array.isArray(value)) {
const copy: JsonValue[] = [];
for (let index = 0; index < value.length; index += 1)
copy.push(preserveJsonValue(value[index]!));
return copy;
}
if (value !== null && typeof value === "object") {
const copy: Record<string, JsonValue> = {};
for (const key of ownEnumerableKeys(value))
Object.defineProperty(copy, key, {
value: preserveJsonValue(value[key]!),
enumerable: true,
configurable: true,
writable: true,
});
return copy;
}
return value;
};
const validatedJsonValue = z
.unknown()
.superRefine((value, context) => {
const issue = jsonValueValidationIssue(value);
if (issue !== undefined)
context.addIssue({ code: "custom", message: issue, continue: false });
})
.transform((value) => value as JsonValue);
// Keep both stages nonrecursive in public and Standard Schema projections.
validatedJsonValue._zod.toJSONSchema = () => ({});
/** Parse bounded JSON and retain all own data members without prototype setters. */
export const jsonValueSchema = z
.unknown()
.nonoptional()
.pipe(validatedJsonValue)
.transform(preserveJsonValue);
jsonValueSchema._zod.toJSONSchema = () => ({});
/** Validate JSON that the current boundary exclusively owns, without a second full-tree copy. */
export const ownedJsonValueSchema = z
.unknown()
.nonoptional()
.pipe(validatedJsonValue);
ownedJsonValueSchema._zod.toJSONSchema = () => ({});
const objectValidation = z.custom<Record<string, JsonValue>>(
isRecord,
"Expected JSON object",
);
const objectProjection = () => ({ type: "object", additionalProperties: {} });
objectValidation._zod.toJSONSchema = objectProjection;
/** JSON object boundary for caller-visible parameter maps. */
export const jsonObjectSchema = jsonValueSchema.pipe(objectValidation);
jsonObjectSchema._zod.toJSONSchema = objectProjection;
/** Validate an exclusively owned JSON object while retaining its existing tree. */
export const ownedJsonObjectSchema =
ownedJsonValueSchema.pipe(objectValidation);
ownedJsonObjectSchema._zod.toJSONSchema = objectProjection;