Files
faunosteo/packages/pdf/tests/helpers/r4-reader-inputs.ts
T

1027 lines
26 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
buildSheetSnapshot,
type Report,
type ReportArchiveEnvelope,
type SheetSnapshot,
} from '@faunosteo/domain'
import {
defaultPdfLabels,
exportReportArchive,
MAX_PDF_BYTES,
readReportArchive,
type ExportResult,
} from '../../src'
import { readFixtureBytes } from './draft'
import { FIXED_EXPORTED_AT, nologosBranding, rasterizerOf } from './goldens'
import { pdfWithAttachment, pdfWithoutAttachment } from './pdf'
import {
ascii,
compressionStreamDeflate,
concat,
craftPdf,
deflate,
filled,
KiB,
MiB,
noise,
objectHolding,
streamObject,
} from './r4-reader-craft'
import {
allLeavesMigrated,
loadGoldenReports,
type GoldenReports,
} from './reports'
/**
* Every input of scan.test.ts, inflate.test.ts, read.test.ts and the reader
* cases of security.test.ts, rebuilt with the same code (O7). One spec per
* test input; specs whose bytes coincide are merged by the driver (the read
* suite re-creates several scan inputs). `exercises` lists the reader
* functions the original case calls on that input.
*/
export type ReaderFunction =
| 'findEmbeddedFiles'
| 'hasArchiveStamp'
| 'locateAttachment'
| 'readAttachedDocument'
| 'readAttachmentBytes'
| 'readReportArchive'
| 'inflateFlate'
export interface CaseRef {
suite: string
case: string
exercises: ReaderFunction[]
}
export interface ReaderContext {
golden: GoldenReports
/** The demo export of scan.test.ts (`exportDemo`), equal to read.test.ts `exportOnDraft(demo)`. */
exported: ExportResult
bomb: () => Uint8Array
}
export interface InputSpec {
slug: string
/** A scanner input (PDF reader functions) or an inflater input. */
kind: 'pdf' | 'flate'
cases: CaseRef[]
build: (ctx: ReaderContext) => Promise<Uint8Array> | Uint8Array
/** Flate inputs: the limits given to inflateFlate; absent = the default cap only. */
limits?: number[]
}
const BOMB_RAW = 64 * MiB
function exportOnDraft(golden: GoldenReports, report: Report) {
return exportReportArchive({
report,
sheet: buildSheetSnapshot(golden.draft, report),
branding: nologosBranding(),
labels: defaultPdfLabels,
locale: 'fr',
rasterizer: rasterizerOf('nologos'),
exportedAt: FIXED_EXPORTED_AT,
})
}
export async function readerContext(): Promise<ReaderContext> {
const golden = loadGoldenReports()
const exported = await exportOnDraft(golden, golden.reports.demo)
let bomb: Uint8Array | null = null
return {
golden,
exported,
bomb: () => (bomb ??= deflate(new Uint8Array(BOMB_RAW))),
}
}
const caseOf =
(suite: string, describe: string) =>
(title: string, ...exercises: ReaderFunction[]): CaseRef => ({
suite,
case: `${describe} > ${title}`,
exercises,
})
const pdfSpec = (
slug: string,
cases: CaseRef[],
build: InputSpec['build'],
): InputSpec => ({ slug, kind: 'pdf', cases, build })
const realAttachment = (ctx: ReaderContext) =>
objectHolding(ctx.exported.pdf, '/Type /EmbeddedFile')
// ---------------------------------------------------------------------------
// scan.test.ts
const fresh = caseOf(
'scan',
'findEmbeddedFiles on a fresh export (no object streams)',
)
const frozen = caseOf(
'scan',
'compatibility: frozen fixtures written with object streams',
)
const selection = caseOf('scan', 'selection between several embedded files')
const crafted = caseOf('scan', 'crafted stream layouts')
const stamp = caseOf('scan', 'the keyword stamp')
const garbage = caseOf('scan', 'garbage never throws')
const bombs = caseOf('scan', 'object-stream bomb (BLK-009)')
const cost = caseOf('scan', 'candidate cost bounds (audit M2)')
const depth = caseOf(
'scan',
'candidate cost bounds (audit M2) > bracket-depth pre-scan before JSON.parse (audit finding 3)',
)
const budget = caseOf(
'scan',
'candidate cost bounds (audit M2) > one 16 MiB budget shared by every candidate',
)
const demoJson = (ctx: ReaderContext) =>
new TextEncoder().encode(JSON.stringify(ctx.golden.reports.demo))
const objStmBomb = (ctx: ReaderContext) => {
const data = ctx.bomb()
return streamObject({
num: 9,
dict: `/Type /ObjStm /N 1 /First 4 /Filter /FlateDecode /Length ${data.byteLength}`,
data,
})
}
const embeddedBomb = (ctx: ReaderContext) => {
const data = ctx.bomb()
return streamObject({
num: 8,
dict: `/Type /EmbeddedFile /Filter /FlateDecode /Length ${data.byteLength}`,
data,
})
}
const openStream = (num: number) =>
ascii(`${num} 0 obj\n<< /Type /EmbeddedFile >>\nstream\n`)
function nestedOpenStreams(): Uint8Array {
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'),
])
}
return craftPdf([ascii('2 0 obj\n'), nested, filled('x', 20 * MiB)])
}
const flateObject = (num: number, raw: Uint8Array) => {
const data = deflate(raw)
return streamObject({
num,
dict: `/Type /EmbeddedFile /Filter /FlateDecode /Length ${data.byteLength}`,
data,
})
}
const garbageObject = (num: number) =>
streamObject({
num,
dict: `/Type /EmbeddedFile /Length ${10 * MiB}`,
data: concat([ascii('{'), filled('x', 10 * MiB - 1)]),
})
const GARBAGE: Array<[string, string, () => Uint8Array]> = [
['empty', 'garbage-empty', () => new Uint8Array(0)],
[
'random bytes',
'garbage-random-bytes',
() => Uint8Array.from({ length: 4096 }, (_, i) => (i * 7919) % 256),
],
[
'the type alone',
'garbage-type-alone',
() => ascii('%PDF-1.7\n/Type /EmbeddedFile'),
],
[
'dict without stream',
'garbage-dict-without-stream',
() => ascii('%PDF-1.7\n<< /Type /EmbeddedFile /Length 3 >>\nendobj'),
],
[
'stream without endstream',
'garbage-stream-without-endstream',
() => ascii('%PDF-1.7\n<< /Type /EmbeddedFile >>\nstream\n{"version":3}'),
],
[
'only the /EmbeddedFiles name tree key',
'garbage-embeddedfiles-key-only',
() => ascii('%PDF-1.7\n<< /Type /EmbeddedFiles >>\nstream\nx\nendstream'),
],
[
'unterminated dictionary',
'garbage-unterminated-dictionary',
() =>
ascii(
'%PDF-1.7\n<< /Type /EmbeddedFile /Length 1 << >>\nstream\nx\nendstream',
),
],
[
'hex /Keywords without a closing bracket',
'garbage-hex-keywords-unclosed',
() => ascii('%PDF-1.7\n<< /Keywords <FEFF0066'),
],
]
const hexName = Buffer.from('osteo-report.json', 'utf16le')
.swap16()
.toString('hex')
const SCAN: InputSpec[] = [
pdfSpec(
'scan.demo-export',
[
fresh(
'finds the one Flate stream, named through its visible Filespec',
'findEmbeddedFiles',
),
fresh(
'the length is the declared /Length and the data ends on endstream',
'findEmbeddedFiles',
),
fresh(
'the export keeps the Filespec, its name and the stamp in clear, no object stream',
'hasArchiveStamp',
),
fresh(
'readAttachmentBytes returns the inflated envelope bytes',
'readAttachmentBytes',
),
depth(
'the real envelope passes the pre-scan and is parsed once',
'locateAttachment',
),
],
(ctx) => ctx.exported.pdf,
),
...(
[
['report-v2.full.pdf', 'v2'],
['report-v2.minimal.pdf', 'v2'],
['report-v3.bare.pdf', 'bare_v3'],
] as const
).map(([name, origin]) =>
pdfSpec(
`scan.${name.replace('.pdf', '').replaceAll('.', '-')}`,
[
frozen(
`${name} → origin ${origin}`,
'findEmbeddedFiles',
'readReportArchive',
),
],
() => readFixtureBytes(name),
),
),
pdfSpec(
'scan.stamped-object-streams',
[
frozen(
'a stamped PDF with object streams hides its stamp: no_attachment (documented)',
'hasArchiveStamp',
'readReportArchive',
),
],
() => pdfWithoutAttachment(['faunosteo-archive/1'], true),
),
pdfSpec(
'scan.decoy-first-named-second',
[
selection(
'a decoy EmbeddedFile first, the named Faunosteo one second: the name wins',
'findEmbeddedFiles',
'readReportArchive',
),
],
(ctx) =>
pdfWithAttachment('osteo-report.json', ctx.golden.reports.demo, {
before: ['other.json', { hello: 'world', version: 2 }],
}),
),
pdfSpec(
'scan.unnamed-decoy-then-real',
[
selection(
'an unnamed decoy without a version is skipped for the versioned JSON after it',
'readReportArchive',
),
],
(ctx) => {
const decoy = streamObject({
num: 2,
dict: '/Type /EmbeddedFile /Length 17',
data: ascii('{"hello":"world"}'),
})
return craftPdf([decoy, realAttachment(ctx)])
},
),
pdfSpec(
'scan.foreign-without-attachment',
[
selection(
'a foreign PDF, with or without attachments: no_attachment',
'readReportArchive',
),
],
() => pdfWithoutAttachment(),
),
pdfSpec(
'scan.foreign-other-json',
[
selection(
'a foreign PDF, with or without attachments: no_attachment',
'readReportArchive',
),
],
() => pdfWithAttachment('other.json', { hello: 'world' }),
),
pdfSpec(
'scan.indirect-length',
[
crafted(
'/Length as an indirect reference: read up to endstream',
'findEmbeddedFiles',
'readReportArchive',
),
],
(ctx) =>
craftPdf([
streamObject({
num: 2,
dict: '/Type /EmbeddedFile /Length 7 0 R',
data: demoJson(ctx),
}),
ascii('7 0 obj\n123456\nendobj\n'),
]),
),
pdfSpec(
'scan.wrong-direct-length',
[
crafted(
'a wrong direct /Length that does not land on endstream: measured instead',
'findEmbeddedFiles',
'readReportArchive',
),
],
(ctx) =>
craftPdf([
streamObject({
num: 2,
dict: '/Type /EmbeddedFile /Length 5',
data: demoJson(ctx),
}),
]),
),
pdfSpec(
'scan.crlf-unspaced-type-params',
[
crafted(
'CRLF after stream, /Type/EmbeddedFile unspaced, a leading /Params dict',
'findEmbeddedFiles',
'readAttachmentBytes',
),
],
(ctx) => {
const data = demoJson(ctx)
return craftPdf([
streamObject({
num: 2,
dict: `/Params << /Size ${data.byteLength} >> /Type/EmbeddedFile/Length ${data.byteLength}`,
data,
eol: '\r\n',
}),
])
},
),
pdfSpec(
'scan.filespec-hex-name',
[
crafted(
'a Filespec with a hex /F name and an /EF listing /UF too names its stream',
'findEmbeddedFiles',
),
],
() =>
craftPdf([
ascii(
`3 0 obj\n<< /Type /Filespec /F <${hexName}> /EF << /F 2 0 R /UF 2 0 R >> >>\nendobj\n`,
),
streamObject({
num: 2,
dict: '/Type /EmbeddedFile /Length 13',
data: ascii('{"version":3}'),
}),
]),
),
pdfSpec(
'scan.keywords-hex-stamp',
[
stamp(
'hex (pdf-lib) and literal /Keywords strings are both read',
'hasArchiveStamp',
),
],
() => pdfWithoutAttachment(['faunosteo-archive/1']),
),
pdfSpec(
'scan.keywords-other',
[
stamp(
'hex (pdf-lib) and literal /Keywords strings are both read',
'hasArchiveStamp',
),
],
() => pdfWithoutAttachment(['other']),
),
pdfSpec(
'scan.keywords-literal-stamp',
[
stamp(
'hex (pdf-lib) and literal /Keywords strings are both read',
'hasArchiveStamp',
'readReportArchive',
),
],
() =>
craftPdf([
ascii('3 0 obj\n<< /Keywords (faunosteo-archive/1) >>\nendobj\n'),
]),
),
...GARBAGE.map(([title, slug, build]) =>
pdfSpec(
`scan.${slug}`,
[
garbage(
`${title}: no candidate, no stamp, no_attachment or invalid_pdf`,
'findEmbeddedFiles',
'hasArchiveStamp',
'readAttachedDocument',
),
],
build,
),
),
pdfSpec(
'scan.caps-40-candidates',
[garbage('caps the candidates at 32', 'findEmbeddedFiles')],
() =>
craftPdf(
Array.from({ length: 40 }, (_, i) =>
streamObject({
num: i + 2,
dict: '/Type /EmbeddedFile /Length 1',
data: ascii('x'),
}),
),
),
),
pdfSpec(
'scan.objstm-bomb-beside-attachment',
[
bombs(
'64 MiB ObjStm bomb beside the attachment: report in ms, bomb never inflated',
'readReportArchive',
),
],
(ctx) =>
craftPdf([
objStmBomb(ctx),
objectHolding(ctx.exported.pdf, '/Type /Filespec'),
realAttachment(ctx),
]),
),
pdfSpec(
'scan.objstm-and-embedded-bombs',
[
bombs(
'EmbeddedFile bomb past 16 MiB: inflation cancelled, no_attachment, no hang',
'readReportArchive',
),
],
(ctx) => craftPdf([objStmBomb(ctx), embeddedBomb(ctx)]),
),
pdfSpec(
'scan.bombs-before-unnamed-attachment',
[
bombs(
'both bombs before an unnamed real attachment: bomb skipped, report read',
'readReportArchive',
),
],
(ctx) =>
craftPdf([objStmBomb(ctx), embeddedBomb(ctx), realAttachment(ctx)]),
),
pdfSpec(
'scan.32-open-streams-20mib',
[
cost(
'32 candidates without any endstream on a 20 MiB file: no candidate, well under a second',
'findEmbeddedFiles',
),
],
() =>
craftPdf([
...Array.from({ length: 32 }, (_, i) => openStream(i + 2)),
filled('x', 20 * MiB),
]),
),
pdfSpec(
'scan.32-nested-open-streams-20mib',
[
cost(
'32 nested EmbeddedFile dicts (descending offsets) without endstream on a 20 MiB file: no candidate, no finder reset',
'findEmbeddedFiles',
),
],
nestedOpenStreams,
),
pdfSpec(
'scan.indirect-lengths-in-a-row',
[
cost(
'several indirect /Length streams in a row: each measured to its own endstream',
'findEmbeddedFiles',
),
],
() =>
craftPdf(
['{"version":3}', '{"version":4,"x":1}', '{"v":5}'].map((text, i) =>
streamObject({
num: i + 2,
dict: `/Type /EmbeddedFile /Length ${i + 20} 0 R`,
data: ascii(text),
}),
),
),
),
pdfSpec(
'scan.measured-past-cap-window',
[
cost(
'a measured stream running past the cap window is not a candidate, the next one is',
'findEmbeddedFiles',
),
],
() =>
craftPdf([
openStream(2),
filled('x', 16 * MiB + 64),
ascii('\nendstream\nendobj\n'),
streamObject({
num: 3,
dict: '/Type /EmbeddedFile /Length 13',
data: ascii('{"version":3}'),
}),
]),
),
pdfSpec(
'scan.32-raw-1mib-brackets',
[
cost(
'32 raw candidates of 1 MiB of "[": no_attachment under a second, JSON.parse never called',
'readReportArchive',
),
],
() =>
craftPdf(
Array.from({ length: 32 }, (_, i) =>
streamObject({
num: i + 2,
dict: `/Type /EmbeddedFile /Length ${MiB}`,
data: filled('[', MiB),
}),
),
),
),
pdfSpec(
'scan.flate-8m-deep-brackets',
[
depth(
'a 16 KB Flate candidate opening 8 million "[" deep: skipped unparsed, no_attachment fast',
'readReportArchive',
),
],
() =>
craftPdf([
flateObject(
2,
concat([ascii('{"version":3,"x":'), filled('[', 8_000_000)]),
),
]),
),
pdfSpec(
'scan.brackets-inside-string',
[
depth(
'2000 "[" inside a string with escaped quotes and backslashes: not rejected, parsed once',
'locateAttachment',
),
],
() => {
const text = `{"version":3,"note":"\\"${'['.repeat(2000)}\\\\ \\" [[[["}`
return craftPdf([
streamObject({
num: 2,
dict: `/Type /EmbeddedFile /Length ${text.length}`,
data: ascii(text),
}),
])
},
),
pdfSpec(
'scan.one-10mib-garbage-then-attachment',
[
budget(
'one 10 MiB garbage object before the attachment: 6 MiB left, the report is read',
'readReportArchive',
),
],
(ctx) => craftPdf([garbageObject(2), realAttachment(ctx)]),
),
pdfSpec(
'scan.two-10mib-garbage-then-attachment',
[
budget(
'two 10 MiB garbage objects before the attachment: budget exhausted, no_attachment',
'readReportArchive',
),
],
(ctx) =>
craftPdf([garbageObject(2), garbageObject(3), realAttachment(ctx)]),
),
]
// ---------------------------------------------------------------------------
// inflate.test.ts
const inflateCase = caseOf('inflate', 'inflateFlate')
const flateSpec = (
slug: string,
cases: CaseRef[],
build: InputSpec['build'],
limits?: number[],
): InputSpec => ({ slug, kind: 'flate', cases, build, limits })
const inflated = (title: string) => [inflateCase(title, 'inflateFlate')]
/** 64 KiB payload: limits at the output length and one byte under it. */
const EXACT_PAYLOAD = 64 * KiB
const INFLATE: InputSpec[] = [
flateSpec(
'inflate.several-slices-200kib',
inflated('round-trips a payload spanning several slices'),
() => deflate(noise(200 * KiB)),
),
flateSpec(
'inflate.slices-100kib',
inflated('feeds the inflater in 16 KiB slices, each awaited'),
() => deflate(noise(100 * KiB)),
),
flateSpec(
'inflate.exact-limit-64kib',
inflated('an output exactly at the limit passes, one byte more is null'),
() => deflate(noise(EXACT_PAYLOAD)),
[EXACT_PAYLOAD, EXACT_PAYLOAD - 1],
),
flateSpec(
'inflate.empty-input',
inflated('empty input: null'),
() => new Uint8Array(0),
),
flateSpec(
'inflate.truncated-stream',
inflated('truncated stream: null'),
() => deflate(noise(4 * KiB)).subarray(0, 2 * KiB),
),
flateSpec('inflate.trailing-junk', inflated('trailing junk: null'), () =>
Uint8Array.from([...deflate(noise(KiB)), 1, 2, 3]),
),
flateSpec('inflate.corrupt-body', inflated('corrupt body: null'), () =>
Uint8Array.from(deflate(noise(KiB)), (b, i) => (i > 8 ? b ^ 0xff : b)),
),
flateSpec('inflate.not-deflate', inflated('not deflate at all: null'), () =>
new TextEncoder().encode('{"version":3}'),
),
flateSpec(
'inflate.bomb-64mib',
inflated(
'a 64 MiB bomb past the default cap is cancelled in well under a second',
),
(ctx) => ctx.bomb(),
),
]
// ---------------------------------------------------------------------------
// read.test.ts
const t4 = caseOf('read', 'T4 roundtrip')
const t5 = caseOf('read', 'T5 v2 archives and the bare v3 fixture')
const t6 = caseOf('read', 'T6 versions')
const presence = caseOf('read', 'attachment presence and selection')
const T5_FIXTURES = ['report-v2.full.pdf', 'report-v2.minimal.pdf'] as const
const slugOfFixture = (name: string) =>
name.replace('.pdf', '').replaceAll('.', '-')
const READ: InputSpec[] = [
pdfSpec(
'read.demo-export',
[t4('reads back the exported envelope whole', 'readReportArchive')],
async (ctx) =>
(await exportOnDraft(ctx.golden, ctx.golden.reports.demo)).pdf,
),
...T5_FIXTURES.flatMap((name) => {
const title = t5(
`${name}: migrated, re-exported, imported again equal`,
'readReportArchive',
)
return [
pdfSpec(`read.${slugOfFixture(name)}`, [title], () =>
readFixtureBytes(name),
),
pdfSpec(
`read.${slugOfFixture(name)}-reexported`,
[title],
async (ctx) => {
const map = ctx.golden.draft.migrationMap
const first = await readReportArchive(readFixtureBytes(name), map)
return (await exportOnDraft(ctx.golden, first.report)).pdf
},
),
]
}),
pdfSpec(
'read.612-leaves-exported',
[
t5(
'keeps the 29 unresolved findings of the 612-leaf report through export and import',
'readReportArchive',
),
],
async (ctx) => {
const migrated = allLeavesMigrated(ctx.golden.draft)
return (await exportOnDraft(ctx.golden, migrated.report)).pdf
},
),
pdfSpec(
'read.report-v3-bare',
[
t5(
'reads report-v3.bare.pdf as a bare v3 with no archive',
'readReportArchive',
),
],
() => readFixtureBytes('report-v3.bare.pdf'),
),
pdfSpec(
'read.envelope-v2-wrapper',
[
t6(
'envelope v2 wrapper: report kept, snapshot rejected as unsupported_envelope',
'readReportArchive',
),
],
(ctx) =>
pdfWithAttachment('osteo-report.json', {
...ctx.exported.envelope,
envelopeVersion: 2,
}),
),
pdfSpec(
'read.envelope-v1-report-v4',
[
t6(
'envelope v1 with a report v4: incompatible_version 4',
'readReportArchive',
),
],
(ctx) => {
const envelope: ReportArchiveEnvelope = ctx.exported.envelope
return pdfWithAttachment('osteo-report.json', {
...envelope,
report: { ...envelope.report, version: 4 },
})
},
),
pdfSpec(
'read.bare-report-v7',
[t6('bare report version 7: incompatible_version 7', 'readReportArchive')],
() => pdfWithAttachment('osteo-report.json', { version: 7 }),
),
pdfSpec(
'read.stamped-without-attachment',
[
presence(
'a stamped PDF without attachment: attachment_stripped',
'readReportArchive',
),
],
() => pdfWithoutAttachment(['faunosteo-archive/1']),
),
pdfSpec(
'read.stamped-object-streams',
[
presence(
'a stamped PDF saved with object streams (pre-BLK-009 layout): the stamp is invisible, no_attachment',
'readReportArchive',
),
],
() => pdfWithoutAttachment(['faunosteo-archive/1'], true),
),
pdfSpec(
'read.foreign-without-attachment',
[presence('a foreign PDF: no_attachment', 'readReportArchive')],
() => pdfWithoutAttachment(),
),
pdfSpec(
'read.foreign-other-json',
[presence('a foreign PDF: no_attachment', 'readReportArchive')],
() => pdfWithAttachment('other.json', { hello: 'world' }),
),
pdfSpec(
'read.named-wins-over-earlier',
[
presence(
'the Filespec named osteo-report.json wins over an earlier EmbeddedFile',
'readReportArchive',
),
],
(ctx) =>
pdfWithAttachment('osteo-report.json', ctx.golden.reports.demo, {
before: ['other.json', { hello: 'world', version: 2 }],
}),
),
...(
[
['oversized-50mib-plus-1', () => new Uint8Array(MAX_PDF_BYTES + 1)],
['not-a-pdf', () => new TextEncoder().encode('not a pdf at all')],
[
'header-at-1024',
() => {
const late = new Uint8Array(2048)
late.set(new TextEncoder().encode('%PDF-1.7'), 1024)
return late
},
],
] as const
).map(([slug, build]) =>
pdfSpec(
`read.${slug}`,
[
presence(
'refuses an oversized file or one without a %PDF- header as invalid_pdf',
'readReportArchive',
),
],
build,
),
),
]
// ---------------------------------------------------------------------------
// security.test.ts (the reader cases)
const UUID = (i: number) => `00000000-0000-4000-8000-00000000000${i}`
type Mutation = (snapshot: SheetSnapshot) => void
/** Copied from security.test.ts `MUTATIONS`: [title, reason, mutation, slug]. */
const MUTATIONS: Array<[string, string, Mutation, string]> = [
[
'5 assets',
'schema',
(s) => {
s.assets = Array.from({ length: 5 }, (_, i) => ({
...s.assets[0]!,
svgAssetId: i === 0 ? s.assets[0]!.svgAssetId : UUID(i),
}))
},
'five-assets',
],
[
'a 3 MiB asset',
'caps',
(s) => {
s.assets[0]!.svg = 'é'.repeat(1_100_000)
},
'asset-3mib',
],
[
'finding depth 9',
'caps',
(s) => {
const leaf = {
id: 'deep',
name: { fr: 'x', en: 'x' },
description: {},
requires_vet_referral: false,
reference_ids: [],
review_status: 'inherited' as const,
children: [] as never[],
}
let node: typeof leaf = leaf
for (let depth = 0; depth < 9; depth += 1) {
node = { ...leaf, id: `n${depth}`, children: [node] as never[] }
}
const zone = s.document.finding_zones.find((z) => z.categories.length)!
zone.categories[0]!.children.push(node)
},
'finding-depth-9',
],
[
'1,025 annotations',
'caps',
(s) => {
s.document.annotations.push(
...Array.from({ length: 1025 }, (_, i) => ({
...s.document.annotations[0]!,
id: `extra-${i}`,
})),
)
},
'annotations-1025',
],
[
'a view chain cycle',
'integrity',
(s) => {
const head = s.document.views.find((v) => v.id === 'head')!
if (head.kind === 'detail') head.parent_view_id = 'head'
},
'view-chain-cycle',
],
[
'a dangling svg_asset_id',
'integrity',
(s) => {
s.document.views.find((v) => v.kind === 'main')!.svg_asset_id = UUID(9)
},
'dangling-svg-asset-id',
],
[
'another sheet (sheet_mismatch)',
'sheet_mismatch',
(s) => {
s.ref.documentSha256 = 'f'.repeat(64)
},
'another-sheet',
],
]
const clone = <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T
const survives = caseOf(
'security',
'import: the report survives a rejected snapshot',
)
const protoAndBomb = caseOf('security', '__proto__ ids and the inflate bomb')
const BOMB_TITLE = 'inflates a normal payload and returns null for a 24 MB bomb'
const SECURITY: InputSpec[] = [
...MUTATIONS.map(([title, reason, mutate, slug]) =>
pdfSpec(
`security.snapshot-${slug}`,
[survives(`${title} → ${reason}`, 'readReportArchive')],
(ctx) => {
const hostile = clone(ctx.exported.envelope)
mutate(hostile.sheetSnapshot)
return pdfWithAttachment('osteo-report.json', hostile)
},
),
),
flateSpec(
'security.inflate-normal-payload',
[protoAndBomb(BOMB_TITLE, 'inflateFlate')],
() =>
compressionStreamDeflate(
new TextEncoder().encode(JSON.stringify({ version: 2 })),
),
),
flateSpec(
'security.inflate-bomb-24mb',
[protoAndBomb(BOMB_TITLE, 'inflateFlate')],
() => compressionStreamDeflate(new Uint8Array(24 * 1024 * 1024)),
),
]
export const SUITE_INPUTS: InputSpec[] = [
...SCAN,
...INFLATE,
...READ,
...SECURITY,
]