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:
1 parent
6adc38b027
commit
3a6e92625a
6 files changed
+310
-32
No files matched your search
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* The capped inflater: round trips, the exact limit, every failure mode as
|
||||
* null, the bomb cancelled early, and the compressed bytes fed to the
|
||||
* platform inflater in slices (the Chromium read-ahead bound; Node cannot
|
||||
* show the memory effect, only the feeding shape).
|
||||
*/
|
||||
import { deflateSync } from 'node:zlib'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { inflateFlate, MAX_ATTACHMENT_BYTES } from '../src/attachment/inflate'
|
||||
|
||||
const KiB = 1024
|
||||
const MiB = 1024 * KiB
|
||||
|
||||
/** Incompressible bytes: the compressed stream is about as long as the input. */
|
||||
function noise(length: number): Uint8Array {
|
||||
let seed = 0x9e3779b9
|
||||
return Uint8Array.from({ length }, () => {
|
||||
seed = (seed * 1664525 + 1013904223) >>> 0
|
||||
return seed >>> 24
|
||||
})
|
||||
}
|
||||
|
||||
const deflate = (data: Uint8Array) => new Uint8Array(deflateSync(data))
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('inflateFlate', () => {
|
||||
it('round-trips a payload spanning several slices', async () => {
|
||||
const payload = noise(200 * KiB)
|
||||
const compressed = deflate(payload)
|
||||
expect(compressed.byteLength).toBeGreaterThan(10 * 16 * KiB)
|
||||
expect(await inflateFlate(compressed)).toEqual(payload)
|
||||
})
|
||||
|
||||
it('feeds the inflater in 16 KiB slices, each awaited', async () => {
|
||||
const compressed = deflate(noise(100 * KiB))
|
||||
const write = vi.spyOn(WritableStreamDefaultWriter.prototype, 'write')
|
||||
await inflateFlate(compressed)
|
||||
const slices = Math.ceil(compressed.byteLength / (16 * KiB))
|
||||
expect(write).toHaveBeenCalledTimes(slices)
|
||||
const sizes = write.mock.calls.map((c) => (c[0] as Uint8Array).byteLength)
|
||||
expect(Math.max(...sizes)).toBe(16 * KiB)
|
||||
})
|
||||
|
||||
it('an output exactly at the limit passes, one byte more is null', async () => {
|
||||
const payload = noise(64 * KiB)
|
||||
const compressed = deflate(payload)
|
||||
expect(await inflateFlate(compressed, payload.byteLength)).toEqual(payload)
|
||||
expect(await inflateFlate(compressed, payload.byteLength - 1)).toBeNull()
|
||||
})
|
||||
|
||||
it.each([
|
||||
['empty input', new Uint8Array(0)],
|
||||
['truncated stream', deflate(noise(4 * KiB)).subarray(0, 2 * KiB)],
|
||||
['trailing junk', Uint8Array.from([...deflate(noise(KiB)), 1, 2, 3])],
|
||||
[
|
||||
'corrupt body',
|
||||
Uint8Array.from(deflate(noise(KiB)), (b, i) => (i > 8 ? b ^ 0xff : b)),
|
||||
],
|
||||
['not deflate at all', new TextEncoder().encode('{"version":3}')],
|
||||
])('%s: null', async (_name, bytes) => {
|
||||
expect(await inflateFlate(bytes)).toBeNull()
|
||||
})
|
||||
|
||||
it('a 64 MiB bomb past the default cap is cancelled in well under a second', async () => {
|
||||
const bomb = deflate(new Uint8Array(64 * MiB))
|
||||
expect(bomb.byteLength).toBeLessThan(MiB)
|
||||
const started = performance.now()
|
||||
expect(await inflateFlate(bomb)).toBeNull()
|
||||
const elapsed = performance.now() - started
|
||||
console.log(`[inflate] 64 MiB bomb: ${elapsed.toFixed(0)} ms`)
|
||||
expect(elapsed).toBeLessThan(1000)
|
||||
expect(MAX_ATTACHMENT_BYTES).toBe(16 * MiB)
|
||||
})
|
||||
})
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user