From b440a2f3fabe46a4d40aa86bfcd4f1ea9b643726 Mon Sep 17 00:00:00 2001 From: bchanot Date: Wed, 14 Oct 2026 23:30:56 +0200 Subject: [PATCH] test(pdf): R3 freeze O4/O5 sanitiser verdicts on the hostile corpus and the product SVGs --- .../tests/fixtures/svg/hostile/animation.svg | 1 + .../core/tests/fixtures/svg/hostile/bom.svg | 1 + .../svg/hostile/comments-pi-cdata.svg | 1 + .../core/tests/fixtures/svg/hostile/css.svg | 1 + .../svg/hostile/dangling-references.svg | 1 + .../fixtures/svg/hostile/data-and-images.svg | 1 + .../fixtures/svg/hostile/doctype-entities.svg | 3 + .../svg/hostile/doctype-in-comment.svg | 1 + .../fixtures/svg/hostile/duplicate-ids.svg | 1 + .../svg/hostile/entity-in-attribute.svg | 1 + .../tests/fixtures/svg/hostile/events.svg | 1 + .../fixtures/svg/hostile/external-urls.svg | 1 + .../fixtures/svg/hostile/external-use.svg | 1 + .../tests/fixtures/svg/hostile/foreign.svg | 1 + .../tests/fixtures/svg/hostile/malformed.svg | 1 + .../tests/fixtures/svg/hostile/namespaces.svg | 1 + .../tests/fixtures/svg/hostile/not-svg.svg | 1 + .../fixtures/svg/hostile/script-urls.svg | 1 + .../tests/fixtures/svg/hostile/script.svg | 1 + .../fixtures/svg/hostile/std-deviation.svg | 1 + .../fixtures/svg/hostile/style-element.svg | 1 + .../fixtures/svg/hostile/unbound-prefix.svg | 1 + .../fixtures/svg/hostile/use-mutual-cycle.svg | 1 + .../fixtures/svg/hostile/use-self-cycle.svg | 1 + .../svg/hostile/viewbox-non-finite.svg | 1 + .../svg/hostile/wrong-root-namespace.svg | 1 + crates/core/tests/oracles/r3/sanitiser.json | 2848 +++++++++++++++++ .../pdf/tests/r3.sanitiser-oracles.test.ts | 168 + 28 files changed, 3044 insertions(+) create mode 100644 crates/core/tests/fixtures/svg/hostile/animation.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/bom.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/comments-pi-cdata.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/css.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/dangling-references.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/data-and-images.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/doctype-entities.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/doctype-in-comment.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/duplicate-ids.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/entity-in-attribute.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/events.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/external-urls.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/external-use.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/foreign.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/malformed.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/namespaces.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/not-svg.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/script-urls.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/script.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/std-deviation.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/style-element.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/unbound-prefix.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/use-mutual-cycle.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/use-self-cycle.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/viewbox-non-finite.svg create mode 100644 crates/core/tests/fixtures/svg/hostile/wrong-root-namespace.svg create mode 100644 crates/core/tests/oracles/r3/sanitiser.json create mode 100644 packages/pdf/tests/r3.sanitiser-oracles.test.ts diff --git a/crates/core/tests/fixtures/svg/hostile/animation.svg b/crates/core/tests/fixtures/svg/hostile/animation.svg new file mode 100644 index 00000000..b8cb3892 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/animation.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/bom.svg b/crates/core/tests/fixtures/svg/hostile/bom.svg new file mode 100644 index 00000000..754c230f --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/bom.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/comments-pi-cdata.svg b/crates/core/tests/fixtures/svg/hostile/comments-pi-cdata.svg new file mode 100644 index 00000000..405c3bf5 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/comments-pi-cdata.svg @@ -0,0 +1 @@ +text x<y \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/css.svg b/crates/core/tests/fixtures/svg/hostile/css.svg new file mode 100644 index 00000000..52c33096 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/css.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/dangling-references.svg b/crates/core/tests/fixtures/svg/hostile/dangling-references.svg new file mode 100644 index 00000000..b3db3f48 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/dangling-references.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/data-and-images.svg b/crates/core/tests/fixtures/svg/hostile/data-and-images.svg new file mode 100644 index 00000000..0df4cc75 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/data-and-images.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/doctype-entities.svg b/crates/core/tests/fixtures/svg/hostile/doctype-entities.svg new file mode 100644 index 00000000..e78f9f90 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/doctype-entities.svg @@ -0,0 +1,3 @@ + +]> +&b; \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/doctype-in-comment.svg b/crates/core/tests/fixtures/svg/hostile/doctype-in-comment.svg new file mode 100644 index 00000000..fb16655d --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/doctype-in-comment.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/duplicate-ids.svg b/crates/core/tests/fixtures/svg/hostile/duplicate-ids.svg new file mode 100644 index 00000000..040fddb5 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/duplicate-ids.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/entity-in-attribute.svg b/crates/core/tests/fixtures/svg/hostile/entity-in-attribute.svg new file mode 100644 index 00000000..085df947 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/entity-in-attribute.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/events.svg b/crates/core/tests/fixtures/svg/hostile/events.svg new file mode 100644 index 00000000..a989dba9 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/events.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/external-urls.svg b/crates/core/tests/fixtures/svg/hostile/external-urls.svg new file mode 100644 index 00000000..aea69323 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/external-urls.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/external-use.svg b/crates/core/tests/fixtures/svg/hostile/external-use.svg new file mode 100644 index 00000000..2f4691cf --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/external-use.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/foreign.svg b/crates/core/tests/fixtures/svg/hostile/foreign.svg new file mode 100644 index 00000000..0bee177c --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/foreign.svg @@ -0,0 +1 @@ +
x
\ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/malformed.svg b/crates/core/tests/fixtures/svg/hostile/malformed.svg new file mode 100644 index 00000000..4df1981f --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/malformed.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/namespaces.svg b/crates/core/tests/fixtures/svg/hostile/namespaces.svg new file mode 100644 index 00000000..667d8418 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/namespaces.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/not-svg.svg b/crates/core/tests/fixtures/svg/hostile/not-svg.svg new file mode 100644 index 00000000..054f2cf8 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/not-svg.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/script-urls.svg b/crates/core/tests/fixtures/svg/hostile/script-urls.svg new file mode 100644 index 00000000..263c1c2f --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/script-urls.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/script.svg b/crates/core/tests/fixtures/svg/hostile/script.svg new file mode 100644 index 00000000..2c1d9399 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/script.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/std-deviation.svg b/crates/core/tests/fixtures/svg/hostile/std-deviation.svg new file mode 100644 index 00000000..14511106 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/std-deviation.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/style-element.svg b/crates/core/tests/fixtures/svg/hostile/style-element.svg new file mode 100644 index 00000000..63eba68c --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/style-element.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/unbound-prefix.svg b/crates/core/tests/fixtures/svg/hostile/unbound-prefix.svg new file mode 100644 index 00000000..da9e554d --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/unbound-prefix.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/use-mutual-cycle.svg b/crates/core/tests/fixtures/svg/hostile/use-mutual-cycle.svg new file mode 100644 index 00000000..b53d7799 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/use-mutual-cycle.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/use-self-cycle.svg b/crates/core/tests/fixtures/svg/hostile/use-self-cycle.svg new file mode 100644 index 00000000..5109c41b --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/use-self-cycle.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/viewbox-non-finite.svg b/crates/core/tests/fixtures/svg/hostile/viewbox-non-finite.svg new file mode 100644 index 00000000..3a08826e --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/viewbox-non-finite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/fixtures/svg/hostile/wrong-root-namespace.svg b/crates/core/tests/fixtures/svg/hostile/wrong-root-namespace.svg new file mode 100644 index 00000000..ed29c1e1 --- /dev/null +++ b/crates/core/tests/fixtures/svg/hostile/wrong-root-namespace.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/crates/core/tests/oracles/r3/sanitiser.json b/crates/core/tests/oracles/r3/sanitiser.json new file mode 100644 index 00000000..f0eb2083 --- /dev/null +++ b/crates/core/tests/oracles/r3/sanitiser.json @@ -0,0 +1,2848 @@ +{ + "hostile": { + "animation.svg": { + "removed": [ + "a", + "animate", + "set", + "animateTransform" + ], + "tree": { + "ns": "http://www.w3.org/2000/svg", + "name": "svg", + "attrs": { + "xmlns": "http://www.w3.org/2000/svg", + "xmlns:xlink": "http://www.w3.org/1999/xlink" + }, + "children": [] + } + }, + "bom.svg": { + "rejected": "malformed" + }, + "comments-pi-cdata.svg": { + "removed": [], + "tree": { + "ns": "http://www.w3.org/2000/svg", + "name": "svg", + "attrs": { + "xmlns": "http://www.w3.org/2000/svg" + }, + "children": [ + { + "comment": " a comment " + }, + { + "pi": "pi" + }, + { + "ns": "http://www.w3.org/2000/svg", + "name": "g", + "attrs": {}, + "children": [ + { + "text": "text " + }, + { + "text": "x`; `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)) + return { path, removed: 'removed' in verdict ? verdict.removed : null, unchanged } + }) + 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'))) + }) +})