// The HTML gate mirrors the Journal plugin's FormatHTML / CheckHTML gate. // The unsafe cases are the six of TestFormatHTMLRejectsUnsafeHTML in // sm-journal-plugin classes/format_html_test.go, as rendered HTML. import { test } from 'node:test' import assert from 'node:assert/strict' import { DANGEROUS_URL, EVENT_HANDLER, UNSAFE_TAG, gateJournalHtml, isSafeJournalHtml } from '../app/utils/htmlGate.ts' const GO_CASES: Array<[string, string]> = [ ['script', ''], ['iframe', ''], ['event', ''], ['javascript', 'x'], ['vbscript', 'x'], ['data', 'x'], ] test('the patterns are the Go regex sources, case-insensitive', () => { assert.equal(UNSAFE_TAG.source, '<(?:script|iframe|object|embed)\\b') assert.equal(EVENT_HANDLER.source, '\\son[a-z]+\\s*=') assert.equal(DANGEROUS_URL.source, '(?:javascript|vbscript|data):') for (const re of [UNSAFE_TAG, EVENT_HANDLER, DANGEROUS_URL]) assert.ok(re.flags.includes('i')) }) for (const [name, html] of GO_CASES) { test(`Go case ${name} is unsafe`, () => { assert.equal(isSafeJournalHtml(html), false) assert.deepEqual(gateJournalHtml(html), { safe: false, html: '' }) }) test(`Go case ${name} in upper case is unsafe`, () => { const upper = html.toUpperCase() assert.equal(isSafeJournalHtml(upper), false) assert.deepEqual(gateJournalHtml(upper), { safe: false, html: '' }) }) } test('object and embed tags and spaced handlers are unsafe', () => { assert.equal(isSafeJournalHtml(''), false) assert.equal(isSafeJournalHtml(''), false) assert.equal(isSafeJournalHtml(''), false) }) test('headings, tables, footnotes and links are safe and pass unchanged', () => { const safe = [ '

Hello

', '
ab
12
', '

See footnote.1

', '

Artykuły las

', ] for (const html of safe) { assert.equal(isSafeJournalHtml(html), true, html) assert.deepEqual(gateJournalHtml(html), { safe: true, html }) } }) test('words starting with "on" are not handlers without an =', () => { assert.equal(isSafeJournalHtml('

one of the onions

'), true) }) test('empty input is safe and empty', () => { for (const html of [null, undefined, '']) { assert.equal(isSafeJournalHtml(html), true) assert.deepEqual(gateJournalHtml(html), { safe: true, html: '' }) } })