fix(plist): bound stored text to the object table (#12312)

* fix(plist): bound stored text to the object table

* test(plist): exercise stored object bounds through the scanner
This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:30:13 +00:00
1 parent 23ad8ce578
commit ed4b372435
2 files changed
+43 -6

No files matched your search

@@ -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;
+5 -1
View File
@@ -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;