diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue index 7387f063..6c55d9e1 100644 --- a/frontend/src/components/document-preview.vue +++ b/frontend/src/components/document-preview.vue @@ -10,6 +10,7 @@ import 'katex/dist/katex.min.css'; import { useI18n } from 'vue-i18n'; import { sanitizeHTML, sanitizeMarkdownHTML } from '@/utils/security'; import { renderDocumentPreviewMarkdown } from '@/utils/documentPreviewMarkdown'; +import { buildHtmlPreview } from '@/utils/htmlPreview'; import { openMermaidFullscreen } from '@/utils/mermaidViewer'; import { renderMermaidToSvg } from '@/utils/mermaidShared'; import { @@ -405,7 +406,8 @@ async function loadPreview() { } case 'html': { if (allowsHtmlScriptPreview()) { - blobUrl.value = URL.createObjectURL(blob); + const previewHtml = buildHtmlPreview(await blob.text()); + blobUrl.value = URL.createObjectURL(new Blob([previewHtml], { type: 'text/html;charset=utf-8' })); } await renderText(blob, ft || 'html'); break; diff --git a/frontend/src/utils/htmlPreview.test.ts b/frontend/src/utils/htmlPreview.test.ts new file mode 100644 index 00000000..277b860f --- /dev/null +++ b/frontend/src/utils/htmlPreview.test.ts @@ -0,0 +1,115 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' +import vm from 'node:vm' +import { buildHtmlPreview } from './htmlPreview.ts' + +const bootstrap = buildHtmlPreview('').slice(''.length) + +function createFrame(overrides: Record = {}) { + const window = Object.defineProperties({}, { + localStorage: { configurable: true, get() { throw new Error('SecurityError') } }, + sessionStorage: { configurable: true, get() { throw new Error('SecurityError') } }, + }) as Window + const context = vm.createContext({ window, ...overrides }) + vm.runInContext(bootstrap, context) + return { window, context } +} + +test('blocked storage no longer prevents generated page initialization and event handlers', () => { + const button = { onclick: null as null | (() => void) } + const { window, context } = createFrame({ button }) + vm.runInContext(` + let best = Number(window.localStorage.getItem('best')) || 0; + button.onclick = () => window.localStorage.setItem('best', ++best); + `, context) + assert.equal(typeof button.onclick, 'function') + button.onclick!() + button.onclick!() + assert.equal(window.localStorage.getItem('best'), '2') +}) + +test('memory storage supports string conversion, keys, removal and clearing', () => { + const { localStorage: storage } = createFrame().window + assert.equal(storage.getItem('missing'), null) + assert.equal(storage.length, 0) + storage.setItem('best', 42 as unknown as string) + storage.setItem('empty', '') + storage.setItem('null', null as unknown as string) + assert.equal(storage.getItem('best'), '42') + assert.equal(storage.getItem('empty'), '') + assert.equal(storage.getItem('null'), 'null') + assert.equal(storage.length, 3) + assert.equal(storage.key(0), 'best') + assert.equal(storage.key(3), null) + assert.equal(storage.key(-1), null) + storage.removeItem('best') + assert.equal(storage.key(0), 'empty') + storage.clear() + assert.equal(storage.length, 0) +}) + +test('property access, deletion and enumeration share the same stored values', () => { + const { localStorage: storage } = createFrame().window + storage.score = 128 + assert.equal(storage.getItem('score'), '128') + storage.setItem('board', '[2,4]') + assert.equal(storage.board, '[2,4]') + assert.deepEqual(Object.keys(storage), ['score', 'board']) + assert.equal(JSON.stringify(storage), '{"score":"128","board":"[2,4]"}') + assert.equal('score' in storage, true) + delete storage.score + assert.equal(storage.score, undefined) + assert.equal('score' in storage, false) + storage.setItem('__proto__', 'safe') + assert.equal(storage.getItem('__proto__'), 'safe') + assert.equal(Object.getPrototypeOf(storage), null) + storage.setItem('getItem', 'stored') + assert.equal(storage.getItem('getItem'), 'stored') +}) + +test('each frame and each storage area has independent, temporary state', () => { + const first = createFrame().window + const second = createFrame().window + first.localStorage.setItem('best', '100') + first.sessionStorage.setItem('best', '20') + assert.equal(first.localStorage.getItem('best'), '100') + assert.equal(first.sessionStorage.getItem('best'), '20') + assert.equal(second.localStorage.getItem('best'), null) + assert.equal(second.sessionStorage.getItem('best'), null) +}) + +test('available native storage is preserved independently for each area', () => { + const native = { length: 1, getItem: () => 'existing' } + const window = { localStorage: native } + vm.runInNewContext(bootstrap, { window }) + assert.equal(window.localStorage, native) + assert.equal(window.localStorage.getItem(), 'existing') + assert.equal((window as unknown as Window).sessionStorage.length, 0) +}) + +test('bootstrap precedes application scripts while preserving doctype, head and all source bytes', () => { + const cases = [ + ['', ''], + ['', ''], + ['\uFEFF\n\n\n\n', ''], + ['', ''], + ['', ''], + ] + for (const [prefix, rest] of cases) { + const source = prefix + rest + const rendered = buildHtmlPreview(source) + assert.equal(rendered, prefix + buildHtmlPreview('') + rest) + assert.equal(rendered.replace(buildHtmlPreview(''), ''), source) + } +}) + +test('only executable artifact previews use the compatibility copy; source and sandbox stay intact', () => { + const source = readFileSync(new URL('../components/document-preview.vue', import.meta.url), 'utf8') + const htmlCase = source.slice(source.indexOf("case 'html':"), source.indexOf("case 'docx':")) + assert.match(htmlCase, /if \(allowsHtmlScriptPreview\(\)\) \{\s*const previewHtml = buildHtmlPreview\(await blob\.text\(\)\)/) + assert.match(htmlCase, /new Blob\(\[previewHtml\], \{ type: 'text\/html;charset=utf-8' \}\)/) + assert.match(htmlCase, /await renderText\(blob, ft \|\| 'html'\)/) + assert.match(source, /sandbox="allow-scripts"/) + assert.doesNotMatch(source, /allow-same-origin/) +}) diff --git a/frontend/src/utils/htmlPreview.ts b/frontend/src/utils/htmlPreview.ts new file mode 100644 index 00000000..60ebe865 --- /dev/null +++ b/frontend/src/utils/htmlPreview.ts @@ -0,0 +1,68 @@ +// This is a standalone script: it runs inside the opaque-origin iframe, never +// in the application window. Keep it independent of imports and bundler helpers. +// Each frame owns two ephemeral stores, discarded when the frame is reloaded. +const storageBootstrap = `` + +/** Add compatibility only to the rendered copy, preserving the original source. */ +export function buildHtmlPreview(html: string): string { + // Keep the doctype first (standards mode), and use an existing head when + // present. Only inspect the document prefix: text resembling tags inside + // comments, scripts or the body must never become an insertion point. + const trivia = String.raw`(?:\s|)*` + const attributes = String.raw`(?:[^<>"']|"[^"]*"|'[^']*')*` + const prefix = new RegExp( + `^${trivia}(?:${trivia})?(?:${trivia})?(?:)?`, + 'i', + ).exec(html)?.[0] ?? '' + return prefix + storageBootstrap + html.slice(prefix.length) +}