fix(pdf): PNG chunk allowlist and mandatory empty IEND — text and profile chunks refused (UPNG nextZero spin)

This commit is contained in:
bchanot committed 2026-10-11 14:29:56 +02:00
1 parent 3a26c61119
commit 0ceb357b18
3 files changed
+129 -34

No files matched your search

+38 -17
View File
@@ -27,9 +27,26 @@ const chunkType = (name: string): number =>
const IHDR_TYPE = chunkType('IHDR')
const IEND_TYPE = chunkType('IEND')
/** A second IHDR resizes the decoder's buffer; APNG frames are not a logo. */
const FORBIDDEN_CHUNKS = new Set(
['IHDR', 'acTL', 'fcTL', 'fdAT'].map(chunkType),
/**
* The chunk types UPNG reads by fixed offsets or by declared length. A
* second IHDR resizes the decoder's buffer, APNG frames are not a logo, and
* every text or profile chunk (`tEXt`, `iTXt`, `zTXt`, `iCCP`) is read with
* `_bin.nextZero` (`while (data[p] != 0) p++`), which never stops when no
* NUL byte follows in the buffer: all of them stay out of the list.
*/
const ALLOWED_CHUNKS = new Set(
[
'PLTE',
'tRNS',
'IDAT',
'IEND',
'gAMA',
'cHRM',
'sRGB',
'sBIT',
'pHYs',
'bKGD',
].map(chunkType),
)
export type PngHeaderIssue = 'signature' | 'dimensions' | 'chunks'
@@ -54,9 +71,10 @@ export interface BrandingImages {
* Why a PNG is refused before any decoding, or null when it fits the
* budget: the signature and a first IHDR chunk of 13 bytes must be there,
* the announced width × height must stay under `PNG_CAPS`, and the chunk
* list must hold no second IHDR, no APNG chunk and no length past the end
* of the buffer. The decoder would otherwise allocate the full bitmap of
* whatever size the last IHDR claims.
* list must hold only allowlisted types, none past the end of the buffer,
* closed by an `IEND` that ends the file. The decoder would otherwise
* allocate the bitmap of whatever size the last IHDR claims, or scan past
* the buffer for a NUL byte that is not there.
*/
export function pngHeaderIssue(bytes: Uint8Array): PngHeaderIssue | null {
if (bytes.byteLength < IHDR_END) return 'signature'
@@ -72,23 +90,26 @@ export function pngHeaderIssue(bytes: Uint8Array): PngHeaderIssue | null {
}
/**
* Walks the chunks after the first IHDR until `IEND` or the end of the
* buffer. UPNG applies every IHDR it meets and sizes its buffer from the
* last one, so the first header alone proves nothing; a declared length
* that overruns the buffer, or a truncated chunk head, is refused as well.
* Walks the chunks after the first IHDR to an `IEND` of length 0 that ends
* the buffer exactly. Any type outside `ALLOWED_CHUNKS` (a second IHDR, an
* APNG chunk, a `tEXt`/`iTXt` whose missing NUL would spin UPNG's
* `nextZero` forever, an unknown type), a declared length past the buffer,
* a truncated chunk head, a missing IEND or bytes after it are refused.
*/
function chunkListIssue(view: DataView): PngHeaderIssue | null {
let offset = IHDR_END
while (offset < view.byteLength) {
if (offset + CHUNK_HEAD > view.byteLength) return 'chunks'
while (offset + CHUNK_HEAD <= view.byteLength) {
const length = view.getUint32(offset)
const type = view.getUint32(offset + 4)
if (offset + CHUNK_OVERHEAD + length > view.byteLength) return 'chunks'
if (type === IEND_TYPE) return null
if (FORBIDDEN_CHUNKS.has(type)) return 'chunks'
offset += CHUNK_OVERHEAD + length
const next = offset + CHUNK_OVERHEAD + length
if (next > view.byteLength) return 'chunks'
if (!ALLOWED_CHUNKS.has(type)) return 'chunks'
if (type === IEND_TYPE) {
return length === 0 && next === view.byteLength ? null : 'chunks'
}
offset = next
}
return null
return 'chunks'
}
function base64ToBytes(base64: string): Uint8Array {