Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

dash: an encrypted or unopenable file tells the loader the app is up (#733)

nyblnetadcba0e56ebc
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf16776f..a414db5c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -2528,6 +2528,31 @@ jobs:
# browser-dressed field must be flagged.
run: node scripts/test-dash-fields-browser.mjs
+ dash-gate-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 dash
+ npm run build:single --prefix dash
+ - name: An encrypted or unopenable dash file opens to its gate
+ # window.bento = { format, locked: true } before the password gate and
+ # the refusal page, so the shell's loader counts the app as mounted
+ # instead of re-running it and failing over the gate; unlocking
+ # replaces it with the real workbook.
+ run: node scripts/test-dash-gate-browser.mjs
+
spaces-save-browser:
runs-on: ubuntu-latest
timeout-minutes: 15
diff --git a/dash/CHANGELOG.md b/dash/CHANGELOG.md
index c41279c5..f43a0f82 100644
--- a/dash/CHANGELOG.md
+++ b/dash/CHANGELOG.md
@@ -12,6 +12,12 @@ There is no server, so a break here would be permanent.
The release that came out of watching somebody use 0.2.0.
+- **Password-protected workbooks open again.** The file showed its password
+ prompt and then failed with "This file could not start" over it, because the
+ file's loader took the prompt for an app that had not started. Dash now tells
+ the loader it is waiting for a password, and the same for a file it cannot
+ open.
+
- **Every field looks the same, in light and dark.** Text fields, selects,
text areas and colour wells across dash share one look: the same border,
fill, corners and height, and the same focus ring. It matches slides. Before,
diff --git a/dash/src/main.ts b/dash/src/main.ts
index 12055a09..910d116e 100644
--- a/dash/src/main.ts
+++ b/dash/src/main.ts
@@ -52,7 +52,7 @@ import { savingFor, saveRevision, type Acknowledge } from './saving.ts'
import { dismissSplash, dismissSplashNow } from './splash.ts'
import { t, i18nApi } from './i18n.ts'
import {
- parseDoc, docBytes, docBudget, rowCount, DOC_BUDGET_FSA, DOC_BUDGET_DOWNLOAD,
+ parseDoc, docBytes, docBudget, rowCount, DOC_BUDGET_FSA, DOC_BUDGET_DOWNLOAD, FORMAT,
type DashDoc, type ParseResult, type Column, type ColumnType, type TableSheet,
type Sheet, type CanvasSheet,
} from './model.ts'
@@ -199,8 +199,22 @@ if (envelope) {
else refuse(res)
}
+/**
+ * The app is UP but has no workbook to expose yet — say so on `window.bento`.
+ *
+ * The shell's loader decides the app mounted by looking at `window.bento`; it
+ * used to ask for `.doc`, which a password gate or a refusal page never has, so
+ * it ran the whole app again through its next path and finally threw "every
+ * loader path was refused" over the gate. `locked` is the shared signal the
+ * loader accepts (slides' loader fix); `boot()` replaces the whole object.
+ */
+function markMountedWithoutDoc(extra: Record<string, unknown> = {}): void {
+ ;(window as unknown as Record<string, unknown>).bento = { format: FORMAT, locked: true, ...extra }
+}
+
/** An encrypted workbook: ask, then take the same boot path. */
async function passwordGate(raw: string): Promise<void> {
+ markMountedWithoutDoc()
dismissSplashNow()
document.body.innerHTML =
`<div class="dx-gate"><h1>${t('This file is encrypted.')}</h1>` +
@@ -231,6 +245,8 @@ async function passwordGate(raw: string): Promise<void> {
* subscription are constructed, and none of them exist on this path.
*/
function refuse(res: Extract<ParseResult, { ok: false }>): void {
+ // up, with nothing to open: `refused` says why `doc` is missing
+ markMountedWithoutDoc({ refused: true })
dismissSplashNow()
const why = res.err === 'empty' ? '' : 'detail' in res ? res.detail : ''
const found = 'found' in res && res.found ? ` (${res.found})` : ''
diff --git a/scripts/test-dash-gate-browser.mjs b/scripts/test-dash-gate-browser.mjs
new file mode 100644
index 00000000..f095584f
--- /dev/null
+++ b/scripts/test-dash-gate-browser.mjs
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// An encrypted or unopenable dash file tells the shell's loader the app is UP.
+//
+// npm run build:single --prefix dash && node scripts/test-dash-gate-browser.mjs
+//
+// The shell's loader decides the app mounted by reading `window.bento`. Dash set
+// it only in boot(), which a password gate and a refusal page never reach, so
+// the loader ran the whole app again through its next path and threw "every
+// loader path was refused" over the gate — encrypted workbooks did not open.
+// Dash now sets `window.bento = { format, locked: true }` before either screen.
+// This drives the packaged shell with an encrypted and a corrupt document and
+// asserts the loader's predicate (doc OR locked) the moment the screen is up,
+// then that unlocking replaces it with the real workbook.
+import { readFileSync } from 'node:fs'
+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 shellPath = new URL('../dash/dist-single/Bento_Dash.bento.html', import.meta.url)
+const shell = readFileSync(shellPath, 'utf8')
+const BLOCK = /(<script type="application\/bento\+json" id="bento-doc">)([\s\S]*?)(<\/script>)/
+if (!BLOCK.test(shell)) throw new Error('#bento-doc not found in the built shell')
+const withBlock = (text) => shell.replace(BLOCK, (_, a, _b, c) => a + text.replace(/</g, '\\u003c') + c)
+
+// The kernel's envelope (kernel/src/save.ts encryptBody): PBKDF2-SHA-256 then
+// AES-GCM-256, standard base64. A low iteration count keeps the rig fast;
+// decryptEnvelope honours the envelope's own `it`.
+const b64 = (u) => Buffer.from(u).toString('base64')
+async function envelope(json, password) {
+ const salt = crypto.getRandomValues(new Uint8Array(16)), iv = crypto.getRandomValues(new Uint8Array(12))
+ const material = await crypto.subtle.importKey('raw', new TextEncoder().encode(password), 'PBKDF2', false, ['deriveKey'])
+ const key = await crypto.subtle.deriveKey({ name: 'PBKDF2', hash: 'SHA-256', salt, iterations: 1000 }, material, { name: 'AES-GCM', length: 256 }, false, ['encrypt'])
+ const ct = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, new TextEncoder().encode(json))
+ return JSON.stringify({ format: 'bento/enc', v: 1, it: 1000, salt: b64(salt), iv: b64(iv), data: b64(new Uint8Array(ct)) })
+}
+const WORKBOOK = JSON.stringify({
+ format: 'bento/dash', version: 1, policy: 'bento-dash-1', docId: 'gate-rig', title: 'Locked workbook',
+ sheets: [{ id: 'sh', kind: 'table', name: 'Data', rids: [[1, 1]], nextRid: 2, columns: [{ id: 'v', name: 'Value', type: 'number' }], data: { v: { enc: 'raw', v: [42] } }, steps: [] }],
+})
+const tmp = new URL('./.gate-rig.html', shellPath)
+const { writeFileSync, rmSync } = await import('node:fs')
+
+// the loader's own predicate, after slides' shared fix
+const predicate = () => !!(window.bento && (window.bento.doc || window.bento.locked))
+
+const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH, headless: true })
+try {
+ const ctx = await browser.newContext()
+ await ctx.route(/^https?:/, (r) => r.abort())
+
+ console.log('an encrypted workbook')
+ writeFileSync(tmp, withBlock(await envelope(WORKBOOK, 'hunter2')))
+ {
+ const p = await ctx.newPage(); const errors = []
+ p.on('pageerror', (e) => errors.push(e.message))
+ await p.goto(tmp.href)
+ await p.waitForSelector('.dx-gate input[type="password"]', { timeout: 10000 })
+ const b = await p.evaluate(() => ({ format: window.bento?.format, locked: window.bento?.locked, doc: !!window.bento?.doc }))
+ ok(b.format === 'bento/dash' && b.locked === true && !b.doc, `at the gate, window.bento is { format: 'bento/dash', locked: true } with no doc — ${JSON.stringify(b)}`)
+ ok(await p.evaluate(predicate), 'so the loader\'s mounted check (doc OR locked) is TRUE while the gate is up')
+ await p.fill('.dx-gate input[type="password"]', 'wrong')
+ await p.click('.dx-unlock')
+ await p.waitForFunction(() => /Wrong password/.test(document.querySelector('.dx-err')?.textContent ?? ''))
+ ok(await p.evaluate(() => window.bento?.locked === true && !window.bento?.doc), 'a wrong password keeps it locked')
+ await p.fill('.dx-gate input[type="password"]', 'hunter2')
+ await p.click('.dx-unlock')
+ await p.waitForFunction(() => window.bento?.doc, null, { timeout: 15000 })
+ const after = await p.evaluate(() => ({ title: window.bento.doc.title, locked: window.bento.locked }))
+ ok(after.title === 'Locked workbook' && after.locked === undefined, `the right password opens the workbook and replaces the locked marker — ${JSON.stringify(after)}`)
+ ok(!errors.some((e) => /loader path/.test(e)), `no "every loader path was refused" error${errors.length ? ' — ' + errors.join('; ') : ''}`)
+ await p.close()
+ }
+
+ console.log('\nan unopenable file')
+ writeFileSync(tmp, withBlock('{"format":"bento/dash","version":99,"sheets":"nope"}'))
+ {
+ const p = await ctx.newPage()
+ await p.goto(tmp.href)
+ await p.waitForSelector('.dx-gate #dx-copy', { timeout: 10000 })
+ const b = await p.evaluate(() => ({ format: window.bento?.format, locked: window.bento?.locked, refused: window.bento?.refused, doc: !!window.bento?.doc }))
+ ok(b.format === 'bento/dash' && b.locked === true && b.refused === true && !b.doc, `the refusal page marks the app up and says why there is no doc — ${JSON.stringify(b)}`)
+ ok(await p.evaluate(predicate), 'and the loader\'s mounted check is TRUE there too')
+ await p.close()
+ }
+
+ console.log('\nan ordinary workbook is unchanged')
+ writeFileSync(tmp, withBlock(WORKBOOK))
+ {
+ const p = await ctx.newPage()
+ await p.goto(tmp.href)
+ await p.waitForFunction(() => window.bento?.doc, null, { timeout: 10000 })
+ ok(await p.evaluate(() => window.bento.locked === undefined && window.bento.doc.title === 'Locked workbook'), 'a plain file boots straight to its doc, never locked')
+ await p.close()
+ }
+} finally {
+ await browser.close()
+ rmSync(tmp, { force: true })
+}
+console.log(`\n${checks - failures}/${checks} checks passed`)
+if (failures) process.exit(1)