diff --git a/src/domain/apple/binaryPlistMaterializer.test.ts b/src/domain/apple/binaryPlistMaterializer.test.ts index 6ac08def4b..17f6682418 100644 --- a/src/domain/apple/binaryPlistMaterializer.test.ts +++ b/src/domain/apple/binaryPlistMaterializer.test.ts @@ -1,5 +1,9 @@ import { expect, it, vi } from "vitest"; -import { binaryPlistObjectTable } from "../../../tests/fixtures/binaryPlistObjectTable.js"; +import { + binaryPlistObjectTable, + readBinaryPlistObjectTableLayout, + writeBinaryPlistObjectOffset, +} from "../../../tests/fixtures/binaryPlistObjectTable.js"; import { createPlistNumberProjection } from "./plistNumbers.js"; import { projectPlistValue } from "./plistValue.js"; import { scanBinaryPlistScalars } from "./plistScalars.js"; @@ -222,10 +226,11 @@ it("reads an unreferenced UTF-16BE string and data object", () => { wide, data, ]); - const { text: decoded, complete } = decodeBinaryPlistText(bytes, budget); - expect(decoded).toContain("é!"); - expect(decoded).toContain("blob"); - expect(complete).toBe(true); + // The final data payload ends exactly at the start of the offset table. + expect(decodeBinaryPlistText(bytes, budget)).toEqual({ + text: "key\nkept\né!\nblob", + complete: true, + }); }); it("honors a byte order mark in a data object", () => { @@ -301,6 +306,34 @@ it("keeps readable text beside an unreferenced string it cannot read", () => { }); }); +it("keeps readable text when an unreferenced string crosses the offset table", () => { + const bytes = source([ + dictionary(1, 2), + text("key"), + text("kept"), + Buffer.from([0x52, 0x61]), // Two declared characters, one in the object area. + ]); + expect(decodeBinaryPlistText(bytes, budget)).toEqual({ + text: "key\nkept", + complete: false, + }); +}); + +it("keeps readable text when an unreferenced entry starts at the offset table", () => { + const bytes = source([ + dictionary(1, 2), + text("key"), + text("kept"), + Buffer.from([0x00]), + ]); + const table = readBinaryPlistObjectTableLayout(bytes); + writeBinaryPlistObjectOffset(bytes, table, 3, table.tableOffset); + expect(decodeBinaryPlistText(bytes, budget)).toEqual({ + text: "key\nkept", + complete: false, + }); +}); + it("bounds the text that overlapping table entries repeat", () => { const entries = 200; const headerBytes = 6; diff --git a/src/domain/apple/binaryPlistMaterializer.ts b/src/domain/apple/binaryPlistMaterializer.ts index 0b36ae7891..f76171fa07 100644 --- a/src/domain/apple/binaryPlistMaterializer.ts +++ b/src/domain/apple/binaryPlistMaterializer.ts @@ -179,6 +179,7 @@ const storedPlistPayload = ( | { readonly type: number; readonly payload: Buffer } | "unreadable" | null => { + if (offset < 8 || offset >= objectEnd) return "unreadable"; const marker = bytes[offset]; if (marker === undefined) return "unreadable"; const type = marker >> 4; @@ -275,11 +276,14 @@ export const decodeBinaryPlistText = ( const texts: string[] = []; let decoded = 0; let complete = true; + // Strict preflight admitted the offset-table position. The table begins + // immediately after the object area and is separate from the trailer. + const objectEnd = Number(layout.value.offsetTable); for (const offset of new Set(layout.value.objectOffsets())) { const stored = storedPlistPayload( bytes, Number(offset), - layout.value.trailerOffset, + objectEnd, layout.value.referenceWidth, ); if (stored === null) continue;