fix(apple): bound binary plist expansion before decoding keyed archives (#1515)
inspect_keyed_archive passed caller-selected binary plists straight to plist.parseBinary, which recurses on every object reference and copies shared containers. A 484-byte archive of forty nested [next, next] arrays grew the process past 4 GB until it died with no result, which would also end an MCP server. A 125-byte self-referencing array overflowed the stack and was reported as an artifact too large to process. The iterative reference walk that already guards decode_interface_builder now runs first, with an 8x keyed-archive decode budget. Over-budget depth or expansion is a limit; a reference cycle is a malformed archive. Its "decoder" bounds admit the same layouts plist.parseBinary reads, so accepted archives stay accepted. Interface Builder behavior is unchanged. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a936303723
commit
2bf812772d
@@ -103,3 +103,88 @@ describe("inert keyed archive decoding", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
/** Encode a bplist00 whose objects reference each other by index. */
|
||||
const binaryPlist = (objects: readonly Buffer[]): Buffer => {
|
||||
const header = Buffer.from("bplist00");
|
||||
const offsets: number[] = [];
|
||||
let position = header.length;
|
||||
for (const object of objects) {
|
||||
offsets.push(position);
|
||||
position += object.length;
|
||||
}
|
||||
const table = Buffer.alloc(objects.length * 4);
|
||||
offsets.forEach((offset, index) => table.writeUInt32BE(offset, index * 4));
|
||||
const trailer = Buffer.alloc(32);
|
||||
trailer[6] = 4;
|
||||
trailer[7] = 2;
|
||||
trailer.writeBigUInt64BE(BigInt(objects.length), 8);
|
||||
trailer.writeBigUInt64BE(0n, 16);
|
||||
trailer.writeBigUInt64BE(BigInt(position), 24);
|
||||
return Buffer.concat([header, ...objects, table, trailer]);
|
||||
};
|
||||
const plistArray = (references: readonly number[]): Buffer => {
|
||||
const bytes = Buffer.alloc(1 + references.length * 2);
|
||||
bytes[0] = 0xa0 | references.length;
|
||||
references.forEach((reference, index) =>
|
||||
bytes.writeUInt16BE(reference, 1 + index * 2),
|
||||
);
|
||||
return bytes;
|
||||
};
|
||||
const plistString = (value: string): Buffer =>
|
||||
Buffer.concat([Buffer.from([0x50 | value.length]), Buffer.from(value)]);
|
||||
// {"$archiver": "NSKeyedArchiver", "$top": {"root": UID 1}, "$objects": object 8}
|
||||
const keyedRoot = (payload: readonly Buffer[]): Buffer =>
|
||||
binaryPlist([
|
||||
Buffer.from([0xd3, 0, 1, 0, 3, 0, 4, 0, 2, 0, 8, 0, 5]),
|
||||
plistString("$archiver"),
|
||||
Buffer.concat([
|
||||
Buffer.from([0x5f, 0x10, 15]),
|
||||
Buffer.from("NSKeyedArchiver"),
|
||||
]),
|
||||
plistString("$objects"),
|
||||
plistString("$top"),
|
||||
Buffer.from([0xd1, 0, 6, 0, 7]),
|
||||
plistString("root"),
|
||||
Buffer.from([0x80, 1]),
|
||||
...payload,
|
||||
]);
|
||||
|
||||
describe("binary plist reference preflight", () => {
|
||||
const selection = { offset: 0, limit: 10 };
|
||||
|
||||
it("reports a reference cycle as malformed, not as an oversized archive", () => {
|
||||
expect(() =>
|
||||
decodeKeyedArchiveBytes(keyedRoot([plistArray([8])]), selection),
|
||||
).toThrow(
|
||||
new TypeError(
|
||||
"binary plist object references form a cycle, which a property list cannot represent",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects exponential shared-container expansion before decoding", () => {
|
||||
// Forty levels of [next, next] expand to 2^40 leaves in a few hundred bytes.
|
||||
const levels = Array.from({ length: 40 }, (_, index) =>
|
||||
plistArray([9 + index, 9 + index]),
|
||||
);
|
||||
expect(() =>
|
||||
decodeKeyedArchiveBytes(
|
||||
keyedRoot([...levels, plistString("x")]),
|
||||
selection,
|
||||
),
|
||||
).toThrow(RangeError);
|
||||
});
|
||||
|
||||
it("decodes shared leaf references", () => {
|
||||
const decoded = decodeKeyedArchiveBytes(
|
||||
keyedRoot([plistArray([9, 9, 9]), plistString("$null")]),
|
||||
selection,
|
||||
);
|
||||
expect(decoded.objects.map(({ id, value }) => [id, value])).toEqual([
|
||||
[0, "$null"],
|
||||
[1, "$null"],
|
||||
[2, "$null"],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user