Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
kernel+dash: a key rotation keeps the CRDT stamp and published links (#624)
nyblnet65d68d39ec1a
diff --git a/dash/src/sync/online.ts b/dash/src/sync/online.tsindex 9ec1cdb3..cf997bb1 100644--- a/dash/src/sync/online.ts+++ b/dash/src/sync/online.ts@@ -30,6 +30,7 @@ import type { Frame, RefusalCode, SyncSession, Transport } from './session.ts'import { offlineEnabled } from '../../../kernel/src/update.ts'import { netWebSocket } from '../../../kernel/src/net.ts'import { lsGet, lsSet } from '../../../kernel/src/storage.ts'+import { carryThroughRotation } from '../../../kernel/src/docfields.ts'export const DEFAULT_SYNC_HOST = 'wss://sync.bento.page'const SNAP_EVERY = 200 // ops between encrypted snapshot uploads@@ -827,8 +828,9 @@ export async function rotateKeys(session: SyncSession, store: Store) {stopSharing(session, store)const fresh = await mintCollab()const doc = store.doc as DashDoc & { collab?: CollabBlock }- const sync = doc.collab?.sync- doc.collab = sync ? { ...fresh, sync } : fresh+ // fresh keys = the revocation; carry the rotate-survivors (sync, links) from+ // the old block (kernel/src/docfields.ts COLLAB_ROTATE_KEEP)+ doc.collab = carryThroughRotation(fresh, doc.collab)// revocation is only real once the FILE holds the new keysstore.markUnsaved()}diff --git a/kernel/src/docfields.ts b/kernel/src/docfields.tsindex 519c3e60..fb638e3f 100644--- a/kernel/src/docfields.ts+++ b/kernel/src/docfields.ts@@ -89,3 +89,22 @@ export function collabForInvite(collab: Obj): Obj {for (const k of COLLAB_INVITE_KEEP) if (Object.hasOwn(collab, k) && collab[k] !== undefined) out[k] = collab[k]return out}++/** On a key rotation ("Reset access…") the collab is rebuilt with FRESH keys —+ * that is the revocation. These sub-fields are carried from the old block onto+ * the fresh one: the CRDT `sync` stamp (so the document's collab history+ * survives the reset) and `links` (published-link records survive a reset — the+ * maintainer's ruling). To make Reset REVOKE links instead, drop 'links' here —+ * a one-line change. */+export const COLLAB_ROTATE_KEEP = ['sync', 'links'] as const++/** Build the rotated collab: the freshly-minted block with the rotate-survivor+ * sub-fields carried over from `old`. Returns a new object; `fresh`'s type is+ * preserved. */+export function carryThroughRotation<T extends object>(fresh: T, old: object | undefined): T {+ if (!old) return fresh+ const out = { ...(fresh as Obj) }+ const src = old as Obj+ for (const k of COLLAB_ROTATE_KEEP) if (Object.hasOwn(src, k) && src[k] !== undefined) out[k] = src[k]+ return out as T+}diff --git a/kernel/src/sync/online.ts b/kernel/src/sync/online.tsindex 087afe33..7199424f 100644--- a/kernel/src/sync/online.ts+++ b/kernel/src/sync/online.ts@@ -13,6 +13,7 @@import type { Op, SyncStateJSON } from './crdt.ts'import type { Frame, HostStore, RefusalCode, SyncDoc, SyncSession, Transport } from './session.ts'import { lsGet, lsSet } from '../storage.ts'+import { carryThroughRotation } from '../docfields.ts'import { offlineEnabled } from '../update.ts'// Every request in the app goes through the one chokepoint (kernel/src/net.ts)// so the offline switch cannot be forgotten — see GHSA-5c3x-xqp6-g94r.@@ -1051,7 +1052,9 @@ export async function rotateKeys(session: SyncSession, store: Store) {stopSharing(session, store)const fresh = await mintCollab()markShared(store, () => {- const sync = store.doc.collab?.sync- store.doc.collab = sync ? { ...fresh, sync } : fresh+ // fresh keys = the revocation; carry the rotate-survivors (sync, links) from+ // the old block (kernel/src/docfields.ts COLLAB_ROTATE_KEEP). Not a commit —+ // a key reset is connection state, not an undoable edit (#605).+ store.doc.collab = carryThroughRotation(fresh, store.doc.collab)})}diff --git a/scripts/test-dash-share-dirty.ts b/scripts/test-dash-share-dirty.tsindex 4dcf4cde..3c925b49 100644--- a/scripts/test-dash-share-dirty.ts+++ b/scripts/test-dash-share-dirty.ts@@ -102,6 +102,21 @@ console.log('starting, stopping and rotating sharing mark the workbook unsaved')disconnectOnline(sess as never)}+console.log('\nrotation keeps the CRDT stamp and published links (shared kernel helper)')+{+ const s = new Store(fresh({ ...(await mintCollab()), on: true })), sess = fakeSession()+ const before = collabOf(s.doc) as Record<string, unknown>+ before.sync = { v: 2, tag: 'keepme' } // set on the live block (parseDoc wouldn't carry unknowns)+ before.links = [{ url: 'https://pub/x' }]+ const oldPriv = before.ownerPriv+ await rotateKeys(sess as never, s)+ const after = collabOf(s.doc) as Record<string, unknown>+ ok((after.sync as { tag?: string })?.tag === 'keepme', 'the CRDT sync stamp survived the rotation')+ ok(JSON.stringify(after.links) === JSON.stringify([{ url: 'https://pub/x' }]), 'published links survived the rotation (dash uses the kernel COLLAB_ROTATE_KEEP)')+ ok(after.ownerPriv !== oldPriv, 'the owner private key is fresh — access was revoked')+ disconnectOnline(sess as never)+}+console.log('\nminting credentials for a NEW workbook stays silent'){// and so does opting a never-saved workbook in: its credentials were minteddiff --git a/scripts/test-sync-session.ts b/scripts/test-sync-session.tsindex b08d8590..f51d7fdf 100644--- a/scripts/test-sync-session.ts+++ b/scripts/test-sync-session.ts@@ -57,7 +57,7 @@ const { Store } = await import('../slides/src/store.ts');const { SyncSession, assetsToOffload } = await import('../slides/src/sync/session.ts');const { newDoc, emptySlide } = await import('../slides/src/model.ts');const { SYNC_V } = await import('../kernel/src/sync/crdt.ts');-const { FROM_LIVE, keepLiveIdentity, CAP_FIELDS, withoutCaps, COLLAB_READER_KEEP, collabForReader, COLLAB_INVITE_KEEP, collabForInvite } = await import('../kernel/src/docfields.ts');+const { FROM_LIVE, keepLiveIdentity, CAP_FIELDS, withoutCaps, COLLAB_READER_KEEP, collabForReader, COLLAB_INVITE_KEEP, collabForInvite, COLLAB_ROTATE_KEEP, carryThroughRotation } = await import('../kernel/src/docfields.ts');let failures = 0, checks = 0;function ok(cond: boolean, msg: string) {@@ -647,6 +647,41 @@ H('docfields: a reader copy with NO sync stamp joins a live room and converges')sW.stop?.(); sR.stop?.();}+H('docfields: a key rotation carries sync + links onto fresh keys, drops the rest');+{+ const fresh = { room: 'NEW', key: 'NK', on: true, v: 2, owner: 'o2', ownerPriv: 'FRESHPRIV', role: 'writer' } as Record<string, unknown>;+ const old = {+ room: 'OLD', key: 'OK', on: true, v: 2, owner: 'o1', ownerPriv: 'OLDPRIV',+ sync: { v: SYNC_V, tag: 'history' }, links: [{ url: 'https://pub/1' }], writerPriv: 'W',+ } as Record<string, unknown>;+ const rot = carryThroughRotation(fresh, old) as Record<string, unknown>;+ ok(rot.room === 'NEW' && rot.key === 'NK' && rot.ownerPriv === 'FRESHPRIV', 'the fresh keys win — the rotation revokes the old ones');+ ok(JSON.stringify(rot.sync) === JSON.stringify(old.sync), 'the CRDT sync stamp is carried over (history survives)');+ ok(JSON.stringify(rot.links) === JSON.stringify(old.links), 'published links survive the reset (maintainer ruling)');+ ok(!('writerPriv' in rot), 'an old private half is NOT carried — it is revoked');+ ok([...COLLAB_ROTATE_KEEP].join() === 'sync,links', 'COLLAB_ROTATE_KEEP is sync + links');+ ok(carryThroughRotation(fresh, undefined) === fresh, 'no old block → the fresh collab is returned as-is');+}++H('rotateKeys rebuilds with fresh keys but preserves sync + links (kernel + dash share the helper)');+{+ const { rotateKeys } = await import('../kernel/src/sync/online.ts');+ const doc = newDoc(); doc.docId = `rot-${Math.random().toString(36).slice(2, 10)}`;+ (doc as unknown as { collab: Record<string, unknown> }).collab = {+ room: 'old-room', key: 'old-key', on: true, v: 2, owner: 'o', ownerPriv: 'OLD',+ sync: { v: SYNC_V, tag: 'keepme' }, links: [{ url: 'https://pub/x' }],+ };+ const store = new Store(JSON.parse(JSON.stringify(doc)));+ const sess = new SyncSession(store);+ await (rotateKeys as (s: unknown, st: unknown) => Promise<void>)(sess, store);+ const c = (store.doc as unknown as { collab: Record<string, unknown> }).collab;+ ok(c.room !== 'old-room' && c.key !== 'old-key', 'rotateKeys minted fresh room + key (access reset)');+ ok((c.sync as { tag?: string })?.tag === 'keepme', 'the sync stamp survived the rotation');+ ok(JSON.stringify(c.links) === JSON.stringify([{ url: 'https://pub/x' }]), 'the published links survived the rotation');+ ok(c.ownerPriv !== 'OLD', 'the old owner private key is gone (revoked)');+ sess.stop?.();+}+// Undo/redo move CONTENT, never identity. Two halves: stopSharing is no longer// its own undo step (kernel markShared), AND the Store keeps the live identity// set (docId, collab, readonly — restoregate's FROM_LIVE) across a restore, so a