fix(pdf): PNG chunk allowlist and mandatory empty IEND — text and profile chunks refused (UPNG nextZero spin)
This commit is contained in:
1 parent
3a26c61119
commit
0ceb357b18
3 files changed
+129
-34
No files matched your search
@@ -611,10 +611,31 @@ function pngChunk(type: string, data: Uint8Array): Uint8Array {
|
||||
const concat = (...parts: Uint8Array[]): Uint8Array =>
|
||||
Uint8Array.from(Buffer.concat(parts.map((p) => Buffer.from(p))))
|
||||
|
||||
const IEND = pngChunk('IEND', new Uint8Array(0))
|
||||
const IDAT = pngChunk('IDAT', new Uint8Array(16))
|
||||
|
||||
/** A 1×1 header followed by a second IHDR announcing 10000 × 10000, then IEND. */
|
||||
function doubledIhdrPng(): Uint8Array {
|
||||
const second = pngHeader(10_000, 10_000).subarray(8)
|
||||
return concat(pngHeader(1, 1), second, pngChunk('IEND', new Uint8Array(0)))
|
||||
return concat(pngHeader(1, 1), second, IEND)
|
||||
}
|
||||
|
||||
/**
|
||||
* The auditor's PoC: a valid 1×1 PNG whose IEND is replaced by a `tEXt`
|
||||
* chunk "AAAA" with a non-zero CRC and no NUL byte, so UPNG's `nextZero`
|
||||
* never stops (`ONE_PX_PNG` minus its 12-byte IEND).
|
||||
*/
|
||||
function textWithoutNulPng(): Uint8Array {
|
||||
const text = pngChunk('tEXt', new TextEncoder().encode('AAAA'))
|
||||
text.fill(0xff, text.byteLength - 4)
|
||||
return concat(ONE_PX_PNG.subarray(0, ONE_PX_PNG.byteLength - 12), text)
|
||||
}
|
||||
|
||||
/** The second PoC: an `iTXt` chunk, then an IEND of length 0x010101 with a non-zero payload. */
|
||||
function itxtThenFatIendPng(): Uint8Array {
|
||||
const itxt = pngChunk('iTXt', new TextEncoder().encode('kw'))
|
||||
const fatIend = pngChunk('IEND', new Uint8Array(0x010101).fill(1))
|
||||
return concat(pngHeader(1, 1), IDAT, itxt, fatIend)
|
||||
}
|
||||
|
||||
describe('PNG budget (MEDIUM)', () => {
|
||||
@@ -626,7 +647,7 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
['4096 × 4096 → accepted', 4096, 4096, null],
|
||||
['8192 × 2048 → accepted', 8192, 2048, null],
|
||||
])('%s', (_name, width, height, issue) => {
|
||||
expect(pngHeaderIssue(pngHeader(width, height))).toBe(issue)
|
||||
expect(pngHeaderIssue(concat(pngHeader(width, height), IEND))).toBe(issue)
|
||||
})
|
||||
|
||||
it('a wrong signature or a truncated header is refused; the real logos pass', () => {
|
||||
@@ -653,11 +674,15 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
const apng = concat(
|
||||
pngHeader(1, 1),
|
||||
pngChunk('acTL', new Uint8Array(8)),
|
||||
pngChunk('IEND', new Uint8Array(0)),
|
||||
IEND,
|
||||
)
|
||||
expect(pngHeaderIssue(apng)).toBe('chunks')
|
||||
for (const type of ['fcTL', 'fdAT']) {
|
||||
const frame = concat(pngHeader(1, 1), pngChunk(type, new Uint8Array(4)))
|
||||
const frame = concat(
|
||||
pngHeader(1, 1),
|
||||
pngChunk(type, new Uint8Array(4)),
|
||||
IEND,
|
||||
)
|
||||
expect(pngHeaderIssue(frame), type).toBe('chunks')
|
||||
}
|
||||
const idat = pngChunk('IDAT', new Uint8Array(16))
|
||||
@@ -665,20 +690,50 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), idat))).toBe('chunks')
|
||||
const partialHead = concat(pngHeader(1, 1), new Uint8Array(5))
|
||||
expect(pngHeaderIssue(partialHead)).toBe('chunks')
|
||||
const wellFormed = concat(
|
||||
pngHeader(1, 1),
|
||||
pngChunk('IDAT', new Uint8Array(16)),
|
||||
pngChunk('IEND', new Uint8Array(0)),
|
||||
)
|
||||
expect(pngHeaderIssue(wellFormed)).toBeNull()
|
||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), IDAT, IEND))).toBeNull()
|
||||
const shortIhdr = pngHeader(1, 1)
|
||||
new DataView(shortIhdr.buffer).setUint32(8, 12)
|
||||
expect(pngHeaderIssue(shortIhdr)).toBe('signature')
|
||||
})
|
||||
|
||||
it('a chunk UPNG reads with nextZero, or any type outside the allowlist, is refused', () => {
|
||||
expect(pngHeaderIssue(textWithoutNulPng())).toBe('chunks')
|
||||
expect(pngHeaderIssue(itxtThenFatIendPng())).toBe('chunks')
|
||||
for (const type of ['tIME', 'zTXt', 'iCCP', 'abCD']) {
|
||||
const png = concat(
|
||||
pngHeader(1, 1),
|
||||
IDAT,
|
||||
pngChunk(type, new Uint8Array(7)),
|
||||
IEND,
|
||||
)
|
||||
expect(pngHeaderIssue(png), type).toBe('chunks')
|
||||
}
|
||||
})
|
||||
|
||||
it('the list must end on an empty IEND that closes the buffer; ancillary chunks pass', () => {
|
||||
const valid = concat(pngHeader(1, 1), IDAT, IEND)
|
||||
expect(pngHeaderIssue(concat(valid, new Uint8Array([0])))).toBe('chunks')
|
||||
expect(pngHeaderIssue(concat(pngHeader(1, 1), IDAT))).toBe('chunks')
|
||||
expect(
|
||||
pngHeaderIssue(
|
||||
concat(pngHeader(1, 1), IDAT, pngChunk('IEND', new Uint8Array(1))),
|
||||
),
|
||||
).toBe('chunks')
|
||||
const ancillary = concat(
|
||||
pngHeader(1, 1),
|
||||
pngChunk('gAMA', new Uint8Array(4)),
|
||||
pngChunk('pHYs', new Uint8Array(9)),
|
||||
pngChunk('sRGB', new Uint8Array(1)),
|
||||
IDAT,
|
||||
IEND,
|
||||
)
|
||||
expect(pngHeaderIssue(ancillary)).toBeNull()
|
||||
})
|
||||
|
||||
it.each([
|
||||
['an oversized header logo', () => pngHeader(16_384, 16_384)],
|
||||
['an oversized header logo', () => concat(pngHeader(16_384, 16_384), IEND)],
|
||||
['a doubled-IHDR header logo', doubledIhdrPng],
|
||||
['a tEXt-without-NUL header logo (UPNG would spin)', textWithoutNulPng],
|
||||
])(
|
||||
'%s fails alone with logo_failed and never reaches embedPng',
|
||||
async (_name, make) => {
|
||||
@@ -692,7 +747,18 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
kind: 'png',
|
||||
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
|
||||
}
|
||||
const embedPng = vi.spyOn(PDFDocument.prototype, 'embedPng')
|
||||
// The spy refuses the hostile bytes itself: a regression that lets
|
||||
// them through fails on the assertion below instead of spinning the
|
||||
// worker in UPNG's nextZero, which no vitest timeout can interrupt.
|
||||
const realEmbedPng = PDFDocument.prototype.embedPng
|
||||
const embedPng = vi
|
||||
.spyOn(PDFDocument.prototype, 'embedPng')
|
||||
.mockImplementation(function (this: PDFDocument, png) {
|
||||
if (Buffer.from(png as Uint8Array).equals(Buffer.from(huge))) {
|
||||
throw new Error('hostile png reached embedPng')
|
||||
}
|
||||
return realEmbedPng.call(this, png)
|
||||
})
|
||||
try {
|
||||
const { warnings } = await exportReportArchive({
|
||||
...baseInput(golden.reports.demo, golden.snapshots.demo),
|
||||
@@ -709,5 +775,6 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
embedPng.mockRestore()
|
||||
}
|
||||
},
|
||||
10_000,
|
||||
)
|
||||
})
|
||||
Reference in new issue
Block a user