2026-05-23 14:13:42 -07:00
import { describe , test , expect } from 'vitest' ;
import { purifyHtml } from './HtmlBlock' ;
2026-08-09 17:10:57 -07:00
import { stableHash } from '../../utils/escape' ;
import fixtureHtml from './__fixtures__/html-block-test-body.html?raw' ;
2026-05-23 14:13:42 -07:00
describe ( 'purifyHtml' , () => {
test ( 'strips script tags' , () => {
expect ( purifyHtml ( '<p>ok</p><script>alert(1)</script>' )). not . toContain ( '<script' );
});
test ( 'strips on-event handlers' , () => {
const out = purifyHtml ( '<a onclick="bad()" href="/x">x</a>' );
expect ( out ). not . toContain ( 'onclick' );
expect ( out ). toContain ( 'href="/x"' );
});
test ( 'blocks javascript: URLs' , () => {
expect ( purifyHtml ( '<a href="javascript:void(0)">x</a>' )). not . toContain ( 'javascript:' );
});
test ( 'allows YouTube iframe' , () => {
const out = purifyHtml ( '<iframe src="https://www.youtube.com/embed/abc" allowfullscreen></iframe>' );
expect ( out ). toContain ( 'youtube.com/embed/abc' );
});
2026-08-09 16:36:01 -07:00
test ( 'allows form/input (Task 24: forms are a deliberate escape-hatch addition) but still strips on*/script inside them' , () => {
const out = purifyHtml ( '<form><input name="x" onfocus="bad()"><script>alert(1)</script></form>' );
expect ( out ). toContain ( '<form' );
expect ( out ). toContain ( '<input name="x">' );
expect ( out ). not . toContain ( 'onfocus' );
expect ( out ). not . toContain ( '<script' );
2026-05-23 14:13:42 -07:00
});
});
2026-07-12 18:31:25 -07:00
2026-08-09 12:47:23 -07:00
describe ( 'purifyHtml markup path (C1 review finding)' , () => {
test ( 'a style attribute survives sanitization (colour picker output must not be silently dropped)' , () => {
const out = purifyHtml ( '<p style="color: #ff0000">red text</p>' );
expect ( out ). toBe ( '<p style="color: #ff0000">red text</p>' );
});
test ( 'an id attribute survives sanitization (anchor targets)' , () => {
const out = purifyHtml ( '<a href="#section" id="section">link</a>' );
expect ( out ). toContain ( 'id="section"' );
});
test ( 'a pasted table survives sanitization' , () => {
const input = '<table><thead><tr><th>Head</th></tr></thead><tbody><tr><td>Cell</td></tr></tbody></table>' ;
expect ( purifyHtml ( input )). toBe ( input );
});
test ( 'script tags still do not survive alongside a style attribute' , () => {
const out = purifyHtml ( '<p style="color:#ff0000">ok</p><script>alert(1)</script>' );
expect ( out ). not . toContain ( '<script' );
expect ( out ). toContain ( 'style="color:#ff0000"' );
});
test ( 'on* handlers still do not survive on an element that also carries style' , () => {
const out = purifyHtml ( '<p style="color:#ff0000" onclick="bad()">x</p>' );
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 ( '<a style="color:#ff0000" href="javascript:void(0)">x</a>' );
expect ( out ). not . toContain ( 'javascript:' );
expect ( out ). toContain ( 'style="color:#ff0000"' );
});
});
2026-07-12 18:31:25 -07:00
describe ( 'purifyHtml iframe sandboxing (M-6)' , () => {
test ( 'forces a restrictive sandbox attribute onto every iframe' , () => {
const out = purifyHtml ( '<iframe src="https://example.com/"></iframe>' );
expect ( out ). toMatch ( /<iframe[^>]*\bsandbox="[^"]+"/ );
});
test ( 'sandbox value omits allow-top-navigation (no top-level nav escape)' , () => {
const out = purifyHtml ( '<iframe src="https://example.com/"></iframe>' );
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 ( '<iframe src="https://www.youtube.com/embed/abc" allowfullscreen></iframe>' );
expect ( out ). toContain ( 'youtube.com/embed/abc' );
expect ( out ). toMatch ( /<iframe[^>]*\bsandbox="[^"]+"/ );
});
test ( 'adds referrerpolicy=no-referrer to iframes' , () => {
const out = purifyHtml ( '<iframe src="https://example.com/"></iframe>' );
expect ( out ). toContain ( 'referrerpolicy="no-referrer"' );
});
test ( 'script/on* attributes are still stripped alongside the sandboxed iframe' , () => {
const out = purifyHtml ( '<iframe src="https://example.com/" onload="alert(1)"></iframe><script>alert(2)</script>' );
expect ( out ). not . toContain ( 'onload' );
expect ( out ). not . toContain ( '<script' );
});
test ( 'repeated calls do not leak/accumulate the hook (no duplicate sandbox attr, no cross-call state)' , () => {
purifyHtml ( '<iframe src="https://a.example/"></iframe>' );
purifyHtml ( '<iframe src="https://b.example/"></iframe>' );
const out = purifyHtml ( '<iframe src="https://c.example/"></iframe>' );
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 ( '<p>hi</p><iframe src="https://example.com/"></iframe>' );
expect ( out ). toContain ( '<p>hi</p>' );
});
});
2026-08-09 17:10:57 -07:00
describe ( 'purifyHtml -- Task 25: block-scoped <style> support' , () => {
test ( 'a block with no <style> at all is untouched: no wrapper div added' , () => {
const out = purifyHtml ( '<p>hello</p>' );
expect ( out ). toBe ( '<p>hello</p>' );
expect ( out ). not . toContain ( '<div' );
});
test ( 'blocks WITHOUT <style> are byte-identical to pre-Task-25 output (no wrapper regression)' , () => {
// Same representative inputs the Task 24 suite already pins to an exact
// string -- re-asserted here under the Task 25 name so a future change
// that starts wrapping every block (not just style-bearing ones) fails
// loudly and obviously, not just as a Task 24 side-effect.
expect ( purifyHtml ( '<p style="color: #ff0000">red text</p>' )). toBe ( '<p style="color: #ff0000">red text</p>' );
const table = '<table><thead><tr><th>Head</th></tr></thead><tbody><tr><td>Cell</td></tr></tbody></table>' ;
expect ( purifyHtml ( table )). toBe ( table );
expect ( purifyHtml ( '<a href="/x">x</a>' )). toBe ( '<a href="/x">x</a>' );
});
test ( 'the full Task 24 fixture (no <style> in it) produces no wrapper and is unaffected' , () => {
// The fixture is the broadest real-world stand-in this repo has for
// "a customer's actual pasted block". It contains no <style>, so this
// is the closest thing to a real before/after diff over a large,
// realistic input: the only lever Task 25 pulled (allowing <style> +
// FORCE_BODY) must produce PRECISELY the same output as before for
// content that never touches that lever.
const out = purifyHtml ( fixtureHtml );
expect ( out ). not . toContain ( '<div class="whp-html-' );
expect ( out ). not . toMatch ( /<style[\s>]/i ); // still no bare <style> in this fixture
});
test ( 'an empty <style></style> (no CSS content) does not trigger a wrapper' , () => {
const out = purifyHtml ( '<p>hi</p><style></style>' );
expect ( out ). not . toContain ( '<div class="whp-html-' );
});
test ( 'a whitespace-only <style> does not trigger a wrapper' , () => {
const out = purifyHtml ( '<p>hi</p><style> \n </style>' );
expect ( out ). not . toContain ( '<div class="whp-html-' );
});
test ( 'a block WITH real <style> content gets wrapped in a scope-class div' , () => {
const out = purifyHtml ( '<style>h1 { color: red; }</style><h1>Hi</h1>' );
expect ( out ). toMatch ( /^<div class="whp-html-[0-9a-z]+">/ );
expect ( out ). toContain ( '<h1>Hi</h1>' );
});
test ( 'the style content is rewritten to only match inside the wrapper (the actual leak-prevention property)' , () => {
const out = purifyHtml ( '<style>h1 { color: red; }</style><h1>Hi</h1>' );
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 ( '<style>h1 { color: red; }</style>' );
});
test ( 'scope class is deterministic: the SAME code produces the SAME class across repeated calls' , () => {
const code = '<style>p { color: blue; }</style><p>x</p>' ;
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 = '<style>h1{color:red}</style>' ;
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 = '<style>h1 { color: red; }</style><h1>same content</h1>' ;
const codeB = '<style>h1 { color: red; }</style><h1>same content</h1>' ;
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 <style> (nothing before it) still survives' , () => {
// Without FORCE_BODY, DOMPurify parses `code` via DOMParser as a mini
// HTML document and serializes only <body>. Per the HTML5 parsing
// algorithm, a <style> tag with nothing before it is implicitly placed
// in the parser's <head>, which DOMPurify's body-only serialization
// never looks at -- the whole block would silently vanish. Confirmed
// empirically against dompurify+jsdom directly before this fix existed.
const out = purifyHtml ( '<style>h1{color:red}</style>' );
expect ( out ). toContain ( '<style>' );
expect ( out ). toContain ( 'color:red' );
});
test ( 'FORCE_BODY regression: leading <style> immediately followed by markup, both survive' , () => {
const out = purifyHtml ( '<style>h1{color:red}</style><h1>Hi</h1>' );
expect ( out ). toContain ( '<h1>Hi</h1>' );
expect ( out ). toMatch ( /<style>[\s\S]*color:\s*red/ );
});
test ( ':root / html / body inside a block map to the block wrapper itself (end-to-end through purifyHtml)' , () => {
const out = purifyHtml ( '<style>:root { --brand: red; } body { margin: 0; }</style><p>x</p>' );
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 ( '<style>@import url("https://evil.example/x.css"); h1{color:red}</style><h1>x</h1>' );
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 (
'<style>@keyframes spin { from { opacity: 0; } to { opacity: 1; } }</style><h1>x</h1>' ,
);
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 <style> blocks in one Custom HTML block are each scoped under the SAME class' , () => {
const out = purifyHtml ( '<style>h1{color:red}</style><h1>A</h1><style>p{color:blue}</style><p>B</p>' );
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 <style>' , () => {
test ( '</style> inside a CSS comment cannot break out into executable markup' , () => {
const out = purifyHtml (
'<style>/* </style><script>alert(1)</script> */ h1{color:red}</style><p>hi</p>' ,
);
expect ( out ). not . toContain ( '<script' );
expect ( out ). not . toMatch ( /on[a-z]+\s*=/i );
});
test ( '</style> inside a CSS string cannot break out into executable markup' , () => {
const out = purifyHtml (
'<style>h1::before{content:"</style><script>alert(1)</script>"}</style><p>hi</p>' ,
);
expect ( out ). not . toContain ( '<script' );
});
test ( 'script/on*/javascript: are still stripped from markup sitting alongside a styled block' , () => {
const out = purifyHtml (
'<style>h1{color:red}</style><p onclick="alert(1)">x</p><script>alert(2)</script><a href="javascript:alert(3)">y</a>' ,
);
expect ( out ). not . toMatch ( /onclick/i );
expect ( out ). not . toContain ( '<script' );
expect ( out ). not . toContain ( 'javascript:' );
});
test ( 'iframe sandboxing still applies alongside a styled block' , () => {
const out = purifyHtml ( '<style>h1{color:red}</style><iframe src="https://example.com/"></iframe>' );
expect ( out ). toMatch ( /<iframe[^>]*\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 (
'<style>div{width:expression(alert(1));behavior:url(evil.htc);-moz-binding:url(evil.xml#x)}</style><div>x</div>' ,
);
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 ( '<style>div{background:url(https://tracker.example/pixel.png)}</style><div>x</div>' );
expect ( out ). toContain ( 'tracker.example' );
});
});