fix(pdf): link URIs as ASCII hex (readable by poppler), PNG budget walks the chunk list (second IHDR and APNG refused)

This commit is contained in:
bchanot committed 2026-10-11 14:14:10 +02:00
1 parent dfd04e0bd7
commit 3a26c61119
4 files changed
+215 -49

No files matched your search

+48 -11
View File
@@ -5,20 +5,34 @@ import { renderBrandingSvg } from './svg/view'
/**
* The branding images embedded once per document. A PNG is decoded from
* its base64 once its header fits the pixel budget; an SVG is sanitised and
* rasterised at the view width. Each slot fails alone: the page prints the
* credit's fallback text instead and the export reports a `logo_failed`
* warning.
* its base64 once its header and chunk list fit the pixel budget; an SVG is
* sanitised and rasterised at the view width. Each slot fails alone: the
* page prints the credit's fallback text instead and the export reports a
* `logo_failed` warning.
*/
/** A logo PNG is at most 4096² pixels and 8192 px a side (STEP6 §15). */
export const PNG_CAPS = { side: 8192, pixels: 4096 * 4096 } as const
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
const IHDR_TYPE = 0x49484452
const IHDR_END = 24
/** A chunk is length (4), type (4), data (length), CRC (4). */
const CHUNK_HEAD = 8
const CHUNK_OVERHEAD = 12
const IHDR_LENGTH = 13
const IHDR_END = PNG_SIGNATURE.length + CHUNK_OVERHEAD + IHDR_LENGTH
export type PngHeaderIssue = 'signature' | 'dimensions'
/** The big-endian uint32 a four-letter chunk type reads as. */
const chunkType = (name: string): number =>
[...name].reduce((acc, char) => acc * 256 + char.charCodeAt(0), 0)
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),
)
export type PngHeaderIssue = 'signature' | 'dimensions' | 'chunks'
export type LogoSlot = Extract<ExportWarning, { kind: 'logo_failed' }>['slot']
@@ -37,20 +51,43 @@ export interface BrandingImages {
}
/**
* Why a PNG is refused before any decoding, or null when its header fits
* the budget: the signature and the IHDR chunk must be there, and the
* announced width × height must stay under `PNG_CAPS`. The decoder would
* otherwise allocate the full bitmap of whatever size the header claims.
* 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.
*/
export function pngHeaderIssue(bytes: Uint8Array): PngHeaderIssue | null {
if (bytes.byteLength < IHDR_END) return 'signature'
if (PNG_SIGNATURE.some((byte, i) => bytes[i] !== byte)) return 'signature'
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
if (view.getUint32(8) !== IHDR_LENGTH) return 'signature'
if (view.getUint32(12) !== IHDR_TYPE) return 'signature'
const width = view.getUint32(16)
const height = view.getUint32(20)
if (width > PNG_CAPS.side || height > PNG_CAPS.side) return 'dimensions'
if (width * height > PNG_CAPS.pixels) return 'dimensions'
return chunkListIssue(view)
}
/**
* 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.
*/
function chunkListIssue(view: DataView): PngHeaderIssue | null {
let offset = IHDR_END
while (offset < view.byteLength) {
if (offset + CHUNK_HEAD > view.byteLength) return 'chunks'
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
}
return null
}
+25 -1
View File
@@ -1,4 +1,5 @@
import { PDFName, PDFHexString, type PDFPage } from 'pdf-lib'
import { ArchiveExportError } from '../errors'
export interface LinkRect {
x: number
@@ -8,6 +9,29 @@ export interface LinkRect {
url: string
}
/** Printable ASCII without the space: what the branding URL schema allows. */
const ASCII_MIN = 0x21
const ASCII_MAX = 0x7e
/**
* The URL as the hex form of its ASCII bytes, one byte per character.
* `PDFHexString.fromText` would write UTF-16BE with a BOM, which readers
* that take `/URI` as raw bytes (poppler) turn into a dead link. A code
* point outside printable ASCII cannot reach the sink: the schema refuses
* it upstream, so it means the branding bypassed validation.
*/
function asciiHexOf(url: string): string {
let hex = ''
for (const char of url) {
const code = char.codePointAt(0) ?? 0
if (code < ASCII_MIN || code > ASCII_MAX) {
throw new ArchiveExportError('branding_invalid', 'url')
}
hex += code.toString(16).padStart(2, '0')
}
return hex
}
/**
* A clickable `/Link` annotation opening `url`, appended to the page's
* Annots. The URI is written as a hex string: pdf-lib does not escape `(`,
@@ -20,7 +44,7 @@ export function addLinkAnnotation(page: PDFPage, rect: LinkRect): void {
const actionDict = context.obj({
Type: 'Action',
S: 'URI',
URI: PDFHexString.fromText(rect.url),
URI: PDFHexString.of(asciiHexOf(rect.url)),
})
const annotDict = context.obj({