From 4b3166144193ffa98370f6837a40489db1910096 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 15 Nov 2026 15:33:08 +0800 Subject: [PATCH] fix(plist): surface repeated dictionary keys in every plist reader (#10781) * fix(plist): surface repeated dictionary keys in keyed archive, Interface Builder, entitlement and Info.plist readers * test(plist): cover repeated keys in Interface Builder archives and entitlements (#10797) --- docs/guides/native/native-investigation.md | 6 ++- src/application/AppBundleExecutable.test.ts | 40 ++++++++++++++++++ src/application/AppBundleExecutable.ts | 29 ++++++++++--- src/application/MacHopper.ts | 2 +- .../apple/BinaryPlistMaterializer.ts | 15 +++++++ .../apple/InterfaceBuilderAnalysis.ts | 28 ++++++++++--- src/artifacts/apple/KeyedArchiveReader.ts | 13 ++---- src/native/NativeMacOSProvider.ts | 2 + src/native/parsers/plist.ts | 20 +++------ .../filesystem/appleXmlDeclarations.test.ts | 21 ++++++++++ .../filesystem/keyedArchiveXmlSyntax.test.ts | 21 ++++++++++ .../providers/native/nativeProvider.test.ts | 42 ++++++++++++++++--- 12 files changed, 197 insertions(+), 42 deletions(-) diff --git a/docs/guides/native/native-investigation.md b/docs/guides/native/native-investigation.md index 9738c9cff3..05fa2cf2b8 100644 --- a/docs/guides/native/native-investigation.md +++ b/docs/guides/native/native-investigation.md @@ -1579,7 +1579,11 @@ A dictionary that declares a key more than once keeps one value in the JSON result, the last one. When `inspect_plist` decodes binary, XML or OpenStep source directly, it reads each dictionary's keys before they collapse and adds a limitation with the count of affected dictionaries and replaced values and the -first repeated key names. Other plist readers may keep a different value. JSON +first repeated key names. `inspect_keyed_archive`, Interface Builder analysis +(per archive path) and the `codesign` entitlements in `inspect_signature` add the +same limitation. Other plist readers may keep a different value. Opening an +app bundle refuses an `Info.plist` that may declare `CFBundleExecutable` more +than once, because which declaration names the program is ambiguous. JSON sources and plists decoded through a `plutil` conversion are not checked: those parsers keep one value per key before REA sees the result. diff --git a/src/application/AppBundleExecutable.test.ts b/src/application/AppBundleExecutable.test.ts index eaaff23396..b0be5a00d4 100644 --- a/src/application/AppBundleExecutable.test.ts +++ b/src/application/AppBundleExecutable.test.ts @@ -141,3 +141,43 @@ describe("app bundle program directory containment", () => { } }); }); + +describe("app bundle Info.plist repeated keys", () => { + const bundleWith = async (dict: string) => { + const root = await createTestTempDirectory("rea-app-plist-repeat-"); + const bundle = join(root, "Fixture.app"); + await mkdir(join(bundle, "Contents", "MacOS"), { recursive: true }); + await writeFile( + join(bundle, "Contents", "Info.plist"), + `${dict}`, + ); + for (const name of ["First", "Last"]) + await writeFile( + join(bundle, "Contents", "MacOS", name), + "inert fixture; never executed", + ); + return bundle; + }; + + it("refuses an Info.plist that declares CFBundleExecutable twice", async () => { + const bundle = await bundleWith( + "CFBundleExecutableFirstCFBundleExecutableLast", + ); + const result = await resolveAppleBundleExecutable(bundle); + expect(result).toMatchObject({ ok: false }); + expect(result.ok ? "" : result.error.message).toContain( + "Info.plist may declare CFBundleExecutable more than once", + ); + }); + + it("still opens a bundle whose unrelated key repeats", async () => { + const bundle = await bundleWith( + "CFBundleExecutableFirstCFBundleNameACFBundleNameB", + ); + const result = await resolveAppleBundleExecutable(bundle); + expect(result).toMatchObject({ + ok: true, + value: { executable: join(bundle, "Contents", "MacOS", "First") }, + }); + }); +}); diff --git a/src/application/AppBundleExecutable.ts b/src/application/AppBundleExecutable.ts index 9c737fd822..0d966c571e 100644 --- a/src/application/AppBundleExecutable.ts +++ b/src/application/AppBundleExecutable.ts @@ -4,7 +4,7 @@ import { extname, join, sep } from "node:path"; import { z } from "zod"; import { preflightPropertyListDecode } from "../artifacts/apple/PropertyListDecodeBudget.js"; -import { decodeBinaryPlistValue } from "../artifacts/apple/BinaryPlistMaterializer.js"; +import { decodeBinaryPropertyList } from "../artifacts/apple/BinaryPlistMaterializer.js"; import { BinaryTargetError } from "../domain/configurationErrors.js"; import { isPathWithinRoot } from "../domain/localPath.js"; import { parseXmlPropertyList } from "../domain/propertyListKeys.js"; @@ -451,7 +451,7 @@ const resolveLayoutExecutable = async ( let name: string; try { - name = executableName(decodeInfoPlistValue(plist)); + name = executableName(decodeInfoPlistValue(plist, ["CFBundleExecutable"])); } catch (cause: unknown) { return err( new BinaryTargetError( @@ -533,14 +533,31 @@ const resolveLayoutExecutable = async ( * Decode one Info.plist's top-level value in-process, binary or XML, so no * host plist tool is required. Values are read with format-aware parsers, not * patterns; an unrelated `__proto__` entry must not make the bundle unreadable. + * + * `keys` are the entries the caller reads. A plist that may declare one of + * them more than once is refused: the decoder keeps the last declaration and + * the system's bundle readers are not shown to agree. A repeat is attributed + * to a key by name only, so a repeat inside a nested dictionary, or one whose + * name was not retained, also refuses. */ -export const decodeInfoPlistValue = (plist: Buffer): unknown => - hasBinaryPlistSignature(plist) - ? decodeBinaryPlistValue( +export const decodeInfoPlistValue = ( + plist: Buffer, + keys: readonly string[], +): unknown => { + const decoded = hasBinaryPlistSignature(plist) + ? decodeBinaryPropertyList( plist, preflightPropertyListDecode(plist, BINARY_PLIST_DECODE_BYTES), ) - : parseXmlPropertyList(decodeXmlPlistText(plist)).value; + : parseXmlPropertyList(decodeXmlPlistText(plist)); + const { names, namesOmitted } = decoded.duplicateKeys; + const repeated = keys.filter((key) => namesOmitted || names.includes(key)); + if (repeated.length > 0) + throw new TypeError( + `Info.plist may declare ${repeated.join(", ")} more than once; which declaration applies is ambiguous`, + ); + return decoded.value; +}; /** The Info.plist decoded but declares no string CFBundleExecutable. */ class UndeclaredBundleExecutable extends Error {} diff --git a/src/application/MacHopper.ts b/src/application/MacHopper.ts index 7a09970289..17471e504d 100644 --- a/src/application/MacHopper.ts +++ b/src/application/MacHopper.ts @@ -523,7 +523,7 @@ export const systemMacHopperInstallHost = ( async minimumSystemVersion(appPath) { const plist = posix.join(appPath, "Contents", "Info.plist"); const declared = minimumSystemVersionSchema.safeParse( - decodeInfoPlistValue(await readFile(plist)), + decodeInfoPlistValue(await readFile(plist), ["LSMinimumSystemVersion"]), ); if (!declared.success) throw new TypeError( diff --git a/src/artifacts/apple/BinaryPlistMaterializer.ts b/src/artifacts/apple/BinaryPlistMaterializer.ts index 3e003eeec0..39e0e07014 100644 --- a/src/artifacts/apple/BinaryPlistMaterializer.ts +++ b/src/artifacts/apple/BinaryPlistMaterializer.ts @@ -6,6 +6,7 @@ import { type PlistDuplicateKeys, } from "../../domain/apple/plistDuplicateKeys.js"; import { byteOrderMarkEncoding } from "../../domain/textByteOrderMark.js"; +import type { ParsedPropertyList } from "../../domain/propertyListKeys.js"; import { binaryPlistObjectEnd, decodeBinaryPlistScalars, @@ -92,6 +93,20 @@ export const binaryPlistDuplicateKeys = ( return duplicates.result(); }; +/** + * A binary plist decoded with the source facts an XML or OpenStep decode + * carries: number literals and the keys a dictionary repeats. + */ +export const decodeBinaryPropertyList = ( + bytes: Buffer, + plan: PlistDecodePreflight, + metadata: BinaryPlistScalarMetadata = scanBinaryPlistScalars(bytes, plan), +): ParsedPropertyList => ({ + value: decodeBinaryPlistValue(bytes, plan, metadata), + numberLiterals: metadata.literals, + duplicateKeys: binaryPlistDuplicateKeys(plan), +}); + /** Decode validated source containers without losing dictionary keys or alias occurrence identity. */ export const decodeBinaryPlistValue = ( bytes: Buffer, diff --git a/src/artifacts/apple/InterfaceBuilderAnalysis.ts b/src/artifacts/apple/InterfaceBuilderAnalysis.ts index bfcc13dcb2..556f7607d6 100644 --- a/src/artifacts/apple/InterfaceBuilderAnalysis.ts +++ b/src/artifacts/apple/InterfaceBuilderAnalysis.ts @@ -1,7 +1,11 @@ import { createHash } from "node:crypto"; import { TextDecoder } from "node:util"; -import { decodeBinaryPlistValue } from "./BinaryPlistMaterializer.js"; +import { decodeBinaryPropertyList } from "./BinaryPlistMaterializer.js"; +import { + plistDuplicateKeyLimitations, + type PlistDuplicateKeys, +} from "../../domain/apple/plistDuplicateKeys.js"; import { appleResourceKeyFields } from "../../domain/apple/appleAssetCatalog.js"; import { @@ -116,6 +120,7 @@ const decodeInterfaceBuilderBundle = async ( const unknownDates = new Map(); const datePrecisionOverlaps = new Map(); const unknownScalars = new Map(); + const duplicateKeys = new Map(); const nonFiniteReals = new Map(); const negativeZeroReals = new Map(); let omitted = 0; @@ -228,6 +233,7 @@ const decodeInterfaceBuilderBundle = async ( unknownDateCount, unknownScalarCount, numberLiterals, + duplicateKeys: repeatedKeys, } = nib === null ? decodePlist(bytes, xmlText, scalarPlan) : { @@ -235,6 +241,7 @@ const decodeInterfaceBuilderBundle = async ( unknownDateCount: 0, unknownScalarCount: 0, numberLiterals: undefined, + duplicateKeys: undefined, }; requireInterfaceBuilderArchive(raw); if (nib !== null && nib.unexaminedSources.length > 0) @@ -253,6 +260,8 @@ const decodeInterfaceBuilderBundle = async ( datePrecisionOverlaps.set(entry.path, datePrecisionOverlapCount); if (unknownScalarCount !== 0) unknownScalars.set(entry.path, unknownScalarCount); + if (repeatedKeys !== undefined && repeatedKeys.replaced > 0) + duplicateKeys.set(entry.path, repeatedKeys); const documentHash = createHash("sha256").update(bytes).digest("hex"); documents.push({ relativePath: entry.path, @@ -306,6 +315,7 @@ const decodeInterfaceBuilderBundle = async ( unknownDates, datePrecisionOverlaps, unknownScalars, + duplicateKeys, nonFiniteReals, negativeZeroReals, invalid, @@ -441,6 +451,7 @@ const finalizeHierarchyCoverage = ( unknownDates: ReadonlyMap; datePrecisionOverlaps: ReadonlyMap; unknownScalars: ReadonlyMap; + duplicateKeys: ReadonlyMap; nonFiniteReals: ReadonlyMap; negativeZeroReals: ReadonlyMap; invalid: readonly string[]; @@ -460,6 +471,7 @@ const finalizeHierarchyCoverage = ( unknownDates, datePrecisionOverlaps, unknownScalars, + duplicateKeys, nonFiniteReals, negativeZeroReals, invalid, @@ -604,6 +616,11 @@ const finalizeHierarchyCoverage = ( ...[...unknownScalars].map( ([path, count]) => `${path}: ${unknownScalarLimitation(count)}`, ), + ...[...duplicateKeys].flatMap(([path, repeated]) => + plistDuplicateKeyLimitations(repeated).map( + (limitation) => `${path}: ${limitation}`, + ), + ), ...[...nonFiniteReals].map( ([path, count]) => `${path}: ${nonFiniteRealLimitation(count)}`, ), @@ -1068,16 +1085,14 @@ const decodePlist = ( readonly unknownDateCount: number; readonly unknownScalarCount: number; readonly numberLiterals: PlistNumberLiterals; + readonly duplicateKeys: PlistDuplicateKeys; } => { const binary = hasBinaryPlistSignature(bytes); const binaryScalarMetadata = binary ? scanBinaryPlistScalars(bytes, scalarPlan) : undefined; - const { value, numberLiterals } = binary - ? { - value: decodeBinaryPlistValue(bytes, scalarPlan, binaryScalarMetadata), - numberLiterals: binaryScalarMetadata!.literals, - } + const { value, numberLiterals, duplicateKeys } = binary + ? decodeBinaryPropertyList(bytes, scalarPlan, binaryScalarMetadata) : parseXmlWithinDecodeBudget(xmlText ?? decodeXmlPlistText(bytes)); const projected = projectPlistValue(value); return { @@ -1085,5 +1100,6 @@ const decodePlist = ( unknownDateCount: projected.unknownDateCount, unknownScalarCount: projected.unknownScalarCount, numberLiterals, + duplicateKeys, }; }; diff --git a/src/artifacts/apple/KeyedArchiveReader.ts b/src/artifacts/apple/KeyedArchiveReader.ts index 3a3859eb4d..2cd1eedb56 100644 --- a/src/artifacts/apple/KeyedArchiveReader.ts +++ b/src/artifacts/apple/KeyedArchiveReader.ts @@ -6,7 +6,8 @@ import { projectStableArtifactFailure } from "../StableArtifactFailure.js"; import { ARTIFACT_GRAPH_PROVIDER } from "../../domain/investigationProviders.js"; import { z } from "zod"; -import { decodeBinaryPlistValue } from "./BinaryPlistMaterializer.js"; +import { decodeBinaryPropertyList } from "./BinaryPlistMaterializer.js"; +import { plistDuplicateKeyLimitations } from "../../domain/apple/plistDuplicateKeys.js"; import { AnalysisAccessDeniedError, AnalysisArtifactChangedError, @@ -110,14 +111,7 @@ export const decodeKeyedArchiveBytes = ( : undefined; const parsed = binaryExpansion !== undefined - ? { - value: decodeBinaryPlistValue( - bytes, - binaryExpansion, - binaryScalarMetadata, - ), - numberLiterals: binaryScalarMetadata!.literals, - } + ? decodeBinaryPropertyList(bytes, binaryExpansion, binaryScalarMetadata) : parseXmlPropertyList(xmlText ?? ""); const { nonFiniteRealCount, @@ -147,6 +141,7 @@ export const decodeKeyedArchiveBytes = ( limitations: [ ...graph.limitations, ...numbers.limitations(), + ...plistDuplicateKeyLimitations(parsed.duplicateKeys), ...(nonFiniteRealCount === 0 ? [] : [nonFiniteRealLimitation(nonFiniteRealCount)]), diff --git a/src/native/NativeMacOSProvider.ts b/src/native/NativeMacOSProvider.ts index 62bf93b8c6..e7b0231996 100644 --- a/src/native/NativeMacOSProvider.ts +++ b/src/native/NativeMacOSProvider.ts @@ -99,6 +99,7 @@ import { import { createPlistNumberProjection } from "../domain/apple/plistNumbers.js"; import type { NativeUiObservationResult } from "../domain/native/nativeUiObservation.js"; import { parseXmlPropertyList } from "../domain/propertyListKeys.js"; +import { plistDuplicateKeyLimitations } from "../domain/apple/plistDuplicateKeys.js"; import { machoKnownCodeSignSelector } from "./MachoArchitectureSelection.js"; import { demangleSwiftSchema, @@ -2183,6 +2184,7 @@ const parseEntitlements = ( value, limitations: [ ...numbers.limitations(), + ...plistDuplicateKeyLimitations(decoded.duplicateKeys), ...(projected.nonFiniteRealCount === 0 ? [] : [nonFiniteRealLimitation(projected.nonFiniteRealCount)]), diff --git a/src/native/parsers/plist.ts b/src/native/parsers/plist.ts index ef12b5af36..7ddeb0b1cf 100644 --- a/src/native/parsers/plist.ts +++ b/src/native/parsers/plist.ts @@ -1,7 +1,4 @@ -import { - binaryPlistDuplicateKeys, - decodeBinaryPlistValue, -} from "../../artifacts/apple/BinaryPlistMaterializer.js"; +import { decodeBinaryPropertyList } from "../../artifacts/apple/BinaryPlistMaterializer.js"; import { plistDuplicateKeyLimitations, type PlistDuplicateKeys, @@ -34,10 +31,7 @@ import { resolvePlistNumberSource, type PlistNumberLiterals, } from "../../domain/apple/plistNumbers.js"; -import { - scanBinaryPlistScalars, - UnsupportedBinaryPlistUidError, -} from "../../artifacts/apple/PlistScalars.js"; +import { UnsupportedBinaryPlistUidError } from "../../artifacts/apple/PlistScalars.js"; import { estimateBinaryPlistExpansion, preflightPropertyListDecode, @@ -389,15 +383,13 @@ export const parsePlistBinary = ( : new PropertyListDecodeBudgetFailure(kind, message), }, ); - const scalarMetadata = scanBinaryPlistScalars(bytes, expansion); - const decoded = decodeBinaryPlistValue(bytes, expansion, scalarMetadata); - const duplicateKeys = binaryPlistDuplicateKeys(expansion); + const decoded = decodeBinaryPropertyList(bytes, expansion); const projected = projectPlistValue( - decoded, + decoded.value, undefined, MAX_NATIVE_PLIST_NESTING_DEPTH, ); - const numbers = createPlistNumberProjection(scalarMetadata.literals); + const numbers = createPlistNumberProjection(decoded.numberLiterals); const value = numbers.project(projected.value); return ok({ value, @@ -412,7 +404,7 @@ export const parsePlistBinary = ( ), ]), ...numbers.limitations(), - ...plistDuplicateKeyLimitations(duplicateKeys), + ...plistDuplicateKeyLimitations(decoded.duplicateKeys), ], }); } catch (cause: unknown) { diff --git a/tests/boundary/filesystem/appleXmlDeclarations.test.ts b/tests/boundary/filesystem/appleXmlDeclarations.test.ts index 1f916a017c..b829dcbb9e 100644 --- a/tests/boundary/filesystem/appleXmlDeclarations.test.ts +++ b/tests/boundary/filesystem/appleXmlDeclarations.test.ts @@ -104,3 +104,24 @@ it.each(["REA-UNSUPPORTED", "ISO-8859-1"])( expect(result.graph.truncated).toBe(true); }, ); + +it("reports a dictionary key an Interface Builder archive declares twice", async () => { + const bundlePath = await createTestTempDirectory("rea-ib-repeated-key-"); + await writeFile( + join(bundlePath, "Panel.nib"), + archive.replace( + "$objects", + "$archiverNSKeyedArchiver$objects", + ), + ); + const result = await analyzeInterfaceBuilderBundle({ + bundlePath, + targetSha256: "a".repeat(64), + }); + expect(result.documents).toHaveLength(1); + expect(result.limitations).toContainEqual( + expect.stringMatching( + /^Panel\.nib: 1 dictionary\(ies\) declare a key more than once \("\$archiver"\)\. REA kept the last value/u, + ), + ); +}); diff --git a/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts b/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts index 6814efbcfd..90c33c25fe 100644 --- a/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts +++ b/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts @@ -66,3 +66,24 @@ it("preserves valid Unicode, entity values and original archive identities", asy expect(result.next_offset).toBeNull(); expect(result.truncated).toBe(true); }); + +it("reports a dictionary key the archive declares twice", async () => { + const root = await createTestTempDirectory("rea-keyed-xml-repeat-"); + await writeFile( + join(root, "Archive.plist"), + archive.replace( + "$version", + "$archiverNSKeyedArchiver$version", + ), + ); + const result = await inspectBundleKeyedArchive({ + bundlePath: root, + targetSha256: "a".repeat(64), + parameters: { path: "Archive.plist" }, + }); + expect(result.limitations).toContainEqual( + expect.stringContaining( + '1 dictionary(ies) declare a key more than once ("$archiver")', + ), + ); +}); diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.ts index 9bf91593bf..85cb142303 100644 --- a/tests/boundary/providers/native/nativeProvider.test.ts +++ b/tests/boundary/providers/native/nativeProvider.test.ts @@ -1593,13 +1593,16 @@ it("rejects malformed selected plist bytes before mocked conversion can invent f expect(await readFile(path, "utf8")).toBe("fixture"); }); -/** Emit entitlements whose dictionary also holds a legal `__proto__` key. */ -class PrototypeEntitlementsRunner extends FixtureRunner { +/** Emit the given entitlements plist for `codesign --entitlements`. */ +class EntitlementsRunner extends FixtureRunner { + constructor(readonly entitlements: string) { + super(); + } + override async run(tool: string, arguments_: readonly string[]) { const result = await super.run(tool, arguments_); if (!result.ok || !arguments_.includes("--entitlements")) return result; - const stdout = - 'com.apple.security.app-sandbox__proto__com.apple.security.get-task-allow'; + const stdout = this.entitlements; return ok({ ...result.value, stdout, @@ -1615,7 +1618,9 @@ describe("native signature entitlements", () => { await writeFile(executable, "fixture"); const client = new NativeMacOSProvider( {}, - new PrototypeEntitlementsRunner(), + new EntitlementsRunner( + 'com.apple.security.app-sandbox__proto__com.apple.security.get-task-allow', + ), "darwin", ).createClient(await nativeMachoTargetForFile(executable)); @@ -1638,6 +1643,33 @@ describe("native signature entitlements", () => { }); }); +describe("native signature repeated entitlement keys", () => { + it("keeps the last value and reports the repeated key as a limitation", async () => { + directory = await createTestTempDirectory("rea-signature-repeated-"); + const executable = join(directory, "fixture"); + await writeFile(executable, "fixture"); + const client = new NativeMacOSProvider( + {}, + new EntitlementsRunner( + 'com.apple.security.get-task-allowcom.apple.security.get-task-allow', + ), + "darwin", + ).createClient(await nativeMachoTargetForFile(executable)); + + const signature = await client.execute("inspect_signature", {}); + + if (!signature.ok) throw signature.error; + expect(signature.value.result).toMatchObject({ + entitlements: { "com.apple.security.get-task-allow": false }, + }); + expect(signature.value.limitations).toContainEqual( + expect.stringMatching( + /^Entitlements: 1 dictionary\(ies\) declare a key more than once \("com\.apple\.security\.get-task-allow"\)\. REA kept the last value for each key; 1 earlier value\(s\) are not reported/u, + ), + ); + }); +}); + /** Fail every codesign call nonzero after writing more than one excerpt. */ class VerboseCodeSignFailureRunner extends FixtureRunner { override async run(tool: string, arguments_: readonly string[]) {