import { describe, test, expect } from 'vitest'; import { purifyHtml } from './HtmlBlock'; import { stableHash } from '../../utils/escape'; import fixtureHtml from './__fixtures__/html-block-test-body.html?raw'; // Ground truth "before" output: purifyHtml(fixtureHtml) computed with the // EXACT HtmlBlock.tsx code as it stood at commit 6a9b227 (the commit // immediately before Task 25 -- `git show // 6a9b227:craft/src/components/basic/HtmlBlock.tsx`), run against the real // dompurify+jsdom, not guessed at or re-derived from reading the code. See // the "byte-diff against 6a9b227" describe block below -- this is the // literal regression check the Task 25 review asked for, after the first // round of `.not.toContain(...)`-style tests passed while FORCE_BODY was // silently changing output for a comment-led, style-free fixture. import preTask25FixtureOutput from './__fixtures__/html-block-test-body.pre-task25-output.html?raw'; describe('purifyHtml', () => { test('strips script tags', () => { expect(purifyHtml('

ok

')).not.toContain(' { const out = purifyHtml('x'); expect(out).not.toContain('onclick'); expect(out).toContain('href="/x"'); }); test('blocks javascript: URLs', () => { expect(purifyHtml('x')).not.toContain('javascript:'); }); test('allows YouTube iframe', () => { const out = purifyHtml(''); expect(out).toContain('youtube.com/embed/abc'); }); test('allows form/input (Task 24: forms are a deliberate escape-hatch addition) but still strips on*/script inside them', () => { const out = purifyHtml('
'); expect(out).toContain(''); expect(out).not.toContain('onfocus'); expect(out).not.toContain(' { test('a style attribute survives sanitization (colour picker output must not be silently dropped)', () => { const out = purifyHtml('

red text

'); expect(out).toBe('

red text

'); }); test('an id attribute survives sanitization (anchor targets)', () => { const out = purifyHtml('link'); expect(out).toContain('id="section"'); }); test('a pasted table survives sanitization', () => { const input = '
Head
Cell
'; expect(purifyHtml(input)).toBe(input); }); test('script tags still do not survive alongside a style attribute', () => { const out = purifyHtml('

ok

'); expect(out).not.toContain(' { const out = purifyHtml('

x

'); expect(out).not.toContain('onclick'); expect(out).toContain('style="color:#ff0000"'); }); test('javascript: URLs still do not survive on an element that also carries style', () => { const out = purifyHtml('x'); expect(out).not.toContain('javascript:'); expect(out).toContain('style="color:#ff0000"'); }); }); describe('purifyHtml iframe sandboxing (M-6)', () => { test('forces a restrictive sandbox attribute onto every iframe', () => { const out = purifyHtml(''); expect(out).toMatch(/]*\bsandbox="[^"]+"/); }); test('sandbox value omits allow-top-navigation (no top-level nav escape)', () => { const out = purifyHtml(''); const sandbox = out.match(/sandbox="([^"]*)"/)![1]; expect(sandbox).not.toMatch(/allow-top-navigation/); }); test('legitimate embeds (YouTube) still work and get sandboxed too', () => { const out = purifyHtml(''); expect(out).toContain('youtube.com/embed/abc'); expect(out).toMatch(/]*\bsandbox="[^"]+"/); }); test('adds referrerpolicy=no-referrer to iframes', () => { const out = purifyHtml(''); expect(out).toContain('referrerpolicy="no-referrer"'); }); test('script/on* attributes are still stripped alongside the sandboxed iframe', () => { const out = purifyHtml(''); expect(out).not.toContain('onload'); expect(out).not.toContain(' { purifyHtml(''); purifyHtml(''); const out = purifyHtml(''); const sandboxMatches = out.match(/sandbox="/g) || []; expect(sandboxMatches.length).toBe(1); }); test('a non-iframe element sanitized alongside an iframe is not touched by the hook', () => { const out = purifyHtml('

hi

'); expect(out).toContain('

hi

'); }); }); describe('purifyHtml -- Task 25: block-scoped (no CSS content) does not trigger a wrapper', () => { const out = purifyHtml('

hi

'); expect(out).not.toContain('
content gets wrapped in a scope-class div', () => { const out = purifyHtml('

Hi

'); expect(out).toMatch(/^
/); expect(out).toContain('

Hi

'); }); test('the style content is rewritten to only match inside the wrapper (the actual leak-prevention property)', () => { const out = purifyHtml('

Hi

'); const scopeClass = out.match(/class="(whp-html-[0-9a-z]+)"/)![1]; expect(out).toContain(`.${scopeClass} h1 { color: red; }`); // The bare, unscoped rule must not appear anywhere in the output -- // that's exactly the leak this feature exists to close. expect(out).not.toContain(''); }); test('scope class is deterministic: the SAME code produces the SAME class across repeated calls', () => { const code = '

x

'; const out1 = purifyHtml(code); const out2 = purifyHtml(code); expect(out1).toBe(out2); const class1 = out1.match(/class="(whp-html-[0-9a-z]+)"/)![1]; const class2 = out2.match(/class="(whp-html-[0-9a-z]+)"/)![1]; expect(class1).toBe(class2); }); test('pinned scope class for a known input -- guards against silent hash-function drift', () => { // If this ever needs to change, it means the hash function itself // changed -- which would silently churn every stored site's HTML on // next save and desync already-published pages from a fresh Preview. // That should be a loud, deliberate decision, not a side-effect of an // unrelated refactor -- hence pinning the literal output here. const code = ''; expect(stableHash(code)).toBe('5fwbyn'); const out = purifyHtml(code); expect(out).toContain('class="whp-html-5fwbyn"'); }); test('scope class is a pure function of `code` -- does not depend on Craft node id or call order', () => { // purifyHtml's signature only ever takes the code string -- there is no // node id parameter it could even reach for. This test documents that // invariant so a future refactor threading a node id through here (as // html-export.ts's renderNode already does for OTHER components, see // its `scopeId` comment) doesn't silently get wired into this path too. const codeA = '

same content

'; const codeB = '

same content

'; expect(codeA).toBe(codeB); // sanity: truly identical strings const outA = purifyHtml(codeA); const outB = purifyHtml(codeB); expect(outA).toBe(outB); }); test('FORCE_BODY regression: a block whose ENTIRE code is a leading '); expect(out).toContain('

Hi

'); expect(out).toContain('

Hi

'); expect(out).toMatch(/

x

'); const scopeClass = out.match(/class="(whp-html-[0-9a-z]+)"/)![1]; expect(out).toContain(`.${scopeClass} { --brand: red; }`); expect(out).toContain(`.${scopeClass} { margin: 0; }`); }); test('@import is stripped end-to-end (network-fetch/exfiltration channel)', () => { const out = purifyHtml('

x

'); expect(out).not.toContain('@import'); expect(out).not.toContain('evil.example'); expect(out).toContain('color:red'); }); test('@keyframes body is not scoped (animation would otherwise break) -- end-to-end through purifyHtml', () => { const out = purifyHtml( '

x

', ); expect(out).toContain('@keyframes spin'); expect(out).toMatch(/@keyframes spin\s*\{\s*from\s*\{\s*opacity:\s*0;?\s*\}\s*to\s*\{\s*opacity:\s*1;?\s*\}\s*\}/); }); test('multiple

A

B

'); const classes = [...out.matchAll(/class="(whp-html-[0-9a-z]+)"/g)].map((m) => m[1]); expect(classes.length).toBeGreaterThanOrEqual(1); expect(new Set(classes).size).toBe(1); // same block -> same scope class everywhere }); }); describe('purifyHtml -- Task 25: security properties of the newly-allowed inside a CSS comment cannot break out into executable markup', () => { const out = purifyHtml( ' */ h1{color:red}

hi

', ); expect(out).not.toContain(' inside a CSS string cannot break out into executable markup', () => { const out = purifyHtml( '"}

hi

', ); expect(out).not.toContain(' { const out = purifyHtml( '

x

y', ); expect(out).not.toMatch(/onclick/i); expect(out).not.toContain(' { const out = purifyHtml(''); expect(out).toMatch(/]*\bsandbox="[^"]+"/); }); test( 'documented reality: DOMPurify does not sanitize CSS declaration values -- ' + 'expression()/behavior/-moz-binding pass through untouched (dead in modern browsers, ' + 'not exploitable there, but not filtered by this pipeline either)', () => { const out = purifyHtml( '
x
', ); expect(out).toContain('expression(alert(1))'); expect(out).toContain('behavior:url(evil.htc)'); expect(out).toContain('-moz-binding:url(evil.xml#x)'); }, ); test('documented reality: url() to a remote host survives (legitimate for background-image, but a known CSS-exfiltration channel already accepted elsewhere in this config)', () => { const out = purifyHtml('
x
'); expect(out).toContain('tracker.example'); }); }); describe('purifyHtml -- review finding: raw byte-diff against HtmlBlock.tsx@6a9b227 (the commit before Task 25)', () => { // Round 1 of this task's tests used `.not.toContain(...)`/`.toContain(...)` // assertions for the "no

x

`; } test('~7000 levels of nested @media does not crash purifyHtml (was: RangeError: Maximum call stack size exceeded)', () => { const code = buildDeeplyNestedMedia(7000); expect(() => purifyHtml(code)).not.toThrow(); const out = purifyHtml(code); expect(out).toContain('

x

'); expect(out).toContain('@media'); }); test('a scope class + wrapper is still produced for the pathological input (best-effort, not a silent no-op)', () => { const code = buildDeeplyNestedMedia(7000); const out = purifyHtml(code); expect(out).toMatch(/^
/); }); }); describe('purifyHtml -- review finding: idempotent over its own prior output', () => { test('running purifyHtml() twice (customer pastes previously-published output into a fresh block) does not nest a second wrapper', () => { const code = '

Hi

'; const once = purifyHtml(code); const twice = purifyHtml(once); expect(twice).toBe(once); // Specifically: no second wrapper div, no double-prefixed selector. expect((twice.match(/
{ const code = '

Hi

'; const gen1 = purifyHtml(code); const gen2 = purifyHtml(gen1); const gen3 = purifyHtml(gen2); expect(gen3).toBe(gen1); }); });