Merge pull request #899 from morluto/fix/interface-builder-plist-data
fix(artifacts): decode flat keyed-archive NIBs with data and shared references
This commit is contained in:
@@ -2,6 +2,7 @@ import { createHash } from "node:crypto";
|
|||||||
import { parseBinary } from "plist";
|
import { parseBinary } from "plist";
|
||||||
|
|
||||||
import { DirectoryArtifactReader } from "../artifacts/DirectoryArtifactReader.js";
|
import { DirectoryArtifactReader } from "../artifacts/DirectoryArtifactReader.js";
|
||||||
|
import { projectPlistValue } from "../domain/plistValue.js";
|
||||||
import {
|
import {
|
||||||
omittedPrototypeKeysLimitation,
|
omittedPrototypeKeysLimitation,
|
||||||
parseXmlPropertyList,
|
parseXmlPropertyList,
|
||||||
@@ -390,6 +391,7 @@ const readEntry = async (
|
|||||||
return Buffer.concat(chunks, length);
|
return Buffer.concat(chunks, length);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Project decoded plist data and dates while retaining omitted-key coverage. */
|
||||||
const decodePlist = (
|
const decodePlist = (
|
||||||
bytes: Buffer,
|
bytes: Buffer,
|
||||||
): { readonly value: JsonValue; readonly omittedPrototypeKeys: number } => {
|
): { readonly value: JsonValue; readonly omittedPrototypeKeys: number } => {
|
||||||
@@ -397,5 +399,5 @@ const decodePlist = (
|
|||||||
bytes.subarray(0, 8).toString("ascii") === "bplist00"
|
bytes.subarray(0, 8).toString("ascii") === "bplist00"
|
||||||
? { value: parseBinary(bytes), omittedPrototypeKeys: 0 }
|
? { value: parseBinary(bytes), omittedPrototypeKeys: 0 }
|
||||||
: parseXmlPropertyList(bytes.toString("utf8"));
|
: parseXmlPropertyList(bytes.toString("utf8"));
|
||||||
return { value: jsonValueSchema.parse(value), omittedPrototypeKeys };
|
return { value: projectPlistValue(value).value, omittedPrototypeKeys };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -318,6 +318,91 @@ describe("keyed archive decoding and graph limits", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("keyed archive object references", () => {
|
||||||
|
it("keeps referenced objects as stubs so shared and cyclic references stay finite", () => {
|
||||||
|
// Each view references its window, superview, and next key view, so
|
||||||
|
// inlining references would expand the table into an exponential tree.
|
||||||
|
const viewCount = 40;
|
||||||
|
const views = Array.from({ length: viewCount }, (_, index) => ({
|
||||||
|
$class: { UID: 1 },
|
||||||
|
NSWindow: { UID: 2 },
|
||||||
|
NSSuperview: { UID: index === 0 ? 2 : 3 + index - 1 },
|
||||||
|
NSNextKeyView: { UID: 3 + ((index + 1) % viewCount) },
|
||||||
|
NSSubviews: index + 1 < viewCount ? [{ UID: 3 + index + 1 }] : [],
|
||||||
|
NSTitle: { UID: 3 + viewCount },
|
||||||
|
}));
|
||||||
|
const parsed = parseInterfaceBuilderRecords({
|
||||||
|
$archiver: "NSKeyedArchiver",
|
||||||
|
$objects: [
|
||||||
|
"$null",
|
||||||
|
{ $classname: "NSView", $classes: ["NSView", "NSObject"] },
|
||||||
|
{ $class: { UID: 3 + viewCount + 1 }, NSContentView: { UID: 3 } },
|
||||||
|
...views,
|
||||||
|
"Title",
|
||||||
|
{ $classname: "NSWindow", $classes: ["NSWindow", "NSObject"] },
|
||||||
|
],
|
||||||
|
$top: { root: { UID: 2 } },
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(parsed.objects).toHaveLength(viewCount + 1);
|
||||||
|
const view = parsed.objects.find(({ id }) => id === "3");
|
||||||
|
expect(view?.name).toBe("NSView");
|
||||||
|
expect(view?.attributes).toEqual({
|
||||||
|
NSWindow: { archiveUID: 2, className: "NSWindow" },
|
||||||
|
NSSuperview: { archiveUID: 2, className: "NSWindow" },
|
||||||
|
NSNextKeyView: { archiveUID: 4, className: "NSView" },
|
||||||
|
NSSubviews: [{ archiveUID: 4, className: "NSView" }],
|
||||||
|
NSTitle: "Title",
|
||||||
|
});
|
||||||
|
expect(parsed.hierarchy).toMatchObject([
|
||||||
|
{
|
||||||
|
objectID: "2",
|
||||||
|
children: [{ objectID: "3", children: [{ objectID: "4" }] }],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves XML CF$UID references and the archived nil", () => {
|
||||||
|
const parsed = parseInterfaceBuilderRecords({
|
||||||
|
$archiver: "NSKeyedArchiver",
|
||||||
|
$objects: [
|
||||||
|
"$null",
|
||||||
|
{
|
||||||
|
$class: { CF$UID: 3 },
|
||||||
|
NSSource: { CF$UID: 2 },
|
||||||
|
source: { CF$UID: 2 },
|
||||||
|
destination: { CF$UID: 0 },
|
||||||
|
label: { CF$UID: 4 },
|
||||||
|
},
|
||||||
|
{ $class: { CF$UID: 5 } },
|
||||||
|
{
|
||||||
|
$classname: "NSNibOutletConnector",
|
||||||
|
$classes: ["NSNibOutletConnector", "NSObject"],
|
||||||
|
},
|
||||||
|
{ $class: { CF$UID: 6 }, "NS.string": "delegate" },
|
||||||
|
{ $classname: "NSButton", $classes: ["NSButton", "NSObject"] },
|
||||||
|
{
|
||||||
|
$classname: "NSMutableString",
|
||||||
|
$classes: ["NSMutableString", "NSString", "NSObject"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
$top: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(parsed.connections).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
kind: "outlet",
|
||||||
|
source_id: "2",
|
||||||
|
destination_id: null,
|
||||||
|
label: "delegate",
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
expect(parsed.objects.find(({ id }) => id === "2")).toMatchObject({
|
||||||
|
class_name: "NSButton",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("AppKit keyed archive connectors", () => {
|
describe("AppKit keyed archive connectors", () => {
|
||||||
it("reads outlet and control connectors from NS-prefixed keys", () => {
|
it("reads outlet and control connectors from NS-prefixed keys", () => {
|
||||||
const parsed = parseInterfaceBuilderRecords({
|
const parsed = parseInterfaceBuilderRecords({
|
||||||
|
|||||||
@@ -117,62 +117,130 @@ export const parseInterfaceBuilderRecords = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A UID decodes as a one-key `UID` (binary) or `CF$UID` (XML) dictionary with
|
||||||
|
* an integer value. Any other dictionary is ordinary archive data.
|
||||||
|
*/
|
||||||
|
const archiveUid = (value: unknown): number | undefined => {
|
||||||
|
if (typeof value !== "object" || value === null || Array.isArray(value))
|
||||||
|
return undefined;
|
||||||
|
if (Object.keys(value).length !== 1) return undefined;
|
||||||
|
const item = record(value);
|
||||||
|
const marker = Object.hasOwn(item, "UID") ? item.UID : item["CF$UID"];
|
||||||
|
return typeof marker === "number" && Number.isInteger(marker)
|
||||||
|
? marker
|
||||||
|
: undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const HIERARCHY_KEY =
|
||||||
|
/(?:subviews|contentview|childviewcontrollers|children|views)$/iu;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read an NSKeyedArchiver object table without expanding it into a tree.
|
||||||
|
* Archived objects reference each other freely, including cycles such as a
|
||||||
|
* view and its superview, so inlining every reference grows exponentially.
|
||||||
|
* A reference instead resolves to the referenced string, or to a stub that
|
||||||
|
* names the referenced object; the hierarchy walk visits each object once.
|
||||||
|
*/
|
||||||
|
const keyedArchiveTable = (objectTable: readonly unknown[]) => {
|
||||||
|
const classNameOf = (value: unknown): string | null => {
|
||||||
|
const classReference = record(value).$class;
|
||||||
|
const uid = archiveUid(classReference);
|
||||||
|
return firstString(
|
||||||
|
record(uid === undefined ? classReference : objectTable[uid]).$classname,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
/** The string an archived string object holds, if it is one. */
|
||||||
|
const archivedString = (target: unknown): string | undefined => {
|
||||||
|
if (typeof target === "string") return target;
|
||||||
|
const text = record(target)["NS.string"];
|
||||||
|
return typeof text === "string" &&
|
||||||
|
/^NS(?:Mutable)?String$/u.test(classNameOf(target) ?? "")
|
||||||
|
? text
|
||||||
|
: undefined;
|
||||||
|
};
|
||||||
|
const text = (value: unknown): string | null => {
|
||||||
|
const uid = archiveUid(value);
|
||||||
|
return firstString(
|
||||||
|
uid === undefined ? value : archivedString(objectTable[uid]),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
const authoredId = (item: Record<string, unknown>): string | null =>
|
||||||
|
firstString(text(item.objectID), text(item["object-id"]));
|
||||||
|
const reference = (uid: number): unknown => {
|
||||||
|
// UID 0 is the archived nil.
|
||||||
|
const target = uid === 0 ? null : objectTable[uid];
|
||||||
|
if (target === undefined || target === null) return null;
|
||||||
|
if (typeof target !== "object") return target;
|
||||||
|
const string = archivedString(target);
|
||||||
|
if (string !== undefined) return string;
|
||||||
|
const objectId = authoredId(record(target));
|
||||||
|
return {
|
||||||
|
archiveUID: uid,
|
||||||
|
...(objectId === null ? {} : { objectID: objectId }),
|
||||||
|
className: classNameOf(target),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
/** Resolve one object's own fields, leaving references to other objects as stubs. */
|
||||||
|
const resolve = (value: unknown): unknown => {
|
||||||
|
const uid = archiveUid(value);
|
||||||
|
if (uid !== undefined) return reference(uid);
|
||||||
|
if (Array.isArray(value)) return value.map(resolve);
|
||||||
|
if (typeof value !== "object" || value === null) return value;
|
||||||
|
const output: Record<string, unknown> = {};
|
||||||
|
for (const [key, child] of Object.entries(value)) {
|
||||||
|
if (key === "$class") output.className = classNameOf(value);
|
||||||
|
else output[key] = resolve(child);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
const hierarchy = (start: unknown): unknown[] => {
|
||||||
|
const visited = new Set<number>();
|
||||||
|
const node = (
|
||||||
|
item: Record<string, unknown>,
|
||||||
|
uid: number | undefined,
|
||||||
|
depth: number,
|
||||||
|
): unknown[] => {
|
||||||
|
const objectId = firstString(
|
||||||
|
authoredId(item),
|
||||||
|
text(item.id),
|
||||||
|
uid === undefined ? null : String(uid),
|
||||||
|
);
|
||||||
|
const children = Object.entries(item)
|
||||||
|
.filter(([key]) => HIERARCHY_KEY.test(key))
|
||||||
|
.flatMap(([, child]) => visit(child, depth + 1));
|
||||||
|
return objectId === null ? children : [{ objectID: objectId, children }];
|
||||||
|
};
|
||||||
|
const visit = (value: unknown, depth: number): unknown[] => {
|
||||||
|
if (depth > 32) return [];
|
||||||
|
const uid = archiveUid(value);
|
||||||
|
if (uid !== undefined) {
|
||||||
|
if (visited.has(uid)) return [];
|
||||||
|
visited.add(uid);
|
||||||
|
const target = objectTable[uid];
|
||||||
|
if (Array.isArray(target)) return visit(target, depth + 1);
|
||||||
|
return typeof target === "object" && target !== null
|
||||||
|
? node(record(target), uid, depth)
|
||||||
|
: [];
|
||||||
|
}
|
||||||
|
if (Array.isArray(value))
|
||||||
|
return value.flatMap((item) => visit(item, depth + 1));
|
||||||
|
if (typeof value !== "object" || value === null) return [];
|
||||||
|
return node(record(value), undefined, depth);
|
||||||
|
};
|
||||||
|
return visit(start, 0);
|
||||||
|
};
|
||||||
|
return { resolve, hierarchy };
|
||||||
|
};
|
||||||
|
|
||||||
/** Project class and connection objects from an NSKeyedArchiver object table. */
|
/** Project class and connection objects from an NSKeyedArchiver object table. */
|
||||||
const parseKeyedArchive = (
|
const parseKeyedArchive = (
|
||||||
root: Record<string, unknown>,
|
root: Record<string, unknown>,
|
||||||
): ReturnType<typeof parseInterfaceBuilderRecords> | null => {
|
): ReturnType<typeof parseInterfaceBuilderRecords> | null => {
|
||||||
if (root.$archiver !== "NSKeyedArchiver" || !Array.isArray(root.$objects))
|
if (root.$archiver !== "NSKeyedArchiver" || !Array.isArray(root.$objects))
|
||||||
return null;
|
return null;
|
||||||
const objectTable = root.$objects;
|
const objectTable: readonly unknown[] = root.$objects;
|
||||||
const resolve = (value: unknown, depth = 0): unknown => {
|
const { resolve, hierarchy: toHierarchy } = keyedArchiveTable(objectTable);
|
||||||
if (depth > 16) return null;
|
|
||||||
const uid = record(value).UID;
|
|
||||||
if (typeof uid === "number") {
|
|
||||||
const target = objectTable[uid];
|
|
||||||
const resolved = target === undefined ? null : resolve(target, depth + 1);
|
|
||||||
return typeof resolved === "object" &&
|
|
||||||
resolved !== null &&
|
|
||||||
!Array.isArray(resolved)
|
|
||||||
? { ...resolved, archiveUID: uid }
|
|
||||||
: resolved;
|
|
||||||
}
|
|
||||||
if (Array.isArray(value))
|
|
||||||
return value.map((item) => resolve(item, depth + 1));
|
|
||||||
if (typeof value !== "object" || value === null) return value;
|
|
||||||
const output: Record<string, unknown> = {};
|
|
||||||
for (const [key, child] of Object.entries(value)) {
|
|
||||||
if (key === "$class") {
|
|
||||||
const classObject = record(
|
|
||||||
typeof record(child).UID === "number"
|
|
||||||
? objectTable[record(child).UID as number]
|
|
||||||
: child,
|
|
||||||
);
|
|
||||||
output.className = firstString(classObject.$classname);
|
|
||||||
} else output[key] = resolve(child, depth + 1);
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
};
|
|
||||||
const toHierarchy = (value: unknown, depth = 0): unknown[] => {
|
|
||||||
if (depth > 32) return [];
|
|
||||||
if (Array.isArray(value))
|
|
||||||
return value.flatMap((item) => toHierarchy(item, depth + 1));
|
|
||||||
if (typeof value !== "object" || value === null) return [];
|
|
||||||
const item = record(value);
|
|
||||||
const objectId = firstString(
|
|
||||||
item.objectID,
|
|
||||||
item["object-id"],
|
|
||||||
item.id,
|
|
||||||
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
|
|
||||||
);
|
|
||||||
const children = Object.entries(item)
|
|
||||||
.filter(([key]) =>
|
|
||||||
/(?:subviews|contentview|childviewcontrollers|children|views)$/iu.test(
|
|
||||||
key,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.flatMap(([, child]) => toHierarchy(child, depth + 1));
|
|
||||||
return objectId === null ? children : [{ objectID: objectId, children }];
|
|
||||||
};
|
|
||||||
const objects: InterfaceBuilderObject[] = [];
|
const objects: InterfaceBuilderObject[] = [];
|
||||||
const connections: InterfaceBuilderConnection[] = [];
|
const connections: InterfaceBuilderConnection[] = [];
|
||||||
let objectCount = 0;
|
let objectCount = 0;
|
||||||
@@ -252,8 +320,7 @@ const parseKeyedArchive = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const top = record(root.$top);
|
const top = record(root.$top);
|
||||||
const topObject = resolve(top.root ?? top.UITopLevelObjectsKey);
|
const hierarchy = toHierarchy(top.root ?? top.UITopLevelObjectsKey);
|
||||||
const hierarchy = toHierarchy(topObject);
|
|
||||||
const classes = Object.fromEntries(
|
const classes = Object.fromEntries(
|
||||||
objectTable.flatMap((raw) => {
|
objectTable.flatMap((raw) => {
|
||||||
const item = record(raw);
|
const item = record(raw);
|
||||||
|
|||||||
@@ -164,6 +164,50 @@ describe("compiled Interface Builder bundle reader", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("flat Interface Builder plist values", () => {
|
||||||
|
it("decodes flat nib archives that hold data and date values", async () => {
|
||||||
|
const root = await createTestTempDirectory("rea-ib-test-");
|
||||||
|
const bundle = join(root, "Example.app");
|
||||||
|
const resources = join(bundle, "Contents", "Resources", "English.lproj");
|
||||||
|
await mkdir(resources, { recursive: true });
|
||||||
|
await writeFile(
|
||||||
|
join(resources, "Binary.nib"),
|
||||||
|
buildBinary({
|
||||||
|
$archiver: "NSKeyedArchiver",
|
||||||
|
$version: 100000,
|
||||||
|
$objects: [
|
||||||
|
"$null",
|
||||||
|
{ $class: { UID: 2 }, title: "Build", NSColor: { UID: 3 } },
|
||||||
|
{ $classname: "NSButton", $classes: ["NSButton", "NSObject"] },
|
||||||
|
{ $class: { UID: 4 }, NSWhite: Buffer.from("0.5\0", "ascii") },
|
||||||
|
{ $classname: "NSColor", $classes: ["NSColor", "NSObject"] },
|
||||||
|
],
|
||||||
|
$top: { root: { UID: 1 } },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await writeFile(
|
||||||
|
join(resources, "Xml.nib"),
|
||||||
|
'<?xml version="1.0"?><plist version="1.0"><dict><key>$archiver</key><string>NSKeyedArchiver</string><key>$objects</key><array><string>$null</string><dict><key>NSWhite</key><data>MC41AA==</data><key>NSDate</key><date>2026-10-07T00:00:00Z</date></dict></array><key>$top</key><dict/></dict></plist>',
|
||||||
|
);
|
||||||
|
|
||||||
|
const analysis = await analyzeInterfaceBuilderBundle({
|
||||||
|
bundlePath: bundle,
|
||||||
|
targetSha256: "d".repeat(64),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(
|
||||||
|
analysis.documents.map(({ relative_path }) => relative_path).sort(),
|
||||||
|
).toEqual([
|
||||||
|
"Contents/Resources/English.lproj/Binary.nib",
|
||||||
|
"Contents/Resources/English.lproj/Xml.nib",
|
||||||
|
]);
|
||||||
|
expect(analysis.graph.coverage).toContainEqual(
|
||||||
|
expect.objectContaining({ facet: "archive_decode", status: "complete" }),
|
||||||
|
);
|
||||||
|
expect(analysis.graph.nodes.map(({ name }) => name)).toContain("Build");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("bounded Interface Builder archive decoding", () => {
|
describe("bounded Interface Builder archive decoding", () => {
|
||||||
it("counts malformed archives against the document limit", async () => {
|
it("counts malformed archives against the document limit", async () => {
|
||||||
const root = await createTestTempDirectory("rea-ib-test-");
|
const root = await createTestTempDirectory("rea-ib-test-");
|
||||||
|
|||||||
Reference in New Issue
Block a user