fix(pdf): inflater fed in 16 KiB slices (browser memory bound), shared 16 MiB candidate budget, monotonic bounded endstream search, JSON object pre-check

This commit is contained in:
bchanot committed 2026-10-11 17:08:56 +02:00
1 parent 6adc38b027
commit 3a6e92625a
6 files changed
+310 -32

No files matched your search

+104 -2
View File
@@ -2,10 +2,11 @@
* BLK-009: the byte-scan attachment reader. Candidates on a fresh export and
* on the frozen fixtures (object-stream layout), crafted layouts (two files,
* indirect `/Length`, `\r\n`, no space after `/Type`), garbage, the keyword
* stamp, and the object-stream bomb the reader never inflates.
* stamp, the object-stream bomb the reader never inflates, and the candidate
* cost bounds (monotonic `endstream` search, shared 16 MiB budget, `{` check).
*/
import { deflateSync } from 'node:zlib'
import { beforeAll, describe, expect, it } from 'vitest'
import { beforeAll, describe, expect, it, vi } from 'vitest'
import { buildSheetSnapshot, type Report } from '@faunosteo/domain'
import {
ArchiveReadError,
@@ -423,3 +424,104 @@ describe('object-stream bomb (BLK-009)', () => {
expect(elapsed).toBeLessThan(2000)
})
})
describe('candidate cost bounds (audit M2)', () => {
const MiB = 1024 * 1024
/** `n 0 obj << /Type /EmbeddedFile >> stream` with no `/Length`: the data runs to `endstream`. */
const openStream = (num: number) =>
ascii(`${num} 0 obj\n<< /Type /EmbeddedFile >>\nstream\n`)
const filled = (byte: string, length: number) => ascii(byte.repeat(length))
const timed = async <T>(label: string, run: () => Promise<T> | T) => {
const started = performance.now()
const result = await run()
const elapsed = performance.now() - started
console.log(`[cost] ${label}: ${elapsed.toFixed(0)} ms`)
return { result, elapsed }
}
it('32 candidates without any endstream on a 20 MiB file: no candidate, well under a second', async () => {
const heads = Array.from({ length: 32 }, (_, i) => openStream(i + 2))
const pdf = craftPdf([...heads, filled('x', 20 * MiB)])
const { result, elapsed } = await timed('32 × no endstream', () =>
findEmbeddedFiles(pdf),
)
expect(result).toEqual([])
expect(elapsed).toBeLessThan(1000)
})
it('several indirect /Length streams in a row: each measured to its own endstream', () => {
const payloads = ['{"version":3}', '{"version":4,"x":1}', '{"v":5}']
const pdf = craftPdf(
payloads.map((text, i) =>
streamObject({
num: i + 2,
dict: `/Type /EmbeddedFile /Length ${i + 20} 0 R`,
data: ascii(text),
}),
),
)
const lengths = findEmbeddedFiles(pdf).map((c) => c.length)
expect(lengths).toEqual(payloads.map((p) => p.length))
})
it('a measured stream running past the cap window is not a candidate, the next one is', () => {
const pdf = craftPdf([
openStream(2),
filled('x', 16 * MiB + 64),
ascii('\nendstream\nendobj\n'),
streamObject({
num: 3,
dict: '/Type /EmbeddedFile /Length 13',
data: ascii('{"version":3}'),
}),
])
const candidates = findEmbeddedFiles(pdf)
expect(candidates).toHaveLength(1)
expect(candidates[0]!.length).toBe(13)
})
it('32 raw candidates of 1 MiB of "[": no_attachment under a second, JSON.parse never called', async () => {
const objects = Array.from({ length: 32 }, (_, i) =>
streamObject({
num: i + 2,
dict: `/Type /EmbeddedFile /Length ${MiB}`,
data: filled('[', MiB),
}),
)
const pdf = craftPdf(objects)
const parse = vi.spyOn(JSON, 'parse')
try {
const { elapsed } = await timed('32 × 1 MiB "["', () =>
rejects(pdf, 'no_attachment'),
)
expect(elapsed).toBeLessThan(1000)
expect(parse).not.toHaveBeenCalled()
} finally {
parse.mockRestore()
}
})
describe('one 16 MiB budget shared by every candidate', () => {
const garbageObject = (num: number) =>
streamObject({
num,
dict: `/Type /EmbeddedFile /Length ${10 * MiB}`,
data: concat([ascii('{'), filled('x', 10 * MiB - 1)]),
})
const real = () => objectHolding(exported, '/Type /EmbeddedFile')
it('one 10 MiB garbage object before the attachment: 6 MiB left, the report is read', async () => {
const pdf = craftPdf([garbageObject(2), real()])
const outcome = await readReportArchive(pdf, map())
expect(outcome.report).toEqual(golden.reports.demo)
})
it('two 10 MiB garbage objects before the attachment: budget exhausted, no_attachment', async () => {
const pdf = craftPdf([garbageObject(2), garbageObject(3), real()])
const { elapsed } = await timed('2 × 10 MiB "{" + attachment', () =>
rejects(pdf, 'no_attachment'),
)
expect(elapsed).toBeLessThan(2000)
})
})
})