Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

type: the can't-open message shows the file's details as text (#737)

nyblnet585fa52ad248
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index eef65e0e..cd75b2e4 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -2661,6 +2661,27 @@ jobs:
- name: Origin probe in WebKit
run: node scripts/test-home-origin-probe.mjs
+ type-cantopen-browser:
+ runs-on: ubuntu-latest
+ timeout-minutes: 15
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-node@v4
+ with:
+ node-version: 24
+ - name: Install browser test runtime
+ # Pinned outside app packages: browser automation is not shipped in documents.
+ run: |
+ npm install --prefix "$RUNNER_TEMP/bento-browser" --no-package-lock --ignore-scripts [email protected]
+ "$RUNNER_TEMP/bento-browser/node_modules/.bin/playwright" install --with-deps chromium
+ echo "PLAYWRIGHT_MODULE=$RUNNER_TEMP/bento-browser/node_modules/playwright/index.mjs" >> "$GITHUB_ENV"
+ - name: Build the tested standalone document
+ run: |
+ npm ci --prefix type
+ npm run build:single --prefix type
+ - name: The can't-open message shows the file's details as text
+ run: node scripts/test-type-cantopen-browser.mjs
+
spaces-save-browser:
runs-on: ubuntu-latest
timeout-minutes: 15
diff --git a/scripts/test-type-cantopen-browser.mjs b/scripts/test-type-cantopen-browser.mjs
new file mode 100644
index 00000000..e7c51308
--- /dev/null
+++ b/scripts/test-type-cantopen-browser.mjs
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// The "This file could not be opened" message shows the file's details as TEXT.
+//
+// npm run build:single --prefix type && node scripts/test-type-cantopen-browser.mjs
+//
+// When bento/type cannot read the document a file carries, it says why and
+// stops (main.ts). The reason can quote the file itself, such as an unknown
+// format's own name or a JSON parser's message about the bytes. That text must
+// reach the page as text. It used to be interpolated into innerHTML, so a file
+// whose `format` was `<img src=x onerror=…>` put a live element on the page and
+// ran its handler.
+//
+// WHAT THIS PROVES, in the packaged document, for files whose document block
+// is (1) a `format` holding markup with an onerror handler, (2) markup that is
+// not JSON at all, and (3) a real bento/enc envelope:
+// - the message is shown, with the file's detail as literal text;
+// - no element from the file reaches the page (no img, no b), and the
+// handler's sentinel never fires;
+//
+// - no text from the file appears anywhere outside that message; type's
+// boot error is a fixed sentence, since the detail is already on screen.
+//
+// Mutations (run, judged by exit code): set the detail line with innerHTML
+// instead of textContent → red; put the detail back into the thrown error
+// → red.
+import { readFileSync, writeFileSync, mkdtempSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+const { chromium } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright')
+
+let checks = 0, failures = 0
+const ok = (c, m) => { checks++; if (!c) { failures++; console.log(` FAIL ${m}`) } else console.log(` ok ${m}`) }
+
+const shell = readFileSync(new URL('../type/dist-single/Bento_Type.bento.html', import.meta.url), 'utf8')
+const dir = mkdtempSync(join(tmpdir(), 'bento-type-cantopen-'))
+const withBlock = (name, block) => {
+ const out = shell.replace(/(<script[^>]*id="bento-doc"[^>]*>)[\s\S]*?(<\/script>)/, (_m, a, b) => a + '\n' + block + '\n' + b)
+ if (out === shell) throw new Error('could not find the #bento-doc block')
+ const f = join(dir, name); writeFileSync(f, out); return 'file://' + f
+}
+const SENTINEL = "window.__fired=1"
+const MARKUP = `<img src=x onerror="${SENTINEL}"><b>bold</b>`
+const esc = (s) => s.replace(/</g, '\\u003c') // as save.ts writes a block
+const CASES = [
+ { name: 'a format holding markup', url: withBlock('fmt.html', esc(JSON.stringify({ format: MARKUP, body: [] }))), literal: '<img src=x onerror=' },
+ { name: 'markup that is not JSON', url: withBlock('json.html', esc(MARKUP + ' {')), literal: null },
+ { name: 'an encrypted (bento/enc) document', url: withBlock('enc.html', JSON.stringify({ format: 'bento/enc', v: 1, it: 300000, salt: 'AAAA', iv: 'AAAA', data: 'AAAA' })), literal: 'this file is a bento/enc document' },
+]
+
+const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH, headless: true })
+try {
+ for (const c of CASES) {
+ const p = await browser.newPage()
+ await p.route(/^https?:/, (r) => r.abort())
+ await p.goto(c.url)
+ await p.waitForFunction(() => document.body.textContent.includes('This file could not be opened'), null, { timeout: 15000 }).catch(() => {})
+ await p.waitForTimeout(800) // long enough for an onerror on a failed image load to have run
+ const r = await p.evaluate(() => {
+ const h1 = [...document.querySelectorAll('h1')].find((h) => h.textContent === 'This file could not be opened')
+ const card = h1?.parentElement
+ return {
+ shown: !!card,
+ detail: card?.querySelectorAll('p')[0]?.textContent ?? '',
+ fromFile: document.querySelectorAll('body img, body b').length,
+ fired: window.__fired === 1,
+ // everything on the page outside type's own message
+ outside: [...document.body.querySelectorAll('*')].filter((e) => !card?.contains(e) && !e.contains(card) && !e.closest('script, style'))
+ .map((e) => e.textContent).join(' '),
+ }
+ })
+ ok(r.shown, `${c.name}: the can't-open message is shown`)
+ if (c.literal) ok(r.detail.includes(c.literal), `${c.name}: the detail reads as literal text ("${r.detail.slice(0, 70)}")`)
+ ok(!/onerror|__fired|<img|<b>|bold/.test(r.outside),
+ `${c.name}: no text from the file appears anywhere outside the message`)
+ ok(r.fromFile === 0 && !r.fired, `${c.name}: nothing from the file became an element (${r.fromFile}) and its handler never ran (${r.fired})`)
+ await p.close()
+ }
+} finally {
+ await browser.close()
+}
+console.log(`\n${checks - failures}/${checks} checks passed`)
+if (failures) process.exit(1)
diff --git a/type/src/main.ts b/type/src/main.ts
index 3038f194..3858a1e2 100644
--- a/type/src/main.ts
+++ b/type/src/main.ts
@@ -85,13 +85,26 @@ if (parsed.ok) {
} else {
// NEVER silently start a new document over a file we could not read — the
// spaces load contract (model.ts). Say what happened and stop.
- document.body.innerHTML =
- `<div style="max-width:44rem;margin:12vh auto;padding:0 2rem;font:15px/1.6 system-ui;color:#e6e9ef">
- <h1 style="font-size:20px">This file could not be opened</h1>
- <p>${parsed.detail}</p>
- <p style="color:#8d95a3">Nothing has been changed. Close this tab rather than saving over it.</p>
- </div>`;
- throw new Error(`bento/type: ${parsed.detail}`);
+ //
+ // Built with textContent, never markup: `detail` carries text FROM THE FILE
+ // (an unknown format's own name, a JSON parser's message quoting the bytes),
+ // so it is shown as text whatever it contains. Guarded by
+ // scripts/test-type-cantopen-browser.mjs.
+ const card = document.createElement('div');
+ card.style.cssText = 'max-width:44rem;margin:12vh auto;padding:0 2rem;font:15px/1.6 system-ui;color:#e6e9ef';
+ const line = (tag: string, text: string, style = '') => {
+ const el = document.createElement(tag);
+ el.textContent = text;
+ if (style) el.style.cssText = style;
+ card.append(el);
+ };
+ line('h1', 'This file could not be opened', 'font-size:20px');
+ line('p', parsed.detail);
+ line('p', 'Nothing has been changed. Close this tab rather than saving over it.', 'color:#8d95a3');
+ document.body.replaceChildren(card);
+ // A FIXED message: whatever reports this error may not treat it as text, and
+ // the detail (which quotes the file) is already on screen above.
+ throw new Error(`bento/type: the document could not be read (${parsed.err})`);
}
// ───────────────────────────────────────────────────────────────── chrome