Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
Preview: leave <noscript>, so iOS can thumbnail a deck too
nyblneted8e65028bbb
diff --git a/kernel/src/save.ts b/kernel/src/save.tsindex 07718a47..06c09b2b 100644--- a/kernel/src/save.ts+++ b/kernel/src/save.ts@@ -212,13 +212,19 @@ const NOSCRIPT_CLOSE = '</nosc' + 'ript'/*** Refuse any preview markup that could unbalance the file.*- * The preview is generated from user content, so it is not shaped by us. Two- * ways it could corrupt the document: a `<script>`/`</script>` would break the- * open/close balance the frozen splice contract (and `scripts/shell-gate.mjs`)- * depends on, and a `</noscript>` would end the host element early, spilling- * the rest of the preview into the visible page. The app sanitizes its own- * output; this is the kernel refusing to take its word for it. Dropping the- * preview costs a thumbnail. Emitting it anyway could brick the file.+ * The preview is generated from user content, so it is not shaped by us. A+ * `<script>`/`</script>` in it would break the open/close balance the frozen+ * splice contract (and `scripts/shell-gate.mjs`) depends on. `</noscript>` is+ * still refused although the preview no longer lives in a `<noscript>`: it+ * costs nothing, and a document written by an older Bento may still carry one.+ *+ * This check got MORE load-bearing when the preview left `<noscript>`. It is no+ * longer inert markup that only a scripting-less renderer ever parses — it now+ * lands in the live DOM of every reader's page until the remover runs.+ *+ * The app sanitizes its own output; this is the kernel refusing to take its+ * word for it. Dropping the preview costs a thumbnail. Emitting it anyway could+ * brick the file.** Exported for `scripts/test-preview.ts`, like previewAllowed.*/@@ -227,6 +233,20 @@ export function previewIsSafe(html: string): boolean {return !lower.includes(SCRIPT_OPEN) && !lower.includes(SCRIPT_CLOSE_START) && !lower.includes(NOSCRIPT_CLOSE)}+/**+ * Deletes the preview, and itself, the instant the parser reaches it.+ *+ * Parser-BLOCKING on purpose: a classic inline script placed immediately after+ * the preview runs before the parser continues, so the browser never paints a+ * frame containing it. That is what makes this free for readers.+ *+ * It is written as a string rather than built from a template because it must+ * stay one line and contain no `</script>`.+ */+const PREVIEW_REMOVER =+ `(function(){var a=document.querySelectorAll('[${PREVIEW_ATTR}]');` ++ `for(var i=a.length;i--;){var n=a[i];if(n.parentNode)n.parentNode.removeChild(n)}})()`+function writePreview(clone: Document, body: string, doc: KernelDoc): void {// REPLACE, NEVER APPEND. `capturePristine()` snapshots the document as it// was loaded, so the shell we are cloning already carries the preview the@@ -235,7 +255,10 @@ function writePreview(clone: Document, body: string, doc: KernelDoc): void {// whether to write a new one — is also how a preview correctly DISAPPEARS// when a plaintext deck gains a password, or when an app stops providing// one. Both of those are silent leaks if the removal is conditional.- for (const stale of Array.from(clone.querySelectorAll(`noscript[${PREVIEW_ATTR}]`))) stale.remove()+ // The selector is deliberately attribute-only: it must sweep the host AND the+ // remover script beside it, and it must still find previews written by an+ // older Bento, which parked them in a <noscript>.+ for (const stale of Array.from(clone.querySelectorAll(`[${PREVIEW_ATTR}]`))) stale.remove()if (!previewProvider || !previewAllowed(body)) return@@ -250,18 +273,44 @@ function writePreview(clone: Document, body: string, doc: KernelDoc): void {}if (!el) return- const host = clone.createElement('noscript')+ // ORDINARY MARKUP, NOT <noscript>, AND A PARSER-BLOCKING REMOVER.+ //+ // `<noscript>` was the obvious home and it is wrong here. It renders only+ // where scripting is DISABLED, and iOS — the platform this feature exists for+ // — satisfies neither half of that: probed with a page whose inline script+ // repaints it, the iOS thumbnailer renders neither the script's result nor+ // the <noscript>, so a deck thumbnailed as its boot splash no matter what we+ // put in the noscript.+ //+ // Since that thumbnailer runs no script, plain markup survives for it. And+ // since every real reader DOES run script, a parser-blocking inline remover+ // placed immediately after deletes the preview before the browser paints a+ // frame containing it. Both audiences get the right answer with no flash and+ // no compromise — which the <noscript> version could not manage.+ //+ // A reader with scripting genuinely off keeps the preview on screen, exactly+ // as before: without scripts the deck cannot render at all, so a still of+ // page one is the best available answer rather than a regression.+ const host = clone.createElement('div')host.setAttribute(PREVIEW_ATTR, '1')host.appendChild(clone.importNode(el, true))if (!previewIsSafe(host.innerHTML)) {console.warn('bento: first-page preview rejected as unsafe, saving without one')return}+ const remover = clone.createElement('script')+ remover.setAttribute(PREVIEW_ATTR, '1')+ remover.textContent = PREVIEW_REMOVER+// Straight after the splash it replaces, so a thumbnailer reaches it before- // the ~550KB of compressed payload at the end of the body.+ // the ~550KB of compressed payload at the end of the body. The remover goes+ // immediately after the host: any markup between them is markup the parser+ // could paint first.const splash = clone.getElementById('bento-splash')- if (splash?.parentNode) splash.parentNode.insertBefore(host, splash.nextSibling)- else (clone.body ?? clone.documentElement).appendChild(host)+ const parent = splash?.parentNode ?? clone.body ?? clone.documentElement+ const after = splash?.parentNode ? splash.nextSibling : null+ parent.insertBefore(host, after)+ parent.insertBefore(remover, host.nextSibling)}/**diff --git a/scripts/shell-gate.mjs b/scripts/shell-gate.mjsindex 8b6e79c1..bf52c727 100644--- a/scripts/shell-gate.mjs+++ b/scripts/shell-gate.mjs@@ -352,31 +352,37 @@ function checkEscapeIsImplemented() {// --- invariant 5: a preview-carrying shell ---------------------------------//// The THIRD class of plaintext content a saved file carries: the static-// first-page preview `kernel/src/save.ts` writes into a-// `<noscript data-bento-preview>` so file managers can thumbnail the deck-// (docs/DECISIONS.md). Like a language pack it is not shaped by us — it is-// rendered from whatever the author typed on page one — but unlike a pack it-// is HTML, not JSON in a script block, so the `<` escape does not and-// cannot apply to it. Its safety rests on the kernel REFUSING to emit markup-// containing a script tag or a `</noscript>`, which is what this checks.+// first-page preview `kernel/src/save.ts` writes as a `[data-bento-preview]`+// element, followed by a parser-blocking remover script that deletes it before+// the page is ever painted (docs/DECISIONS.md). Like a language pack it is not+// shaped by us — it is rendered from whatever the author typed on page one —+// but unlike a pack it is HTML, not JSON in a script block, so the `<`+// escape does not and cannot apply to it. Its safety rests on the kernel+// REFUSING to emit markup containing a script tag, which is what this checks —+// and that refusal carries MORE weight now the preview lands in the live DOM+// instead of sitting inert inside a `<noscript>`.const NOSCRIPT_CLOSE = '</nosc' + 'ript>'+const SCRIPT_OPEN_TAG = '<scr' + 'ipt'/** A preview whose content came from a hostile deck: entity-escaped exactly as* a DOM serializer escapes text, plus quotes, RTL, emoji and separators. */const ADVERSARIAL_PREVIEW =- `<noscript data-bento-preview="1"><div style="position:fixed;left:0;top:0;background:#0D1B2E">` ++ `<div data-bento-preview="1"><div style="position:fixed;left:0;top:0;background:#0D1B2E">` +`<div></script> <script>alert(1)</script> ` +`<script type="application/bento+json" id="bento-doc">{"hijacked":true}</script>` +`</noscript> <!-- --> <![CDATA[ & " ' \` \\ ` +`مرحبا שלום 🎌 a b c</div>` +- `</div>${NOSCRIPT_CLOSE}`+ `</div></div>` ++ // the remover that now ships beside every preview: a REAL script element,+ // so the shell's open/close balance has to survive one more of them+ `${SCRIPT_OPEN_TAG} data-bento-preview="1">/* remover */${SCRIPT_CLOSE}`/** …and the same content with the escapes NOT applied — what the kernel's* `previewIsSafe` refusal exists to keep out of a file. */const UNSAFE_PREVIEW =- `<noscript data-bento-preview="1"><div>${SCRIPT_CLOSE} ` +- `${DOC_BLOCK_OPEN}{"hijacked":true}${SCRIPT_CLOSE}</div>${NOSCRIPT_CLOSE}`+ `<div data-bento-preview="1"><div>${SCRIPT_CLOSE} ` ++ `${DOC_BLOCK_OPEN}{"hijacked":true}${SCRIPT_CLOSE}</div></div>`/** Insert a preview where `writePreview` does — straight after the splash. */const withPreview = (html, preview) =>@@ -429,6 +435,12 @@ function checkPreviewRulesAreImplemented() {fail('kernel/src/save.ts previewAllowed no longer checks BOTH the password flag and the body envelope')if (!/previewIsSafe\(host\.innerHTML\)/.test(text))fail('kernel/src/save.ts no longer refuses unsafe first-page preview markup')+ // The preview is a full-bleed overlay. Since it left <noscript> it is live+ // markup, and the remover is the ONLY thing keeping it off a reader's screen.+ if (!/PREVIEW_REMOVER/.test(text) || !/removeChild/.test(text))+ fail('kernel/src/save.ts no longer emits the preview remover — every reader would stare at page one')+ if (!/insertBefore\(remover, host\.nextSibling\)/.test(text))+ fail('kernel/src/save.ts no longer places the remover immediately after the preview — a frame could paint between the two')}/** Throws with a GATE: message on the first violated invariant. */diff --git a/scripts/test-preview.ts b/scripts/test-preview.tsindex 9ab765b0..a0b81cfb 100644--- a/scripts/test-preview.ts+++ b/scripts/test-preview.ts@@ -16,8 +16,9 @@// every password-protected deck ever saved, and the owner would have no// way to notice.// 2. THE OUTPUT REFUSAL. The preview is generated from author content. A-// script tag or a `</noscript>` reaching the file unbalances it and-// breaks the frozen splice contract every shipped updater relies on.+// script tag reaching the file unbalances it and breaks the frozen+// splice contract every shipped updater relies on. `</noscript>` is+// still refused: it costs nothing and older files carry one.//// The rest of the feature — laying the page out, fitting it to a viewport,// staying inside the byte budget — needs a DOM and is verified in a browser@@ -95,7 +96,7 @@ ok(previewIsSafe('<div>مرحبا שלום 🎌 a b c</div>') === true, 'RTLok(previewIsSafe(`<div>${SCRIPT_CLOSE}</div>`) === false, 'a bare script close is refused')ok(previewIsSafe(`<div>${SCRIPT_OPEN}alert(1)${SCRIPT_CLOSE}</div>`) === false, 'a script element is refused')ok(previewIsSafe(`<div>${NOSCRIPT_CLOSE}<h1>escaped</h1></div>`) === false,- 'a noscript close is refused — it would end the host and spill into the page')+ 'a noscript close is refused — older files park the preview in one')// Case and attribute variants: an HTML parser ends a script element at// `</script` regardless of case or what follows, so the check must too.