fix(pdf): candidates sorted by data offset, JSON depth pre-scan before parse, budget wording; chore(memory): BLK-009 mitigated with gate, EVAL-014, journal, TODO, CLAUDE.md import notes

This commit is contained in:
bchanot committed 2026-10-11 17:31:07 +02:00
1 parent 3a6e92625a
commit 62b2b9d86a
9 files changed
+188 -30

No files matched your search

+83 -2
View File
@@ -3,7 +3,8 @@
* on the frozen fixtures (object-stream layout), crafted layouts (two files,
* indirect `/Length`, `\r\n`, no space after `/Type`), garbage, the keyword
* stamp, the object-stream bomb the reader never inflates, and the candidate
* cost bounds (monotonic `endstream` search, shared 16 MiB budget, `{` check).
* cost bounds (monotonic `endstream` search over offset-sorted candidates,
* shared 16 MiB budget, `{` check, bracket-depth pre-scan before JSON.parse).
*/
import { deflateSync } from 'node:zlib'
import { beforeAll, describe, expect, it, vi } from 'vitest'
@@ -19,7 +20,11 @@ import {
type EmbeddedFileCandidate,
} from '../src'
import { inflateFlate } from '../src/attachment/inflate'
import { decodePdfString, hasArchiveStamp } from '../src/attachment/scan'
import {
decodePdfString,
hasArchiveStamp,
locateAttachment,
} from '../src/attachment/scan'
import { readFixtureBytes } from './helpers/draft'
import {
FIXED_EXPORTED_AT,
@@ -449,6 +454,25 @@ describe('candidate cost bounds (audit M2)', () => {
expect(elapsed).toBeLessThan(1000)
})
it('32 nested EmbeddedFile dicts (descending offsets) without endstream on a 20 MiB file: no candidate, no finder reset', async () => {
// Outermost `/Type` first in the file, its `stream` last: offsets come out
// reversed; the sort by offset keeps the endstream search to one pass.
let nested: Uint8Array = ascii('<< /Type /EmbeddedFile >>\nstream\n')
for (let level = 1; level < 32; level += 1) {
nested = concat([
ascii('<< /Type /EmbeddedFile /A '),
nested,
ascii(' >>\nstream\n'),
])
}
const pdf = craftPdf([ascii('2 0 obj\n'), nested, filled('x', 20 * MiB)])
const { result, elapsed } = await timed('32 nested, no endstream', () =>
findEmbeddedFiles(pdf),
)
expect(result).toEqual([])
expect(elapsed).toBeLessThan(500)
})
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(
@@ -501,6 +525,63 @@ describe('candidate cost bounds (audit M2)', () => {
}
})
describe('bracket-depth pre-scan before JSON.parse (audit finding 3)', () => {
const flateObject = (num: number, raw: Uint8Array) => {
const data = new Uint8Array(deflateSync(raw))
return streamObject({
num,
dict: `/Type /EmbeddedFile /Filter /FlateDecode /Length ${data.byteLength}`,
data,
})
}
const withSpy = async (run: () => Promise<void>) => {
const parse = vi.spyOn(JSON, 'parse')
try {
await run()
return parse.mock.calls.length
} finally {
parse.mockRestore()
}
}
it('a 16 KB Flate candidate opening 8 million "[" deep: skipped unparsed, no_attachment fast', async () => {
const raw = concat([ascii('{"version":3,"x":'), filled('[', 8_000_000)])
const object = flateObject(2, raw)
expect(object.byteLength).toBeLessThan(16 * 1024)
const calls = await withSpy(async () => {
const { elapsed } = await timed('8M-deep candidate', () =>
rejects(craftPdf([object]), 'no_attachment'),
)
expect(elapsed).toBeLessThan(100)
})
expect(calls).toBe(0)
})
it('2000 "[" inside a string with escaped quotes and backslashes: not rejected, parsed once', async () => {
const text = `{"version":3,"note":"\\"${'['.repeat(2000)}\\\\ \\" [[[["}`
const pdf = craftPdf([
streamObject({
num: 2,
dict: `/Type /EmbeddedFile /Length ${text.length}`,
data: ascii(text),
}),
])
const calls = await withSpy(async () => {
const located = await locateAttachment(pdf)
expect(located.data).toMatchObject({ version: 3 })
})
expect(calls).toBe(1)
})
it('the real envelope passes the pre-scan and is parsed once', async () => {
const calls = await withSpy(async () => {
const located = await locateAttachment(exported)
expect(located.data).toMatchObject({ envelopeVersion: 1 })
})
expect(calls).toBe(1)
})
})
describe('one 16 MiB budget shared by every candidate', () => {
const garbageObject = (num: number) =>
streamObject({