2026-05-23 14:13:42 -07:00
|
|
|
import { describe, test, expect } from 'vitest';
|
|
|
|
|
import { purifyHtml } from './HtmlBlock';
|
|
|
|
|
|
|
|
|
|
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>');
|
|
|
|
|
});
|
|
|
|
|
});
|