Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
scripts: one app-payload fingerprint for the publish gates, and a shell with no payload never passes (#625)
nyblnet6bb644f01876
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 354eb7ad..99a7363d 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -1094,6 +1094,12 @@ jobs:# this rig pins the rule and the order.run: node scripts/test-publish-account.ts+ - name: app payload fingerprint — a shell with no payload never passes the publish gates+ # publish-site's shell-consistency gate hashed the shell and every deck+ # to null when its matcher went stale, and null === null passed it while+ # it checked nothing. One shared helper; null is "cannot tell", never a match.+ run: node scripts/test-apphash.ts+- name: client vouch rig — the other half of read-only# Drives a REAL OnlineTransport (kernel's and dash's twin) through a# fake socket in a signed room. Every copy holds the room key, so adiff --git a/scripts/gh-account.mjs b/scripts/gh-account.mjsindex 90cee1fa..8861fa20 100644--- a/scripts/gh-account.mjs+++ b/scripts/gh-account.mjs@@ -8,12 +8,12 @@//// WHY THIS EXISTS. Three releases running, publish-site.mjs mirrored and// pushed the site and THEN `gh release create` failed: the release worktree-// lived under a temp directory outside ~/devel, where the shell's chpwd hook selects+// lived under a temp directory outside ~/personal, where the shell's chpwd hook selects// the WORK gh profile — a different account, no scope for this repo — and// gh's config follows the environment the script was launched from, not the// repo it is pointed at. Each time the lead created the release by hand from-// ~/devel/bento. The account is now checked BEFORE the rsync: a mismatch-// prints the exact command to run from a path under ~/devel and exits+// ~/personal/bento. The account is now checked BEFORE the rsync: a mismatch+// prints the exact command to run from a path under ~/personal and exits// non-zero with nothing published. Publishing the site and then failing the// release is the one order that must never happen — the update channel goes// live while the repo shows no release.@@ -88,7 +88,7 @@ export function noOwnerMessage(dir) {return [`could not read the repository owner: ${dir} has no origin remote (a detached or copied checkout?).`,` Nothing was published. The gh account check cannot run without knowing whose repo this is, and a check that skips is a check that does not exist.`,- ` Fix: run the publish from a checkout with an origin remote (any worktree under ~/devel), or set BENTO_RELEASE_OWNER=<github owner>.`,+ ` Fix: run the publish from a checkout with an origin remote (any worktree under ~/personal), or set BENTO_RELEASE_OWNER=<github owner>.`,].join('\n')}@@ -99,7 +99,7 @@ export function mismatchMessage({ account, owner, repoRoot, cmd }) {return [`gh is authenticated as ${account ?? 'nobody'}; a release on this repo needs ${owner}.`,` Nothing was published (this check runs before the site is mirrored).`,- ` The shell picks the gh profile by directory — run the publish from a path under ~/devel, not from a job or temp worktree:`,+ ` The shell picks the gh profile by directory — run the publish from a path under ~/personal, not from a job or temp worktree:`,` cd ${repoRoot} && ${cmd}`,` (or: gh auth switch --user ${owner}, then re-run from here).`,].join('\n')diff --git a/scripts/lib/apphash.mjs b/scripts/lib/apphash.mjsnew file mode 100644index 00000000..d56c1adb--- /dev/null+++ b/scripts/lib/apphash.mjs@@ -0,0 +1,15 @@+// App payload fingerprint — the shell's deflated runtime blocks+// (`bento/deflate-b86`, or `deflate-b64` in shells before 1.2.0). Two files+// with the same hash embed the same runtime, whatever document they carry.+// Returns null for a file with no payload blocks: callers comparing hashes+// must treat null as "cannot tell", never as a match — null === null once+// let a gate pass that was checking nothing.+import { createHash } from 'node:crypto'++export const appHash = (html) => {+ const blocks = [...html.matchAll(/type="bento\/deflate-b(?:64|86)"[^>]*>([^<]+)</g)].map((m) => m[1])+ return blocks.length ? createHash('sha256').update(blocks.join('')).digest('hex') : null+}++// True only when both files carry a payload and it is the same one.+export const sameApp = (a, b) => a !== null && a === bdiff --git a/scripts/publish-site.mjs b/scripts/publish-site.mjsindex 0afc11f6..2604779b 100755--- a/scripts/publish-site.mjs+++ b/scripts/publish-site.mjs@@ -24,13 +24,13 @@import { execFileSync } from 'node:child_process'import { existsSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'-import { createHash } from 'node:crypto'import { dirname, join, resolve } from 'node:path'import { tmpdir } from 'node:os'import { fileURLToPath } from 'node:url'import { gatePackIndex } from './sign-packs.mjs'import { walk, plannedDeletions, groupDeletions, supersededPacks } from './site-inventory.mjs'import { APPS, RELEASE_MARKER, tagFor } from './apps.mjs'+import { appHash, sameApp } from './lib/apphash.mjs'import { accountMayRelease, activeAccount, mismatchMessage, noOwnerMessage, ownerOfRemote, repoRemote } from './gh-account.mjs'const root = dirname(dirname(fileURLToPath(import.meta.url)))@@ -101,23 +101,21 @@ if (doGallery) {// The gallery templates, the 404 deck AND the guestbook EMBED the shell, so// they have to be rebuilt/re-shelled whenever the shell changes (release.mjs// does this — the guestbook is re-shelled in place, preserving its room). Hash-// the shell's app payload (the bento/deflate-b64 blocks) and refuse to publish-// if any embedded-shell deck carries a different one — otherwise a stale deck-// would ship on top of a fresh shell.+// the shell's app payload (scripts/lib/apphash.mjs) and refuse to publish if+// any embedded-shell deck carries a different one — otherwise a stale deck+// would ship on top of a fresh shell. A file with NO payload is a refusal too:+// null === null once let this gate pass while comparing nothing.const shellFile = join(site, 'releases/slides/Bento_Slides.bento.html')if (existsSync(shellFile)) {- const appHash = (file) => {- const blocks = [...readFileSync(file, 'utf8').matchAll(/type="bento\/deflate-b(?:64|86)"[^>]*>([^<]+)</g)].map((m) => m[1])- return blocks.length ? createHash('sha256').update(blocks.join('')).digest('hex') : null- }- const shellHash = appHash(shellFile)+ const shellHash = appHash(readFileSync(shellFile, 'utf8'))+ if (shellHash === null) die(`the release shell carries no runtime payload blocks — cannot check the example decks against it:\n · ${shellFile.slice(site.length + 1)}`)const galleryDir = join(site, 'gallery')const decks = [...(existsSync(galleryDir) ? readdirSync(galleryDir).filter((f) => f.endsWith('.bento.html')).map((f) => join(galleryDir, f)) : []),join(site, '404.bento.html'),join(site, 'guestbook.bento.html'),].filter(existsSync)- const stale = decks.filter((d) => appHash(d) !== shellHash)+ const stale = decks.filter((d) => !sameApp(appHash(readFileSync(d, 'utf8')), shellHash))if (stale.length) {die('example decks are on a DIFFERENT shell than the release — rebuild them\n' +@@ -325,7 +323,7 @@ if (!existsSync(join(site, 'guestbook.bento.html'))) {// releases running it was not (a job/temp worktree, where the shell's chpwd// hook selects the work profile), and the failure came AFTER the site was// live. So: which account is gh, and may it release here? Checked here, with-// nothing mirrored yet; the message names the command to run from ~/devel.+// nothing mirrored yet; the message names the command to run from ~/personal.// scripts/gh-account.mjs holds the rule; scripts/test-publish-account.ts the// cases. --dry skips it (a dry run publishes nothing either way); every real// publish runs it, because every real publish ends in the release step.@@ -338,7 +336,7 @@ if (!dry) {// never skip: an owner that cannot be read is a refusal, not a warningif (!owner) die(noOwnerMessage(root))else if (!accountMayRelease(account, owner, allowed)) {- die(mismatchMessage({ account, owner, repoRoot: root.startsWith(process.env.HOME ?? '') ? root : '~/devel/bento', cmd: `node scripts/publish-site.mjs ${args.map((a) => (/\s/.test(a) ? JSON.stringify(a) : a)).join(' ')}` }))+ die(mismatchMessage({ account, owner, repoRoot: root.startsWith(process.env.HOME ?? '') ? root : '~/personal/bento', cmd: `node scripts/publish-site.mjs ${args.map((a) => (/\s/.test(a) ? JSON.stringify(a) : a)).join(' ')}` }))} else console.log(`• gh account: ${account} ✓ (may release on ${owner})`)}diff --git a/scripts/reseed-guestbook.mjs b/scripts/reseed-guestbook.mjsindex 576264c4..95feba1e 100644--- a/scripts/reseed-guestbook.mjs+++ b/scripts/reseed-guestbook.mjs@@ -28,10 +28,10 @@// treat guestbook re-seeding as best-effort.import { existsSync, readFileSync } from 'node:fs'-import { createHash } from 'node:crypto'import { dirname, join } from 'node:path'import { fileURLToPath } from 'node:url'import { spliceDoc, extractDoc } from './guestbook-deck.mjs'+import { appHash, sameApp } from './lib/apphash.mjs'const root = dirname(dirname(fileURLToPath(import.meta.url)))const args = process.argv.slice(2)@@ -43,12 +43,8 @@ const dry = args.includes('--dry')const warn = (m) => { console.warn(`⚠ guestbook re-seed skipped: ${m}`) }-// App payload fingerprint — the deflate-b64 runtime blocks. Two decks with the-// same hash embed the same shell (identical runtime), whatever their doc holds.-const appHash = (html) => {- const blocks = [...html.matchAll(/type="bento\/deflate-b(?:64|86)"[^>]*>([^<]+)</g)].map((m) => m[1])- return blocks.length ? createHash('sha256').update(blocks.join('')).digest('hex') : null-}+// Two decks with the same appHash (scripts/lib/apphash.mjs) embed the same+// shell, whatever their doc holds.const shellFile = opt('shell', null) ?? [join(root, 'site/releases/slides/Bento_Slides.bento.html'),@@ -64,6 +60,9 @@ if (!adminKey) { warn('admin key file is empty'); process.exit(0) }const base = (opt('base', 'https://bento.page')).replace(/\/$/, '')const freshShell = readFileSync(shellFile, 'utf8')const freshHash = appHash(freshShell)+// a shell with no payload blocks is not a shell to re-seed onto — and its null+// hash would crash every message below+if (freshHash === null) { warn(`${shellFile} carries no runtime payload blocks`); process.exit(0) }try {// 1 · the daemon's OWN current deck (carries the live room creds + walls seed)@@ -72,7 +71,7 @@ try {const curHtml = await curResp.text()const curHash = appHash(curHtml)- if (curHash && curHash === freshHash) {+ if (sameApp(curHash, freshHash)) {console.log(`✓ guestbook daemon already on the current shell (${freshHash.slice(0, 12)}…) — nothing to do`)process.exit(0)}diff --git a/scripts/test-apphash.ts b/scripts/test-apphash.tsnew file mode 100644index 00000000..9cc205b2--- /dev/null+++ b/scripts/test-apphash.ts@@ -0,0 +1,66 @@+#!/usr/bin/env node+// SPDX-License-Identifier: MIT+// Copyright (c) 2026 The Bento authors+// The app payload fingerprint the publish gates compare (scripts/lib/apphash.mjs):+//+// node scripts/test-apphash.ts+//+// WHAT THIS PROVES. appHash reads both payload encodings — `deflate-b86`+// (1.2.0 on) and `deflate-b64` (older shells) — and returns null ONLY for a+// file with no payload blocks; sameApp never treats two nulls as a match.+// That last rule is the point: run from a checkout older than the b86 switch,+// publish-site's shell-consistency gate hashed the shell and every deck to+// null and passed with null === null, checking nothing, while+// reseed-guestbook crashed on null.slice. Both scripts now use this one+// helper (no private copies, which is how one of them went stale), and both+// REFUSE a payload-less shell instead of comparing it.++import { readFileSync } from 'node:fs'+import { appHash, sameApp } from './lib/apphash.mjs'++let failures = 0, checks = 0+const ok = (cond: boolean, msg: string) => { checks++; if (!cond) { failures++; console.log(` FAIL ${msg}`) } else console.log(` ok ${msg}`) }++const shell = (type: string, payloads: string[]) =>+ `<!doctype html><html><head><script type="application/json" id="bento-doc">{}</script></head><body>` ++ payloads.map((p) => `<script type="bento/${type}" data-kind="js">${p}</script>`).join('') + `</body></html>`+// base86 text carries characters base64 never does (the old matcher's+// [A-Za-z0-9+/=] class stopped at the first of them)+const B86 = ['k#9$Qz!r*~^;`(a)', 'Zp%&-_.{}|:?@[]']++console.log('\nappHash')+const h86 = appHash(shell('deflate-b86', B86))+ok(typeof h86 === 'string' && /^[0-9a-f]{64}$/.test(h86), 'a deflate-b86 shell hashes')+ok(typeof appHash(shell('deflate-b64', ['QUJD', 'REVG'])) === 'string', 'a deflate-b64 shell (pre-1.2.0) hashes')+ok(appHash(shell('deflate-b86', B86)) === h86, 'deterministic')+ok(appHash(shell('deflate-b86', [B86[0], B86[1] + 'x'])) !== h86, 'any payload byte changes it')+ok(appHash(shell('deflate-b86', [B86[0] + B86[1].slice(0, 3), B86[1].slice(3)])) === h86, 'the payload, not how it is split across blocks, is what is hashed')+ok(appHash('<!doctype html><html><body><p>no runtime</p></body></html>') === null, 'a file with no payload blocks is null')+// the old matcher, run on today's shell: the vacuous-gate scenario, reproduced+const oldMatcher = (html: string) => [...html.matchAll(/type="bento\/deflate-b64"[^>]*>([A-Za-z0-9+/=]+)</g)].length+ok(oldMatcher(shell('deflate-b86', B86)) === 0 && appHash(shell('deflate-b86', B86)) !== null,+ 'the pre-#495 matcher finds nothing in a b86 shell; appHash finds it')++console.log('\nsameApp')+ok(sameApp(h86, h86), 'the same payload matches')+ok(!sameApp(h86, appHash(shell('deflate-b64', ['QUJD']))), 'a different payload does not')+ok(!sameApp(null, null), 'null never matches null — "cannot tell" is not "the same"')+ok(!sameApp(null, h86) && !sameApp(h86, null), 'null never matches a hash')++console.log('\nthe scripts use it, and refuse a payload-less shell')+const strip = (s: string) => s.replace(/^\s*\/\/.*$/gm, '')+for (const f of ['scripts/publish-site.mjs', 'scripts/reseed-guestbook.mjs']) {+ const src = strip(readFileSync(f, 'utf8'))+ ok(/import \{ appHash, sameApp \} from '\.\/lib\/apphash\.mjs'/.test(src) && !/deflate-b/.test(src) && !/createHash/.test(src),+ `${f}: imports the shared helper, keeps no private matcher`)+ ok(!/appHash\([^)]*\)\s*[!=]==/.test(src) && !/[!=]==\s*(shellHash|freshHash|curHash)\b(?!\s*===?\s*null)/.test(src.replace(/(shellHash|freshHash) === null/g, '')),+ `${f}: compares hashes only through sameApp`)+}+const ps = strip(readFileSync('scripts/publish-site.mjs', 'utf8'))+ok(/if \(shellHash === null\) die\(/.test(ps), 'publish-site: a release shell with no payload is a refusal')+const rs = strip(readFileSync('scripts/reseed-guestbook.mjs', 'utf8'))+const guard = rs.indexOf('if (freshHash === null)'), firstUse = rs.indexOf('freshHash.slice')+ok(guard >= 0 && firstUse > guard, 'reseed-guestbook: stops on a payload-less shell before any freshHash.slice')++console.log(`\n${checks - failures}/${checks} checks passed`)+if (failures) process.exit(1)diff --git a/scripts/test-publish-account.ts b/scripts/test-publish-account.tsindex c5251f94..fbb0374e 100644--- a/scripts/test-publish-account.ts+++ b/scripts/test-publish-account.ts@@ -11,7 +11,7 @@// inactive account is not mistaken for the active one); the rule refuses a// mismatched owner, nobody, and accepts the owner or a listed collaborator// case-insensitively; the message names the command to run from a path-// under ~/devel. And the gate sits BEFORE the rsync in publish-site.mjs —+// under ~/personal. And the gate sits BEFORE the rsync in publish-site.mjs —// the whole point: the site must never go live and the release then fail.import { readFileSync, mkdtempSync, rmSync, writeFileSync, cpSync } from 'node:fs'