fix(apple): budget Interface Builder decoding and projection

This commit is contained in:
morluto
2026-10-09 05:15:23 +08:00
parent f2e9c491ea
commit dbcf8c3391
7 changed files with 861 additions and 49 deletions
+158 -8
View File
@@ -1,8 +1,12 @@
import type { JsonValue } from "../../domain/jsonValue.js";
import { TextDecoder } from "node:util";
import {
InterfaceBuilderDecodeBudget,
InterfaceBuilderDecodeBudgetExceeded,
} from "./InterfaceBuilderDecodeBudget.js";
const MAGIC = Buffer.from("NIBArchive", "ascii");
const MAX_RECORDS = 1_000_000;
const DEFAULT_DECODE_BUDGET_BYTES = 128 * 1024 * 1024;
interface NibObjectRecord {
readonly class_index: number;
@@ -31,7 +35,10 @@ export interface NibArchiveDocument {
}
/** Decode Apple's length-prefixed NIBArchive representation without loading UI classes. */
export const decodeNibArchive = (bytes: Buffer): NibArchiveDocument => {
export const decodeNibArchive = (
bytes: Buffer,
budget = new InterfaceBuilderDecodeBudget(DEFAULT_DECODE_BUDGET_BYTES),
): NibArchiveDocument => {
if (bytes.length < 50 || !bytes.subarray(0, MAGIC.length).equals(MAGIC))
throw new TypeError("NIBArchive magic or header is invalid");
@@ -57,6 +64,19 @@ export const decodeNibArchive = (bytes: Buffer): NibArchiveDocument => {
valuesOffset,
classesOffset,
]);
const projectedFieldCount = preflightProjectedFieldCount(
bytes,
objectsOffset,
keysOffset,
objectCount,
valueCount,
classCount,
budget.remainingBytes,
);
budget.reserve(
objectCount * 96 + projectedFieldCount * 96,
"NIBArchive object and projected field counts exceed the aggregate Interface Builder decode budget",
);
const objectRecords = parseObjects(
bytes,
objectsOffset,
@@ -65,8 +85,21 @@ export const decodeNibArchive = (bytes: Buffer): NibArchiveDocument => {
valueCount,
classCount,
);
const keys = parseStrings(bytes, keysOffset, valuesOffset, keyCount, "key");
const classes = parseClasses(bytes, classesOffset, bytes.length, classCount);
const keys = parseStrings(
bytes,
keysOffset,
valuesOffset,
keyCount,
"key",
budget,
);
const classes = parseClasses(
bytes,
classesOffset,
bytes.length,
classCount,
budget,
);
const neededValues = valueIndexes(objectRecords, valueCount);
const values = parseValues(
bytes,
@@ -75,6 +108,7 @@ export const decodeNibArchive = (bytes: Buffer): NibArchiveDocument => {
valueCount,
keyCount,
neededValues,
budget,
);
for (const { value } of values.values()) {
if (
@@ -158,18 +192,64 @@ const parseObjects = (
return output;
};
const preflightProjectedFieldCount = (
bytes: Buffer,
start: number,
end: number,
objectCount: number,
valueCount: number,
classCount: number,
maxBytes: number,
): number => {
if (objectCount * 96 > maxBytes)
throw new InterfaceBuilderDecodeBudgetExceeded(
"aggregate_decode_budget_exhausted",
"NIBArchive object count exceeds the aggregate Interface Builder decode budget",
);
const cursor = { offset: start };
let projectedFields = 0;
for (let index = 0; index < objectCount; index += 1) {
const classIndex = readVarint(bytes, cursor, end);
const valueStart = readVarint(bytes, cursor, end);
const fieldCount = readVarint(bytes, cursor, end);
if (
classIndex >= classCount ||
valueStart > valueCount ||
fieldCount > valueCount - valueStart
)
throw new TypeError(
`NIBArchive object ${index} references an invalid table range`,
);
projectedFields += fieldCount;
if (
!Number.isSafeInteger(projectedFields) ||
projectedFields * 96 > maxBytes - objectCount * 96
)
throw new InterfaceBuilderDecodeBudgetExceeded(
"aggregate_decode_budget_exhausted",
"NIBArchive projected field ranges exceed the aggregate Interface Builder decode budget",
);
}
return projectedFields;
};
const parseStrings = (
bytes: Buffer,
start: number,
end: number,
count: number,
label: string,
budget: InterfaceBuilderDecodeBudget,
): string[] => {
const cursor = { offset: start };
const output: string[] = [];
for (let index = 0; index < count; index += 1) {
const length = readVarint(bytes, cursor, end);
const next = checkedEnd(cursor.offset, length, end, `${label} ${index}`);
budget.reserve(
32 + length * 2,
`NIBArchive ${label} strings exceed the aggregate Interface Builder decode budget`,
);
const value = decodeUtf8(bytes, cursor.offset, next, `${label} ${index}`);
cursor.offset = next;
output.push(value);
@@ -182,6 +262,7 @@ const parseClasses = (
start: number,
end: number,
count: number,
budget: InterfaceBuilderDecodeBudget,
): string[] => {
const cursor = { offset: start };
const output: string[] = [];
@@ -200,6 +281,10 @@ const parseClasses = (
);
cursor.offset += extraCount * 4;
const next = checkedEnd(cursor.offset, length, end, `class ${index}`);
budget.reserve(
32 + length * 2,
"NIBArchive class names exceed the aggregate Interface Builder decode budget",
);
const name = decodeUtf8(
bytes,
cursor.offset,
@@ -219,6 +304,7 @@ const parseValues = (
count: number,
keyCount: number,
needed: ReadonlySet<number>,
budget: InterfaceBuilderDecodeBudget,
): Map<number, NibValueRecord> => {
const cursor = { offset: start };
const output = new Map<number, NibValueRecord>();
@@ -230,8 +316,16 @@ const parseValues = (
);
ensureRange(cursor.offset, 1, end, `value ${index} type`);
const type = bytes[cursor.offset++] ?? -1;
const value = readValue(bytes, cursor, end, type, index);
if (needed.has(index)) output.set(index, { key_index: keyIndex, value });
if (!needed.has(index)) {
skipValue(bytes, cursor, end, type, index);
continue;
}
budget.reserve(
64,
"NIBArchive decoded values exceed the aggregate Interface Builder decode budget",
);
const value = readValue(bytes, cursor, end, type, index, budget);
output.set(index, { key_index: keyIndex, value });
}
if (cursor.offset > end)
throw new TypeError("NIBArchive value table overlaps the next table");
@@ -244,6 +338,7 @@ const readValue = (
end: number,
type: number,
index: number,
budget: InterfaceBuilderDecodeBudget,
): JsonValue => {
switch (type) {
case 0:
@@ -299,6 +394,10 @@ const readValue = (
end,
`value ${index} data`,
);
budget.reserve(
32 + Math.ceil((length * 4) / 3),
`NIBArchive data value ${index} exceeds the aggregate Interface Builder decode budget`,
);
const encoded = bytes.subarray(cursor.offset, next).toString("base64");
cursor.offset = next;
return { $nib_data_base64: encoded };
@@ -319,6 +418,57 @@ const readValue = (
}
};
const skipValue = (
bytes: Buffer,
cursor: { offset: number },
end: number,
type: number,
index: number,
): void => {
switch (type) {
case 0:
ensureRange(cursor.offset, 1, end, `value ${index}`);
cursor.offset += 1;
return;
case 1:
ensureRange(cursor.offset, 2, end, `value ${index}`);
cursor.offset += 2;
return;
case 2:
case 10:
ensureRange(cursor.offset, 4, end, `value ${index}`);
cursor.offset += 4;
return;
case 3:
case 7:
ensureRange(cursor.offset, 8, end, `value ${index}`);
cursor.offset += 8;
return;
case 6:
ensureRange(cursor.offset, 4, end, `value ${index}`);
cursor.offset += 4;
return;
case 4:
case 5:
case 9:
return;
case 8: {
const length = readVarint(bytes, cursor, end);
cursor.offset = checkedEnd(
cursor.offset,
length,
end,
`value ${index} data`,
);
return;
}
default:
throw new TypeError(
`NIBArchive value ${index} has unsupported type ${type}`,
);
}
};
const valueIndexes = (
objects: readonly NibObjectRecord[],
total: number,
@@ -340,8 +490,8 @@ const valueIndexes = (
const readCount = (bytes: Buffer, offset: number, label: string): number => {
const count = readU32(bytes, offset);
if (count > MAX_RECORDS)
throw new TypeError(`NIBArchive ${label} count exceeds the decoder bound`);
if (count > bytes.length)
throw new TypeError(`NIBArchive ${label} count exceeds the archive length`);
return count;
};