// ============================================================ // Contract test for the Notes reader sanitizer. Confirms that // DOMPurify (loaded via jsdom here) strips the payloads we care // about — script tags, inline event handlers, iframes, object // tags — while preserving the formatting tags we DO allow. // // Why this test exists: // The original Notes module used a homegrown walker-based // sanitizer. When the body round-trips through localStorage or // a compromised row, a bypass in the sanitizer means session // theft. We swapped to DOMPurify; this test holds the contract // so a regression is loud. // ============================================================ const { test } = require('node:test'); const assert = require('node:assert/strict'); const { JSDOM } = require('jsdom'); const createDOMPurify = require('dompurify'); // Match the allowlist used in public/js/notes.js `sanitizeHtml`. const ALLOWED_TAGS = ['p','br','strong','em','b','i','u','s','h2','h3','h4', 'ul','ol','li','a','blockquote','code','pre','hr']; const ALLOWED_ATTR = ['href','target','rel']; function makeSanitizer() { const window = new JSDOM('').window; const DOMPurify = createDOMPurify(window); return function sanitize(html) { return DOMPurify.sanitize(html, { ALLOWED_TAGS, ALLOWED_ATTR, FORBID_ATTR: ['style','onerror','onload','onclick','onmouseover'], ALLOW_DATA_ATTR: false, }); }; } test('strips '); assert.ok(!out.includes('still safe
'); assert.ok(!out.includes('