Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

type: adopt the kernel doc-fields allowlist for share copies (#628)

nyblnet3c2ee933f30e
diff --git a/scripts/test-export-secrets.ts b/scripts/test-export-secrets.ts
index ef66d371..e3000b0a 100644
--- a/scripts/test-export-secrets.ts
+++ b/scripts/test-export-secrets.ts
@@ -448,50 +448,16 @@ function exportedBody(src: string, name: string): string {
return src.slice(open, i + 1)
}
-// The delete-based stripper TEXT checks hold only for an app that still mints
-// copies that way. type does (inline in collab.ts, through share.ts's stripper).
-// spaces moved to the kernel ALLOWLIST (collabForReader / collabForInvite) on
-// 2026-10-04; its builders are proven BEHAVIOURALLY in scripts/test-spaces-invite.ts
-// — this rig runs under strip-only node and cannot import a file with a parameter
-// property, so it keeps spaces' SOURCE guard here and the behaviour lives there.
-const SHARE_APPS = ['type']
-for (const app of SHARE_APPS) {
+// spaces and type both mint copies through the kernel ALLOWLIST
+// (collabForReader / collabForInvite) now. Their builders are proven BEHAVIOURALLY
+// in scripts/test-spaces-invite.ts and scripts/test-type-share.ts (bundled via
+// esbuild, running the real functions); this rig runs under strip-only node and
+// cannot import a file with a parameter property, so for each it keeps a SOURCE
+// guard: the builders route through the kernel helpers and reintroduce neither the
+// delete-stripper nor a spread-the-source bypass. The cross-app "no builder
+// bypasses" row lands with the slides adoption (the last one).
+for (const app of ['spaces', 'type']) {
const rel = `${app}/src/share.ts`
- let src: string
- try { src = read(rel) } catch {
- ok(false, `${rel} exists — share copies must be minted in one place`)
- continue
- }
- const stripper = exportedBody(src, 'stripCollabSecrets')
- ok(!!stripper, `${rel}: found stripCollabSecrets() — the checks below are worthless without it`)
- ok(/delete doc\.collab\.ownerPriv\b/.test(stripper), `${rel}: the stripper drops ownerPriv`)
- ok(/delete doc\.collab\.writerPriv\b/.test(stripper), `${rel}: the stripper drops writerPriv`)
- ok(/delete doc\.collab\.invite\b/.test(stripper), `${rel}: the stripper drops any invite it holds`)
- ok(/delete doc\.collab\b(?!\.)/.test(stripper),
- `${rel}: the stripper drops the whole block by default — the room key is a capability too`)
-
- const inviteFn = exportedBody(src, 'inviteCopy')
- ok(/stripCollabSecrets\(\s*out\s*,\s*\{\s*keepRoom:\s*true\s*\}\s*\)/.test(inviteFn),
- `${rel}: inviteCopy strips before it delegates`)
- ok(/mintInvite\(/.test(inviteFn), `${rel}: inviteCopy mints a SCOPED invite rather than passing the room's own keys`)
- const masked = mask(inviteFn)
- ok(masked.indexOf('stripCollabSecrets(') < masked.indexOf('mintInvite('),
- `${rel}: it strips FIRST — a stray writerPriv beside an invite is a second, unrevokable way in`)
- ok(!/ownerPriv\s*[,}]/.test(mask(exportedBody(src, 'readerCopy'))),
- `${rel}: readerCopy never re-attaches a private key`)
-
- // type's call site: collab.ts must build every copy through share.ts.
- const collab = read(`${app}/src/collab.ts`)
- ok(/inviteCopy\(/.test(collab) && /readerCopy\(/.test(collab) && !/stripCollabSecrets/.test(collab),
- `${app}/src/collab.ts mints share copies only through share.ts`)
-}
-
-// spaces: minted through the kernel allowlist now. SOURCE guard (behaviour is in
-// scripts/test-spaces-invite.ts): both builders route through the kernel helper,
-// the delete-based stripper is gone, and neither rebuilds collab by SPREADING the
-// source (`{ ...c }`) — the denylist bypass that would carry every secret back.
-{
- const rel = 'spaces/src/share.ts'
const src = read(rel)
ok(!/stripCollabSecrets/.test(src), `${rel}: the delete-based stripper is gone — the kernel allowlist replaces it`)
const inviteFn = mask(exportedBody(src, 'inviteCopy'))
@@ -499,11 +465,24 @@ for (const app of SHARE_APPS) {
ok(/collabForInvite\(/.test(inviteFn), `${rel}: inviteCopy routes through collabForInvite`)
ok(/collabForReader\(/.test(readerFn), `${rel}: readerCopy routes through collabForReader`)
ok(/mintInvite\(/.test(inviteFn), `${rel}: inviteCopy still mints a SCOPED invite`)
- ok(!/\{\s*\.\.\.c\b/.test(inviteFn) && !/\{\s*\.\.\.c\b/.test(readerFn),
- `${rel}: neither builder rebuilds collab by spreading the source — that bypass carries every secret`)
+ // the bypass: rebuilding collab by spreading ANY source expression (`{ ...c }`,
+ // `{ ...doc.collab }`, `{ ...out.collab }`) instead of the kernel helper carries
+ // every secret. The only spread allowed is of a collabFor* projection.
+ ok(!/\{\s*\.\.\.(?!collabFor)/.test(inviteFn) && !/\{\s*\.\.\.(?!collabFor)/.test(readerFn),
+ `${rel}: neither builder rebuilds collab by spreading the source — only a collabFor* projection`)
+}
+
+// type's call site: collab.ts must build every copy through share.ts.
+{
+ const collab = read('type/src/collab.ts')
+ ok(/inviteCopy\(/.test(collab) && /readerCopy\(/.test(collab) && !/stripCollabSecrets/.test(collab),
+ 'type/src/collab.ts mints share copies only through share.ts')
+}
- // the call site (unchanged by the migration): the share button derives its
- // document and writes through the encrypt-aware hook, never the ordinary save.
+// spaces' call site: the share button derives its document and writes through the
+// encrypt-aware hook, never the ordinary save path. (Its share.ts source guard is
+// in the loop above, alongside type.)
+{
const ed = read('spaces/src/editor.ts')
const share = bodies(ed).get('shareCopy') ?? ''
ok(!!share, 'spaces/src/editor.ts has a shareCopy()')
diff --git a/scripts/test-type-share.ts b/scripts/test-type-share.ts
index b6f640f5..7d334f30 100644
--- a/scripts/test-type-share.ts
+++ b/scripts/test-type-share.ts
@@ -25,7 +25,7 @@
import { register } from 'node:module'
import { readFileSync } from 'node:fs'
register('./lib/ts-resolve-hooks.mjs', import.meta.url)
-const { readerCopy, inviteCopy, stripCollabSecrets } = await import('../type/src/share.ts')
+const { readerCopy, inviteCopy } = await import('../type/src/share.ts')
const { emptyDoc, parseDoc } = await import('../type/src/model.ts')
const { mintCollab } = await import('../kernel/src/sync/online.ts')
import type { TypeDoc } from '../type/src/model.ts'
@@ -80,10 +80,46 @@ console.log('\nan editor copy writes only through its own invite\n')
ok(e.collab?.invite?.role === 'writer' && e.collab?.on === true, 'a live editor invite')
}
-console.log('\nthe default strip removes the room too\n')
+// The allowlist bar (security, 2026-10-04): every builder, from ONE source
+// carrying every known secret, an unknown field and a JSON own __proto__ key —
+// only the allowlist survives, nothing by name and nothing new by accident. (The
+// whole-block default drop is docForExport's, pinned in test-export-secrets.ts.)
+console.log('\nevery builder, from one poisoned source\n')
{
- const d = arrived(); stripCollabSecrets(d)
- ok(!('collab' in d), 'with no keepRoom, the whole collab block goes — the room key is a capability')
+ const poisoned = arrived()
+ const pc = JSON.parse('{"__proto__":{"polluted":true}}') as Record<string, unknown>
+ Object.assign(pc, poisoned.collab, {
+ writerPriv: 'WRITER-PRIVATE',
+ invite: { pub: 'SRC', priv: 'INVITE-PRIVATE', role: 'writer', sig: 's' },
+ audience: { invite: { priv: 'AUD-PRIVATE' } },
+ sync: { v: 2, tag: 'stamp' }, links: [{ url: 'https://pub' }], futureSecret: 'LEAK',
+ })
+ poisoned.collab = pc as TypeDoc['collab']
+
+ const READER_KEYS = ['room', 'key', 'owner', 'writerPub', 'on', 'v', 'role']
+ const INVITE_KEYS = ['room', 'key', 'owner', 'writerPub', 'on', 'v', 'sync', 'invite', 'role']
+ const POISON = ['writerPriv', 'ownerPriv', 'audience', 'futureSecret', 'links']
+
+ const rc = readerCopy(poisoned).collab as Record<string, unknown>
+ const ic = (await inviteCopy(poisoned, (await mintCollab()).ownerPriv as string)).collab as Record<string, unknown>
+
+ ok(Object.keys(rc).every((k) => READER_KEYS.includes(k)),
+ `reader copy: ONLY reader-allowlist keys survive (${Object.keys(rc).sort().join(',')})`)
+ ok(rc.role === 'reader' && rc.sync === undefined, 'reader: role forced reader, no sync stamp')
+ ok(Object.keys(ic).every((k) => INVITE_KEYS.includes(k)),
+ `invite copy: ONLY invite-allowlist keys survive (${Object.keys(ic).sort().join(',')})`)
+ ok(ic.sync !== undefined && (ic.invite as { pub: string }).pub !== 'SRC',
+ 'invite: keeps sync, carries a FRESH invite not the source\'s')
+ ok(ic.role === 'writer', 'invite: top-level role from the fresh writer invite (type mints writer invites only)')
+ for (const f of POISON) { ok(!(f in rc), `reader drops ${f}`); ok(!(f in ic), `invite drops ${f}`) }
+ ok(!Object.hasOwn(rc, '__proto__') && !Object.hasOwn(ic, '__proto__'), 'neither copy carries a __proto__ own key')
+ ok(({} as Record<string, unknown>).polluted === undefined, 'and the __proto__ source key polluted nothing (Object.hasOwn)')
+
+ // the planted bypass: rebuilding collab by SPREADING the source keeps every
+ // secret; the exact-key-set check above is what turns that red.
+ const bypass = { ...pc, role: 'reader' } as Record<string, unknown>
+ ok('writerPriv' in bypass && !Object.keys(bypass).every((k) => READER_KEYS.includes(k)),
+ 'a spread-the-source bypass FAILS the reader key-set check (not vacuous)')
}
console.log('\nevery copy is minted in share.ts\n')
diff --git a/type/src/share.ts b/type/src/share.ts
index d1a743fe..54f8e9b6 100644
--- a/type/src/share.ts
+++ b/type/src/share.ts
@@ -6,62 +6,49 @@
// copy…"). Minted in one place, DOM-free, so the copy that ships is the copy
// the rigs inspect.
//
-// It used to be built inline in collab.ts, and its stripper dropped ownerPriv
-// and the invite but NOT writerPriv. bento/type never mints a writer key — its
-// rooms are v2, owner-keyed — but `parseDoc` carries `collab` through verbatim,
-// so a document that ARRIVED holding a legacy writer key (an older file, a
-// pasted JSON) handed that key to every view-only copy made from it: a "reader"
-// holding a private key that writes, which defeats the read-only tier at the
-// relay. Nothing caught it because scripts/test-export-secrets.ts's share-copy
-// checks ran against spaces alone; this file is shaped to that same contract
-// (stripCollabSecrets / inviteCopy / readerCopy) so they now cover type too.
+// It used to be built inline in collab.ts with a hand list of fields to delete
+// that dropped ownerPriv and the invite but NOT a legacy writer key. bento/type
+// never mints a writer key — its rooms are v2, owner-keyed — but `parseDoc`
+// carries `collab` through verbatim, so a document that ARRIVED holding a legacy
+// writer key (an older file, a pasted JSON) handed that key to every view-only
+// copy made from it: a "reader" holding a private key that writes, which defeats
+// the read-only tier at the relay. The kernel ALLOWLIST closes that whole class —
+// a copy keeps only what is on the list, so an arrived-with field it does not
+// know is dropped, not carried.
//
-// Still a HAND LIST of private fields. The kernel's shared allowlist of what a
-// copy may keep will replace it; until then, a new private field in `collab`
-// must be added to stripCollabSecrets.
+// The private fields a copy must not carry are now the kernel's shared ALLOWLIST
+// (kernel/src/docfields.ts): a reader/invite copy keeps only what is on the list,
+// so a new field in `collab` — private or not — fails CLOSED instead of riding
+// along. The whole-block default drop lives in docForExport.
import { mintInvite } from './sync/online.ts';
+import { collabForReader, collabForInvite } from '../../kernel/src/docfields.ts';
import type { TypeDoc } from './model.ts';
const clone = (doc: TypeDoc): TypeDoc => JSON.parse(JSON.stringify(doc)) as TypeDoc;
-/**
- * Remove every private key a copy for someone else must not carry.
- *
- * By default the WHOLE block goes — the room key is a capability too. With
- * `keepRoom`, the room and its public keys stay (so the copy can follow the
- * live session) and every private key goes: the owner's, any legacy writer
- * key, and any invite keypair this document holds.
- */
-export function stripCollabSecrets(doc: TypeDoc, opts: { keepRoom?: boolean } = {}): void {
- if (!doc.collab) return;
- if (!opts.keepRoom) { delete doc.collab; return; }
- delete doc.collab.ownerPriv;
- delete doc.collab.writerPriv;
- delete doc.collab.invite;
-}
-
/**
* An EDITOR copy: joins live with edit access through its own owner-signed
- * invite, which the owner can revoke. It strips FIRST — a writer key left
- * beside the invite would be a second way in that no revocation reaches.
+ * invite, which the owner can revoke. The allowlist keeps room/read-key/public
+ * keys + sync and drops every private half; the fresh invite and the top-level
+ * role come from mintInvite, never from the source — so a legacy writer key or a
+ * source invite can never ride along.
*/
export async function inviteCopy(doc: TypeDoc, ownerPriv: string): Promise<TypeDoc> {
const out = clone(doc);
- stripCollabSecrets(out, { keepRoom: true });
- out.collab!.invite = await mintInvite(ownerPriv, 'writer');
- out.collab!.on = true;
+ const invite = await mintInvite(ownerPriv, 'writer');
+ out.collab = { ...collabForInvite(out.collab!, invite), on: true } as TypeDoc['collab'];
return out;
}
/**
- * A VIEW-ONLY copy: follows the live session and never writes. It carries the
- * room and the public keys, and no private key of any kind; the relay drops
- * anything it tries to send.
+ * A VIEW-ONLY copy: follows the live session and never writes. The allowlist
+ * keeps the room and the public keys, forces role:'reader', and drops every
+ * private key, the sync stamp AND anything unknown; the relay drops anything it
+ * tries to send.
*/
export function readerCopy(doc: TypeDoc): TypeDoc {
const out = clone(doc);
- out.collab = { ...out.collab!, role: 'reader', on: true, sync: undefined };
- stripCollabSecrets(out, { keepRoom: true });
+ out.collab = { ...collabForReader(out.collab!), on: true } as TypeDoc['collab'];
return out;
}
diff --git a/type/src/store.ts b/type/src/store.ts
index 9c4e40ee..bfa13a9a 100644
--- a/type/src/store.ts
+++ b/type/src/store.ts
@@ -20,6 +20,7 @@
import type { Block, TypeDoc } from './model.ts';
import { copyIsReceiveOnly } from './model.ts';
+import { FROM_LIVE } from '../../kernel/src/docfields.ts';
type Snap =
| { kind: 'doc'; doc: TypeDoc }
@@ -48,15 +49,16 @@ const LIMIT = 200;
* thing #588's read-only lock reads, so undo can never unlock a
* view-only copy
* readonly — the file's mode
- * The same list as bento/slides' FROM_LIVE (restoregate.ts). A local copy for
- * now; kernel will lift one shared list for every app.
+ * Now the shared kernel list (kernel/src/docfields.ts), re-exported here. The
+ * kernel superset also carries `template`, which type has no field for — keeping
+ * a field the document never has is a no-op.
*
* Consequence worth knowing: undoing a whole-document REPLACE (Replace from
* JSON, loadDoc, restoring a recovery snapshot) brings back the earlier
* CONTENT under the identity that replace brought in — identity is not
* undoable, whichever action changed it. bento/slides behaves the same.
*/
-export const FROM_LIVE = ['docId', 'collab', 'readonly'] as const;
+export { FROM_LIVE };
export function keepLiveIdentity(doc: TypeDoc, live: TypeDoc): void {
const d = doc as unknown as Record<string, unknown>;