fix(pdf): PNG gate inflates IDAT natively against the exact raw size, validates IHDR fields, refuses zero dimensions and interlace (UPNG inflateRaw spin)

This commit is contained in:
bchanot committed 2026-10-11 14:52:03 +02:00
1 parent 0ceb357b18
commit cb95627a11
4 files changed
+412 -76

No files matched your search

+27 -12
View File
@@ -162,7 +162,7 @@ Two operations are distinct:
| snapshot document | ≤ 64 views, ≤ 1,024 annotations, ≤ 8,192 finding nodes, depth ≤ 8, names ≤ 512 chars |
| branding | PNG base64 ≤ 512 KiB, SVG ≤ 256 KiB, http(s) URLs ≤ 2,048 chars |
| branding URL | `http(s)://` then printable ASCII only, without `"` `'` `(` `)` `<` `>` `\`, no space or control character |
| branding PNG | valid signature, one IHDR (13 bytes) first, then only `PLTE` `tRNS` `IDAT` `gAMA` `cHRM` `sRGB` `sBIT` `pHYs` `bKGD`, no chunk past the buffer, an empty `IEND` as the last bytes, each side ≤ 8,192 px, ≤ 16,777,216 px (4096²) |
| branding PNG | valid signature, one IHDR (13 bytes) first with a colour type and bit depth the spec allows, compression and filter method 0, no interlace; each side 1 to 8,192 px, ≤ 16,777,216 px (4096²), expected raw scanline size ≤ 16 MiB; then only `PLTE` `tRNS` `IDAT` `gAMA` `cHRM` `sRGB` `sBIT` `pHYs` `bKGD`, no chunk past the buffer, an empty `IEND` as the last bytes; the IDAT stream inflated natively and yielding exactly the raw scanline size |
| report at export | ≤ 500 observations, ≤ 8 views per observation |
| SVG sanitiser | ≤ 20,000 elements, nesting ≤ 64, ≤ 256 `<use>`, blur `stdDeviation` ≤ 100 |
@@ -188,17 +188,32 @@ receives (`ArchiveExportError('branding_invalid')` before any page is
drawn), even from an archive that passed the reader. The URI itself is the
hex form of the URL's ASCII bytes (`<68747470…>`), not UTF-16 with a BOM,
so readers that take `/URI` as raw bytes (poppler) follow it. A PNG logo is
checked before decoding: the signature and a first 13-byte IHDR, then the
chunk list is walked and must end on an empty `IEND` that closes the file.
Only the chunk types the decoder reads by fixed offsets or by declared
length are accepted (`PLTE`, `tRNS`, `IDAT`, `IEND`, `gAMA`, `cHRM`, `sRGB`,
`sBIT`, `pHYs`, `bKGD`); a second IHDR (the decoder would size its buffer
from the last one), an APNG chunk, a text or profile chunk (`tEXt`, `iTXt`,
`zTXt`, `iCCP`: the decoder scans them for a NUL byte and never stops when
none follows in the buffer), an unknown type, a chunk length past the end
of the buffer, a missing IEND or bytes after it, or a header over the pixel
budget refuses the slot. The archive accepts only PNGs ending on IEND, even
though the decoder reads a PNG without one. That slot alone prints its
checked before pdf-lib's decoder (UPNG) sees it, because that decoder trusts
its input: it sizes its bitmap from whatever IHDR claims, its `inflateRaw`
never checks the input bound (a truncated or crafted IDAT stream makes it
read past the buffer and never return), and it scans text chunks for a NUL
byte that may be missing. The gate therefore validates, in order: the
signature and a first 13-byte IHDR; a width and height of at least 1 under
the pixel budget; a colour type and bit depth the PNG spec allows (0: 1 to
16 bits, 2 and 4 and 6: 8 or 16, 3: 1 to 8), compression and filter method
0 and no interlace (the archived logos are never interlaced, so Adam7 pass
sizes are not computed); an expected raw scanline size,
`(ceil(width × bits per pixel / 8) + 1) × height`, of at most 16 MiB, the
attachment cap the native inflater enforces, which is the effective budget
(4096² at 8-bit RGBA is 67 MB of scanlines and is refused; a 512 px wide
logo is under 1 MB); then the chunk list, which must end on an empty `IEND`
that closes the file and may hold only the chunk types the decoder reads by
fixed offsets or by declared length (`PLTE`, `tRNS`, `IDAT`, `IEND`,
`gAMA`, `cHRM`, `sRGB`, `sBIT`, `pHYs`, `bKGD`): a second IHDR, an APNG
chunk, a text or profile chunk (`tEXt`, `iTXt`, `zTXt`, `iCCP`), an unknown
type, a chunk length past the end of the buffer, a missing IEND or bytes
after it refuse the slot. Last, the IDAT payloads are concatenated and
inflated with the platform's `DecompressionStream`, bounded to the expected
raw size: a truncated, corrupt or oversized stream, or one that does not
yield exactly that size, refuses the slot. The allowlist alone would not
make the decoder safe; the native inflation is what keeps it from reading
an unbounded stream. The archive accepts only PNGs ending on IEND, even
though the decoder reads a PNG without one. A refused slot alone prints its
fallback text and the export reports `logo_failed`.
### Known limits