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:
1 parent
dfd04e0bd7
commit
3a26c61119
4 files changed
+215
-49
No files matched your search
@@ -482,15 +482,55 @@ function reexportOptions() {
|
||||
}
|
||||
}
|
||||
|
||||
/** The same payload without spaces: PDF syntax needs none, and the sink lets it through to the hex string. */
|
||||
const TIGHT_INJECTION_URL = INJECTION_URL.replaceAll(' ', '')
|
||||
|
||||
/** The saved bytes of a one-page PDF holding one link to `url`, objects uncompressed so the bytes can be read. */
|
||||
async function savedLink(url: string): Promise<Uint8Array> {
|
||||
const doc = await PDFDocument.create()
|
||||
addLinkAnnotation(doc.addPage(), { x: 0, y: 0, width: 10, height: 10, url })
|
||||
return doc.save({ useObjectStreams: false })
|
||||
}
|
||||
|
||||
/** `<hex>` of the URL's ASCII bytes, as the sink must write `/URI`. */
|
||||
const asciiHex = (url: string): string =>
|
||||
Array.from(url, (c) => c.charCodeAt(0).toString(16).padStart(2, '0')).join('')
|
||||
|
||||
describe('branding URL injection (HIGH)', () => {
|
||||
it('writes /URI as the hex of the ASCII bytes, no UTF-16 BOM, and reloads to the URL', async () => {
|
||||
const url = 'https://faunosteo.com'
|
||||
const bytes = await savedLink(url)
|
||||
const raw = Buffer.from(bytes).toString('latin1')
|
||||
const uriMatch = /\/URI\s*<([0-9A-Fa-f]+)>/.exec(raw)
|
||||
expect(uriMatch?.[1]?.toLowerCase()).toBe(asciiHex(url))
|
||||
expect(uriMatch?.[1]?.toUpperCase().startsWith('FEFF')).toBe(false)
|
||||
expect(Buffer.from(uriMatch![1]!, 'hex').toString('latin1')).toBe(url)
|
||||
const [link] = await readLinks(bytes)
|
||||
expect(link?.uri).toBe(url)
|
||||
})
|
||||
|
||||
it('a URL with a code point above 0x7e or below 0x21 throws branding_invalid at the sink', async () => {
|
||||
for (const url of [
|
||||
'https://faunosté.com',
|
||||
'https://a.example/ b',
|
||||
INJECTION_URL,
|
||||
'https://a.example/\u00ff',
|
||||
]) {
|
||||
await expect(savedLink(url), url).rejects.toMatchObject({
|
||||
kind: 'branding_invalid',
|
||||
})
|
||||
await expect(savedLink(url), url).rejects.toBeInstanceOf(
|
||||
ArchiveExportError,
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('the link sink alone keeps the whole URL inside the /URI string', async () => {
|
||||
const doc = await PDFDocument.create()
|
||||
const rect = { x: 0, y: 0, width: 10, height: 10, url: INJECTION_URL }
|
||||
addLinkAnnotation(doc.addPage(), rect)
|
||||
const bytes = await doc.save()
|
||||
const bytes = await savedLink(TIGHT_INJECTION_URL)
|
||||
const raw = Buffer.from(bytes).toString('latin1')
|
||||
expect(raw).not.toContain('/JavaScript')
|
||||
expect(raw).not.toContain('/Next')
|
||||
expect(raw).not.toContain('/JS')
|
||||
const reloaded = await PDFDocument.load(bytes)
|
||||
expect(javaScriptActions(reloaded)).toEqual([])
|
||||
const action = reloaded
|
||||
@@ -501,7 +541,7 @@ describe('branding URL injection (HIGH)', () => {
|
||||
expect(action.has(PDFName.of('Next'))).toBe(false)
|
||||
expect(action.get(PDFName.of('S'))).toBe(PDFName.of('URI'))
|
||||
const [link] = await readLinks(bytes)
|
||||
expect(link?.uri).toBe(INJECTION_URL)
|
||||
expect(link?.uri).toBe(TIGHT_INJECTION_URL)
|
||||
})
|
||||
|
||||
it('exportFromEnvelope refuses the URL before drawing anything: branding_invalid', async () => {
|
||||
@@ -556,6 +596,27 @@ function pngHeader(
|
||||
return bytes
|
||||
}
|
||||
|
||||
/** One chunk: length, four-letter type, `data`, a zero CRC. */
|
||||
function pngChunk(type: string, data: Uint8Array): Uint8Array {
|
||||
const bytes = new Uint8Array(12 + data.byteLength)
|
||||
new DataView(bytes.buffer).setUint32(0, data.byteLength)
|
||||
bytes.set(
|
||||
Array.from(type, (c) => c.charCodeAt(0)),
|
||||
4,
|
||||
)
|
||||
bytes.set(data, 8)
|
||||
return bytes
|
||||
}
|
||||
|
||||
const concat = (...parts: Uint8Array[]): Uint8Array =>
|
||||
Uint8Array.from(Buffer.concat(parts.map((p) => Buffer.from(p))))
|
||||
|
||||
/** 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)))
|
||||
}
|
||||
|
||||
describe('PNG budget (MEDIUM)', () => {
|
||||
it.each([
|
||||
['8193 × 1 → dimensions', 8193, 1, 'dimensions'],
|
||||
@@ -583,33 +644,70 @@ describe('PNG budget (MEDIUM)', () => {
|
||||
if (asset?.kind !== 'png') throw new Error('png expected')
|
||||
expect(pngHeaderIssue(Buffer.from(asset.base64, 'base64'))).toBeNull()
|
||||
}
|
||||
const touchIcon = readFileSync(resolve(PUBLIC_DIR, 'apple-touch-icon.png'))
|
||||
expect(pngHeaderIssue(new Uint8Array(touchIcon))).toBeNull()
|
||||
})
|
||||
|
||||
it('an oversized header logo fails alone with logo_failed and never reaches embedPng', async () => {
|
||||
const branding = nologosBranding()
|
||||
const huge = pngHeader(16_384, 16_384)
|
||||
branding.headerLogo = {
|
||||
kind: 'png',
|
||||
base64: Buffer.from(huge).toString('base64'),
|
||||
}
|
||||
branding.footerLogo = {
|
||||
kind: 'png',
|
||||
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
|
||||
}
|
||||
const embedPng = vi.spyOn(PDFDocument.prototype, 'embedPng')
|
||||
try {
|
||||
const { warnings } = await exportReportArchive({
|
||||
...baseInput(golden.reports.demo, golden.snapshots.demo),
|
||||
branding,
|
||||
})
|
||||
expect(warnings).toEqual([{ kind: 'logo_failed', slot: 'headerLogo' }])
|
||||
const sizes = embedPng.mock.calls.map(
|
||||
([png]) => (png as Uint8Array).byteLength,
|
||||
)
|
||||
expect(sizes).not.toContain(huge.byteLength)
|
||||
expect(sizes).toContain(ONE_PX_PNG.byteLength)
|
||||
} finally {
|
||||
embedPng.mockRestore()
|
||||
it('the chunk walk refuses a second IHDR, an APNG acTL and a length past the buffer', () => {
|
||||
expect(pngHeaderIssue(doubledIhdrPng())).toBe('chunks')
|
||||
const apng = concat(
|
||||
pngHeader(1, 1),
|
||||
pngChunk('acTL', new Uint8Array(8)),
|
||||
pngChunk('IEND', new Uint8Array(0)),
|
||||
)
|
||||
expect(pngHeaderIssue(apng)).toBe('chunks')
|
||||
for (const type of ['fcTL', 'fdAT']) {
|
||||
const frame = concat(pngHeader(1, 1), pngChunk(type, new Uint8Array(4)))
|
||||
expect(pngHeaderIssue(frame), type).toBe('chunks')
|
||||
}
|
||||
const idat = pngChunk('IDAT', new Uint8Array(16))
|
||||
new DataView(idat.buffer).setUint32(0, 1_000_000)
|
||||
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()
|
||||
const shortIhdr = pngHeader(1, 1)
|
||||
new DataView(shortIhdr.buffer).setUint32(8, 12)
|
||||
expect(pngHeaderIssue(shortIhdr)).toBe('signature')
|
||||
})
|
||||
|
||||
it.each([
|
||||
['an oversized header logo', () => pngHeader(16_384, 16_384)],
|
||||
['a doubled-IHDR header logo', doubledIhdrPng],
|
||||
])(
|
||||
'%s fails alone with logo_failed and never reaches embedPng',
|
||||
async (_name, make) => {
|
||||
const branding = nologosBranding()
|
||||
const huge = make()
|
||||
branding.headerLogo = {
|
||||
kind: 'png',
|
||||
base64: Buffer.from(huge).toString('base64'),
|
||||
}
|
||||
branding.footerLogo = {
|
||||
kind: 'png',
|
||||
base64: Buffer.from(ONE_PX_PNG).toString('base64'),
|
||||
}
|
||||
const embedPng = vi.spyOn(PDFDocument.prototype, 'embedPng')
|
||||
try {
|
||||
const { warnings } = await exportReportArchive({
|
||||
...baseInput(golden.reports.demo, golden.snapshots.demo),
|
||||
branding,
|
||||
})
|
||||
expect(warnings).toEqual([{ kind: 'logo_failed', slot: 'headerLogo' }])
|
||||
const base64 = (png: Uint8Array) => Buffer.from(png).toString('base64')
|
||||
const embedded = embedPng.mock.calls.map(([png]) =>
|
||||
base64(png as Uint8Array),
|
||||
)
|
||||
expect(embedded).not.toContain(base64(huge))
|
||||
expect(embedded).toContain(base64(ONE_PX_PNG))
|
||||
} finally {
|
||||
embedPng.mockRestore()
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
Reference in new issue
Block a user