Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

dash: a restore keeps this workbook's room, keys and identity — like slides' restore gate (#611)

nyblnet4e55206d84eb
diff --git a/dash/CHANGELOG.md b/dash/CHANGELOG.md
index 67673c6b..e281a9d8 100644
--- a/dash/CHANGELOG.md
+++ b/dash/CHANGELOG.md
@@ -12,6 +12,13 @@ There is no server, so a break here would be permanent.
The release that came out of watching somebody use 0.2.0.
+- **Restoring unsaved changes or an old version keeps this workbook's own
+ sharing room, keys and identity.** A restore brings back the CONTENT of a
+ snapshot; the snapshot's copies of the docId, the room and its keys, and the
+ read-only and template flags are now ignored, the same way slides' restore
+ gate treats them. Snapshots share one browser store on file://, so any page
+ opened there could have filed one under this workbook's id, and Restore
+ would have moved the workbook into that page's room.
- **Stopping sharing or rotating its keys now marks the workbook unsaved.**
Rotating the keys is how you cut off everyone you sent a copy to, and it only
takes effect once the file holds the new keys. It used to change the workbook
diff --git a/dash/src/about.ts b/dash/src/about.ts
index cb8ed3ae..4d0f86c7 100644
--- a/dash/src/about.ts
+++ b/dash/src/about.ts
@@ -55,7 +55,7 @@ import { addVersion, listVersions } from '../../kernel/src/autosave.ts'
// The alternative was a third module for one banner, or a hook wired through
// main.ts that a future edit could quietly forget to pass, which would put the
// two restore paths straight back into disagreeing about reversibility.
-import { offerUndoRestore } from './recovery.ts'
+import { offerUndoRestore, restoredWorkbook } from './recovery.ts'
import { t } from './i18n.ts'
import { docBudget, docBytes, parseDoc, rowCount, type DashDoc, type DocMeta , docForExport } from './model.ts'
@@ -324,12 +324,15 @@ export function openAbout(hooks: AboutHooks): void {
b.addEventListener('click', () => {
// A snapshot is this app's own JSON, but it is still parsed rather than
// trusted: it may predate an id repair, or a sheet this build refuses.
- const res = parseDoc(snap.json)
- if (!res.ok) { alert(t('That version could not be read.')); return }
+ // And its docId, room, keys and locks are THIS workbook's, not the
+ // snapshot's — see FROM_LIVE in recovery.ts for why a stored version
+ // is not evidence of any of them.
+ const restored = restoredWorkbook(snap.json, store.doc)
+ if (!restored) { alert(t('That version could not be read.')); return }
// Held BEFORE the swap: `replaceDoc` empties both undo stacks, so this
// object is the only route back to the workbook on screen right now.
const before = store.doc
- if (!replaceWorkbook(hooks, res.doc)) {
+ if (!replaceWorkbook(hooks, restored)) {
alert(t('That version has no table sheet to show.'))
return
}
diff --git a/dash/src/recovery.ts b/dash/src/recovery.ts
index a8d80a4b..008da336 100644
--- a/dash/src/recovery.ts
+++ b/dash/src/recovery.ts
@@ -143,15 +143,62 @@ export interface DecideInput {
* is not offered — the alternative is a Restore button that hands the app a
* document it will then refuse, which is a dead end wearing a live button.
*/
+/**
+ * The fields a restore takes from the LIVE workbook, never from the snapshot.
+ *
+ * A recovery entry or a kept version is a CONTENT snapshot of THIS workbook,
+ * not a new file — so who it is (`docId`), which room it shares into and with
+ * which keys (`collab`), and what it may do (`readonly`, `template`) are facts
+ * about the workbook on screen. The snapshot's copies of them are not
+ * evidence: on file:// every local document shares one IndexedDB origin, and
+ * the store is keyed by docId, so any page opened in the same browser can
+ * write an entry under this workbook's docId. Before this, that entry's
+ * `collab` rode `parseDoc` straight into `replaceDoc`, and one Restore click
+ * moved the workbook into the writer's room.
+ *
+ * `template` is dash's own: restored, it would stop the automatic save to the
+ * file (writeback.ts refuses a template) and make the next open re-mint the
+ * identity. Same class as the other three.
+ *
+ * Opening a FILE and Replace from JSON do not come through here. A file
+ * carries its own capability by design; Replace from JSON keeps the live room
+ * already (about.ts).
+ */
+export const FROM_LIVE = ['docId', 'collab', 'readonly', 'template'] as const
+
+/** The snapshot's content under the live workbook's identity. */
+export function keepLiveIdentity(next: DashDoc, live: DashDoc): DashDoc {
+ const out = { ...next } as Record<string, unknown>
+ const from = live as unknown as Record<string, unknown>
+ for (const k of FROM_LIVE) {
+ if (from[k] !== undefined) out[k] = from[k]
+ else delete out[k]
+ }
+ return out as unknown as DashDoc
+}
+
+/**
+ * A stored snapshot, parsed and put under the live workbook's identity — the
+ * ONE way a recovery entry or a kept version becomes a document. Both the
+ * banner (`decide`) and Version history (about.ts) call this, so neither can
+ * forget the identity half. `null` when the snapshot cannot be opened.
+ */
+export function restoredWorkbook(json: string, live: DashDoc): DashDoc | null {
+ const res = parseDoc(json)
+ return res.ok ? keepLiveIdentity(res.doc, live) : null
+}
+
export function decide(input: DecideInput): RecoveryDecision {
if (input.encrypted) return { offer: false, why: 'encrypted' }
if (input.readOnly) return { offer: false, why: 'read-only' }
if (!input.snapshot) return { offer: false, why: 'none' }
if (input.snapshot.docId !== input.doc.docId) return { offer: false, why: 'other-doc' }
- const res = parseDoc(input.snapshot.json)
- if (!res.ok) return { offer: false, why: 'unreadable' }
- if (contentKey(res.doc) === contentKey(input.doc)) return { offer: false, why: 'same' }
- return { offer: true, doc: res.doc, at: input.snapshot.at }
+ // Identity from the live workbook BEFORE the comparison, so a snapshot that
+ // differs only in its room is not even offered.
+ const doc = restoredWorkbook(input.snapshot.json, input.doc)
+ if (!doc) return { offer: false, why: 'unreadable' }
+ if (contentKey(doc) === contentKey(input.doc)) return { offer: false, why: 'same' }
+ return { offer: true, doc, at: input.snapshot.at }
}
// --- swapping a whole workbook in -------------------------------------------
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md
index 607c4a8d..6f2a184f 100644
--- a/docs/DECISIONS.md
+++ b/docs/DECISIONS.md
@@ -7442,6 +7442,21 @@ rigs treat a drawn arrow as a deliberate difference: named in
`test-maths-lite.ts`, counted with the identical ones in the coverage floor.
Cost: +966 B of shell.
+## 2026-10-04 — dash: a restore takes content from the snapshot, identity from the live workbook
+
+Dash's recovery banner and Version history now go through
+`restoredWorkbook(json, live)` (`dash/src/recovery.ts`), the same split as
+slides' `gateRestored`. Four fields come from the LIVE workbook, never the
+snapshot: `docId`, `collab`, `readonly`, and — dash's own addition — `template`,
+because a restored template flag stops the automatic save to the file and
+re-mints the identity on the next open. A field the live workbook lacks is
+removed, so a roomless workbook cannot gain a room from a snapshot.
+
+`parseDoc` is deliberately unchanged: opening or dropping a FILE adopts that
+file's own capability, which is the design. Replace from JSON already keeps the
+live `collab` (about.ts) and still takes `docId`/`readonly` from the paste — a
+deliberate user action, left as is.
+
## 2026-10-04 — dash: sharing changes mark the workbook unsaved; copy roles are an allowlist
Dash carries its own copy of the sharing verbs (`dash/src/sync/online.ts`), and
diff --git a/scripts/test-dash-recovery.ts b/scripts/test-dash-recovery.ts
index 74f155f1..47399655 100644
--- a/scripts/test-dash-recovery.ts
+++ b/scripts/test-dash-recovery.ts
@@ -340,8 +340,14 @@ console.log('\nrestoring a whole workbook is reversible, from both surfaces')
const about = readSrc('about.ts')
const recovery = readSrc('recovery.ts')
- ok(about.includes("import { offerUndoRestore } from './recovery.ts'"),
+ ok(/import \{[^}]*\bofferUndoRestore\b[^}]*\} from '\.\/recovery\.ts'/.test(about),
'about.ts imports the shared offer rather than growing a second one')
+ // Version history's click handler is DOM-only, so it is checked here, by
+ // what it calls: a stored version becomes a document ONLY through
+ // `restoredWorkbook`, which keeps the live identity. A bare parseDoc of a
+ // snapshot is the planted-room hole (see 'a PLANTED snapshot' below).
+ ok(/restoredWorkbook\(snap\.json, store\.doc\)/.test(about) && !/parseDoc\(snap\.json\)/.test(about),
+ 'Version history restores through restoredWorkbook, never a bare parseDoc of the snapshot')
ok(about.includes('offerUndoRestore(hooks, before,'),
'and its version restore calls it, holding the pre-restore document')
ok(!/confirm\(t\('Restore this version/.test(about),
@@ -450,5 +456,65 @@ console.log('\nrestoring a whole workbook is reversible, from both surfaces')
'the host contract admits an array at all — the string-only signature was what forced the join')
}
+console.log('\na PLANTED snapshot cannot move this workbook into another room')
+// On file:// every local document shares one IndexedDB origin, and the store
+// is keyed by docId — so any page opened in the same browser can write an
+// entry under THIS workbook's docId. Its JSON is the attacker's to write. The
+// restore must keep the live workbook's identity, and take only the content.
+{
+ const { restoredWorkbook, swapWorkbook, FROM_LIVE } = await import('../dash/src/recovery.ts')
+ const { Store } = await import('../dash/src/store.ts')
+ const mine = { room: 'w-MINE', key: 'k-mine', on: true, owner: 'o-mine', ownerPriv: 'p-mine', v: 2 }
+ const theirs = { room: 'w-ATTACKER', key: 'k-attacker', on: true, owner: 'o-attacker', ownerPriv: 'p-attacker', v: 2 }
+ const live = loaded({ collab: mine } as Partial<DashDoc>)
+ // the planted row: filed under MY docId, carrying THEIR identity — and a
+ // real content change, so the banner has something to offer
+ const planted = workbook({ docId: 'doc-ATTACKER', collab: theirs, readonly: true, template: true, title: 'Bait' } as Partial<DashDoc>)
+ const row: Snapshot = { docId: live.docId, at: 1_700_000_000_000, title: 'Bait', json: JSON.stringify(planted) }
+
+ ok(FROM_LIVE.join() === 'docId,collab,readonly,template',
+ 'four fields come from the live workbook; adding or removing one is a decision, not an oversight')
+
+ const d = decide({ doc: live, encrypted: false, readOnly: false, snapshot: row })
+ ok(d.offer, 'the banner still offers it — the content differs, and refusing would hide real unsaved work')
+ if (d.offer) {
+ const c = (d.doc as { collab?: { room?: string; key?: string } }).collab
+ ok(c?.room === 'w-MINE' && c?.key === 'k-mine', `RECOVERY keeps MY room and key, not the planted ones — got ${c?.room}`)
+ ok(d.doc.docId === live.docId, `and my docId — got ${d.doc.docId}`)
+ ok(!(d.doc as { readonly?: boolean }).readonly, 'a planted readonly does not lock me out')
+ ok(!(d.doc as { template?: boolean }).template, 'a planted template does not stop the automatic save or fork my identity')
+ ok(d.doc.title === 'Bait', 'the CONTENT is the snapshot\'s — that is what a restore is for')
+
+ // end to end, through the swap the Restore button makes
+ const store = new Store(live)
+ const host = { store, showingSheet: () => 'sheet-1', showSheet: () => {} }
+ ok(swapWorkbook(host, d.doc), 'the restore goes through')
+ const after = store.doc as { collab?: { room?: string }; docId: string }
+ ok(after.collab?.room === 'w-MINE' && after.docId === live.docId,
+ `AFTER the Restore click the workbook is still in MY room — ${after.collab?.room}`)
+ }
+
+ // Version history: the same function, the same answer
+ const v = restoredWorkbook(JSON.stringify(planted), live) as (DashDoc & { collab?: { room?: string } }) | null
+ ok(v?.collab?.room === 'w-MINE' && v?.docId === live.docId && !(v as { readonly?: boolean }).readonly,
+ `VERSION HISTORY restore keeps my identity too — got ${v?.collab?.room}`)
+
+ // a live workbook that has no room does not GAIN one from a snapshot
+ const bare = loaded()
+ const b = restoredWorkbook(JSON.stringify(planted), bare) as { collab?: unknown } | null
+ ok(b !== null && b.collab === undefined, 'a workbook with no room does not acquire the planted one')
+
+ // a row that differs ONLY in its room is not even offered
+ const onlyRoom = workbook({ collab: theirs } as Partial<DashDoc>)
+ const same = decide({ doc: loaded({ collab: mine } as Partial<DashDoc>), encrypted: false, readOnly: false,
+ snapshot: { ...row, json: JSON.stringify(onlyRoom) } })
+ ok(!same.offer && same.why === 'same', 'a snapshot whose only difference is a room is not offered at all')
+
+ // the paths that SHOULD adopt a file's identity are untouched: parseDoc
+ const opened = parseDoc(JSON.stringify(planted))
+ ok(opened.ok && (opened.doc as { collab?: { room?: string } }).collab?.room === 'w-ATTACKER',
+ 'parseDoc itself still keeps a file\'s own room — opening a FILE carries its capability by design')
+}
+
console.log(`\n${checks - failures}/${checks} checks passed`)
process.exit(failures ? 1 : 0)