test(pdf): R3 freeze O4/O5 sanitiser verdicts on the hostile corpus and the product SVGs

This commit is contained in:
bchanot committed 2026-10-14 23:30:56 +02:00
1 parent d1aa35e97c
commit b440a2f3fa
28 files changed
+3044

No files matched your search

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><a id="l"><path d="M0 0h1"/></a><animate attributeName="href" values="javascript:alert(1)"/><set attributeName="onclick" to="alert(1)"/><animateTransform attributeName="transform"/></svg>

After

Width:  |  Height:  |  Size: 270 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1"/></svg>
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><!-- a comment --><?pi data?><g>text <![CDATA[x<y]]><path d="M0 0h1"/></g></svg>

After

Width:  |  Height:  |  Size: 120 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0" style="position:fixed;width:100vw;height:100vh"/><path d="M0 0" style="fill:red;\66 ill:url(x)"/><path d="M0 0" style="background:image-set(url(x) 1x)"/><path d="M0 0" style="behavior:url(x.htc)"/><path d="M0 0" style="fill:expression(alert(1))"/><path d="M0 0" style="fill:rgb(1,2,3)"/><path d="M0 0" style="fill:#fff;stroke:none;cursor:pointer"/><path d="M0 0" style="content:attr(x)"/></svg>

After

Width:  |  Height:  |  Size: 449 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a"/><linearGradient id="b" xlink:href="#a"/><path d="M0 0h1" fill="url(#missing)"/><use href="#nope"/><path d="M0 0h1" stroke="url(#b)"/></svg>

After

Width:  |  Height:  |  Size: 246 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><use href="data:image/svg+xml;base64,PHN2Zz48L3N2Zz4="/><image href="data:image/png;base64,iVBOR" width="1" height="1"/><image xlink:href="http://evil.test/i.png"/></svg>

After

Width:  |  Height:  |  Size: 253 B

@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<!DOCTYPE svg [<!ENTITY a "aaaaaaaaaa"><!ENTITY b "&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;">]>
<svg xmlns="http://www.w3.org/2000/svg"><text>&b;</text></svg>

After

Width:  |  Height:  |  Size: 170 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><!-- <!DOCTYPE note --><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 87 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path id="x" d="M0 0h1"/><g id="x"/></svg>

After

Width:  |  Height:  |  Size: 82 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1" fill="&#x75;rl(http://evil.test/x)"/><path d="M0 0h1" fill="&amp;#x75;"/><path d="M0 0h1" fill="#&#x30;00"/></svg>

After

Width:  |  Height:  |  Size: 171 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"><path d="M0 0h1" onclick="alert(2)" ONMOUSEOVER="x"/><g onfocusin="y"/></svg>

After

Width:  |  Height:  |  Size: 135 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1" fill="url(http://evil.test/p)"/><path d="M0 0h1" style="fill:url(https://evil.test/p)"/><path d="url(//e)"/><path d="M0 0" filter="url( #ok)"/><filter id="ok"/></svg>

After

Width:  |  Height:  |  Size: 223 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path id="p" d="M0 0h1"/><use href="http://evil.test/x.svg#a"/><use xlink:href="//evil.test/x.svg#b"/><use href="#p"/></svg>

After

Width:  |  Height:  |  Size: 207 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><foreignObject width="10" height="10"><div xmlns="http://www.w3.org/1999/xhtml">x</div></foreignObject><iframe src="x"/><object data="x"/><embed src="x"/><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 218 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1"><g></svg>

After

Width:  |  Height:  |  Size: 66 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:s="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"><s:path d="M0 0h1"/><path xmlns="http://evil.test/ns" d="M0 0h1"/><path d="M0 0h1" inkscape:label="x" xml:space="preserve" xml:lang="fr"/></svg>

After

Width:  |  Height:  |  Size: 282 B

+1
View File
@@ -0,0 +1 @@
<html xmlns="http://www.w3.org/1999/xhtml"><body/></html>
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><a href="javascript:alert(1)"><path d="M0 0h1"/></a><use xlink:href="javascript:alert(2)"/><path id="p" d="M0 0" fill=" javascript:x"/></svg>

After

Width:  |  Height:  |  Size: 224 B

+1
View File
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><script>alert(1)</script><script xlink:href="x.js"/><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 159 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><filter id="f"><feGaussianBlur stdDeviation="500 3"/><feGaussianBlur stdDeviation="NaN"/><feGaussianBlur stdDeviation="2.5"/></filter></svg>

After

Width:  |  Height:  |  Size: 180 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><style>@import url(http://evil.test/x.css); path{fill:red}</style><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 130 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg"><path d="M0 0h1" xlink:href="#p"/></svg>

After

Width:  |  Height:  |  Size: 80 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="a"><use xlink:href="#b"/></g><g id="b"><use xlink:href="#a"/></g></svg>

After

Width:  |  Height:  |  Size: 161 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g id="a"><use xlink:href="#a"/></g></svg>

After

Width:  |  Height:  |  Size: 125 B

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 Infinity NaN"><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 91 B

@@ -0,0 +1 @@
<svg xmlns="http://evil.test/ns"><path d="M0 0h1"/></svg>

After

Width:  |  Height:  |  Size: 57 B

File diff suppressed because it is too large. Load diff
@@ -0,0 +1,168 @@
/**
* 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 `<use href="#p"/>`; `chain` = L levels of
* groups each holding W uses of the previous level; `depth` = N nested `<g>` under
* the root (root depth 1); `elements` = the root plus N-1 `<path/>`; `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 = '<svg xmlns="http://www.w3.org/2000/svg">'
export function generated(spec: Spec): string {
switch (spec.kind) {
case 'uses':
return `${OPEN}<path id="p" d="M0 0h1"/>${'<use href="#p"/>'.repeat(spec.n)}</svg>`
case 'chain': {
let body = '<path id="l0" d="M0 0h1"/>'
for (let level = 1; level <= spec.levels; level++) {
body += `<g id="l${level}">${`<use href="#l${level - 1}"/>`.repeat(spec.width)}</g>`
}
return `${OPEN}${body}</svg>`
}
case 'depth':
return `${OPEN}${'<g>'.repeat(spec.n - 1)}${'</g>'.repeat(spec.n - 1)}</svg>`
case 'elements':
return `${OPEN}${'<path/>'.repeat(spec.n - 1)}</svg>`
case 'bytes': {
const frame = `${OPEN}<!---->` + '</svg>'
return `${OPEN}<!--${'x'.repeat(spec.n - frame.length)}--></svg>`
}
case 'attributes': {
const attrs = Array.from({ length: spec.n }, (_, i) => ` data-a${i}="${i}"`).join('')
return `${OPEN}<path d="M0 0h1"${attrs}/></svg>`
}
}
}
type TreeNode =
| { ns: string | null; name: string; attrs: Record<string, string>; 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<string, string> = {}
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')))
})
})