test: pin embed expansion order and the editor policy mirror

- expansion runs last at the sinks, never in payload transforms
- only the exact placeholder serialization expands; stable across calls
- the Go policy negatives are stripped the same way in the mirror
This commit is contained in:
2026-10-10 16:03:08 +02:00
parent 759e684e86
commit 782aa4da7d
2 changed files with 96 additions and 0 deletions

View File

@@ -118,3 +118,58 @@ test('the editor corpus embed entries expand after sanitizing', () => {
assert.equal(expandEmbeds(clean), clean, entry.name)
}
})
test('expansion is last: sanitizing the expanded output would strip the iframe', () => {
// Why the order matters: the sanitizer never keeps an iframe, so a sink that
// sanitized after expanding would lose every video.
const again = sanitizeJournalHtml(IFRAME)
assert.equal(iframeCount(again), 0)
assert.doesNotMatch(again, /youtube-nocookie/)
})
test('the sinks expand after sanitizing and the payload transforms never expand', () => {
const read = (path: string) => readFileSync(new URL(path, import.meta.url), 'utf8')
for (const sink of ['../app/components/Journal/PostBody.vue', '../app/components/Journal/CategoryHeader.vue']) {
const source = read(sink)
assert.match(source, /expandEmbeds\(sanitizeJournalHtml\(/, `${sink} must render expandEmbeds(sanitizeJournalHtml(...))`)
assert.equal((source.match(/expandEmbeds\(/g) ?? []).length, 1, `${sink} expands once`)
}
for (const transform of ['../app/utils/postView.ts', '../app/journal-pages/CategoryPage.vue']) {
assert.doesNotMatch(read(transform), /expandEmbeds/, `${transform} must not expand`)
}
})
test('only the exact serialization expands: case, spacing and entities stay inert', () => {
const inert = [
`<figure data-embed="YouTube" data-id="${ID}"></figure>`,
`<figure data-embed="youtube " data-id="${ID}"></figure>`,
`<figure data-embed="youtube" data-id="${ID}"></figure>`,
`<figure data-embed="youtube" data-id="${ID}" ></figure>`,
`<FIGURE data-embed="youtube" data-id="${ID}"></FIGURE>`,
`<figure data-embed="youtube" data-id="javascript:1"></figure>`,
`<figure data-embed="youtube" data-id="dQw4w9WgXc&#81;"></figure>`,
`<figure data-embed="youtube" data-id="${ID}">\n</figure>`,
`<figure data-embed="youtube" data-id=""></figure>`,
]
for (const html of inert) {
assert.equal(expandEmbeds(html), html, html)
assert.equal(iframeCount(expandEmbeds(html)), 0, html)
}
})
test('expansion is stable across calls (the global pattern keeps no state)', () => {
const body = `<p>a</p>${PLACEHOLDER}<p>b</p>${PLACEHOLDER}`
const first = expandEmbeds(body)
assert.equal(expandEmbeds(body), first)
assert.equal(EMBED_PLACEHOLDER.lastIndex, 0)
assert.equal(iframeCount(first), 2)
})
test('the iframe src is a fixed host plus the validated id, never stored text', () => {
const out = sink(`<figure data-embed="youtube" data-id="${ID}"></figure>`)
const src = /<iframe src="([^"]+)"/.exec(out)?.[1]
assert.equal(src, `https://www.youtube-nocookie.com/embed/${ID}`)
assert.match(out, /referrerpolicy="strict-origin-when-cross-origin"/)
assert.match(out, /loading="lazy"/)
assert.doesNotMatch(out, /allow="[^"]*(autoplay|camera|microphone|geolocation)/)
})

View File

@@ -292,3 +292,44 @@ test('img alt keeps free text with a colon, a question mark and quotes', () => {
// area alt keeps the UGC pattern; only img alt is widened.
assert.equal(sanitizeJournalHtml('<map name="m"><area alt="a: b?" href="/x" /></map>'), '<map name="m"><area href="/x" /></map>')
})
test('the four editor rules accept exactly the editor values (D-05 amended mirror)', () => {
const kept = [
PLACEHOLDER,
'<p><img src="/a.jpg" alt="any text: with ? and &quot;quotes&quot;" /></p>',
'<ol start="1"><li>a</li></ol>',
'<ol start="42"><li>a</li></ol>',
]
for (const html of kept) {
assert.equal(sanitizeJournalHtml(html), html, html)
assert.equal(isSafeJournalHtml(html), true, html)
}
})
test('the Go policy negatives are stripped the same way here', () => {
const cases: Array<[string, string, string]> = [
['upper-case provider', '<figure data-embed="YouTube" data-id="dQw4w9WgXcQ"></figure>', '<figure data-id="dQw4w9WgXcQ"></figure>'],
['provider with a suffix', '<figure data-embed="youtube2" data-id="dQw4w9WgXcQ"></figure>', '<figure data-id="dQw4w9WgXcQ"></figure>'],
['empty id', '<figure data-embed="youtube" data-id=""></figure>', '<figure data-embed="youtube"></figure>'],
['id with a URL', '<figure data-embed="youtube" data-id="javascript:1"></figure>', '<figure data-embed="youtube"></figure>'],
['id with a space', '<figure data-embed="youtube" data-id="dQw4w9 gXcQ"></figure>', '<figure data-embed="youtube"></figure>'],
['embed attributes on p', '<p data-embed="youtube" data-id="dQw4w9WgXcQ">x</p>', '<p>x</p>'],
['embed attributes on a', '<a href="/x" data-embed="youtube" data-id="dQw4w9WgXcQ">x</a>', '<a href="/x">x</a>'],
['data attribute alone on figure', '<figure data-url="https://evil.test/"></figure>', '<figure></figure>'],
['script inside the placeholder', '<figure data-embed="youtube" data-id="dQw4w9WgXcQ"><script>alert(1)</script></figure>', PLACEHOLDER],
['iframe alone', '<iframe src="https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ"></iframe>', ''],
['start with a fraction', '<ol start="1.5"><li>a</li></ol>', '<ol><li>a</li></ol>'],
['alt on p', '<p alt="x">y</p>', '<p>y</p>'],
]
for (const [name, html, want] of cases) {
assert.equal(sanitizeJournalHtml(html), want, name)
assert.equal(isSafeJournalHtml(html), false, `${name} reported safe`)
}
})
test('every corpus entry stays free of danger after the sink pipeline', () => {
for (const entry of CORPUS) {
const out = sanitizeJournalHtml(sanitizeJournalHtml(entry.html))
for (const pattern of DANGER) assert.doesNotMatch(out, pattern, `${entry.name}: ${pattern}`)
}
})