From 11896a6eb1864f4f45e4ab756bdfea93427b4eb9 Mon Sep 17 00:00:00 2001 From: Lewis Wong Date: Sat, 21 Nov 2026 14:20:32 +0800 Subject: [PATCH] fix(apple): decode keyed archives that name another archiver Foundation's NSKeyedUnarchiver decodes a keyed archive whatever its $archiver names. SceneKit writes .scn scenes with SCNKeyedArchiver, so inspect_keyed_archive refused system and third-party scenes such as Audio MIDI Setup's SpeakerRoom.scn as "not an NSKeyedArchiver archive" and pointed the caller at inspect_plist. An archive named NSKeyedArchiver keeps its current handling, including format failures for a damaged table. Another string archiver name is a keyed archive when the plist also has an $objects table and $top roots; without them it stays an ordinary property list with the inspect_plist workflow. Co-Authored-By: Claude Opus 5.5 --- .../apple/KeyedArchiveReader.test.ts | 2 +- src/artifacts/apple/KeyedArchiveReader.ts | 4 +-- src/domain/apple/keyedArchive.ts | 21 +++++++++-- .../filesystem/keyedArchiveSelection.test.ts | 36 +++++++++++++++++++ 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/artifacts/apple/KeyedArchiveReader.test.ts b/src/artifacts/apple/KeyedArchiveReader.test.ts index 64600b68d4..86f2c27c78 100644 --- a/src/artifacts/apple/KeyedArchiveReader.test.ts +++ b/src/artifacts/apple/KeyedArchiveReader.test.ts @@ -303,7 +303,7 @@ describe("inert keyed archive decoding", () => { offset: 0, limit: 1, }), - ).toThrow("NSKeyedArchiver"); + ).toThrow("not a keyed archive"); expect(() => decodeKeyedArchiveBytes(Buffer.from(buildBinary(archive)), { root: "missing", diff --git a/src/artifacts/apple/KeyedArchiveReader.ts b/src/artifacts/apple/KeyedArchiveReader.ts index 39e651e692..ac4a84ab50 100644 --- a/src/artifacts/apple/KeyedArchiveReader.ts +++ b/src/artifacts/apple/KeyedArchiveReader.ts @@ -181,8 +181,8 @@ const keyedArchiveKindRemediation = ( if (kind === "nib-archive") return "Decode compiled NIBArchive files with decode_interface_builder on the app bundle."; return platform === "darwin" - ? "Inspect an ordinary property list with inspect_plist; inspect_keyed_archive decodes NSKeyedArchiver archives only." - : "Select an NSKeyedArchiver archive; inspect_keyed_archive decodes only those. inspect_plist, which reads ordinary property lists, requires a macOS host."; + ? "Inspect an ordinary property list with inspect_plist; inspect_keyed_archive decodes keyed archives only." + : "Select a keyed archive; inspect_keyed_archive decodes only those. inspect_plist, which reads ordinary property lists, requires a macOS host."; }; interface BundleKeyedArchiveInput { diff --git a/src/domain/apple/keyedArchive.ts b/src/domain/apple/keyedArchive.ts index 4bc08d7f00..3cc143fb8c 100644 --- a/src/domain/apple/keyedArchive.ts +++ b/src/domain/apple/keyedArchive.ts @@ -109,13 +109,28 @@ const uid = ( return object === undefined ? undefined : plistUidValue(object); }; +/** + * Foundation decodes a keyed archive whatever its $archiver names; SceneKit + * writes SCNKeyedArchiver. Another name is an archive only with an $objects + * table and $top roots, so an ordinary plist keeps the workflow that reads it. + */ +const isKeyedArchive = ( + archive: Record | undefined, + wrappedScalars: WeakSet, +): archive is Record => + archive !== undefined && + (archive.$archiver === "NSKeyedArchiver" || + (typeof archive.$archiver === "string" && + Array.isArray(archive.$objects) && + record(archive.$top, wrappedScalars) !== undefined)); + const archiverMismatch = ( archive: Record | undefined, ): string => { if (archive === undefined) return "its root is not a dictionary"; if (!Object.hasOwn(archive, "$archiver")) return "it has no $archiver key"; return typeof archive.$archiver === "string" - ? `its $archiver is ${JSON.stringify(archive.$archiver)}` + ? `its $archiver is ${JSON.stringify(archive.$archiver)} without an $objects table and $top roots` : "its $archiver is not a string"; }; @@ -137,9 +152,9 @@ export const projectKeyedArchive = ( wrappedScalars.add(scalar), ); const archive = record(projected.value, wrappedScalars); - if (archive?.$archiver !== "NSKeyedArchiver") + if (!isKeyedArchive(archive, wrappedScalars)) throw new KeyedArchiveKindError( - `Selected property list is not an NSKeyedArchiver archive: ${archiverMismatch(archive)}`, + `Selected property list is not a keyed archive: ${archiverMismatch(archive)}`, "property-list", ); if (!Array.isArray(archive.$objects)) diff --git a/tests/boundary/filesystem/keyedArchiveSelection.test.ts b/tests/boundary/filesystem/keyedArchiveSelection.test.ts index 76a9f0f888..23133a2d2b 100644 --- a/tests/boundary/filesystem/keyedArchiveSelection.test.ts +++ b/tests/boundary/filesystem/keyedArchiveSelection.test.ts @@ -208,6 +208,14 @@ it.each([ 'its $archiver is "NSArchiver"', "inspect_plist", ], + [ + "plist with another archiver and scalar $top", + Buffer.from( + build({ $archiver: "MyArchiver", $objects: ["$null"], $top: "root" }), + ), + 'its $archiver is "MyArchiver" without an $objects table and $top roots', + "inspect_plist", + ], [ "compiled NIBArchive", Buffer.concat([Buffer.from("NIBArchive"), Buffer.alloc(40)]), @@ -265,6 +273,34 @@ it.each([ }, ); +it("decodes an archive another keyed archiver wrote, as Foundation does", async () => { + // SceneKit writes .scn scenes with SCNKeyedArchiver; NSKeyedUnarchiver reads them. + const root = await bundle(); + await writeFile( + join(root, "Contents", "Resources", "Scene.scn"), + Buffer.from( + build({ + $archiver: "SCNKeyedArchiver", + $version: 100000, + $objects: ["$null", "value"], + $top: { root: { CF$UID: 1 }, other: { CF$UID: 1 } }, + }), + ), + ); + const { roots, objects, references, limitations } = await inspect(root, { + path: "Contents/Resources/Model.plist", + }); + await expect( + inspect(root, { path: "Contents/Resources/Scene.scn" }), + ).resolves.toMatchObject({ + archive_path: "Contents/Resources/Scene.scn", + roots, + objects, + references, + limitations, + }); +}); + it("keeps a damaged NSKeyedArchiver archive a format failure", async () => { const root = await bundle(); await writeFile(