test(embeds): add registry, pair-aware sanitizer, hydration tests, and README docs
This commit is contained in:
@@ -2,11 +2,22 @@
|
||||
// <figure data-embed="youtube" data-id="ID"></figure>; the sinks render
|
||||
// expandEmbeds(sanitizeJournalHtml(html)), so the sanitizer runs first and the
|
||||
// only iframe in the output is the one built from a validated placeholder.
|
||||
import { test } from 'node:test'
|
||||
import { afterEach, test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { EMBED_PLACEHOLDER, EMBED_PROVIDERS, expandEmbeds } from '../app/utils/embeds.ts'
|
||||
import { sanitizeJournalHtml } from '../app/utils/htmlGate.ts'
|
||||
import {
|
||||
createJournalEmbedRegistry,
|
||||
defineJournalEmbedProviders,
|
||||
getJournalEmbedRegistry,
|
||||
registerJournalEmbedProvider,
|
||||
resetJournalEmbedRegistry,
|
||||
} from '../app/utils/embedRegistry.ts'
|
||||
import { gateJournalHtml, isSafeJournalHtml, sanitizeJournalHtml } from '../app/utils/htmlGate.ts'
|
||||
|
||||
afterEach(() => {
|
||||
resetJournalEmbedRegistry()
|
||||
})
|
||||
|
||||
const ID = 'dQw4w9WgXcQ'
|
||||
const PLACEHOLDER = `<figure data-embed="youtube" data-id="${ID}"></figure>`
|
||||
@@ -175,3 +186,121 @@ test('the iframe src is a fixed host plus the validated id, never stored text',
|
||||
assert.match(out, /loading="lazy"/)
|
||||
assert.doesNotMatch(out, /allow="[^"]*(autoplay|camera|microphone|geolocation)/)
|
||||
})
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Phase 38: Extensible embed provider registry, pair-aware sanitizer & SSR
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
test('registry supports dual registration helpers and rejects prototype-pollution keys', () => {
|
||||
const scoped = createJournalEmbedRegistry({
|
||||
viz: { idPattern: /^[a-z0-9][a-z0-9-]{0,63}$/ },
|
||||
})
|
||||
assert.equal(scoped.has('youtube'), true)
|
||||
assert.equal(scoped.has('viz'), true)
|
||||
assert.equal(scoped.match('viz', 'chart-2026'), true)
|
||||
assert.equal(scoped.match('viz', 'Chart-2026'), false)
|
||||
assert.equal(scoped.match('youtube', 'chart-2026'), false)
|
||||
|
||||
// Prototype keys never match and cannot be registered.
|
||||
for (const bad of ['__proto__', 'constructor', 'toString', 'valueOf', '../x', 'YouTube', '']) {
|
||||
assert.equal(scoped.has(bad), false, bad)
|
||||
assert.equal(scoped.get(bad), undefined, bad)
|
||||
assert.equal(scoped.match(bad, 'abc'), false, bad)
|
||||
assert.throws(() => scoped.register(bad, { idPattern: /^[a-z]+$/ }), /Invalid journal embed provider key/)
|
||||
}
|
||||
|
||||
// Global singleton helpers + reset.
|
||||
registerJournalEmbedProvider('viz', { idPattern: '^[a-z0-9-]+$' })
|
||||
defineJournalEmbedProviders({ acme: { idPattern: /^acme-[0-9]+$/ } })
|
||||
assert.equal(getJournalEmbedRegistry().has('viz'), true)
|
||||
assert.equal(getJournalEmbedRegistry().has('acme'), true)
|
||||
resetJournalEmbedRegistry()
|
||||
assert.equal(getJournalEmbedRegistry().has('viz'), false)
|
||||
assert.equal(getJournalEmbedRegistry().has('acme'), false)
|
||||
assert.equal(getJournalEmbedRegistry().has('youtube'), true)
|
||||
})
|
||||
|
||||
test('pair-aware htmlGate allows custom embed with responsive fallback img and strips mismatches/XSS', () => {
|
||||
registerJournalEmbedProvider('viz', {
|
||||
idPattern: /^[a-z0-9][a-z0-9-]{0,63}$/,
|
||||
mount: () => {},
|
||||
})
|
||||
|
||||
const valid =
|
||||
'<figure data-embed="viz" data-id="lions-mane-studies" data-aspect="16/9" data-height="420px">' +
|
||||
'<img src="/media/abc123/chart.webp" srcset="/media/abc123/chart-480w.webp 480w, /media/abc123/chart-960w.webp 960w" sizes="(max-width: 768px) 100vw, 800px" alt="Chart" width="800" height="450" />' +
|
||||
'<figcaption>Figure 1</figcaption>' +
|
||||
'</figure>'
|
||||
assert.equal(sanitizeJournalHtml(valid), valid)
|
||||
assert.equal(isSafeJournalHtml(valid), true)
|
||||
assert.equal(gateJournalHtml(valid).html, valid)
|
||||
|
||||
// Cross-provider ID mismatches strip all embed attributes.
|
||||
assert.equal(
|
||||
sanitizeJournalHtml('<figure data-embed="youtube" data-id="lions-mane-studies" data-aspect="16/9"></figure>'),
|
||||
'<figure></figure>',
|
||||
)
|
||||
assert.equal(
|
||||
sanitizeJournalHtml(`<figure data-embed="viz" data-id="${ID}" data-height="420px"></figure>`),
|
||||
'<figure></figure>',
|
||||
)
|
||||
assert.equal(isSafeJournalHtml('<figure data-embed="youtube" data-id="lions-mane-studies"></figure>'), false)
|
||||
|
||||
// Invalid data-aspect / data-height (CSS injection vectors) are stripped.
|
||||
assert.equal(
|
||||
sanitizeJournalHtml('<figure data-embed="viz" data-id="ok-id" data-aspect="16/9; background:url(javascript:alert(1))"></figure>'),
|
||||
'<figure data-embed="viz" data-id="ok-id"></figure>',
|
||||
)
|
||||
assert.equal(
|
||||
sanitizeJournalHtml('<figure data-embed="viz" data-id="ok-id" data-height="-100px"></figure>'),
|
||||
'<figure data-embed="viz" data-id="ok-id"></figure>',
|
||||
)
|
||||
|
||||
// XSS inside custom embed is stripped while safe fallback survives.
|
||||
const xss =
|
||||
'<figure data-embed="viz" data-id="ok-id" data-aspect="4/3">' +
|
||||
'<script>alert(1)</script>' +
|
||||
'<iframe src="https://evil.test"></iframe>' +
|
||||
'<svg onload="alert(1)"><circle r="1"/></svg>' +
|
||||
'<img src="/media/abc/ok.webp" alt="Safe" onerror="alert(1)">' +
|
||||
'<figcaption>Caption</figcaption>' +
|
||||
'</figure>'
|
||||
const cleaned = sanitizeJournalHtml(xss)
|
||||
assert.doesNotMatch(cleaned, /<script|<iframe|<svg|onerror|alert/i)
|
||||
assert.equal(
|
||||
cleaned,
|
||||
'<figure data-embed="viz" data-id="ok-id" data-aspect="4/3"><img src="/media/abc/ok.webp" alt="Safe" /><figcaption>Caption</figcaption></figure>',
|
||||
)
|
||||
})
|
||||
|
||||
test('expandEmbeds preserves SSR fallback + adds classes/CLS styles for client-hydrated providers and allows youtube override', () => {
|
||||
registerJournalEmbedProvider('viz', {
|
||||
idPattern: /^[a-z0-9-]+$/,
|
||||
mount: () => {},
|
||||
aspectRatio: '16/9',
|
||||
minHeight: '360px',
|
||||
})
|
||||
|
||||
const raw =
|
||||
'<figure data-embed="viz" data-id="chart-1" data-aspect="4/3">' +
|
||||
'<img src="/media/abc/chart.webp" alt="Chart" width="800" height="600">' +
|
||||
'<figcaption>Figure 1</figcaption>' +
|
||||
'</figure>'
|
||||
const expanded = expandEmbeds(sanitizeJournalHtml(raw))
|
||||
assert.match(expanded, /class="journal-embed journal-embed--viz"/)
|
||||
assert.match(expanded, /data-embed="viz"/)
|
||||
assert.match(expanded, /data-id="chart-1"/)
|
||||
assert.match(expanded, /style="aspect-ratio:4\/3;min-height:360px"/)
|
||||
assert.match(expanded, /<img src="\/media\/abc\/chart\.webp" alt="Chart" width="800" height="600" \/>/)
|
||||
assert.match(expanded, /<figcaption>Figure 1<\/figcaption>/)
|
||||
|
||||
// Built-in YouTube remains byte-identical.
|
||||
assert.equal(expandEmbeds(PLACEHOLDER), IFRAME)
|
||||
|
||||
// Overriding youtube replaces SSR rendering.
|
||||
registerJournalEmbedProvider('youtube', {
|
||||
idPattern: /^[A-Za-z0-9_-]{11}$/,
|
||||
renderSsr: (id) => `<figure class="custom-yt" data-id="${id}"></figure>`,
|
||||
})
|
||||
assert.equal(expandEmbeds(PLACEHOLDER), `<figure class="custom-yt" data-id="${ID}"></figure>`)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user