Files
faunosteo/packages/core-wasm/tests/pdf-import.budget.test.ts
T
bchanot 593893a7c2 fix(domain): OD-3 value count runs past the depth cap; the rescue-copy guard covers the archive text
Security gate finding (R4.2): prescan stopped counting once the depth exceeded 64, so a
16 MiB rescue copy nested 65 deep passed the value budget with 66 values and built its
792 MiB tree. The count now runs to the end (the depth verdict unchanged, the PDF probe
unchanged); the class is in the budget suite (48 MiB, refused) and pinned in cargo.
2026-10-18 16:28:32 +02:00

395 lines
13 KiB
TypeScript

/**
* R4.2e, OD-3 (plan §B.3, §B.8 W4): what the PDF import costs in the shipped module, one
* fresh instance per case (the worker of R4.3 is disposable too), on the legitimate archives
* and on the hostile classes of §B.3: a 16 MiB token-dense attachment, the same through
* the rescue copy (`draft.restore`), the 50 MiB file, the 16 MiB inflation, 32 oversized
* candidates, a 16 MiB document at the depth cap. Printed for RUST.md §10.3: outcome, wall
* time, linear memory growth of the instance (never shrinks, so "after" is the high-water
* mark) and the Node process RSS growth (the glue's copies and the result string).
*
* Loose assertions only: no case traps or exceeds the W4 linear budget once the OD-3 cap
* applies; the printed numbers are the record. `R4_BUDGET_ALL=1` also runs the slowest
* classes (the 50 MiB scan, the 32 inflations).
*/
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { deflateSync } from 'node:zlib'
import {
CoreRuntime,
type GlueModule,
type GlueSheetHandle,
} from '../src/index'
import { loadProofModule, nodeCoreSource } from '../src/node'
const ROOT = fileURLToPath(new URL('../../../', import.meta.url))
const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft')
const FIXTURES = join(ROOT, 'apps/app/tests/fixtures')
const MiB = 1024 * 1024
const ALL = process.env.R4_BUDGET_ALL === '1'
const read = (path: string) => readFileSync(path, 'utf8')
function draftBundle(): string {
const manifest = JSON.parse(read(join(SEED, 'assets.json'))) as Array<{
file: string
sha256: string
svg_asset_id: string
}>
const assets: Record<string, string> = {}
const assetHashes: Record<string, string> = {}
for (const asset of manifest) {
assets[asset.svg_asset_id] = read(join(SEED, 'assets', asset.file))
assetHashes[asset.svg_asset_id] = asset.sha256
}
return JSON.stringify({
status: 'draft',
ref: JSON.parse(read(join(SEED, 'sheet-ref.json'))),
document: JSON.parse(read(join(SEED, 'document.json'))),
migrationMap: JSON.parse(read(join(SEED, 'report-migration-map.json'))),
assets,
assetHashes,
})
}
const BUNDLE = draftBundle()
// ---------------------------------------------------------------------------
// Minimal PDFs the scanner reads (plan §B.1.2): every object in clear.
interface Stream {
bytes: Uint8Array
flate?: boolean
name?: string
}
const ascii = (text: string) => new TextEncoder().encode(text)
function concat(parts: Uint8Array[]): Uint8Array {
const out = new Uint8Array(parts.reduce((n, p) => n + p.byteLength, 0))
let at = 0
for (const part of parts) {
out.set(part, at)
at += part.byteLength
}
return out
}
/** A PDF holding `streams` as EmbeddedFile objects, Filespecs for the named ones, the stamp. */
function pdfWith(streams: Stream[], padding = 0): Uint8Array {
const parts: Uint8Array[] = [ascii('%PDF-1.7\n%\xE2\xE3\xCF\xD3\n')]
if (padding > 0) parts.push(ascii(`%${'x'.repeat(padding - 2)}\n`))
let n = 1
for (const stream of streams) {
const filter = stream.flate ? ' /Filter /FlateDecode' : ''
parts.push(
ascii(
`${n} 0 obj\n<< /Type /EmbeddedFile /Length ${stream.bytes.byteLength}${filter} >>\nstream\n`,
),
stream.bytes,
ascii('\nendstream\nendobj\n'),
)
if (stream.name) {
parts.push(
ascii(
`${n + 1} 0 obj\n<< /Type /Filespec /F (${stream.name}) /EF << /F ${n} 0 R >> >>\nendobj\n`,
),
)
}
n += 2
}
parts.push(
ascii(`${n} 0 obj\n<< /Keywords (faunosteo-archive/1) >>\nendobj\n%%EOF\n`),
)
return concat(parts)
}
const zeros = (bytes: number) => new Uint8Array(bytes)
const deflate = (bytes: Uint8Array) => new Uint8Array(deflateSync(bytes))
/** `{"version":3,"a":[1,1,…]}` of about `bytes` bytes: one value per two bytes. */
function denseNumbers(bytes: number): Uint8Array {
const head = '{"version":3,"a":['
const count = Math.floor((bytes - head.length - 2) / 2)
return ascii(`${head}${'1,'.repeat(count - 1)}1]}`)
}
/** `{"version":3,"a":["","",…]}`: one allocated string per three bytes. */
function denseStrings(bytes: number): Uint8Array {
const head = '{"version":3,"a":['
const count = Math.floor((bytes - head.length - 2) / 3)
return ascii(`${head}${'"",'.repeat(count - 1)}""]}`)
}
/** `{"version":3,"a":[[[…[1,1,…]…]]]}` nested to the depth cap, about `bytes` bytes. */
function deepArrays(bytes: number, depth: number): Uint8Array {
const head = `{"version":3,"a":${'['.repeat(depth - 1)}`
const tail = `${']'.repeat(depth - 1)}}`
const count = Math.floor((bytes - head.length - tail.length) / 2)
return ascii(`${head}${'1,'.repeat(count - 1)}1${tail}`)
}
// ---------------------------------------------------------------------------
// One fresh instance per case
interface Instance {
runtime: CoreRuntime
sheet: GlueSheetHandle
memory: () => number
}
async function freshInstance(): Promise<Instance> {
let memory: WebAssembly.Memory | undefined
let glue: GlueModule | undefined
const source = nodeCoreSource('pkg')
const runtime = new CoreRuntime({
compile: source.compile,
importGlue: async (n) => {
glue = await source.importGlue(n)
return {
...glue,
default: async (input: { module_or_path: WebAssembly.Module }) => {
const exports = (await glue!.default(input)) as {
memory?: WebAssembly.Memory
}
memory = exports.memory
return exports
},
} as GlueModule
},
})
if (!(await runtime.ready())) throw new Error('module failed to load')
const sheet = new glue!.SheetHandle(BUNDLE)
return { runtime, sheet, memory: () => memory?.buffer.byteLength ?? 0 }
}
interface Measure {
outcome: string
ms: number
linearMiB: number
rssMiB: number
}
function measure(instance: Instance, run: () => string): Measure {
const rss0 = process.memoryUsage().rss
const linear0 = instance.memory()
const started = performance.now()
let outcome: string
try {
const envelope = JSON.parse(run()) as { ok: boolean; kind?: string }
outcome = envelope.ok ? 'ok' : envelope.kind!
} catch (error) {
outcome = `trap: ${(error as Error).constructor.name}`
}
const ms = performance.now() - started
return {
outcome,
ms,
linearMiB: (instance.memory() - linear0) / MiB,
rssMiB: (process.memoryUsage().rss - rss0) / MiB,
}
}
interface Case {
name: string
slow?: boolean
build: () => Uint8Array
/** `pdf` through import_pdf (default), `copy` through draft.restore as a rescue copy. */
path?: 'pdf' | 'copy'
}
/** The demo's envelope with `report` and a fresh snapshot: what the export writes for it. */
function envelopePdf(instance: Instance, reportFile: string): Uint8Array {
const demo = JSON.parse(
instance.sheet.import_pdf(
new Uint8Array(readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.logos.pdf'))),
),
) as { value: { read: { archive: Record<string, unknown> } } }
const report = JSON.parse(read(join(FIXTURES, reportFile)))
const snapshot = JSON.parse(
instance.sheet.call('archive.snapshot', JSON.stringify({ report })),
) as { value: unknown }
const envelope = {
...demo.value.read.archive,
report,
sheetSnapshot: snapshot.value,
}
return pdfWith([
{
bytes: ascii(JSON.stringify(envelope, null, 2)),
name: 'osteo-report.json',
},
])
}
const CASES: Case[] = [
{
name: 'demo fr logos (R3 export, 237 KB)',
build: () =>
new Uint8Array(readFileSync(join(FIXTURES, 'pdf/r3/demo.fr.logos.pdf'))),
},
{
name: '612 leaves (O7 export)',
build: () =>
new Uint8Array(
readFileSync(
join(FIXTURES, 'pdf/r4/read/read.612-leaves-exported.pdf'),
),
),
},
{
name: 'values-1m (O8 hostile, 2 MB attachment)',
build: () =>
new Uint8Array(
readFileSync(join(FIXTURES, 'pdf/r4/tolerance/values-1m.pdf')),
),
},
{
name: '16 MiB dense numbers (8.4 M values)',
build: () =>
pdfWith([
{ bytes: denseNumbers(16 * MiB - 64), name: 'osteo-report.json' },
]),
},
{
name: '16 MiB dense strings (5.6 M values)',
build: () =>
pdfWith([
{ bytes: denseStrings(16 * MiB - 64), name: 'osteo-report.json' },
]),
},
{
name: '16 MiB at depth 64',
build: () =>
pdfWith([
{ bytes: deepArrays(16 * MiB - 64, 64), name: 'osteo-report.json' },
]),
},
{
name: '16 MiB at depth 65 as a rescue copy (security gate: counted past the depth cap)',
path: 'copy',
build: () => deepArrays(16 * MiB - 64, 65),
},
{
name: '16 MiB dense numbers as a rescue copy',
path: 'copy',
build: () => denseNumbers(16 * MiB - 64),
},
{
name: '16 MiB inflation (zeros, not JSON)',
build: () =>
pdfWith([
{
bytes: deflate(zeros(16 * MiB)),
flate: true,
name: 'osteo-report.json',
},
]),
},
{
name: '16 MiB + 1 inflation (bomb, refused)',
build: () =>
pdfWith([
{
bytes: deflate(zeros(16 * MiB + 1)),
flate: true,
name: 'osteo-report.json',
},
]),
},
{
name: '32 bombs (17 MiB each) before the attachment',
slow: true,
build: () =>
pdfWith([
...Array.from({ length: 32 }, () => ({
bytes: deflate(zeros(17 * MiB)),
flate: true,
})),
{ bytes: ascii('{"version":3}'), name: 'osteo-report.json' },
]),
},
{
name: '50 MiB file, attachment at the end',
slow: true,
build: () =>
pdfWith(
[{ bytes: ascii('{"version":3}'), name: 'osteo-report.json' }],
50 * MiB - 400,
),
},
]
describe('R4 import budget (shipped module, fresh instance per case)', () => {
it('prints the cost of each class and stays within W4 where the cap applies', async () => {
const rows: string[] = [
'| Case | Outcome | ms | Linear MiB | Node RSS MiB |',
'|---|---|---|---|---|',
]
for (const file of ['r4/reports/L.json', 'r4/reports/CAP.json']) {
const instance = await freshInstance()
const pdf = envelopePdf(instance, file)
const fresh = await freshInstance()
const m = measure(fresh, () => fresh.sheet.import_pdf(pdf))
rows.push(
`| ${file} envelope (${(pdf.byteLength / 1024).toFixed(0)} KB) | ${m.outcome} | ${m.ms.toFixed(0)} | ${m.linearMiB.toFixed(1)} | ${m.rssMiB.toFixed(1)} |`,
)
expect(m.outcome, file).toBe('ok')
}
for (const c of CASES) {
if (c.slow && !ALL) continue
const bytes = c.build()
const instance = await freshInstance()
const m = measure(instance, () =>
c.path === 'copy'
? instance.sheet.call(
'draft.restore',
JSON.stringify({
draft: new TextDecoder().decode(bytes),
archive: null,
}),
)
: instance.sheet.import_pdf(bytes),
)
rows.push(
`| ${c.name} | ${m.outcome} | ${m.ms.toFixed(0)} | ${m.linearMiB.toFixed(1)} | ${m.rssMiB.toFixed(1)} |`,
)
// W4: no trap, linear growth under 256 MiB on every hostile class.
expect(m.outcome, c.name).not.toMatch(/^trap/)
expect(m.linearMiB, c.name).toBeLessThan(256)
expect(instance.runtime.status, c.name).toBe('ready')
}
console.info(`[core-wasm] R4 import budget\n${rows.join('\n')}`)
}, 600_000)
it('with the cap off, a 16 MiB token-dense document costs most of the ceiling (proofs build)', async () => {
const proofs = await loadProofModule()
const bytes = pdfWith([
{ bytes: denseNumbers(16 * MiB - 64), name: 'osteo-report.json' },
])
const started = performance.now()
let outcome = 'returned'
try {
outcome = proofs.import_pdf_unbounded_for_test(BUNDLE, bytes)
} catch (error) {
outcome = `trap: ${(error as Error).constructor.name}`
}
console.info(
`[core-wasm] cap off, 16 MiB dense numbers: ${outcome} after ${(performance.now() - started).toFixed(0)} ms`,
)
// The tree fits under 1 GiB (784 MiB measured): the cap, not the ceiling, is the guard.
expect(outcome).toBe('Import(NoAttachment)')
}, 120_000)
it('traps at the 1 GiB linear ceiling instead of growing (proofs build)', async () => {
const proofs = await loadProofModule()
expect(proofs.allocate_for_test(256)).toBe(256)
let trapped = 'returned'
try {
proofs.allocate_for_test(1100)
} catch (error) {
trapped = (error as Error).constructor.name
}
expect(trapped).toBe('RuntimeError')
}, 120_000)
})