From e20e61362bf505dd73582049699077e1c40a2136 Mon Sep 17 00:00:00 2001 From: Dave Nicolson Date: Sat, 21 Nov 2026 22:56:40 +0100 Subject: [PATCH] Validate binary plist structure and bound reads --- bplistParser.ts | 208 +++++++++++++++++++++++++++------------------ test/parse.test.ts | 106 +++++++++++++++++++++++ 2 files changed, 233 insertions(+), 81 deletions(-) diff --git a/bplistParser.ts b/bplistParser.ts index 38c810e..04c79f4 100644 --- a/bplistParser.ts +++ b/bplistParser.ts @@ -98,33 +98,56 @@ export function parseBuffer(buffer: Buffer): [T] { if (debug) { console.log("offsetSize: " + offsetSize); } + if (offsetSize < 1 || offsetSize > 8) { + throw new Error("Invalid binary plist. Offset size must be between 1 and 8."); + } const objectRefSize = trailer.readUInt8(7); if (debug) { console.log("objectRefSize: " + objectRefSize); } - const numObjects = readUInt64BE(trailer, 8); + if (objectRefSize < 1 || objectRefSize > 8) { + throw new Error("Invalid binary plist. Object reference size must be between 1 and 8."); + } + const numObjectsValue = readUInt64BE(trailer, 8); if (debug) { - console.log("numObjects: " + numObjects); + console.log("numObjects: " + numObjectsValue); + } + if (numObjectsValue === 0n) { + throw new Error("Invalid binary plist. Object count must be at least 1."); + } + if (numObjectsValue > BigInt(maxObjectCount)) { + throw new Error("maxObjectCount exceeded"); } - const topObject = readUInt64BE(trailer, 16); + const numObjects = Number(numObjectsValue); + const topObjectValue = readUInt64BE(trailer, 16); if (debug) { - console.log("topObject: " + topObject); + console.log("topObject: " + topObjectValue); + } + if (topObjectValue >= numObjectsValue) { + throw new Error("Invalid binary plist. Top object is outside the object table."); } - const offsetTableOffset = readUInt64BE(trailer, 24); + const topObject = Number(topObjectValue); + const offsetTableOffsetValue = readUInt64BE(trailer, 24); if (debug) { - console.log("offsetTableOffset: " + offsetTableOffset); + console.log("offsetTableOffset: " + offsetTableOffsetValue); } - - if (numObjects > maxObjectCount) { - throw new Error("maxObjectCount exceeded"); + const offsetTableSize = BigInt(numObjects * offsetSize); + const objectDataEndValue = BigInt(buffer.length - 32); + if (offsetTableOffsetValue < 8n || offsetTableOffsetValue + offsetTableSize > objectDataEndValue) { + throw new Error("Invalid binary plist. Offset table is outside the object data area."); } + const offsetTableOffset = Number(offsetTableOffsetValue); // Handle offset table const offsetTable: number[] = []; for (let i = 0; i < numObjects; i++) { const offsetBytes = buffer.slice(offsetTableOffset + i * offsetSize, offsetTableOffset + (i + 1) * offsetSize); - offsetTable[i] = readUInt(offsetBytes, 0); + const objectOffsetValue = readBigUInt(offsetBytes); + if (objectOffsetValue < 8n || objectOffsetValue >= offsetTableOffsetValue) { + throw new Error("Invalid binary plist. Object #" + i + " has an offset outside the object data area."); + } + offsetTable[i] = Number(objectOffsetValue); if (debug) { console.log("Offset for Object #" + i + " is " + offsetTable[i] + " [" + offsetTable[i].toString(16) + "]"); } @@ -142,6 +165,9 @@ export function parseBuffer(buffer: Buffer): [T] { const objectsBeingParsed = new Set(); function parseObject(tableOffset: number): any { + if (!Number.isInteger(tableOffset) || tableOffset < 0 || tableOffset >= offsetTable.length) { + throw new Error("Invalid binary plist. Object reference is outside the object table."); + } if (++expandedObjectCount > maxExpandedObjectCount) { throw new Error("maxExpandedObjectCount exceeded"); } @@ -158,9 +184,20 @@ export function parseBuffer(buffer: Buffer): [T] { function parseObjectAt(tableOffset: number): any { const offset = offsetTable[tableOffset]; + if (offset < 8 || offset >= offsetTableOffset || offset >= buffer.length) { + throw new Error("Invalid binary plist. Object offset is outside the object data area."); + } const type = buffer[offset]; const objType = (type & 0xF0) >> 4; //First 4 bits const objInfo = (type & 0x0F); //Second 4 bits + + function readObjectBytes(start: number, length: number, name: string): Buffer { + if (!Number.isSafeInteger(start) || !Number.isSafeInteger(length) || start < 8 || length < 0 || start + length > offsetTableOffset) { + throw new Error("Invalid binary plist. " + name + " extends outside the object data area."); + } + return buffer.subarray(start, start + length); + } + switch (objType) { case 0x0: return parseSimple(); @@ -204,12 +241,10 @@ export function parseBuffer(buffer: Buffer): [T] { function parseInteger(): number | bigint { const length = Math.pow(2, objInfo); - if (length < maxObjectSize) { - const data = buffer.slice(offset + 1, offset + 1 + length); - return readInteger(data); + if (length > 16) { + throw new Error("Invalid binary plist. Unsupported integer length: " + length); } - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); - + return readInteger(readObjectBytes(offset + 1, length, "integer")); } function parseUID(): UID { @@ -222,125 +257,138 @@ export function parseBuffer(buffer: Buffer): [T] { function parseReal(): number | undefined { const length = Math.pow(2, objInfo); - if (length < maxObjectSize) { - const realBuffer = buffer.slice(offset + 1, offset + 1 + length); - if (length === 4) { - return realBuffer.readFloatBE(0); - } - if (length === 8) { - return realBuffer.readDoubleBE(0); - } - } else { - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); + const realBuffer = readObjectBytes(offset + 1, length, "real"); + if (length === 4) { + return realBuffer.readFloatBE(0); + } + if (length === 8) { + return realBuffer.readDoubleBE(0); } } function parseDate(): Date { if (objInfo !== 0x3) { - console.error("Unknown date type :" + objInfo + ". Parsing anyway..."); + throw new Error("Invalid binary plist. Unsupported date length marker: " + objInfo); } - const dateBuffer = buffer.slice(offset + 1, offset + 9); + const dateBuffer = readObjectBytes(offset + 1, 8, "date"); return new Date(EPOCH + (1000 * dateBuffer.readDoubleBE(0))); } function parseData(): Buffer { - let dataoffset = 1; - let length = objInfo; + let dataOffset = 1; + let lengthValue = BigInt(objInfo); if (objInfo === 0xF) { - const int_type = buffer[offset + 1]; - const intType = (int_type & 0xF0) / 0x10; + const intTypeByte = readObjectBytes(offset + 1, 1, "data length marker")[0]; + const intType = (intTypeByte & 0xF0) >> 4; if (intType !== 0x1) { - console.error("0x4: UNEXPECTED LENGTH-INT TYPE! " + intType); + throw new Error("Invalid binary plist. Unexpected data length marker type: " + intType); } - const intInfo = int_type & 0x0F; + const intInfo = intTypeByte & 0x0F; const intLength = Math.pow(2, intInfo); - dataoffset = 2 + intLength; - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); + if (intLength > 8) { + throw new Error("Invalid binary plist. Unsupported data length width: " + intLength); + } + dataOffset = 2 + intLength; + lengthValue = readBigUInt(readObjectBytes(offset + 2, intLength, "data length")); } - if (length < maxObjectSize) { - return buffer.slice(offset + dataoffset, offset + dataoffset + length); + if (lengthValue > BigInt(maxObjectSize)) { + throw new Error("Too little heap space available! Wanted to read " + lengthValue + " bytes, but only " + maxObjectSize + " are available."); } - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); + return readObjectBytes(offset + dataOffset, Number(lengthValue), "data"); } function parsePlistString(isUtf16?: boolean): string { const utf16Flag = isUtf16 || false; let enc: BufferEncoding = "utf8"; - let length = objInfo; - let stroffset = 1; + let lengthValue = BigInt(objInfo); + let stringOffset = 1; if (objInfo === 0xF) { - const int_type = buffer[offset + 1]; - const intType = (int_type & 0xF0) / 0x10; + const intTypeByte = readObjectBytes(offset + 1, 1, "string length marker")[0]; + const intType = (intTypeByte & 0xF0) >> 4; if (intType !== 0x1) { - console.error("UNEXPECTED LENGTH-INT TYPE! " + intType); + throw new Error("Invalid binary plist. Unexpected string length marker type: " + intType); } - const intInfo = int_type & 0x0F; + const intInfo = intTypeByte & 0x0F; const intLength = Math.pow(2, intInfo); - stroffset = 2 + intLength; - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); - } - // length is String length -> to get byte length multiply by 2, as 1 character takes 2 bytes in UTF-16 - length *= (utf16Flag ? 2 : 1); - if (length < maxObjectSize) { - let plistString = Buffer.from(buffer.slice(offset + stroffset, offset + stroffset + length)); - if (utf16Flag) { - plistString = swapBytes(plistString); - enc = "ucs2"; + if (intLength > 8) { + throw new Error("Invalid binary plist. Unsupported string length width: " + intLength); } - return plistString.toString(enc); + stringOffset = 2 + intLength; + lengthValue = readBigUInt(readObjectBytes(offset + 2, intLength, "string length")); } - throw new Error("Too little heap space available! Wanted to read " + length + " bytes, but only " + maxObjectSize + " are available."); + lengthValue *= utf16Flag ? 2n : 1n; + if (lengthValue > BigInt(maxObjectSize)) { + throw new Error("Too little heap space available! Wanted to read " + lengthValue + " bytes, but only " + maxObjectSize + " are available."); + } + let plistString = Buffer.from(readObjectBytes(offset + stringOffset, Number(lengthValue), "string")); + if (utf16Flag) { + plistString = swapBytes(plistString); + enc = "ucs2"; + } + return plistString.toString(enc); } function parseArray(): any[] { - let length = objInfo; - let arrayoffset = 1; + let lengthValue = BigInt(objInfo); + let arrayOffset = 1; if (objInfo === 0xF) { - const int_type = buffer[offset + 1]; - const intType = (int_type & 0xF0) / 0x10; + const intTypeByte = readObjectBytes(offset + 1, 1, "array length marker")[0]; + const intType = (intTypeByte & 0xF0) >> 4; if (intType !== 0x1) { - console.error("0xa: UNEXPECTED LENGTH-INT TYPE! " + intType); + throw new Error("Invalid binary plist. Unexpected array length marker type: " + intType); } - const intInfo = int_type & 0x0F; + const intInfo = intTypeByte & 0x0F; const intLength = Math.pow(2, intInfo); - arrayoffset = 2 + intLength; - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); + if (intLength > 8) { + throw new Error("Invalid binary plist. Unsupported array length width: " + intLength); + } + arrayOffset = 2 + intLength; + lengthValue = readBigUInt(readObjectBytes(offset + 2, intLength, "array length")); } - if (length * objectRefSize > maxObjectSize) { + const refsSize = lengthValue * BigInt(objectRefSize); + if (refsSize > BigInt(maxObjectSize)) { throw new Error("Too little heap space available!"); } + const length = Number(lengthValue); + const refs = readObjectBytes(offset + arrayOffset, Number(refsSize), "array references"); const array: any[] = []; for (let i = 0; i < length; i++) { - const objRef = readUInt(buffer.slice(offset + arrayoffset + i * objectRefSize, offset + arrayoffset + (i + 1) * objectRefSize)); + const objRef = readUInt(refs.subarray(i * objectRefSize, (i + 1) * objectRefSize)); array[i] = parseObject(objRef); } return array; } function parseDictionary(): Record { - let length = objInfo; - let dictoffset = 1; + let lengthValue = BigInt(objInfo); + let dictOffset = 1; if (objInfo === 0xF) { - const int_type = buffer[offset + 1]; - const intType = (int_type & 0xF0) / 0x10; + const intTypeByte = readObjectBytes(offset + 1, 1, "dictionary length marker")[0]; + const intType = (intTypeByte & 0xF0) >> 4; if (intType !== 0x1) { - console.error("0xD: UNEXPECTED LENGTH-INT TYPE! " + intType); + throw new Error("Invalid binary plist. Unexpected dictionary length marker type: " + intType); } - const intInfo = int_type & 0x0F; + const intInfo = intTypeByte & 0x0F; const intLength = Math.pow(2, intInfo); - dictoffset = 2 + intLength; - length = readUInt(buffer.slice(offset + 2, offset + 2 + intLength)); + if (intLength > 8) { + throw new Error("Invalid binary plist. Unsupported dictionary length width: " + intLength); + } + dictOffset = 2 + intLength; + lengthValue = readBigUInt(readObjectBytes(offset + 2, intLength, "dictionary length")); } - if (length * 2 * objectRefSize > maxObjectSize) { + const refsSize = lengthValue * 2n * BigInt(objectRefSize); + if (refsSize > BigInt(maxObjectSize)) { throw new Error("Too little heap space available!"); } + const length = Number(lengthValue); + const refs = readObjectBytes(offset + dictOffset, Number(refsSize), "dictionary references"); if (debug) { console.log("Parsing dictionary #" + tableOffset); } const dict: Record = {}; for (let i = 0; i < length; i++) { - const keyRef = readUInt(buffer.slice(offset + dictoffset + i * objectRefSize, offset + dictoffset + (i + 1) * objectRefSize)); - const valRef = readUInt(buffer.slice(offset + dictoffset + (length * objectRefSize) + i * objectRefSize, offset + dictoffset + (length * objectRefSize) + (i + 1) * objectRefSize)); + const keyRef = readUInt(refs.subarray(i * objectRefSize, (i + 1) * objectRefSize)); + const valRef = readUInt(refs.subarray((length + i) * objectRefSize, (length + i + 1) * objectRefSize)); const key = parseObject(keyRef); const val = parseObject(valRef); if (debug) { @@ -392,10 +440,8 @@ function readInteger(buffer: Buffer): number | bigint { return simplifyInteger(value); } -// we're just going to toss the high order bits because javascript doesn't have 64-bit ints -function readUInt64BE(buffer: Buffer, start: number): number { - const data = buffer.slice(start, start + 8); - return readUInt(data, 0); +function readUInt64BE(buffer: Buffer, start: number): bigint { + return readBigUInt(buffer.subarray(start, start + 8)); } function swapBytes(buffer: T): T { diff --git a/test/parse.test.ts b/test/parse.test.ts index b598c66..ed5a555 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -171,8 +171,114 @@ describe('bplist-parser', function () { bplist.parseBuffer(Buffer.from('bplist00')); }, /Invalid binary plist/); }); + + it('rejects malformed trailer and offset table values', function () { + const base = makeIntegerArray([{ value: 1n, bytes: 1, expected: 1 }]); + const trailerOffset = base.length - 32; + const tableOffset = Number(readBigUInt(base.subarray(trailerOffset + 24, trailerOffset + 32))); + + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { objectCount: 0n })), /Object count/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { topObject: 0xffffffffffffffffn })), /Top object/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { offsetSize: 0 })), /Offset size/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { offsetSize: 255 })), /Offset size/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { objectRefSize: 0 })), /Object reference size/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { objectRefSize: 9 })), /Object reference size/); + assert.throws(() => bplist.parseBuffer(replaceTrailer(base, { offsetTableOffset: BigInt(tableOffset + 1000) })), /Offset table/); + assert.throws(() => bplist.parseBuffer(replaceFirstObjectOffset(base, 0n)), /outside/); + assert.throws(() => bplist.parseBuffer(replaceFirstObjectOffset(base, BigInt(tableOffset))), /outside/); + assert.throws(() => bplist.parseBuffer(replaceFirstObjectOffset(base, BigInt(tableOffset + 1000))), /outside/); + }); + + it('rejects object references outside the object table', function () { + const root = Buffer.concat([Buffer.from([0xA1]), Buffer.from([1])]); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(root)), /Object reference/); + }); + + it('rejects payloads that extend outside the object data area', function () { + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.from([0x14, 1, 2]))), /integer extends outside/); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.concat([Buffer.from([0x23]), Buffer.alloc(7)]))), /real extends outside/); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.from([0x33, 1]))), /date extends outside/); + }); + + it('rejects unsupported integer, date, and extended length markers', function () { + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.concat([Buffer.from([0x15]), Buffer.alloc(32)]))), /Unsupported integer length/); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.concat([Buffer.from([0x34]), Buffer.alloc(8)]))), /Unsupported date length marker/); + + for (const marker of [0x4F, 0x5F, 0x6F, 0xAF, 0xDF]) { + const object = Buffer.concat([Buffer.from([marker, 0x14]), Buffer.alloc(16)]); + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(object)), /Unsupported \w+ length width/); + } + + assert.throws(() => bplist.parseBuffer(makeSingleObjectPlist(Buffer.from([0x4F, 0x24]))), /Unexpected data length marker type/); + }); }); +type TrailerOverrides = { + objectCount?: bigint; + topObject?: bigint; + offsetSize?: number; + objectRefSize?: number; + offsetTableOffset?: bigint; +}; + +function makeSingleObjectPlist(object: Buffer): Buffer { + const offsetTableOffset = 8 + object.length; + const offsetSize = byteWidth(offsetTableOffset); + const trailer = Buffer.concat([ + Buffer.alloc(6), + Buffer.from([offsetSize, 1]), + writeUIntBE(1, 8), + writeUIntBE(0, 8), + writeUIntBE(offsetTableOffset, 8) + ]); + + return Buffer.concat([ + Buffer.from('bplist00'), + object, + writeUIntBE(8, offsetSize), + trailer + ]); +} + +function replaceTrailer(input: Buffer, overrides: TrailerOverrides): Buffer { + const result = Buffer.from(input); + const trailerOffset = result.length - 32; + + if (overrides.objectCount !== undefined) { + writeUIntBE(overrides.objectCount, 8).copy(result, trailerOffset + 8); + } + if (overrides.topObject !== undefined) { + writeUIntBE(overrides.topObject, 8).copy(result, trailerOffset + 16); + } + if (overrides.offsetSize !== undefined) { + result[trailerOffset + 6] = overrides.offsetSize; + } + if (overrides.objectRefSize !== undefined) { + result[trailerOffset + 7] = overrides.objectRefSize; + } + if (overrides.offsetTableOffset !== undefined) { + writeUIntBE(overrides.offsetTableOffset, 8).copy(result, trailerOffset + 24); + } + return result; +} + +function replaceFirstObjectOffset(input: Buffer, value: bigint): Buffer { + const result = Buffer.from(input); + const trailerOffset = result.length - 32; + const offsetSize = result[trailerOffset + 6]; + const tableOffset = Number(readBigUInt(result.subarray(trailerOffset + 24, trailerOffset + 32))); + writeUIntBE(value, offsetSize).copy(result, tableOffset); + return result; +} + +function readBigUInt(buffer: Buffer): bigint { + let value = 0n; + for (const byte of buffer) { + value = (value << 8n) | BigInt(byte); + } + return value; +} + type IntegerCase = { value: bigint; bytes: number; expected: number | bigint }; function makeIntegerArray(cases: IntegerCase[]): Buffer {