/** * R3 freeze O4 + O5 (plan 2026-10-14 ยง3): what the TypeScript sanitiser does * with every case of the hostile corpus (`crates/core/tests/fixtures/svg/ * hostile/*.svg`, plus generated cases defined by a small spec both sides * build the same way) and with the legitimate SVGs of the product (the sheet * drawing, the partner logo, the public SVGs): rejected with a reason, or * kept with the list of removed names and the sanitised tree. The Rust * sanitiser must give the same verdict where both apply the same rule; where * the plan declares Rust stricter, the Rust test states its own verdict next * to this one. `MAKE_R3_ORACLES=1` writes * `crates/core/tests/oracles/r3/sanitiser.json`. */ import { mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' import { DOMParser, type Node as XmlNode } from '@xmldom/xmldom' import { describe, expect, it } from 'vitest' import { parseSvg, serializeSvg } from '../src/svg/parse' import { sanitizeSvgRoot } from '../src/svg/sanitize' import { SvgRejectedError } from '../src/errors' const HERE = dirname(fileURLToPath(import.meta.url)) const ROOT = resolve(HERE, '../../..') const HOSTILE = resolve(ROOT, 'crates/core/tests/fixtures/svg/hostile') const OUT = resolve(ROOT, 'crates/core/tests/oracles/r3/sanitiser.json') const MAKE = process.env.MAKE_R3_ORACLES === '1' /** The legitimate SVGs that must come out untouched (zero removal, same tree). */ const LEGITIMATE = [ 'supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg', 'apps/app/src/assets/brand/paws-in-harmony.svg', 'apps/app/public/favicon.svg', 'apps/app/public/logo-horizontal.svg', ] /** * Generated cases: `uses` = a path then N ``; `chain` = L levels of * groups each holding W uses of the previous level; `depth` = N nested `` under * the root (root depth 1); `elements` = the root plus N-1 ``; `bytes` = a root * whose comment pads the text to N bytes; `attributes` = one path with N attributes. */ type Spec = | { kind: 'uses'; n: number } | { kind: 'chain'; levels: number; width: number } | { kind: 'depth'; n: number } | { kind: 'elements'; n: number } | { kind: 'bytes'; n: number } | { kind: 'attributes'; n: number } const SPECS: Spec[] = [ { kind: 'uses', n: 256 }, { kind: 'uses', n: 257 }, { kind: 'chain', levels: 8, width: 30 }, { kind: 'depth', n: 64 }, { kind: 'depth', n: 65 }, { kind: 'elements', n: 20000 }, { kind: 'elements', n: 20001 }, { kind: 'bytes', n: 2 * 1024 * 1024 }, { kind: 'bytes', n: 2 * 1024 * 1024 + 1 }, { kind: 'attributes', n: 2000 }, ] const OPEN = '' export function generated(spec: Spec): string { switch (spec.kind) { case 'uses': return `${OPEN}${''.repeat(spec.n)}` case 'chain': { let body = '' for (let level = 1; level <= spec.levels; level++) { body += `${``.repeat(spec.width)}` } return `${OPEN}${body}` } case 'depth': return `${OPEN}${''.repeat(spec.n - 1)}${''.repeat(spec.n - 1)}` case 'elements': return `${OPEN}${''.repeat(spec.n - 1)}` case 'bytes': { const frame = `${OPEN}` + '' return `${OPEN}` } case 'attributes': { const attrs = Array.from({ length: spec.n }, (_, i) => ` data-a${i}="${i}"`).join('') return `${OPEN}` } } } type TreeNode = | { ns: string | null; name: string; attrs: Record; children: TreeNode[] } | { text: string } | { comment: string } | { pi: string } function treeOf(node: XmlNode): TreeNode { if (node.nodeType === 3 || node.nodeType === 4) return { text: node.nodeValue ?? '' } if (node.nodeType === 8) return { comment: node.nodeValue ?? '' } if (node.nodeType === 7) return { pi: node.nodeName } const el = node as unknown as Element const attrs: Record = {} const names = Array.from({ length: el.attributes.length }, (_, i) => el.attributes[i]!.name) for (const name of names.sort()) attrs[name] = el.getAttribute(name) ?? '' const children = Array.from({ length: node.childNodes.length }, (_, i) => node.childNodes[i]!) return { ns: el.namespaceURI, name: el.localName, attrs, children: children.map(treeOf) } } type Verdict = | { rejected: string } | { removed: string[]; tree?: TreeNode; bytes?: number } /** Big generated outcomes keep the removed list and the output size, not the tree. */ function verdictOf(text: string, keepTree: boolean): Verdict { try { const root = parseSvg(text) const { removed } = sanitizeSvgRoot(root) const svg = serializeSvg(root) if (!keepTree) return { removed: [...new Set(removed)].sort(), bytes: svg.length } const tree = treeOf(new DOMParser().parseFromString(svg, 'image/svg+xml').documentElement!) return { removed, tree } } catch (error) { if (error instanceof SvgRejectedError) return { rejected: error.reason } throw error } } /** The tree xmldom gives the source itself: what a zero-removal sanitiser must keep. */ function sourceTree(text: string): TreeNode { return treeOf(new DOMParser().parseFromString(text, 'image/svg+xml').documentElement!) } function build() { const hostile = Object.fromEntries( readdirSync(HOSTILE) .filter((f) => f.endsWith('.svg')) .sort() .map((f) => [f, verdictOf(readFileSync(resolve(HOSTILE, f), 'utf8'), true)]), ) const specs = SPECS.map((spec) => ({ spec, verdict: verdictOf(generated(spec), false) })) const legitimate = LEGITIMATE.map((path) => { const text = readFileSync(resolve(ROOT, path), 'utf8') const verdict = verdictOf(text, true) const unchanged = 'removed' in verdict && JSON.stringify(verdict.tree) === JSON.stringify(sourceTree(text)) const tree = 'removed' in verdict ? verdict.tree : null return { path, removed: 'removed' in verdict ? verdict.removed : null, unchanged, tree } }) return { hostile, specs, legitimate } } describe('R3 sanitiser oracles (TypeScript freeze)', () => { it('keeps every legitimate SVG untouched', () => { for (const entry of build().legitimate) { expect(entry, entry.path).toMatchObject({ removed: [], unchanged: true }) } }) it('matches the frozen verdicts', () => { const actual = build() if (MAKE) { mkdirSync(dirname(OUT), { recursive: true }) writeFileSync(OUT, `${JSON.stringify(actual, null, 1)}\n`) return } expect(actual).toEqual(JSON.parse(readFileSync(OUT, 'utf8'))) }) })