Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

spaces: whether a copy may write is an allowlist of roles, failing closed (#599)

nyblnet1bdc4248828e
diff --git a/scripts/test-spaces-invite.ts b/scripts/test-spaces-invite.ts
index 07c75f35..6008a59b 100644
--- a/scripts/test-spaces-invite.ts
+++ b/scripts/test-spaces-invite.ts
@@ -196,5 +196,59 @@ ok(!isOwner(memberDoc), 'an invited copy does not read as the owner')
ok((await inviteCopy(memberDoc)) === null, 'inviteCopy() refuses — it holds no key to root a chain in')
ok(canWrite(memberDoc), '…but it can still write, which is the point of an invite')
+// ---------------------------------------------------------------------------
+console.log('\nwhich copies may write — an allowlist that fails closed')
+
+// share.ts used to ask `role !== 'reader'`, which answers "yes" for every role
+// invented after it. The kernel's 'audience' (a live-show member, receive-only
+// transport) is the one that exists today; the rest stand in for tomorrow's.
+// Every gate — canWrite, isReaderCopy, the boot lock in main.ts, the People
+// label in collabui.ts — answers through copyCanWrite, so this pins all of them.
+{
+ const { copyCanWrite, isReaderCopy } = await import('../spaces/src/share.ts')
+ const { Store } = await import('../spaces/src/store.ts')
+ const withCollab = (collab: unknown): SpacesDoc => ({ ...JSON.parse(JSON.stringify(doc)), collab }) as SpacesDoc
+ const room = { v: 2, on: true, room: c.room, key: c.key, owner: c.owner }
+ const writes = (d: SpacesDoc) => canWrite(d) && !isReaderCopy(d)
+ const locked = (d: SpacesDoc) => !canWrite(d) && isReaderCopy(d)
+
+ ok(writes(doc), 'an OWNER copy writes (no role field; marked by ownerPriv)')
+ ok(writes(memberDoc), 'an invited editor writes')
+ ok(writes(withCollab({ ...room, role: 'writer' })), "role 'writer' writes")
+ ok(writes(withCollab({ on: true, room: 'r1', key: 'K', writerPub: 'WP', writerPriv: 'WK' })),
+ 'a LEGACY copy with no role field writes — every pre-role file is one')
+ const plain = withCollab(undefined)
+ delete plain.collab
+ ok(!isReaderCopy(plain), 'a plain file with NO collab is not locked — it opens editable exactly as before')
+
+ ok(locked(viewer!), "role 'reader' is read-only")
+ ok(locked(withCollab({ ...room, role: 'audience',
+ invite: { pub: 'IP', priv: 'IK', role: 'audience', sig: 'S' } })),
+ "role 'audience' is read-only — though it carries an invite, which is what made it read as an Editor")
+ ok(locked(withCollab({ ...room, role: 'presenter-of-tomorrow' })),
+ 'a role nobody has taught this about fails CLOSED, not open')
+ ok(locked(withCollab({ ...room, role: 7 })), 'a non-string role is read-only')
+ ok(locked(withCollab({ ...room, role: null })), 'a null role is read-only — null is not absent')
+ ok(locked(withCollab(JSON.parse(`{"room":"w1","key":"K","role":"__proto__"}`))), "role '__proto__' is read-only")
+ ok(locked(withCollab(JSON.parse(`{"room":"w1","key":"K","role":"owner"}`))),
+ "role 'owner' is read-only — it is not a role value; ownership is a key, not a label")
+ // Inherited `role` must not answer for the file in either direction.
+ ok(!copyCanWrite(Object.assign(Object.create({ role: 'writer' }), { role: 'reader', room: 'w1', key: 'K' })),
+ 'an own reader role is not overridden by an inherited writer one')
+
+ // The lock as main.ts derives it, and what it does to a commit.
+ const audienceDoc = withCollab({ ...room, role: 'audience' })
+ const s = new Store(audienceDoc)
+ if (audienceDoc.readonly || isReaderCopy(audienceDoc)) s.readOnly = true
+ const before = JSON.stringify(s.doc)
+ s.commit(() => { s.doc.title = 'changed by an audience' })
+ ok(s.readOnly && JSON.stringify(s.doc) === before && !s.dirty,
+ 'an audience copy boots with a read-only store, and its commit is a no-op')
+ const ws = new Store(withCollab({ ...room, role: 'writer' }))
+ if (isReaderCopy(ws.doc)) ws.readOnly = true
+ ws.commit(() => { ws.doc.title = 'changed by a writer' })
+ ok(!ws.readOnly && ws.doc.title === 'changed by a writer', '…and a writer copy commits as before')
+}
+
console.log(`\n${checks - failures}/${checks} checks passed`)
if (failures) process.exit(1)
diff --git a/spaces/CHANGELOG.md b/spaces/CHANGELOG.md
index 820b8829..e2973980 100644
--- a/spaces/CHANGELOG.md
+++ b/spaces/CHANGELOG.md
@@ -477,6 +477,16 @@ Versions follow `0.MINOR.PATCH` while pre-1.0.
2026, from the same file. `bento.journal()` opens today's for an agent, and
`bento.journal('2026-08-06')` any day's.
+- **A copy that cannot write never gets writer chrome.** Whether a copy may
+ write was decided by `collab.role !== 'reader'`, so the live-show
+ `'audience'` role — and any role added later — passed as a writer: an
+ audience copy opened editable, was labelled Editor in People, and made local
+ commits the relay then refused. It is an allowlist now (`copyCanWrite` in
+ `share.ts`, the same shape as bento/type's): no role field or `'writer'`
+ writes, anything else opens view-only. Every gate — the boot lock, the Share
+ popover, the People label — asks that one function. Plain files and legacy
+ rooms open exactly as before.
+
## [0.1.0] — 2026-08-03
First release.
diff --git a/spaces/src/collabui.ts b/spaces/src/collabui.ts
index 649040d7..2496ef81 100644
--- a/spaces/src/collabui.ts
+++ b/spaces/src/collabui.ts
@@ -237,7 +237,10 @@ export class CollabUi {
const doc = this.host.store.doc
const c = doc.collab
if (!c) return {}
- if (c.role === 'reader') return { role: 'viewer' }
+ // Any copy that cannot write is a Viewer whatever else it carries — an
+ // audience copy holds an owner-signed INVITE (its show ticket), and the
+ // invite branch below would otherwise have called it an Editor.
+ if (isReaderCopy(doc)) return { role: 'viewer' }
if (isOwner(doc)) return { role: 'owner', pub: c.owner }
if (c.v === 2 && c.invite) {
let pub: string | undefined
diff --git a/spaces/src/main.ts b/spaces/src/main.ts
index 1693813e..331a7845 100644
--- a/spaces/src/main.ts
+++ b/spaces/src/main.ts
@@ -207,7 +207,8 @@ function boot(doc: SpacesDoc, repaired: string[], frozen?: 'policy' | 'version')
// `frozen` is the other, unrelated reason to lock: this build does not
// understand the file and must not rewrite it.
//
- // `collab.role === 'reader'` is the THIRD, unrelated reason, and it is the
+ // A collab role that cannot write — 'reader', 'audience', or one this build
+ // does not know (share.ts copyCanWrite, an allowlist) — is the THIRD, unrelated reason, and it is the
// only one of the three that keeps receiving: a view-only copy follows the
// live session and can never send to it. The lock here is a courtesy to the
// person holding it — the ENFORCEMENT is the relay, which pins a verified
diff --git a/spaces/src/share.ts b/spaces/src/share.ts
index 2a3a69ce..77760478 100644
--- a/spaces/src/share.ts
+++ b/spaces/src/share.ts
@@ -76,9 +76,31 @@ export function isOwner(doc: SpacesDoc): boolean {
return !!(c && c.v === 2 && c.owner && c.ownerPriv)
}
+/**
+ * May THIS COPY write to its room? The one answer every gate in spaces asks.
+ *
+ * An ALLOWLIST, and the shape is the point. This used to be
+ * `role !== 'reader'`, which answers "yes" for every role invented after it —
+ * so when the kernel added 'audience' (a live-show member whose transport is
+ * receive-only), an audience copy opened in spaces got writer chrome and made
+ * local commits the relay then refused. Failing CLOSED is the safe direction:
+ * a future role that can write shows view-only until it is taught here, a
+ * visible and harmless bug; the old shape's failure was invisible.
+ *
+ * Absent means writer because every file older than the role field is one —
+ * the owner copy included, which is marked by `ownerPriv`, not by a role.
+ * Read as an OWN property, so nothing inherited can answer for the file.
+ * Same shape and name as type's (type/src/model.ts).
+ */
+export function copyCanWrite(collab: SpacesDoc['collab'] | undefined): boolean {
+ if (!collab || typeof collab !== 'object') return false
+ if (!Object.prototype.hasOwnProperty.call(collab, 'role')) return true
+ return collab.role === undefined || collab.role === 'writer'
+}
+
/** Can this copy write at all? A reader copy holds no signing key. */
export function canWrite(doc: SpacesDoc): boolean {
- return !!doc.collab && doc.collab.role !== 'reader'
+ return copyCanWrite(doc.collab)
}
/**
@@ -138,7 +160,9 @@ export function readerCopy(doc: SpacesDoc): SpacesDoc | null {
* at all. Both lock the editor; only this one keeps receiving.
*/
export function isReaderCopy(doc: SpacesDoc): boolean {
- return doc.collab?.role === 'reader'
+ // Every collab copy that is not an allowlisted writer — 'reader', 'audience',
+ // and any role not yet invented. No collab at all is a plain file: editable.
+ return !!doc.collab && !copyCanWrite(doc.collab)
}
/**