@cero-base/core 1.15.2 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cero-base/core",
3
- "version": "1.15.2",
3
+ "version": "1.16.0",
4
4
  "description": "cero p2p primitives — identity, storage, network, database, blobs, rpc, pairing.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -136,7 +136,7 @@
136
136
  },
137
137
  "dependencies": {
138
138
  "@hyperswarm/secret-stream": "^6.9.1",
139
- "autobee": "2.0.0-rc.27",
139
+ "autobee": "2.0.0-rc.28",
140
140
  "autobee-encryption": "0.1.3",
141
141
  "b4a": "^1.8.1",
142
142
  "bare-crypto": "^1.15.3",
@@ -96,24 +96,70 @@ export function makeDispatcher(
96
96
  await view.insert(col, op)
97
97
  }
98
98
 
99
- const upsert = (name, col, kind) => async (op, ctx) => {
99
+ // Being an admitted writer is not a licence to write rows: a demoted member
100
+ // keeps its writer seat, and an unenrolled one resolves to no role at all.
101
+ // Genesis is exempt — the room has no members to check against yet.
102
+ // Being an admitted writer is not a licence to write rows: a member demoted
103
+ // to reader keeps its writer seat (set-member does not revoke it), and would
104
+ // otherwise keep writing every collection.
105
+ //
106
+ // A signer we cannot resolve to a role is allowed through: admission is
107
+ // optimistic, so a recovering device's first ops can apply before its own
108
+ // add-writer has been adopted in this view, and refusing those would drop
109
+ // them permanently. Being an admitted writer is the gate in that window.
110
+ const mayWrite = async (ctx) => {
111
+ const r = await getSignerRole(ctx.view, ctx.key)
112
+ return r === null || can(r, WRITE)
113
+ }
114
+
115
+ // Every row records the member who wrote it, so ownership is already known —
116
+ // an `own` collection just enforces it: the author may change their own rows,
117
+ // and anyone who can REMOVE may moderate the rest.
118
+ const mayTouch = async (ctx, existing, own) => {
119
+ if (!own || existing?.memberId == null) return true
120
+ if (existing.memberId === (await getSignerMember(ctx.view, ctx.key))) return true
121
+ return can(await getSignerRole(ctx.view, ctx.key), REMOVE)
122
+ }
123
+
124
+ const upsert = (name, col, kind, own) => async (op, ctx) => {
125
+ if (!(await mayWrite(ctx))) return
100
126
  const d = ctx.key && (await getDevice(ctx.view, toId(ctx.key)))
101
127
  op.memberId = d?.memberId || null
102
- if (kind === COLLECTION) await insert(ctx.view, name, col, op)
103
- else await ctx.view.insert(col, op)
128
+ if (kind !== COLLECTION) return ctx.view.insert(col, op)
129
+ // `add` on an id that already exists is an overwrite, so it answers to the
130
+ // same ownership rule as `set`
131
+ if (own && !(await mayTouch(ctx, await ctx.view.get(col, { id: op.id }), own))) return
132
+ await insert(ctx.view, name, col, op)
104
133
  }
105
- const update = (name, col) => async (op, ctx) => {
134
+ const update = (name, col, own) => async (op, ctx) => {
135
+ if (!(await mayWrite(ctx))) return
106
136
  const existing = await ctx.view.get(col, { id: op.id })
107
137
  if (!existing) return
108
- await insert(ctx.view, name, col, { ...existing, ...op })
138
+ if (!(await mayTouch(ctx, existing, own))) return
139
+ // memberId is attribution, so it is derived here and never merged off the
140
+ // wire — otherwise an edit could leave the row credited to its victim
141
+ const d = ctx.key && (await getDevice(ctx.view, toId(ctx.key)))
142
+ await insert(ctx.view, name, col, { ...existing, ...op, memberId: d?.memberId || null })
143
+ }
144
+ const remove = (col, own) => async (op, ctx) => {
145
+ if (!(await mayWrite(ctx))) return
146
+ if (own && !(await mayTouch(ctx, await ctx.view.get(col, { id: op.id }), own))) return
147
+ return ctx.view.delete(col, op)
109
148
  }
110
- const remove = (col) => async (op, ctx) => ctx.view.delete(col, op)
111
149
  const add = (verb, fn) => dispatcher.add(`@${ns}/${verb}`, fn)
112
150
 
113
151
  add('add-writer', async (op, ctx) => {
114
152
  if (!isKey(op.master) || !isKey(op.writer) || !isSig(op.sig)) return
115
153
  if (!Identity.verify(op.master, addWriterPayload(ctx.dbKey, op.writer, ctx.key), op.sig)) return
116
- if (!(await isGenesis(ctx.view)) && !can(await getRole(ctx.view, op.master), INVITE)) return
154
+ const genesis = await isGenesis(ctx.view)
155
+ const inviter = await getRole(ctx.view, op.master)
156
+ if (!genesis && !can(inviter, INVITE)) return
157
+ // admission is one decision: a rank we cannot grant admits nobody. Without
158
+ // this the paired add-member drops silently and leaves an unlisted writer
159
+ // that resolves, through its device row, to the inviter's own authority.
160
+ // Genesis is exempt for the same reason the INVITE check is — the room has
161
+ // no members yet to grant from.
162
+ if (!genesis && op.role && !grants(inviter, op.role)) return
117
163
  // always an indexer, like claim-writer below. The op's optional isIndexer
118
164
  // bool decodes to false when absent (nothing sets it), which silently made
119
165
  // every device a weight-1 writer — autobee 2.0 gc's caught-up non-indexer
@@ -122,7 +168,9 @@ export function makeDispatcher(
122
168
  const ts = op.ts || 0
123
169
  await insert(ctx.view, 'devices', `@${ns}/devices`, {
124
170
  id: toId(op.writer),
125
- memberId: toId(op.master),
171
+ // bind to the joiner when the op says so: if their member row never
172
+ // lands the device resolves to nobody, instead of to the inviter
173
+ memberId: op.memberId || toId(op.master),
126
174
  createdAt: ts,
127
175
  updatedAt: ts
128
176
  })
@@ -177,14 +225,39 @@ export function makeDispatcher(
177
225
  add('set-member', async (op, ctx) => {
178
226
  const existing = await getMember(ctx.view, op.id)
179
227
  if (!existing) return
180
- const next = { ...existing, ...op }
181
- if (next.role !== existing.role) {
228
+ // What the op may change is decided by WHOSE row it is, not by a list of
229
+ // field names — the member row is extensible (profile-sync adds avatar and
230
+ // whatever else an app declares), so a name list would silently drop them.
231
+ // Your own profile is yours to write; another member's row is untouchable
232
+ // apart from an authorised role change. Identity and provenance never come
233
+ // off the wire, so nobody overwrites the key a member was admitted with.
234
+ const isSelf = op.id === (await getSignerMember(ctx.view, ctx.key))
235
+ const next = isSelf ? { ...op } : { ...existing }
236
+ next.key = existing.key
237
+ next.role = existing.role
238
+ next.createdAt = existing.createdAt
239
+ next.updatedAt = op.updatedAt || existing.updatedAt
240
+
241
+ if (op.role && op.role !== existing.role) {
182
242
  const r = await getSignerRole(ctx.view, ctx.key)
183
- if (!can(r, ASSIGN) || !grants(r, next.role) || !outranks(r, existing.role)) {
184
- next.role = existing.role
243
+ if (can(r, ASSIGN) && grants(r, op.role) && outranks(r, existing.role)) {
244
+ next.role = op.role
185
245
  }
186
246
  }
187
247
  await insert(ctx.view, 'members', `@${ns}/members`, next)
248
+ // a demotion that takes WRITE away must take the writer seat with it —
249
+ // otherwise the member keeps appending, and only the apply-time WRITE
250
+ // gate stands between them and every collection
251
+ if (can(existing.role, WRITE) && !can(next.role, WRITE)) {
252
+ if (next.key) await ctx.host.removeWriter(next.key)
253
+ for (const device of await ctx.view.find(`@${ns}/devices`, {}).toArray()) {
254
+ if (device.memberId !== next.id) continue
255
+ await ctx.host.removeWriter(toKey(device.id))
256
+ }
257
+ // the device rows stay: they are how a key resolves to a member, and
258
+ // deleting them would leave the demoted writer unresolvable — which the
259
+ // apply-time gate reads as "not yet enrolled" and lets through
260
+ }
188
261
  })
189
262
 
190
263
  add('del-member', async (op, ctx) => {
@@ -325,14 +398,14 @@ export function makeDispatcher(
325
398
  }
326
399
  const col = `@${ns}/${name}`
327
400
  const kind = info.kind === SINGLE ? SINGLE : COLLECTION
328
- add(`set-${name}`, kind === SINGLE ? upsert(name, col, kind) : update(name, col))
401
+ add(`set-${name}`, kind === SINGLE ? upsert(name, col, kind) : update(name, col, info.own))
329
402
  if (info.kind === SINGLE) {
330
403
  // wipe the keyless single row (the dummy id in the op is ignored)
331
404
  add(`del-${name}`, async (op, ctx) => ctx.view.delete(col, {}))
332
405
  continue
333
406
  }
334
- add(`add-${name}`, upsert(name, col, COLLECTION))
335
- add(`del-${name}`, remove(col))
407
+ add(`add-${name}`, upsert(name, col, COLLECTION, info.own))
408
+ add(`del-${name}`, remove(col, info.own))
336
409
  }
337
410
 
338
411
  return {
package/src/lib/schema.js CHANGED
@@ -48,12 +48,18 @@ export const t = {
48
48
  /**
49
49
  * Multi-row table keyed by `id`.
50
50
  *
51
+ * `own` restricts a row to the member who wrote it — anyone may add rows, but
52
+ * only the author changes or deletes theirs. Members holding REMOVE can still
53
+ * moderate. Without it a writer may edit any row in the collection.
54
+ *
51
55
  * @param {Record<string, Prim>} fields
56
+ * @param {{ indexes?: Record<string, string[]>, own?: boolean }} [opts]
52
57
  * @returns {TypeDef}
53
58
  */
54
59
  collection(fields, opts) {
55
60
  const def = { kind: COLLECTION, fields }
56
61
  if (opts?.indexes) def.indexes = opts.indexes
62
+ if (opts?.own) def.own = true
57
63
  return def
58
64
  },
59
65
 
package/src/lib/utils.js CHANGED
@@ -54,6 +54,11 @@ export function can(role, perm) {
54
54
  return !!perms && (perms.includes('*') || perms.includes(perm))
55
55
  }
56
56
 
57
+ // A role that is not one of the four ranks grants nothing and outranks
58
+ // nothing, so every gate built on `grants` silently refuses it. Callers that
59
+ // accept a role from an app or a peer must reject it loudly instead.
60
+ export const isRank = (role) => RANK[role] != null
61
+
57
62
  export const grants = (a, b) => RANK[a] != null && RANK[b] != null && RANK[a] >= RANK[b]
58
63
  export const outranks = (a, b) => RANK[a] != null && RANK[b] != null && RANK[a] > RANK[b]
59
64
 
@@ -26,10 +26,18 @@ export namespace t {
26
26
  /**
27
27
  * Multi-row table keyed by `id`.
28
28
  *
29
+ * `own` restricts a row to the member who wrote it — anyone may add rows, but
30
+ * only the author changes or deletes theirs. Members holding REMOVE can still
31
+ * moderate. Without it a writer may edit any row in the collection.
32
+ *
29
33
  * @param {Record<string, Prim>} fields
34
+ * @param {{ indexes?: Record<string, string[]>, own?: boolean }} [opts]
30
35
  * @returns {TypeDef}
31
36
  */
32
- function collection(fields: Record<string, Prim>, opts: any): TypeDef;
37
+ function collection(fields: Record<string, Prim>, opts?: {
38
+ indexes?: Record<string, string[]>;
39
+ own?: boolean;
40
+ }): TypeDef;
33
41
  /**
34
42
  * RPC-style mutation that doesn't persist a row.
35
43
  *
@@ -44,5 +44,6 @@ export const toKey: (id: string) => Uint8Array;
44
44
  export const addWriterPayload: (dbKey: Uint8Array, writer: Uint8Array, appender: Uint8Array) => Uint8Array;
45
45
  /** @type {(dbKey: Uint8Array, writer: Uint8Array) => Uint8Array} */
46
46
  export const claimWriterPayload: (dbKey: Uint8Array, writer: Uint8Array) => Uint8Array;
47
+ export function isRank(role: any): boolean;
47
48
  export function grants(a: any, b: any): boolean;
48
49
  export function outranks(a: any, b: any): boolean;