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:
1 parent
0ceb357b18
commit
cb95627a11
4 files changed
+412
-76
No files matched your search
+27
-12
@@ -162,7 +162,7 @@ Two operations are distinct:
|
|||||||
| snapshot document | ≤ 64 views, ≤ 1,024 annotations, ≤ 8,192 finding nodes, depth ≤ 8, names ≤ 512 chars |
|
| 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 | 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 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 |
|
| report at export | ≤ 500 observations, ≤ 8 views per observation |
|
||||||
| SVG sanitiser | ≤ 20,000 elements, nesting ≤ 64, ≤ 256 `<use>`, blur `stdDeviation` ≤ 100 |
|
| 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
|
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,
|
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
|
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
|
checked before pdf-lib's decoder (UPNG) sees it, because that decoder trusts
|
||||||
chunk list is walked and must end on an empty `IEND` that closes the file.
|
its input: it sizes its bitmap from whatever IHDR claims, its `inflateRaw`
|
||||||
Only the chunk types the decoder reads by fixed offsets or by declared
|
never checks the input bound (a truncated or crafted IDAT stream makes it
|
||||||
length are accepted (`PLTE`, `tRNS`, `IDAT`, `IEND`, `gAMA`, `cHRM`, `sRGB`,
|
read past the buffer and never return), and it scans text chunks for a NUL
|
||||||
`sBIT`, `pHYs`, `bKGD`); a second IHDR (the decoder would size its buffer
|
byte that may be missing. The gate therefore validates, in order: the
|
||||||
from the last one), an APNG chunk, a text or profile chunk (`tEXt`, `iTXt`,
|
signature and a first 13-byte IHDR; a width and height of at least 1 under
|
||||||
`zTXt`, `iCCP`: the decoder scans them for a NUL byte and never stops when
|
the pixel budget; a colour type and bit depth the PNG spec allows (0: 1 to
|
||||||
none follows in the buffer), an unknown type, a chunk length past the end
|
16 bits, 2 and 4 and 6: 8 or 16, 3: 1 to 8), compression and filter method
|
||||||
of the buffer, a missing IEND or bytes after it, or a header over the pixel
|
0 and no interlace (the archived logos are never interlaced, so Adam7 pass
|
||||||
budget refuses the slot. The archive accepts only PNGs ending on IEND, even
|
sizes are not computed); an expected raw scanline size,
|
||||||
though the decoder reads a PNG without one. That slot alone prints its
|
`(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`.
|
fallback text and the export reports `logo_failed`.
|
||||||
|
|
||||||
### Known limits
|
### Known limits
|
||||||
|
|||||||
@@ -14,9 +14,14 @@ function concat(chunks: Uint8Array[], total: number): Uint8Array {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
/** FlateDecode under the cap. Null on failure or when the cap is exceeded. */
|
/**
|
||||||
|
* FlateDecode (zlib framing) under `limit`, the attachment cap by default.
|
||||||
|
* Null on any failure (truncated or corrupt stream, trailing junk, bad
|
||||||
|
* checksum) or as soon as the output exceeds the limit.
|
||||||
|
*/
|
||||||
export async function inflateFlate(
|
export async function inflateFlate(
|
||||||
bytes: Uint8Array,
|
bytes: Uint8Array,
|
||||||
|
limit: number = MAX_ATTACHMENT_BYTES,
|
||||||
): Promise<Uint8Array | null> {
|
): Promise<Uint8Array | null> {
|
||||||
if (typeof DecompressionStream === 'undefined') return null
|
if (typeof DecompressionStream === 'undefined') return null
|
||||||
try {
|
try {
|
||||||
@@ -31,7 +36,7 @@ export async function inflateFlate(
|
|||||||
const { done, value } = await reader.read()
|
const { done, value } = await reader.read()
|
||||||
if (done) break
|
if (done) break
|
||||||
total += value.byteLength
|
total += value.byteLength
|
||||||
if (total > MAX_ATTACHMENT_BYTES) {
|
if (total > limit) {
|
||||||
await reader.cancel()
|
await reader.cancel()
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
+129
-20
@@ -1,18 +1,27 @@
|
|||||||
import type { PDFDocument, PDFImage } from 'pdf-lib'
|
import type { PDFDocument, PDFImage } from 'pdf-lib'
|
||||||
import type { Branding, Credit, ImageAsset } from '@faunosteo/domain'
|
import type { Branding, Credit, ImageAsset } from '@faunosteo/domain'
|
||||||
|
import { inflateFlate, MAX_ATTACHMENT_BYTES } from './attachment/inflate'
|
||||||
import type { ExportWarning, Rasterizer } from './types'
|
import type { ExportWarning, Rasterizer } from './types'
|
||||||
import { renderBrandingSvg } from './svg/view'
|
import { renderBrandingSvg } from './svg/view'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The branding images embedded once per document. A PNG is decoded from
|
* The branding images embedded once per document. A PNG is decoded from
|
||||||
* its base64 once its header and chunk list fit the pixel budget; an SVG is
|
* its base64 once its header, chunk list and IDAT stream pass `pngIssue`;
|
||||||
* sanitised and rasterised at the view width. Each slot fails alone: the
|
* an SVG is sanitised and rasterised at the view width. Each slot fails
|
||||||
* page prints the credit's fallback text instead and the export reports a
|
* alone: the page prints the credit's fallback text instead and the export
|
||||||
* `logo_failed` warning.
|
* 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
|
* A logo PNG is at most 4096² pixels and 8192 px a side (STEP6 §15), and
|
||||||
|
* its raw (filtered, uncompressed) scanlines fit the attachment cap, the
|
||||||
|
* most the native inflater is allowed to produce.
|
||||||
|
*/
|
||||||
|
export const PNG_CAPS = {
|
||||||
|
side: 8192,
|
||||||
|
pixels: 4096 * 4096,
|
||||||
|
rawBytes: MAX_ATTACHMENT_BYTES,
|
||||||
|
} as const
|
||||||
|
|
||||||
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
|
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
|
||||||
/** A chunk is length (4), type (4), data (length), CRC (4). */
|
/** A chunk is length (4), type (4), data (length), CRC (4). */
|
||||||
@@ -26,6 +35,7 @@ const chunkType = (name: string): number =>
|
|||||||
[...name].reduce((acc, char) => acc * 256 + char.charCodeAt(0), 0)
|
[...name].reduce((acc, char) => acc * 256 + char.charCodeAt(0), 0)
|
||||||
|
|
||||||
const IHDR_TYPE = chunkType('IHDR')
|
const IHDR_TYPE = chunkType('IHDR')
|
||||||
|
const IDAT_TYPE = chunkType('IDAT')
|
||||||
const IEND_TYPE = chunkType('IEND')
|
const IEND_TYPE = chunkType('IEND')
|
||||||
/**
|
/**
|
||||||
* The chunk types UPNG reads by fixed offsets or by declared length. A
|
* The chunk types UPNG reads by fixed offsets or by declared length. A
|
||||||
@@ -49,7 +59,28 @@ const ALLOWED_CHUNKS = new Set(
|
|||||||
].map(chunkType),
|
].map(chunkType),
|
||||||
)
|
)
|
||||||
|
|
||||||
export type PngHeaderIssue = 'signature' | 'dimensions' | 'chunks'
|
/** Samples per pixel and the bit depths the PNG spec allows, by colour type. */
|
||||||
|
const CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }
|
||||||
|
const DEPTHS: Record<number, readonly number[]> = {
|
||||||
|
0: [1, 2, 4, 8, 16],
|
||||||
|
2: [8, 16],
|
||||||
|
3: [1, 2, 4, 8],
|
||||||
|
4: [8, 16],
|
||||||
|
6: [8, 16],
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PngIssue = 'signature' | 'dimensions' | 'ihdr' | 'chunks' | 'idat'
|
||||||
|
|
||||||
|
/** The 13 IHDR fields, read as announced. */
|
||||||
|
interface PngHeader {
|
||||||
|
width: number
|
||||||
|
height: number
|
||||||
|
depth: number
|
||||||
|
colourType: number
|
||||||
|
compression: number
|
||||||
|
filter: number
|
||||||
|
interlace: number
|
||||||
|
}
|
||||||
|
|
||||||
export type LogoSlot = Extract<ExportWarning, { kind: 'logo_failed' }>['slot']
|
export type LogoSlot = Extract<ExportWarning, { kind: 'logo_failed' }>['slot']
|
||||||
|
|
||||||
@@ -68,25 +99,66 @@ export interface BrandingImages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Why a PNG is refused before any decoding, or null when it fits the
|
* Why a PNG is refused before UPNG sees it, or null when it may be decoded.
|
||||||
* budget: the signature and a first IHDR chunk of 13 bytes must be there,
|
* In order: the signature and a first 13-byte IHDR (`signature`); the
|
||||||
* the announced width × height must stay under `PNG_CAPS`, and the chunk
|
* announced size under `PNG_CAPS` (`dimensions`); IHDR fields the spec
|
||||||
* list must hold only allowlisted types, none past the end of the buffer,
|
* allows, non-interlaced (`ihdr`); an allowlisted chunk list closed by an
|
||||||
* closed by an `IEND` that ends the file. The decoder would otherwise
|
* empty `IEND` (`chunks`); an IDAT stream the native inflater accepts and
|
||||||
* allocate the bitmap of whatever size the last IHDR claims, or scan past
|
* that yields exactly the raw scanline size (`idat`). UPNG's `inflateRaw`
|
||||||
* the buffer for a NUL byte that is not there.
|
* never checks its input bound, so a truncated or crafted stream makes it
|
||||||
|
* read past the buffer and never return; its bitmap is sized from whatever
|
||||||
|
* IHDR claims; and `nextZero` scans text chunks for a NUL that may be
|
||||||
|
* missing. Each check closes one of those paths.
|
||||||
*/
|
*/
|
||||||
export function pngHeaderIssue(bytes: Uint8Array): PngHeaderIssue | null {
|
export async function pngIssue(bytes: Uint8Array): Promise<PngIssue | null> {
|
||||||
if (bytes.byteLength < IHDR_END) return 'signature'
|
if (bytes.byteLength < IHDR_END) return 'signature'
|
||||||
if (PNG_SIGNATURE.some((byte, i) => bytes[i] !== byte)) return 'signature'
|
if (PNG_SIGNATURE.some((byte, i) => bytes[i] !== byte)) return 'signature'
|
||||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
||||||
if (view.getUint32(8) !== IHDR_LENGTH) return 'signature'
|
if (view.getUint32(8) !== IHDR_LENGTH) return 'signature'
|
||||||
if (view.getUint32(12) !== IHDR_TYPE) return 'signature'
|
if (view.getUint32(12) !== IHDR_TYPE) return 'signature'
|
||||||
const width = view.getUint32(16)
|
const header = readHeader(view)
|
||||||
const height = view.getUint32(20)
|
const headerProblem = headerIssue(header)
|
||||||
|
if (headerProblem) return headerProblem
|
||||||
|
const rawSize = expectedRawSize(header)
|
||||||
|
if (rawSize > PNG_CAPS.rawBytes) return 'dimensions'
|
||||||
|
return chunkListIssue(view) ?? idatIssue(view, rawSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
function readHeader(view: DataView): PngHeader {
|
||||||
|
return {
|
||||||
|
width: view.getUint32(16),
|
||||||
|
height: view.getUint32(20),
|
||||||
|
depth: view.getUint8(24),
|
||||||
|
colourType: view.getUint8(25),
|
||||||
|
compression: view.getUint8(26),
|
||||||
|
filter: view.getUint8(27),
|
||||||
|
interlace: view.getUint8(28),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size budget, then the IHDR fields: a side of 0 would give the page
|
||||||
|
* stream an Infinity scale, an unknown colour type or depth (255 at 4096²
|
||||||
|
* costs seconds of CPU in UPNG) has no raw size to check against, and an
|
||||||
|
* interlaced image is refused outright: no archived logo is interlaced and
|
||||||
|
* the Adam7 pass sizes are not worth computing here.
|
||||||
|
*/
|
||||||
|
function headerIssue(header: PngHeader): PngIssue | null {
|
||||||
|
const { width, height, depth, colourType } = header
|
||||||
|
if (width < 1 || height < 1) return 'dimensions'
|
||||||
if (width > PNG_CAPS.side || height > PNG_CAPS.side) return 'dimensions'
|
if (width > PNG_CAPS.side || height > PNG_CAPS.side) return 'dimensions'
|
||||||
if (width * height > PNG_CAPS.pixels) return 'dimensions'
|
if (width * height > PNG_CAPS.pixels) return 'dimensions'
|
||||||
return chunkListIssue(view)
|
if (!DEPTHS[colourType]?.includes(depth)) return 'ihdr'
|
||||||
|
if (header.compression !== 0 || header.filter !== 0) return 'ihdr'
|
||||||
|
if (header.interlace !== 0) return 'ihdr'
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Non-interlaced raw size: each scanline is a filter byte plus its packed samples. */
|
||||||
|
function expectedRawSize(header: PngHeader): number {
|
||||||
|
const bitsPerPixel = CHANNELS[header.colourType]! * header.depth
|
||||||
|
const rowBytes = Math.ceil((header.width * bitsPerPixel) / 8) + 1
|
||||||
|
return rowBytes * header.height
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,7 +168,7 @@ export function pngHeaderIssue(bytes: Uint8Array): PngHeaderIssue | null {
|
|||||||
* `nextZero` forever, an unknown type), a declared length past the buffer,
|
* `nextZero` forever, an unknown type), a declared length past the buffer,
|
||||||
* a truncated chunk head, a missing IEND or bytes after it are refused.
|
* a truncated chunk head, a missing IEND or bytes after it are refused.
|
||||||
*/
|
*/
|
||||||
function chunkListIssue(view: DataView): PngHeaderIssue | null {
|
function chunkListIssue(view: DataView): PngIssue | null {
|
||||||
let offset = IHDR_END
|
let offset = IHDR_END
|
||||||
while (offset + CHUNK_HEAD <= view.byteLength) {
|
while (offset + CHUNK_HEAD <= view.byteLength) {
|
||||||
const length = view.getUint32(offset)
|
const length = view.getUint32(offset)
|
||||||
@@ -112,6 +184,43 @@ function chunkListIssue(view: DataView): PngHeaderIssue | null {
|
|||||||
return 'chunks'
|
return 'chunks'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The IDAT payloads in file order, one zlib stream; the chunk list is already known sound. */
|
||||||
|
function idatPayload(view: DataView): Uint8Array {
|
||||||
|
const parts: Uint8Array[] = []
|
||||||
|
let total = 0
|
||||||
|
let offset = IHDR_END
|
||||||
|
while (offset + CHUNK_HEAD <= view.byteLength) {
|
||||||
|
const length = view.getUint32(offset)
|
||||||
|
if (view.getUint32(offset + 4) === IDAT_TYPE) {
|
||||||
|
const start = view.byteOffset + offset + CHUNK_HEAD
|
||||||
|
parts.push(new Uint8Array(view.buffer, start, length))
|
||||||
|
total += length
|
||||||
|
}
|
||||||
|
offset += CHUNK_OVERHEAD + length
|
||||||
|
}
|
||||||
|
const payload = new Uint8Array(total)
|
||||||
|
let at = 0
|
||||||
|
for (const part of parts) {
|
||||||
|
payload.set(part, at)
|
||||||
|
at += part.byteLength
|
||||||
|
}
|
||||||
|
return payload
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inflates the IDAT stream with the native, bounded inflater before UPNG's
|
||||||
|
* unbounded `inflateRaw` does: a truncated, corrupt or oversized stream, or
|
||||||
|
* one that does not yield exactly the raw scanline size, is refused.
|
||||||
|
*/
|
||||||
|
async function idatIssue(
|
||||||
|
view: DataView,
|
||||||
|
rawSize: number,
|
||||||
|
): Promise<PngIssue | null> {
|
||||||
|
const raw = await inflateFlate(idatPayload(view), rawSize)
|
||||||
|
if (!raw || raw.byteLength !== rawSize) return 'idat'
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
function base64ToBytes(base64: string): Uint8Array {
|
function base64ToBytes(base64: string): Uint8Array {
|
||||||
const binary = atob(base64)
|
const binary = atob(base64)
|
||||||
const bytes = new Uint8Array(binary.length)
|
const bytes = new Uint8Array(binary.length)
|
||||||
@@ -126,7 +235,7 @@ async function embedImage(
|
|||||||
): Promise<PDFImage> {
|
): Promise<PDFImage> {
|
||||||
if (asset.kind === 'png') {
|
if (asset.kind === 'png') {
|
||||||
const bytes = base64ToBytes(asset.base64)
|
const bytes = base64ToBytes(asset.base64)
|
||||||
const issue = pngHeaderIssue(bytes)
|
const issue = await pngIssue(bytes)
|
||||||
if (issue) throw new Error(`png refused: ${issue}`)
|
if (issue) throw new Error(`png refused: ${issue}`)
|
||||||
return pdfDoc.embedPng(bytes)
|
return pdfDoc.embedPng(bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
*/
|
*/
|
||||||
import { readFileSync } from 'node:fs'
|
import { readFileSync } from 'node:fs'
|
||||||
import { resolve } from 'node:path'
|
import { resolve } from 'node:path'
|
||||||
|
import { deflateSync } from 'node:zlib'
|
||||||
import { beforeAll, describe, expect, it, vi } from 'vitest'
|
import { beforeAll, describe, expect, it, vi } from 'vitest'
|
||||||
import { PDFDict, PDFDocument, PDFName } from 'pdf-lib'
|
import { PDFDict, PDFDocument, PDFName } from 'pdf-lib'
|
||||||
import {
|
import {
|
||||||
@@ -26,7 +27,7 @@ import {
|
|||||||
type Rasterizer,
|
type Rasterizer,
|
||||||
} from '../src'
|
} from '../src'
|
||||||
import { inflateFlate } from '../src/attachment/inflate'
|
import { inflateFlate } from '../src/attachment/inflate'
|
||||||
import { pngHeaderIssue } from '../src/branding'
|
import { pngIssue } from '../src/branding'
|
||||||
import { SvgRejectedError } from '../src/errors'
|
import { SvgRejectedError } from '../src/errors'
|
||||||
import { addLinkAnnotation } from '../src/render/linkAnnotation'
|
import { addLinkAnnotation } from '../src/render/linkAnnotation'
|
||||||
import { parseSvg, serializeSvg } from '../src/svg/parse'
|
import { parseSvg, serializeSvg } from '../src/svg/parse'
|
||||||
@@ -579,20 +580,37 @@ describe('branding URL injection (HIGH)', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
/** A PNG signature and an IHDR chunk announcing `width` × `height`, nothing after. */
|
interface IhdrFields {
|
||||||
|
depth?: number
|
||||||
|
colourType?: number
|
||||||
|
compression?: number
|
||||||
|
filter?: number
|
||||||
|
interlace?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const PNG_SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A PNG signature and an IHDR chunk announcing `width` × `height`, 8-bit
|
||||||
|
* greyscale non-interlaced unless `fields` says otherwise, nothing after.
|
||||||
|
*/
|
||||||
function pngHeader(
|
function pngHeader(
|
||||||
width: number,
|
width: number,
|
||||||
height: number,
|
height: number,
|
||||||
|
fields: IhdrFields = {},
|
||||||
signature = true,
|
signature = true,
|
||||||
): Uint8Array {
|
): Uint8Array {
|
||||||
const bytes = new Uint8Array(33)
|
const bytes = new Uint8Array(33)
|
||||||
bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
bytes.set(PNG_SIGNATURE)
|
||||||
if (!signature) bytes[3] = 0x48
|
if (!signature) bytes[3] = 0x48
|
||||||
const view = new DataView(bytes.buffer)
|
const view = new DataView(bytes.buffer)
|
||||||
view.setUint32(8, 13)
|
view.setUint32(8, 13)
|
||||||
bytes.set([0x49, 0x48, 0x44, 0x52], 12)
|
bytes.set([0x49, 0x48, 0x44, 0x52], 12)
|
||||||
view.setUint32(16, width)
|
view.setUint32(16, width)
|
||||||
view.setUint32(20, height)
|
view.setUint32(20, height)
|
||||||
|
const { depth = 8, colourType = 0, compression = 0 } = fields
|
||||||
|
const { filter = 0, interlace = 0 } = fields
|
||||||
|
bytes.set([depth, colourType, compression, filter, interlace], 24)
|
||||||
return bytes
|
return bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -611,8 +629,36 @@ function pngChunk(type: string, data: Uint8Array): Uint8Array {
|
|||||||
const concat = (...parts: Uint8Array[]): Uint8Array =>
|
const concat = (...parts: Uint8Array[]): Uint8Array =>
|
||||||
Uint8Array.from(Buffer.concat(parts.map((p) => Buffer.from(p))))
|
Uint8Array.from(Buffer.concat(parts.map((p) => Buffer.from(p))))
|
||||||
|
|
||||||
|
const CHANNELS: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }
|
||||||
|
|
||||||
|
/** Raw scanlines of a non-interlaced image: a filter byte then the packed samples, per row. */
|
||||||
|
function rawSize(
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
fields: IhdrFields = {},
|
||||||
|
): number {
|
||||||
|
const { depth = 8, colourType = 0 } = fields
|
||||||
|
const bitsPerPixel = CHANNELS[colourType]! * depth
|
||||||
|
return (Math.ceil((width * bitsPerPixel) / 8) + 1) * height
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An IDAT chunk holding the zlib stream of `raw`. */
|
||||||
|
const idatOf = (raw: Uint8Array): Uint8Array =>
|
||||||
|
pngChunk('IDAT', deflateSync(raw))
|
||||||
|
|
||||||
const IEND = pngChunk('IEND', new Uint8Array(0))
|
const IEND = pngChunk('IEND', new Uint8Array(0))
|
||||||
const IDAT = pngChunk('IDAT', new Uint8Array(16))
|
/** The IDAT of a 1×1 8-bit greyscale image (2 raw bytes), what `pngHeader(1, 1)` announces. */
|
||||||
|
const IDAT = idatOf(new Uint8Array(2))
|
||||||
|
|
||||||
|
/** A complete valid PNG of zero pixels: header, one IDAT of the exact raw size, IEND. */
|
||||||
|
function validPng(
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
fields: IhdrFields = {},
|
||||||
|
): Uint8Array {
|
||||||
|
const raw = new Uint8Array(rawSize(width, height, fields))
|
||||||
|
return concat(pngHeader(width, height, fields), idatOf(raw), IEND)
|
||||||
|
}
|
||||||
|
|
||||||
/** A 1×1 header followed by a second IHDR announcing 10000 × 10000, then IEND. */
|
/** A 1×1 header followed by a second IHDR announcing 10000 × 10000, then IEND. */
|
||||||
function doubledIhdrPng(): Uint8Array {
|
function doubledIhdrPng(): Uint8Array {
|
||||||
@@ -638,24 +684,158 @@ function itxtThenFatIendPng(): Uint8Array {
|
|||||||
return concat(pngHeader(1, 1), IDAT, itxt, fatIend)
|
return concat(pngHeader(1, 1), IDAT, itxt, fatIend)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The third PoC, 77 bytes: 16×16 RGBA, a zlib IDAT cut to 20 bytes, IEND.
|
||||||
|
* Signature and chunk list are sound; UPNG's `inflateRaw` reads past the
|
||||||
|
* end of the stream and never returns.
|
||||||
|
*/
|
||||||
|
function truncatedIdatPng(): Uint8Array {
|
||||||
|
const raw = Uint8Array.from(
|
||||||
|
{ length: rawSize(16, 16, { colourType: 6 }) },
|
||||||
|
(_, i) => i & 0xff,
|
||||||
|
)
|
||||||
|
const stream = deflateSync(raw).subarray(0, 20)
|
||||||
|
const header = pngHeader(16, 16, { colourType: 6 })
|
||||||
|
return concat(header, pngChunk('IDAT', stream), IEND)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Chunk {
|
||||||
|
type: string
|
||||||
|
data: Uint8Array
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The chunks of a well-formed PNG, in file order, CRCs dropped. */
|
||||||
|
function chunksOf(png: Uint8Array): Chunk[] {
|
||||||
|
const view = new DataView(png.buffer, png.byteOffset, png.byteLength)
|
||||||
|
const chunks: Chunk[] = []
|
||||||
|
let offset = 8
|
||||||
|
while (offset < png.byteLength) {
|
||||||
|
const length = view.getUint32(offset)
|
||||||
|
const type = String.fromCharCode(...png.subarray(offset + 4, offset + 8))
|
||||||
|
chunks.push({ type, data: png.subarray(offset + 8, offset + 8 + length) })
|
||||||
|
offset += 12 + length
|
||||||
|
}
|
||||||
|
return chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
const pngOf = (chunks: Chunk[]): Uint8Array =>
|
||||||
|
concat(
|
||||||
|
Uint8Array.from(PNG_SIGNATURE),
|
||||||
|
...chunks.map((c) => pngChunk(c.type, c.data)),
|
||||||
|
)
|
||||||
|
|
||||||
|
function headerLogoBytes(): Uint8Array {
|
||||||
|
const asset = logosBranding().headerLogo
|
||||||
|
if (asset?.kind !== 'png') throw new Error('png expected')
|
||||||
|
return Uint8Array.from(Buffer.from(asset.base64, 'base64'))
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The real header logo with the last 5 bytes of its IDAT stream removed: the chunk list stays sound. */
|
||||||
|
function cutLogoPng(): Uint8Array {
|
||||||
|
const chunks = chunksOf(headerLogoBytes())
|
||||||
|
const last = chunks.filter((c) => c.type === 'IDAT').at(-1)!
|
||||||
|
last.data = last.data.subarray(0, last.data.byteLength - 5)
|
||||||
|
return pngOf(chunks)
|
||||||
|
}
|
||||||
|
|
||||||
describe('PNG budget (MEDIUM)', () => {
|
describe('PNG budget (MEDIUM)', () => {
|
||||||
it.each([
|
it.each([
|
||||||
['8193 × 1 → dimensions', 8193, 1, 'dimensions'],
|
['8193 × 1', 8193, 1],
|
||||||
['1 × 8193 → dimensions', 1, 8193, 'dimensions'],
|
['1 × 8193', 1, 8193],
|
||||||
['4097 × 4096 (over 4096²) → dimensions', 4097, 4096, 'dimensions'],
|
['4097 × 4096 (over 4096²)', 4097, 4096],
|
||||||
['12000 × 12000 (144 Mpx) → dimensions', 12_000, 12_000, 'dimensions'],
|
['12000 × 12000 (144 Mpx)', 12_000, 12_000],
|
||||||
['4096 × 4096 → accepted', 4096, 4096, null],
|
['0 × 0', 0, 0],
|
||||||
['8192 × 2048 → accepted', 8192, 2048, null],
|
['1 × 0', 1, 0],
|
||||||
])('%s', (_name, width, height, issue) => {
|
['8192 × 0', 8192, 0],
|
||||||
expect(pngHeaderIssue(concat(pngHeader(width, height), IEND))).toBe(issue)
|
])('%s → dimensions', async (_name, width, height) => {
|
||||||
|
const png = concat(pngHeader(width, height), IEND)
|
||||||
|
expect(await pngIssue(png)).toBe('dimensions')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('a wrong signature or a truncated header is refused; the real logos pass', () => {
|
it('the raw-size budget: 16 MiB of scanlines at most, whatever the pixel count', async () => {
|
||||||
expect(pngHeaderIssue(pngHeader(1, 1, false))).toBe('signature')
|
// 1-bit grey: 513 bytes a row.
|
||||||
expect(pngHeaderIssue(pngHeader(1, 1).subarray(0, 20))).toBe('signature')
|
expect(await pngIssue(validPng(4096, 4096, { depth: 1 }))).toBeNull()
|
||||||
expect(pngHeaderIssue(new TextEncoder().encode('<svg/>'))).toBe('signature')
|
expect(await pngIssue(validPng(8192, 2048, { depth: 1 }))).toBeNull()
|
||||||
expect(pngHeaderIssue(ONE_PX_PNG)).toBeNull()
|
// 8-bit grey: (4095 + 1) × 4096 is exactly 16 MiB, one more row byte is over.
|
||||||
expect(pngHeaderIssue(TWO_BY_ONE_PNG)).toBeNull()
|
expect(await pngIssue(validPng(4095, 4096))).toBeNull()
|
||||||
|
const overByARow = concat(pngHeader(4096, 4096), IEND)
|
||||||
|
expect(await pngIssue(overByARow)).toBe('dimensions')
|
||||||
|
// 8-bit RGBA at 4096²: 67 MB of scanlines, refused before any inflation.
|
||||||
|
const rgba = concat(pngHeader(4096, 4096, { colourType: 6 }), IEND)
|
||||||
|
expect(await pngIssue(rgba)).toBe('dimensions')
|
||||||
|
expect(rawSize(4096, 4096, { colourType: 6 })).toBeGreaterThan(
|
||||||
|
16 * 1024 * 1024,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
['depth 255', { depth: 255 }],
|
||||||
|
['colour type 5', { colourType: 5 }],
|
||||||
|
['interlace 1 (Adam7)', { interlace: 1 }],
|
||||||
|
['filter method 1', { filter: 1 }],
|
||||||
|
['compression method 1', { compression: 1 }],
|
||||||
|
['depth 4 for RGBA', { depth: 4, colourType: 6 }],
|
||||||
|
['depth 16 for a palette', { depth: 16, colourType: 3 }],
|
||||||
|
])('%s → ihdr', async (_name, fields: IhdrFields) => {
|
||||||
|
expect(await pngIssue(validPng(4, 3, fields))).toBe('ihdr')
|
||||||
|
const atTheCap = concat(pngHeader(4096, 4096, fields), IEND)
|
||||||
|
expect(await pngIssue(atTheCap)).toBe('ihdr')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('every colour type and depth the spec allows passes with a matching IDAT', async () => {
|
||||||
|
const allowed: Array<[number, number[]]> = [
|
||||||
|
[0, [1, 2, 4, 8, 16]],
|
||||||
|
[2, [8, 16]],
|
||||||
|
[3, [1, 2, 4, 8]],
|
||||||
|
[4, [8, 16]],
|
||||||
|
[6, [8, 16]],
|
||||||
|
]
|
||||||
|
for (const [colourType, depths] of allowed) {
|
||||||
|
for (const depth of depths) {
|
||||||
|
const png = validPng(5, 3, { depth, colourType })
|
||||||
|
expect(
|
||||||
|
await pngIssue(png),
|
||||||
|
`type ${colourType} depth ${depth}`,
|
||||||
|
).toBeNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('an IDAT stream that is truncated, empty, not zlib or off by one raw byte → idat', async () => {
|
||||||
|
const poc = truncatedIdatPng()
|
||||||
|
expect(poc.byteLength).toBe(77)
|
||||||
|
expect(await pngIssue(poc)).toBe('idat')
|
||||||
|
expect(await pngIssue(cutLogoPng())).toBe('idat')
|
||||||
|
expect(await pngIssue(concat(pngHeader(1, 1), IEND))).toBe('idat')
|
||||||
|
const notZlib = pngChunk('IDAT', new Uint8Array(16))
|
||||||
|
expect(await pngIssue(concat(pngHeader(1, 1), notZlib, IEND))).toBe('idat')
|
||||||
|
for (const delta of [1, -1]) {
|
||||||
|
const raw = new Uint8Array(rawSize(4, 3) + delta)
|
||||||
|
const png = concat(pngHeader(4, 3), idatOf(raw), IEND)
|
||||||
|
expect(await pngIssue(png), `off by ${delta}`).toBe('idat')
|
||||||
|
}
|
||||||
|
expect(await pngIssue(validPng(4, 3))).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('the IDAT stream may span several chunks', async () => {
|
||||||
|
const stream = deflateSync(new Uint8Array(rawSize(4, 3)))
|
||||||
|
const split = Math.floor(stream.byteLength / 2)
|
||||||
|
const png = concat(
|
||||||
|
pngHeader(4, 3),
|
||||||
|
pngChunk('IDAT', stream.subarray(0, split)),
|
||||||
|
pngChunk('IDAT', stream.subarray(split)),
|
||||||
|
IEND,
|
||||||
|
)
|
||||||
|
expect(await pngIssue(png)).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a wrong signature or a truncated header is refused; the real logos pass', async () => {
|
||||||
|
expect(await pngIssue(pngHeader(1, 1, {}, false))).toBe('signature')
|
||||||
|
expect(await pngIssue(pngHeader(1, 1).subarray(0, 20))).toBe('signature')
|
||||||
|
const svg = new TextEncoder().encode('<svg/>')
|
||||||
|
expect(await pngIssue(svg)).toBe('signature')
|
||||||
|
expect(await pngIssue(ONE_PX_PNG)).toBeNull()
|
||||||
|
expect(await pngIssue(TWO_BY_ONE_PNG)).toBeNull()
|
||||||
const logos = logosBranding()
|
const logos = logosBranding()
|
||||||
for (const asset of [
|
for (const asset of [
|
||||||
logos.headerLogo,
|
logos.headerLogo,
|
||||||
@@ -663,42 +843,42 @@ describe('PNG budget (MEDIUM)', () => {
|
|||||||
logos.poweredBy!.logo,
|
logos.poweredBy!.logo,
|
||||||
]) {
|
]) {
|
||||||
if (asset?.kind !== 'png') throw new Error('png expected')
|
if (asset?.kind !== 'png') throw new Error('png expected')
|
||||||
expect(pngHeaderIssue(Buffer.from(asset.base64, 'base64'))).toBeNull()
|
expect(await pngIssue(Buffer.from(asset.base64, 'base64'))).toBeNull()
|
||||||
}
|
}
|
||||||
const touchIcon = readFileSync(resolve(PUBLIC_DIR, 'apple-touch-icon.png'))
|
const touchIcon = readFileSync(resolve(PUBLIC_DIR, 'apple-touch-icon.png'))
|
||||||
expect(pngHeaderIssue(new Uint8Array(touchIcon))).toBeNull()
|
expect(await pngIssue(new Uint8Array(touchIcon))).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('the chunk walk refuses a second IHDR, an APNG acTL and a length past the buffer', () => {
|
it('the chunk walk refuses a second IHDR, an APNG acTL and a length past the buffer', async () => {
|
||||||
expect(pngHeaderIssue(doubledIhdrPng())).toBe('chunks')
|
expect(await pngIssue(doubledIhdrPng())).toBe('chunks')
|
||||||
const apng = concat(
|
const apng = concat(
|
||||||
pngHeader(1, 1),
|
pngHeader(1, 1),
|
||||||
pngChunk('acTL', new Uint8Array(8)),
|
pngChunk('acTL', new Uint8Array(8)),
|
||||||
IEND,
|
IEND,
|
||||||
)
|
)
|
||||||
expect(pngHeaderIssue(apng)).toBe('chunks')
|
expect(await pngIssue(apng)).toBe('chunks')
|
||||||
for (const type of ['fcTL', 'fdAT']) {
|
for (const type of ['fcTL', 'fdAT']) {
|
||||||
const frame = concat(
|
const frame = concat(
|
||||||
pngHeader(1, 1),
|
pngHeader(1, 1),
|
||||||
pngChunk(type, new Uint8Array(4)),
|
pngChunk(type, new Uint8Array(4)),
|
||||||
IEND,
|
IEND,
|
||||||
)
|
)
|
||||||
expect(pngHeaderIssue(frame), type).toBe('chunks')
|
expect(await pngIssue(frame), type).toBe('chunks')
|
||||||
}
|
}
|
||||||
const idat = pngChunk('IDAT', new Uint8Array(16))
|
const idat = pngChunk('IDAT', new Uint8Array(16))
|
||||||
new DataView(idat.buffer).setUint32(0, 1_000_000)
|
new DataView(idat.buffer).setUint32(0, 1_000_000)
|
||||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), idat))).toBe('chunks')
|
expect(await pngIssue(concat(pngHeader(1, 1), idat))).toBe('chunks')
|
||||||
const partialHead = concat(pngHeader(1, 1), new Uint8Array(5))
|
const partialHead = concat(pngHeader(1, 1), new Uint8Array(5))
|
||||||
expect(pngHeaderIssue(partialHead)).toBe('chunks')
|
expect(await pngIssue(partialHead)).toBe('chunks')
|
||||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), IDAT, IEND))).toBeNull()
|
expect(await pngIssue(concat(pngHeader(1, 1), IDAT, IEND))).toBeNull()
|
||||||
const shortIhdr = pngHeader(1, 1)
|
const shortIhdr = pngHeader(1, 1)
|
||||||
new DataView(shortIhdr.buffer).setUint32(8, 12)
|
new DataView(shortIhdr.buffer).setUint32(8, 12)
|
||||||
expect(pngHeaderIssue(shortIhdr)).toBe('signature')
|
expect(await pngIssue(shortIhdr)).toBe('signature')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('a chunk UPNG reads with nextZero, or any type outside the allowlist, is refused', () => {
|
it('a chunk UPNG reads with nextZero, or any type outside the allowlist, is refused', async () => {
|
||||||
expect(pngHeaderIssue(textWithoutNulPng())).toBe('chunks')
|
expect(await pngIssue(textWithoutNulPng())).toBe('chunks')
|
||||||
expect(pngHeaderIssue(itxtThenFatIendPng())).toBe('chunks')
|
expect(await pngIssue(itxtThenFatIendPng())).toBe('chunks')
|
||||||
for (const type of ['tIME', 'zTXt', 'iCCP', 'abCD']) {
|
for (const type of ['tIME', 'zTXt', 'iCCP', 'abCD']) {
|
||||||
const png = concat(
|
const png = concat(
|
||||||
pngHeader(1, 1),
|
pngHeader(1, 1),
|
||||||
@@ -706,19 +886,18 @@ describe('PNG budget (MEDIUM)', () => {
|
|||||||
pngChunk(type, new Uint8Array(7)),
|
pngChunk(type, new Uint8Array(7)),
|
||||||
IEND,
|
IEND,
|
||||||
)
|
)
|
||||||
expect(pngHeaderIssue(png), type).toBe('chunks')
|
expect(await pngIssue(png), type).toBe('chunks')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('the list must end on an empty IEND that closes the buffer; ancillary chunks pass', () => {
|
it('the list must end on an empty IEND that closes the buffer; ancillary chunks pass', async () => {
|
||||||
const valid = concat(pngHeader(1, 1), IDAT, IEND)
|
const valid = concat(pngHeader(1, 1), IDAT, IEND)
|
||||||
expect(pngHeaderIssue(concat(valid, new Uint8Array([0])))).toBe('chunks')
|
expect(await pngIssue(concat(valid, new Uint8Array([0])))).toBe('chunks')
|
||||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), IDAT))).toBe('chunks')
|
expect(await pngIssue(concat(pngHeader(1, 1), IDAT))).toBe('chunks')
|
||||||
expect(
|
const fatIend = pngChunk('IEND', new Uint8Array(1))
|
||||||
pngHeaderIssue(
|
expect(await pngIssue(concat(pngHeader(1, 1), IDAT, fatIend))).toBe(
|
||||||
concat(pngHeader(1, 1), IDAT, pngChunk('IEND', new Uint8Array(1))),
|
'chunks',
|
||||||
),
|
)
|
||||||
).toBe('chunks')
|
|
||||||
const ancillary = concat(
|
const ancillary = concat(
|
||||||
pngHeader(1, 1),
|
pngHeader(1, 1),
|
||||||
pngChunk('gAMA', new Uint8Array(4)),
|
pngChunk('gAMA', new Uint8Array(4)),
|
||||||
@@ -727,13 +906,18 @@ describe('PNG budget (MEDIUM)', () => {
|
|||||||
IDAT,
|
IDAT,
|
||||||
IEND,
|
IEND,
|
||||||
)
|
)
|
||||||
expect(pngHeaderIssue(ancillary)).toBeNull()
|
expect(await pngIssue(ancillary)).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
['an oversized header logo', () => concat(pngHeader(16_384, 16_384), IEND)],
|
['an oversized header logo', () => concat(pngHeader(16_384, 16_384), IEND)],
|
||||||
['a doubled-IHDR header logo', doubledIhdrPng],
|
['a doubled-IHDR header logo', doubledIhdrPng],
|
||||||
['a tEXt-without-NUL header logo (UPNG would spin)', textWithoutNulPng],
|
['a tEXt-without-NUL header logo (UPNG would spin)', textWithoutNulPng],
|
||||||
|
[
|
||||||
|
'a truncated-IDAT header logo (UPNG inflateRaw would read past the buffer)',
|
||||||
|
truncatedIdatPng,
|
||||||
|
],
|
||||||
|
['a 1 × 0 header logo', () => concat(pngHeader(1, 0), IDAT, IEND)],
|
||||||
])(
|
])(
|
||||||
'%s fails alone with logo_failed and never reaches embedPng',
|
'%s fails alone with logo_failed and never reaches embedPng',
|
||||||
async (_name, make) => {
|
async (_name, make) => {
|
||||||
@@ -749,7 +933,8 @@ describe('PNG budget (MEDIUM)', () => {
|
|||||||
}
|
}
|
||||||
// The spy refuses the hostile bytes itself: a regression that lets
|
// The spy refuses the hostile bytes itself: a regression that lets
|
||||||
// them through fails on the assertion below instead of spinning the
|
// them through fails on the assertion below instead of spinning the
|
||||||
// worker in UPNG's nextZero, which no vitest timeout can interrupt.
|
// worker in UPNG's nextZero or inflateRaw, which no vitest timeout
|
||||||
|
// can interrupt.
|
||||||
const realEmbedPng = PDFDocument.prototype.embedPng
|
const realEmbedPng = PDFDocument.prototype.embedPng
|
||||||
const embedPng = vi
|
const embedPng = vi
|
||||||
.spyOn(PDFDocument.prototype, 'embedPng')
|
.spyOn(PDFDocument.prototype, 'embedPng')
|
||||||
@@ -777,4 +962,26 @@ describe('PNG budget (MEDIUM)', () => {
|
|||||||
},
|
},
|
||||||
10_000,
|
10_000,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it('the real logos reach embedPng when the gate passes them', async () => {
|
||||||
|
const embedPng = vi.spyOn(PDFDocument.prototype, 'embedPng')
|
||||||
|
try {
|
||||||
|
const { warnings } = await exportReportArchive({
|
||||||
|
...baseInput(golden.reports.demo, golden.snapshots.demo),
|
||||||
|
branding: logosBranding(),
|
||||||
|
rasterizer: rasterizerOf('logos'),
|
||||||
|
})
|
||||||
|
expect(warnings).toEqual([])
|
||||||
|
const embedded = embedPng.mock.calls.map(([png]) =>
|
||||||
|
Buffer.from(png as Uint8Array).toString('base64'),
|
||||||
|
)
|
||||||
|
const logos = logosBranding()
|
||||||
|
for (const asset of [logos.headerLogo, logos.footerLogo]) {
|
||||||
|
if (asset?.kind !== 'png') throw new Error('png expected')
|
||||||
|
expect(embedded).toContain(asset.base64)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
embedPng.mockRestore()
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user