@noy-db/hub 0.1.0-pre.3

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.
Files changed (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +197 -0
  3. package/dist/aggregate/index.cjs +476 -0
  4. package/dist/aggregate/index.cjs.map +1 -0
  5. package/dist/aggregate/index.d.cts +38 -0
  6. package/dist/aggregate/index.d.ts +38 -0
  7. package/dist/aggregate/index.js +53 -0
  8. package/dist/aggregate/index.js.map +1 -0
  9. package/dist/blobs/index.cjs +1480 -0
  10. package/dist/blobs/index.cjs.map +1 -0
  11. package/dist/blobs/index.d.cts +45 -0
  12. package/dist/blobs/index.d.ts +45 -0
  13. package/dist/blobs/index.js +48 -0
  14. package/dist/blobs/index.js.map +1 -0
  15. package/dist/bundle/index.cjs +436 -0
  16. package/dist/bundle/index.cjs.map +1 -0
  17. package/dist/bundle/index.d.cts +7 -0
  18. package/dist/bundle/index.d.ts +7 -0
  19. package/dist/bundle/index.js +40 -0
  20. package/dist/bundle/index.js.map +1 -0
  21. package/dist/chunk-2QR2PQTT.js +217 -0
  22. package/dist/chunk-2QR2PQTT.js.map +1 -0
  23. package/dist/chunk-4OWFYIDQ.js +79 -0
  24. package/dist/chunk-4OWFYIDQ.js.map +1 -0
  25. package/dist/chunk-5AATM2M2.js +90 -0
  26. package/dist/chunk-5AATM2M2.js.map +1 -0
  27. package/dist/chunk-ACLDOTNQ.js +543 -0
  28. package/dist/chunk-ACLDOTNQ.js.map +1 -0
  29. package/dist/chunk-BTDCBVJW.js +160 -0
  30. package/dist/chunk-BTDCBVJW.js.map +1 -0
  31. package/dist/chunk-CIMZBAZB.js +72 -0
  32. package/dist/chunk-CIMZBAZB.js.map +1 -0
  33. package/dist/chunk-E445ICYI.js +365 -0
  34. package/dist/chunk-E445ICYI.js.map +1 -0
  35. package/dist/chunk-EXQRC2L4.js +722 -0
  36. package/dist/chunk-EXQRC2L4.js.map +1 -0
  37. package/dist/chunk-FZU343FL.js +32 -0
  38. package/dist/chunk-FZU343FL.js.map +1 -0
  39. package/dist/chunk-GJILMRPO.js +354 -0
  40. package/dist/chunk-GJILMRPO.js.map +1 -0
  41. package/dist/chunk-GOUT6DND.js +1285 -0
  42. package/dist/chunk-GOUT6DND.js.map +1 -0
  43. package/dist/chunk-J66GRPNH.js +111 -0
  44. package/dist/chunk-J66GRPNH.js.map +1 -0
  45. package/dist/chunk-M2F2JAWB.js +464 -0
  46. package/dist/chunk-M2F2JAWB.js.map +1 -0
  47. package/dist/chunk-M5INGEFC.js +84 -0
  48. package/dist/chunk-M5INGEFC.js.map +1 -0
  49. package/dist/chunk-M62XNWRA.js +72 -0
  50. package/dist/chunk-M62XNWRA.js.map +1 -0
  51. package/dist/chunk-MR4424N3.js +275 -0
  52. package/dist/chunk-MR4424N3.js.map +1 -0
  53. package/dist/chunk-NPC4LFV5.js +132 -0
  54. package/dist/chunk-NPC4LFV5.js.map +1 -0
  55. package/dist/chunk-NXFEYLVG.js +311 -0
  56. package/dist/chunk-NXFEYLVG.js.map +1 -0
  57. package/dist/chunk-R36SIKES.js +79 -0
  58. package/dist/chunk-R36SIKES.js.map +1 -0
  59. package/dist/chunk-TDR6T5CJ.js +381 -0
  60. package/dist/chunk-TDR6T5CJ.js.map +1 -0
  61. package/dist/chunk-UF3BUNQZ.js +1 -0
  62. package/dist/chunk-UF3BUNQZ.js.map +1 -0
  63. package/dist/chunk-UQFSPSWG.js +1109 -0
  64. package/dist/chunk-UQFSPSWG.js.map +1 -0
  65. package/dist/chunk-USKYUS74.js +793 -0
  66. package/dist/chunk-USKYUS74.js.map +1 -0
  67. package/dist/chunk-XCL3WP6J.js +121 -0
  68. package/dist/chunk-XCL3WP6J.js.map +1 -0
  69. package/dist/chunk-XHFOENR2.js +680 -0
  70. package/dist/chunk-XHFOENR2.js.map +1 -0
  71. package/dist/chunk-ZFKD4QMV.js +430 -0
  72. package/dist/chunk-ZFKD4QMV.js.map +1 -0
  73. package/dist/chunk-ZLMV3TUA.js +490 -0
  74. package/dist/chunk-ZLMV3TUA.js.map +1 -0
  75. package/dist/chunk-ZRG4V3F5.js +17 -0
  76. package/dist/chunk-ZRG4V3F5.js.map +1 -0
  77. package/dist/consent/index.cjs +204 -0
  78. package/dist/consent/index.cjs.map +1 -0
  79. package/dist/consent/index.d.cts +24 -0
  80. package/dist/consent/index.d.ts +24 -0
  81. package/dist/consent/index.js +23 -0
  82. package/dist/consent/index.js.map +1 -0
  83. package/dist/crdt/index.cjs +152 -0
  84. package/dist/crdt/index.cjs.map +1 -0
  85. package/dist/crdt/index.d.cts +30 -0
  86. package/dist/crdt/index.d.ts +30 -0
  87. package/dist/crdt/index.js +24 -0
  88. package/dist/crdt/index.js.map +1 -0
  89. package/dist/crypto-IVKU7YTT.js +44 -0
  90. package/dist/crypto-IVKU7YTT.js.map +1 -0
  91. package/dist/delegation-XDJCBTI2.js +16 -0
  92. package/dist/delegation-XDJCBTI2.js.map +1 -0
  93. package/dist/dev-unlock-CeXic1xC.d.cts +263 -0
  94. package/dist/dev-unlock-KrKkcqD3.d.ts +263 -0
  95. package/dist/hash-9KO1BGxh.d.cts +63 -0
  96. package/dist/hash-ChfJjRjQ.d.ts +63 -0
  97. package/dist/history/index.cjs +1215 -0
  98. package/dist/history/index.cjs.map +1 -0
  99. package/dist/history/index.d.cts +62 -0
  100. package/dist/history/index.d.ts +62 -0
  101. package/dist/history/index.js +79 -0
  102. package/dist/history/index.js.map +1 -0
  103. package/dist/i18n/index.cjs +746 -0
  104. package/dist/i18n/index.cjs.map +1 -0
  105. package/dist/i18n/index.d.cts +38 -0
  106. package/dist/i18n/index.d.ts +38 -0
  107. package/dist/i18n/index.js +55 -0
  108. package/dist/i18n/index.js.map +1 -0
  109. package/dist/index-BRHBCmLt.d.ts +1940 -0
  110. package/dist/index-C8kQtmOk.d.ts +380 -0
  111. package/dist/index-DN-J-5wT.d.cts +1940 -0
  112. package/dist/index-DhjMjz7L.d.cts +380 -0
  113. package/dist/index.cjs +14756 -0
  114. package/dist/index.cjs.map +1 -0
  115. package/dist/index.d.cts +269 -0
  116. package/dist/index.d.ts +269 -0
  117. package/dist/index.js +6085 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/indexing/index.cjs +736 -0
  120. package/dist/indexing/index.cjs.map +1 -0
  121. package/dist/indexing/index.d.cts +36 -0
  122. package/dist/indexing/index.d.ts +36 -0
  123. package/dist/indexing/index.js +77 -0
  124. package/dist/indexing/index.js.map +1 -0
  125. package/dist/lazy-builder-BwEoBQZ9.d.ts +304 -0
  126. package/dist/lazy-builder-CZVLKh0Z.d.cts +304 -0
  127. package/dist/ledger-2NX4L7PN.js +33 -0
  128. package/dist/ledger-2NX4L7PN.js.map +1 -0
  129. package/dist/mime-magic-CBBSOkjm.d.cts +50 -0
  130. package/dist/mime-magic-CBBSOkjm.d.ts +50 -0
  131. package/dist/periods/index.cjs +1035 -0
  132. package/dist/periods/index.cjs.map +1 -0
  133. package/dist/periods/index.d.cts +21 -0
  134. package/dist/periods/index.d.ts +21 -0
  135. package/dist/periods/index.js +25 -0
  136. package/dist/periods/index.js.map +1 -0
  137. package/dist/predicate-SBHmi6D0.d.cts +161 -0
  138. package/dist/predicate-SBHmi6D0.d.ts +161 -0
  139. package/dist/query/index.cjs +1957 -0
  140. package/dist/query/index.cjs.map +1 -0
  141. package/dist/query/index.d.cts +3 -0
  142. package/dist/query/index.d.ts +3 -0
  143. package/dist/query/index.js +62 -0
  144. package/dist/query/index.js.map +1 -0
  145. package/dist/session/index.cjs +487 -0
  146. package/dist/session/index.cjs.map +1 -0
  147. package/dist/session/index.d.cts +45 -0
  148. package/dist/session/index.d.ts +45 -0
  149. package/dist/session/index.js +44 -0
  150. package/dist/session/index.js.map +1 -0
  151. package/dist/shadow/index.cjs +133 -0
  152. package/dist/shadow/index.cjs.map +1 -0
  153. package/dist/shadow/index.d.cts +16 -0
  154. package/dist/shadow/index.d.ts +16 -0
  155. package/dist/shadow/index.js +20 -0
  156. package/dist/shadow/index.js.map +1 -0
  157. package/dist/store/index.cjs +1069 -0
  158. package/dist/store/index.cjs.map +1 -0
  159. package/dist/store/index.d.cts +491 -0
  160. package/dist/store/index.d.ts +491 -0
  161. package/dist/store/index.js +34 -0
  162. package/dist/store/index.js.map +1 -0
  163. package/dist/strategy-BSxFXGzb.d.cts +110 -0
  164. package/dist/strategy-BSxFXGzb.d.ts +110 -0
  165. package/dist/strategy-D-SrOLCl.d.cts +548 -0
  166. package/dist/strategy-D-SrOLCl.d.ts +548 -0
  167. package/dist/sync/index.cjs +1062 -0
  168. package/dist/sync/index.cjs.map +1 -0
  169. package/dist/sync/index.d.cts +42 -0
  170. package/dist/sync/index.d.ts +42 -0
  171. package/dist/sync/index.js +28 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/team/index.cjs +1233 -0
  174. package/dist/team/index.cjs.map +1 -0
  175. package/dist/team/index.d.cts +117 -0
  176. package/dist/team/index.d.ts +117 -0
  177. package/dist/team/index.js +39 -0
  178. package/dist/team/index.js.map +1 -0
  179. package/dist/tx/index.cjs +212 -0
  180. package/dist/tx/index.cjs.map +1 -0
  181. package/dist/tx/index.d.cts +20 -0
  182. package/dist/tx/index.d.ts +20 -0
  183. package/dist/tx/index.js +20 -0
  184. package/dist/tx/index.js.map +1 -0
  185. package/dist/types-BZpCZB8N.d.ts +7526 -0
  186. package/dist/types-Bfs0qr5F.d.cts +7526 -0
  187. package/dist/ulid-COREQ2RQ.js +9 -0
  188. package/dist/ulid-COREQ2RQ.js.map +1 -0
  189. package/dist/util/index.cjs +230 -0
  190. package/dist/util/index.cjs.map +1 -0
  191. package/dist/util/index.d.cts +77 -0
  192. package/dist/util/index.d.ts +77 -0
  193. package/dist/util/index.js +190 -0
  194. package/dist/util/index.js.map +1 -0
  195. package/package.json +244 -0
@@ -0,0 +1,464 @@
1
+ import {
2
+ NOYDB_FORMAT_VERSION,
3
+ NOYDB_KEYRING_VERSION
4
+ } from "./chunk-ZRG4V3F5.js";
5
+ import {
6
+ base64ToBuffer,
7
+ bufferToBase64,
8
+ decrypt,
9
+ deriveKey,
10
+ encrypt,
11
+ generateDEK,
12
+ generateSalt,
13
+ unwrapKey,
14
+ wrapKey
15
+ } from "./chunk-MR4424N3.js";
16
+ import {
17
+ KeyringExpiredError,
18
+ NoAccessError,
19
+ PermissionDeniedError,
20
+ PrivilegeEscalationError
21
+ } from "./chunk-ACLDOTNQ.js";
22
+
23
+ // src/team/keyring.ts
24
+ var ADMIN_GRANTABLE_TARGETS = ["operator", "viewer", "client", "admin"];
25
+ function canGrant(callerRole, targetRole) {
26
+ if (callerRole === "owner") return true;
27
+ if (callerRole === "admin") return ADMIN_GRANTABLE_TARGETS.includes(targetRole);
28
+ return false;
29
+ }
30
+ function canRevoke(callerRole, targetRole) {
31
+ if (targetRole === "owner") return false;
32
+ if (callerRole === "owner") return true;
33
+ if (callerRole === "admin") return ADMIN_GRANTABLE_TARGETS.includes(targetRole);
34
+ return false;
35
+ }
36
+ async function loadKeyring(adapter, vault, userId, passphrase) {
37
+ const envelope = await adapter.get(vault, "_keyring", userId);
38
+ if (!envelope) {
39
+ throw new NoAccessError(`No keyring found for user "${userId}" in vault "${vault}"`);
40
+ }
41
+ const keyringFile = JSON.parse(envelope._data);
42
+ if (keyringFile.expires_at !== void 0) {
43
+ const cutoff = Date.parse(keyringFile.expires_at);
44
+ if (Number.isFinite(cutoff) && Date.now() >= cutoff) {
45
+ throw new KeyringExpiredError({ userId: keyringFile.user_id, expiresAt: keyringFile.expires_at });
46
+ }
47
+ }
48
+ const salt = base64ToBuffer(keyringFile.salt);
49
+ const kek = await deriveKey(passphrase, salt);
50
+ const deks = /* @__PURE__ */ new Map();
51
+ for (const [collName, wrappedDek] of Object.entries(keyringFile.deks)) {
52
+ const dek = await unwrapKey(wrappedDek, kek);
53
+ deks.set(collName, dek);
54
+ }
55
+ return {
56
+ userId: keyringFile.user_id,
57
+ displayName: keyringFile.display_name,
58
+ role: keyringFile.role,
59
+ permissions: keyringFile.permissions,
60
+ deks,
61
+ kek,
62
+ salt,
63
+ ...keyringFile.export_capability !== void 0 && { exportCapability: keyringFile.export_capability },
64
+ ...keyringFile.import_capability !== void 0 && { importCapability: keyringFile.import_capability }
65
+ };
66
+ }
67
+ async function createOwnerKeyring(adapter, vault, userId, passphrase) {
68
+ const salt = generateSalt();
69
+ const kek = await deriveKey(passphrase, salt);
70
+ const keyringFile = {
71
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
72
+ user_id: userId,
73
+ display_name: userId,
74
+ role: "owner",
75
+ permissions: {},
76
+ deks: {},
77
+ salt: bufferToBase64(salt),
78
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
79
+ granted_by: userId
80
+ };
81
+ await writeKeyringFile(adapter, vault, userId, keyringFile);
82
+ return {
83
+ userId,
84
+ displayName: userId,
85
+ role: "owner",
86
+ permissions: {},
87
+ deks: /* @__PURE__ */ new Map(),
88
+ kek,
89
+ salt
90
+ };
91
+ }
92
+ async function grant(adapter, vault, callerKeyring, options) {
93
+ if (!canGrant(callerKeyring.role, options.role)) {
94
+ throw new PermissionDeniedError(
95
+ `Role "${callerKeyring.role}" cannot grant role "${options.role}"`
96
+ );
97
+ }
98
+ const permissions = resolvePermissions(options.role, options.permissions);
99
+ const newSalt = generateSalt();
100
+ const newKek = await deriveKey(options.passphrase, newSalt);
101
+ const wrappedDeks = {};
102
+ for (const collName of Object.keys(permissions)) {
103
+ const dek = callerKeyring.deks.get(collName);
104
+ if (dek) {
105
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
106
+ }
107
+ }
108
+ if (options.role === "owner" || options.role === "admin" || options.role === "viewer") {
109
+ for (const [collName, dek] of callerKeyring.deks) {
110
+ if (!(collName in wrappedDeks)) {
111
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
112
+ }
113
+ }
114
+ }
115
+ for (const [collName, dek] of callerKeyring.deks) {
116
+ if (collName.startsWith("_") && !(collName in wrappedDeks)) {
117
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
118
+ }
119
+ }
120
+ for (const collName of Object.keys(wrappedDeks)) {
121
+ if (!callerKeyring.deks.has(collName)) {
122
+ throw new PrivilegeEscalationError(collName);
123
+ }
124
+ }
125
+ const keyringFile = {
126
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
127
+ user_id: options.userId,
128
+ display_name: options.displayName,
129
+ role: options.role,
130
+ permissions,
131
+ deks: wrappedDeks,
132
+ salt: bufferToBase64(newSalt),
133
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
134
+ granted_by: callerKeyring.userId,
135
+ ...options.exportCapability !== void 0 && { export_capability: options.exportCapability },
136
+ ...options.importCapability !== void 0 && { import_capability: options.importCapability }
137
+ };
138
+ await writeKeyringFile(adapter, vault, options.userId, keyringFile);
139
+ }
140
+ async function findAdminDescendants(adapter, vault, rootUserId) {
141
+ const allUserIds = await adapter.list(vault, "_keyring");
142
+ const childrenByParent = /* @__PURE__ */ new Map();
143
+ for (const userId of allUserIds) {
144
+ const env = await adapter.get(vault, "_keyring", userId);
145
+ if (!env) continue;
146
+ const kf = JSON.parse(env._data);
147
+ if (kf.role !== "admin") continue;
148
+ if (kf.user_id === rootUserId) continue;
149
+ const list = childrenByParent.get(kf.granted_by) ?? [];
150
+ list.push(kf.user_id);
151
+ childrenByParent.set(kf.granted_by, list);
152
+ }
153
+ const visited = /* @__PURE__ */ new Set();
154
+ const order = [];
155
+ const stack = [...childrenByParent.get(rootUserId) ?? []];
156
+ while (stack.length > 0) {
157
+ const next = stack.pop();
158
+ if (visited.has(next)) continue;
159
+ visited.add(next);
160
+ order.push(next);
161
+ for (const grandchild of childrenByParent.get(next) ?? []) {
162
+ if (!visited.has(grandchild)) stack.push(grandchild);
163
+ }
164
+ }
165
+ return order;
166
+ }
167
+ async function revoke(adapter, vault, callerKeyring, options) {
168
+ const targetEnvelope = await adapter.get(vault, "_keyring", options.userId);
169
+ if (!targetEnvelope) {
170
+ throw new NoAccessError(`User "${options.userId}" has no keyring in vault "${vault}"`);
171
+ }
172
+ const targetKeyring = JSON.parse(targetEnvelope._data);
173
+ if (!canRevoke(callerKeyring.role, targetKeyring.role)) {
174
+ throw new PermissionDeniedError(
175
+ `Role "${callerKeyring.role}" cannot revoke role "${targetKeyring.role}"`
176
+ );
177
+ }
178
+ const cascadeMode = options.cascade ?? "strict";
179
+ const usersToRevoke = [options.userId];
180
+ const affectedCollections = new Set(Object.keys(targetKeyring.deks));
181
+ if (targetKeyring.role === "admin") {
182
+ const descendants = await findAdminDescendants(adapter, vault, options.userId);
183
+ if (descendants.length > 0) {
184
+ if (cascadeMode === "warn") {
185
+ console.warn(
186
+ `[noy-db] revoke(${options.userId}): cascade='warn' \u2014 leaving ${descendants.length} descendant admin(s) in place: ${descendants.join(", ")}. These admins were granted by the revoked user (transitively) and will become orphans in the delegation tree.`
187
+ );
188
+ } else {
189
+ for (const userId of descendants) {
190
+ const descEnv = await adapter.get(vault, "_keyring", userId);
191
+ if (!descEnv) continue;
192
+ const descKf = JSON.parse(descEnv._data);
193
+ usersToRevoke.push(userId);
194
+ for (const c of Object.keys(descKf.deks)) affectedCollections.add(c);
195
+ }
196
+ }
197
+ }
198
+ }
199
+ for (const userId of usersToRevoke) {
200
+ await adapter.delete(vault, "_keyring", userId);
201
+ }
202
+ if (options.rotateKeys !== false && affectedCollections.size > 0) {
203
+ await rotateKeys(adapter, vault, callerKeyring, [...affectedCollections]);
204
+ }
205
+ }
206
+ async function rotateKeys(adapter, vault, callerKeyring, collections) {
207
+ const newDeks = /* @__PURE__ */ new Map();
208
+ for (const collName of collections) {
209
+ newDeks.set(collName, await generateDEK());
210
+ }
211
+ for (const collName of collections) {
212
+ const oldDek = callerKeyring.deks.get(collName);
213
+ const newDek = newDeks.get(collName);
214
+ if (!oldDek) continue;
215
+ const ids = await adapter.list(vault, collName);
216
+ for (const id of ids) {
217
+ const envelope = await adapter.get(vault, collName, id);
218
+ if (!envelope || !envelope._iv) continue;
219
+ const plaintext = await decrypt(envelope._iv, envelope._data, oldDek);
220
+ const { iv, data } = await encrypt(plaintext, newDek);
221
+ const newEnvelope = {
222
+ _noydb: NOYDB_FORMAT_VERSION,
223
+ _v: envelope._v,
224
+ _ts: (/* @__PURE__ */ new Date()).toISOString(),
225
+ _iv: iv,
226
+ _data: data
227
+ };
228
+ await adapter.put(vault, collName, id, newEnvelope);
229
+ }
230
+ }
231
+ for (const [collName, newDek] of newDeks) {
232
+ callerKeyring.deks.set(collName, newDek);
233
+ }
234
+ await persistKeyring(adapter, vault, callerKeyring);
235
+ const userIds = await adapter.list(vault, "_keyring");
236
+ for (const userId of userIds) {
237
+ if (userId === callerKeyring.userId) continue;
238
+ const userEnvelope = await adapter.get(vault, "_keyring", userId);
239
+ if (!userEnvelope) continue;
240
+ const userKeyringFile = JSON.parse(userEnvelope._data);
241
+ const updatedDeks = { ...userKeyringFile.deks };
242
+ for (const collName of collections) {
243
+ delete updatedDeks[collName];
244
+ }
245
+ const updatedPermissions = { ...userKeyringFile.permissions };
246
+ for (const collName of collections) {
247
+ delete updatedPermissions[collName];
248
+ }
249
+ const updatedKeyring = {
250
+ ...userKeyringFile,
251
+ deks: updatedDeks,
252
+ permissions: updatedPermissions
253
+ };
254
+ await writeKeyringFile(adapter, vault, userId, updatedKeyring);
255
+ }
256
+ }
257
+ async function changeSecret(adapter, vault, keyring, newPassphrase) {
258
+ const newSalt = generateSalt();
259
+ const newKek = await deriveKey(newPassphrase, newSalt);
260
+ const wrappedDeks = {};
261
+ for (const [collName, dek] of keyring.deks) {
262
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
263
+ }
264
+ const keyringFile = {
265
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
266
+ user_id: keyring.userId,
267
+ display_name: keyring.displayName,
268
+ role: keyring.role,
269
+ permissions: keyring.permissions,
270
+ deks: wrappedDeks,
271
+ salt: bufferToBase64(newSalt),
272
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
273
+ granted_by: keyring.userId
274
+ };
275
+ await writeKeyringFile(adapter, vault, keyring.userId, keyringFile);
276
+ return {
277
+ userId: keyring.userId,
278
+ displayName: keyring.displayName,
279
+ role: keyring.role,
280
+ permissions: keyring.permissions,
281
+ deks: keyring.deks,
282
+ // Same DEKs, different wrapping
283
+ kek: newKek,
284
+ salt: newSalt
285
+ };
286
+ }
287
+ async function buildRecipientKeyringFile(callerKeyring, recipient) {
288
+ const role = recipient.role ?? "viewer";
289
+ const permissions = resolvePermissions(role, recipient.permissions);
290
+ const newSalt = generateSalt();
291
+ const newKek = await deriveKey(recipient.passphrase, newSalt);
292
+ const wrappedDeks = {};
293
+ for (const collName of Object.keys(permissions)) {
294
+ const dek = callerKeyring.deks.get(collName);
295
+ if (dek) {
296
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
297
+ }
298
+ }
299
+ if (role === "owner" || role === "admin" || role === "viewer") {
300
+ for (const [collName, dek] of callerKeyring.deks) {
301
+ if (!(collName in wrappedDeks)) {
302
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
303
+ }
304
+ }
305
+ }
306
+ for (const [collName, dek] of callerKeyring.deks) {
307
+ if (collName.startsWith("_") && !(collName in wrappedDeks)) {
308
+ wrappedDeks[collName] = await wrapKey(dek, newKek);
309
+ }
310
+ }
311
+ for (const collName of Object.keys(wrappedDeks)) {
312
+ if (!callerKeyring.deks.has(collName)) {
313
+ throw new PrivilegeEscalationError(collName);
314
+ }
315
+ }
316
+ return {
317
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
318
+ user_id: recipient.id,
319
+ display_name: recipient.displayName ?? recipient.id,
320
+ role,
321
+ permissions,
322
+ deks: wrappedDeks,
323
+ salt: bufferToBase64(newSalt),
324
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
325
+ granted_by: callerKeyring.userId,
326
+ ...recipient.exportCapability !== void 0 ? { export_capability: recipient.exportCapability } : {},
327
+ ...recipient.importCapability !== void 0 ? { import_capability: recipient.importCapability } : {},
328
+ ...recipient.expiresAt !== void 0 ? { expires_at: recipient.expiresAt } : {}
329
+ };
330
+ }
331
+ async function listUsers(adapter, vault) {
332
+ const userIds = await adapter.list(vault, "_keyring");
333
+ const users = [];
334
+ for (const userId of userIds) {
335
+ const envelope = await adapter.get(vault, "_keyring", userId);
336
+ if (!envelope) continue;
337
+ const kf = JSON.parse(envelope._data);
338
+ users.push({
339
+ userId: kf.user_id,
340
+ displayName: kf.display_name,
341
+ role: kf.role,
342
+ permissions: kf.permissions,
343
+ createdAt: kf.created_at,
344
+ grantedBy: kf.granted_by
345
+ });
346
+ }
347
+ return users;
348
+ }
349
+ async function ensureCollectionDEK(adapter, vault, keyring) {
350
+ const inFlight = /* @__PURE__ */ new Map();
351
+ return async (collectionName) => {
352
+ const existing = keyring.deks.get(collectionName);
353
+ if (existing) return existing;
354
+ const pending = inFlight.get(collectionName);
355
+ if (pending) return pending;
356
+ const promise = (async () => {
357
+ const dek = await generateDEK();
358
+ keyring.deks.set(collectionName, dek);
359
+ await persistKeyring(adapter, vault, keyring);
360
+ return dek;
361
+ })();
362
+ inFlight.set(collectionName, promise);
363
+ try {
364
+ return await promise;
365
+ } finally {
366
+ inFlight.delete(collectionName);
367
+ }
368
+ };
369
+ }
370
+ function hasWritePermission(keyring, collectionName) {
371
+ if (keyring.role === "owner" || keyring.role === "admin") return true;
372
+ if (keyring.role === "viewer" || keyring.role === "client") return false;
373
+ return keyring.permissions[collectionName] === "rw";
374
+ }
375
+ function hasAccess(keyring, collectionName) {
376
+ if (keyring.role === "owner" || keyring.role === "admin" || keyring.role === "viewer") return true;
377
+ return collectionName in keyring.permissions;
378
+ }
379
+ async function persistKeyring(adapter, vault, keyring) {
380
+ const wrappedDeks = {};
381
+ for (const [collName, dek] of keyring.deks) {
382
+ wrappedDeks[collName] = await wrapKey(dek, keyring.kek);
383
+ }
384
+ const keyringFile = {
385
+ _noydb_keyring: NOYDB_KEYRING_VERSION,
386
+ user_id: keyring.userId,
387
+ display_name: keyring.displayName,
388
+ role: keyring.role,
389
+ permissions: keyring.permissions,
390
+ deks: wrappedDeks,
391
+ salt: bufferToBase64(keyring.salt),
392
+ created_at: (/* @__PURE__ */ new Date()).toISOString(),
393
+ granted_by: keyring.userId,
394
+ ...keyring.exportCapability !== void 0 && { export_capability: keyring.exportCapability },
395
+ ...keyring.importCapability !== void 0 && { import_capability: keyring.importCapability }
396
+ };
397
+ await writeKeyringFile(adapter, vault, keyring.userId, keyringFile);
398
+ }
399
+ function defaultBundleCapability(role) {
400
+ return role === "owner" || role === "admin";
401
+ }
402
+ function hasExportCapability(keyring, tier, format) {
403
+ const cap = keyring.exportCapability;
404
+ if (tier === "plaintext") {
405
+ const allowed = cap?.plaintext ?? [];
406
+ return allowed.includes("*") || format !== void 0 && allowed.includes(format);
407
+ }
408
+ return cap?.bundle ?? defaultBundleCapability(keyring.role);
409
+ }
410
+ function evaluateExportCapability(capability, role, tier, format) {
411
+ if (tier === "plaintext") {
412
+ const allowed = capability?.plaintext ?? [];
413
+ return allowed.includes("*") || format !== void 0 && allowed.includes(format);
414
+ }
415
+ return capability?.bundle ?? defaultBundleCapability(role);
416
+ }
417
+ function hasImportCapability(keyring, tier, format) {
418
+ const cap = keyring.importCapability;
419
+ if (tier === "plaintext") {
420
+ const allowed = cap?.plaintext ?? [];
421
+ return allowed.includes("*") || format !== void 0 && allowed.includes(format);
422
+ }
423
+ return cap?.bundle === true;
424
+ }
425
+ function evaluateImportCapability(capability, _role, tier, format) {
426
+ if (tier === "plaintext") {
427
+ const allowed = capability?.plaintext ?? [];
428
+ return allowed.includes("*") || format !== void 0 && allowed.includes(format);
429
+ }
430
+ return capability?.bundle === true;
431
+ }
432
+ function resolvePermissions(role, explicit) {
433
+ if (role === "owner" || role === "admin" || role === "viewer") return {};
434
+ return explicit ?? {};
435
+ }
436
+ async function writeKeyringFile(adapter, vault, userId, keyringFile) {
437
+ const envelope = {
438
+ _noydb: 1,
439
+ _v: 1,
440
+ _ts: (/* @__PURE__ */ new Date()).toISOString(),
441
+ _iv: "",
442
+ _data: JSON.stringify(keyringFile)
443
+ };
444
+ await adapter.put(vault, "_keyring", userId, envelope);
445
+ }
446
+
447
+ export {
448
+ loadKeyring,
449
+ createOwnerKeyring,
450
+ grant,
451
+ revoke,
452
+ rotateKeys,
453
+ changeSecret,
454
+ buildRecipientKeyringFile,
455
+ listUsers,
456
+ ensureCollectionDEK,
457
+ hasWritePermission,
458
+ hasAccess,
459
+ hasExportCapability,
460
+ evaluateExportCapability,
461
+ hasImportCapability,
462
+ evaluateImportCapability
463
+ };
464
+ //# sourceMappingURL=chunk-M2F2JAWB.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/team/keyring.ts"],"sourcesContent":["import type { NoydbStore, KeyringFile, Role, Permissions, GrantOptions, RevokeOptions, UserInfo, EncryptedEnvelope, ExportCapability, ExportFormat, ImportCapability } from '../types.js'\nimport { NOYDB_KEYRING_VERSION, NOYDB_FORMAT_VERSION } from '../types.js'\nimport {\n deriveKey,\n generateDEK,\n generateSalt,\n wrapKey,\n unwrapKey,\n encrypt,\n decrypt,\n bufferToBase64,\n base64ToBuffer,\n} from '../crypto.js'\nimport { NoAccessError, PermissionDeniedError, PrivilegeEscalationError, KeyringExpiredError } from '../errors.js'\n\n// ─── Roles that can grant/revoke ───────────────────────────────────────\n\n/**\n * Roles that an `admin` is allowed to grant and revoke.\n *\n * Includes `'admin'` itself: the model bottlenecked all admin\n * onboarding through the single `owner` principal, which made lateral\n * delegation impossible and left a single-owner bus-factor risk\n * unresolved even when multiple trusted humans existed. opens up\n * admin↔admin lateral delegation, with two guardrails:\n *\n * 1. **No privilege escalation.** Enforced in `grant()`: every DEK\n * wrapped into the new admin's keyring must be present in the\n * grantor's own DEK set. Today this is structurally trivially\n * true (admin grants always inherit the full caller DEK set),\n * but the check is wired in so future per-collection admin scoping\n * cannot accidentally bypass it. See `PrivilegeEscalationError`.\n *\n * 2. **Cascade on revoke.** Enforced in `revoke()`: when an admin is\n * revoked, every admin they (transitively) granted is either\n * revoked too (`cascade: 'strict'`, default) or left in place with\n * a console warning (`cascade: 'warn'`). The walk uses the\n * `granted_by` field on each keyring file as the parent pointer.\n */\nconst ADMIN_GRANTABLE_TARGETS: readonly Role[] = ['operator', 'viewer', 'client', 'admin']\n\nfunction canGrant(callerRole: Role, targetRole: Role): boolean {\n if (callerRole === 'owner') return true\n if (callerRole === 'admin') return ADMIN_GRANTABLE_TARGETS.includes(targetRole)\n return false\n}\n\nfunction canRevoke(callerRole: Role, targetRole: Role): boolean {\n if (targetRole === 'owner') return false // owner cannot be revoked\n if (callerRole === 'owner') return true\n if (callerRole === 'admin') return ADMIN_GRANTABLE_TARGETS.includes(targetRole)\n return false\n}\n\n// ─── Unlocked Keyring ──────────────────────────────────────────────────\n\n/** In-memory representation of an unlocked keyring. */\nexport interface UnlockedKeyring {\n readonly userId: string\n readonly displayName: string\n readonly role: Role\n readonly permissions: Permissions\n readonly deks: Map<string, CryptoKey>\n readonly kek: CryptoKey\n readonly salt: Uint8Array\n /**\n * `@noy-db/as-*` export capability. Absent when the\n * keyring was written before this RFC landed — role-based defaults\n * apply via `hasExportCapability`.\n */\n readonly exportCapability?: ExportCapability\n /**\n * `@noy-db/as-*` import capability (issue ). Absent when the\n * keyring was written before landed — default-closed semantics\n * apply via `hasImportCapability` (no plaintext format granted, no\n * bundle import granted, regardless of role).\n */\n readonly importCapability?: ImportCapability\n}\n\n// ─── Load / Create ─────────────────────────────────────────────────────\n\n/** Load and unlock a user's keyring for a vault. */\nexport async function loadKeyring(\n adapter: NoydbStore,\n vault: string,\n userId: string,\n passphrase: string,\n): Promise<UnlockedKeyring> {\n const envelope = await adapter.get(vault, '_keyring', userId)\n\n if (!envelope) {\n throw new NoAccessError(`No keyring found for user \"${userId}\" in vault \"${vault}\"`)\n }\n\n const keyringFile = JSON.parse(envelope._data) as KeyringFile\n\n // — refuse to unwrap an expired slot. Check happens before any\n // KEK derivation so an expired slot doesn't leak timing on the\n // passphrase. Comparison uses Date.parse → ms-since-epoch; an\n // unparseable expires_at is treated as \"no expiry\" so a malformed\n // value can't silently lock users out (it'll surface in tests).\n if (keyringFile.expires_at !== undefined) {\n const cutoff = Date.parse(keyringFile.expires_at)\n if (Number.isFinite(cutoff) && Date.now() >= cutoff) {\n throw new KeyringExpiredError({ userId: keyringFile.user_id, expiresAt: keyringFile.expires_at })\n }\n }\n\n const salt = base64ToBuffer(keyringFile.salt)\n const kek = await deriveKey(passphrase, salt)\n\n const deks = new Map<string, CryptoKey>()\n for (const [collName, wrappedDek] of Object.entries(keyringFile.deks)) {\n const dek = await unwrapKey(wrappedDek, kek)\n deks.set(collName, dek)\n }\n\n return {\n userId: keyringFile.user_id,\n displayName: keyringFile.display_name,\n role: keyringFile.role,\n permissions: keyringFile.permissions,\n deks,\n kek,\n salt,\n ...(keyringFile.export_capability !== undefined && { exportCapability: keyringFile.export_capability }),\n ...(keyringFile.import_capability !== undefined && { importCapability: keyringFile.import_capability }),\n }\n}\n\n/** Create the initial owner keyring for a new vault. */\nexport async function createOwnerKeyring(\n adapter: NoydbStore,\n vault: string,\n userId: string,\n passphrase: string,\n): Promise<UnlockedKeyring> {\n const salt = generateSalt()\n const kek = await deriveKey(passphrase, salt)\n\n const keyringFile: KeyringFile = {\n _noydb_keyring: NOYDB_KEYRING_VERSION,\n user_id: userId,\n display_name: userId,\n role: 'owner',\n permissions: {},\n deks: {},\n salt: bufferToBase64(salt),\n created_at: new Date().toISOString(),\n granted_by: userId,\n }\n\n await writeKeyringFile(adapter, vault, userId, keyringFile)\n\n return {\n userId,\n displayName: userId,\n role: 'owner',\n permissions: {},\n deks: new Map(),\n kek,\n salt,\n }\n}\n\n// ─── Grant ─────────────────────────────────────────────────────────────\n\n/** Grant access to a new user. Caller must have grant privilege. */\nexport async function grant(\n adapter: NoydbStore,\n vault: string,\n callerKeyring: UnlockedKeyring,\n options: GrantOptions,\n): Promise<void> {\n if (!canGrant(callerKeyring.role, options.role)) {\n throw new PermissionDeniedError(\n `Role \"${callerKeyring.role}\" cannot grant role \"${options.role}\"`,\n )\n }\n\n // Determine which collections the new user gets access to\n const permissions = resolvePermissions(options.role, options.permissions)\n\n // Derive the new user's KEK from their passphrase\n const newSalt = generateSalt()\n const newKek = await deriveKey(options.passphrase, newSalt)\n\n // Wrap the appropriate DEKs with the new user's KEK\n const wrappedDeks: Record<string, string> = {}\n for (const collName of Object.keys(permissions)) {\n const dek = callerKeyring.deks.get(collName)\n if (dek) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n\n // For owner/admin/viewer roles, wrap ALL known DEKs\n if (options.role === 'owner' || options.role === 'admin' || options.role === 'viewer') {\n for (const [collName, dek] of callerKeyring.deks) {\n if (!(collName in wrappedDeks)) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n }\n\n // For ALL roles, propagate system-prefixed collection DEKs\n // (`_ledger`, `_history`, `_sync`, …). These are internal collections\n // that any user with access to the vault must be able to\n // read and write — for example, the hash-chained ledger writes\n // an entry on every put/delete, so operators and clients with write\n // access to a single data collection still need the `_ledger` DEK.\n //\n // Trade-off: a granted user can decrypt every system-collection\n // entry, including ones they would not otherwise have access to\n // (e.g., an operator on `invoices` can read ledger entries for\n // mutations in `salaries`). This is a metadata leak, not a\n // plaintext leak — the ledger entries record collection names,\n // record ids, and ciphertext hashes, but never plaintext records.\n // Per-collection ledger DEKs are tracked as a follow-up.\n for (const [collName, dek] of callerKeyring.deks) {\n if (collName.startsWith('_') && !(collName in wrappedDeks)) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n\n // Anti-privilege-escalation check. Every DEK we just\n // wrapped into the new keyring must come from the caller's own DEK\n // set — the grantor cannot give the grantee access to a collection\n // they themselves can't read. Today this is structurally trivially\n // satisfied because every wrapped DEK was looked up in\n // `callerKeyring.deks` above, but the explicit check is wired in\n // so a future change (per-collection admin scoping, escrow-based\n // re-wrapping, etc.) cannot accidentally let a widening grant\n // through. See `PrivilegeEscalationError` for the rationale.\n for (const collName of Object.keys(wrappedDeks)) {\n if (!callerKeyring.deks.has(collName)) {\n throw new PrivilegeEscalationError(collName)\n }\n }\n\n const keyringFile: KeyringFile = {\n _noydb_keyring: NOYDB_KEYRING_VERSION,\n user_id: options.userId,\n display_name: options.displayName,\n role: options.role,\n permissions,\n deks: wrappedDeks,\n salt: bufferToBase64(newSalt),\n created_at: new Date().toISOString(),\n granted_by: callerKeyring.userId,\n ...(options.exportCapability !== undefined && { export_capability: options.exportCapability }),\n ...(options.importCapability !== undefined && { import_capability: options.importCapability }),\n }\n\n await writeKeyringFile(adapter, vault, options.userId, keyringFile)\n}\n\n// ─── Revoke ────────────────────────────────────────────────────────────\n\n/**\n * Walk every keyring in the vault to find admins that the given\n * `rootUserId` (transitively) granted, via the `granted_by` parent\n * pointer recorded on each keyring file.\n *\n * Returns the set of descendant admin user-ids in DFS order, NOT\n * including the root itself. Non-admin descendants are excluded\n * because operators/viewers/clients cannot grant other users — they\n * are leaves in the delegation tree and cleaning them up is the\n * caller's job (or the next rotate, since they'd lose key access\n * anyway when the cascading admin's collections rotate).\n *\n * The walk uses a visited set keyed by user-id so cycles introduced\n * by re-grants (admin-A revoked, then re-granted later by admin-B who\n * was originally granted by A) terminate cleanly.\n */\nasync function findAdminDescendants(\n adapter: NoydbStore,\n vault: string,\n rootUserId: string,\n): Promise<string[]> {\n const allUserIds = await adapter.list(vault, '_keyring')\n\n // Build a map: parentUserId → child KeyringFiles. We only ever\n // descend into admins, so non-admin children are skipped at the\n // edge level rather than after a recursive call.\n const childrenByParent = new Map<string, string[]>()\n for (const userId of allUserIds) {\n const env = await adapter.get(vault, '_keyring', userId)\n if (!env) continue\n const kf = JSON.parse(env._data) as KeyringFile\n if (kf.role !== 'admin') continue // only admins can grant — leaves are uninteresting\n if (kf.user_id === rootUserId) continue // self-edges are noise\n const list = childrenByParent.get(kf.granted_by) ?? []\n list.push(kf.user_id)\n childrenByParent.set(kf.granted_by, list)\n }\n\n const visited = new Set<string>()\n const order: string[] = []\n const stack: string[] = [...(childrenByParent.get(rootUserId) ?? [])]\n while (stack.length > 0) {\n const next = stack.pop()!\n if (visited.has(next)) continue\n visited.add(next)\n order.push(next)\n for (const grandchild of childrenByParent.get(next) ?? []) {\n if (!visited.has(grandchild)) stack.push(grandchild)\n }\n }\n return order\n}\n\n/** Revoke a user's access. Optionally rotate keys for affected collections. */\nexport async function revoke(\n adapter: NoydbStore,\n vault: string,\n callerKeyring: UnlockedKeyring,\n options: RevokeOptions,\n): Promise<void> {\n // Load the target's keyring to check their role\n const targetEnvelope = await adapter.get(vault, '_keyring', options.userId)\n if (!targetEnvelope) {\n throw new NoAccessError(`User \"${options.userId}\" has no keyring in vault \"${vault}\"`)\n }\n\n const targetKeyring = JSON.parse(targetEnvelope._data) as KeyringFile\n\n if (!canRevoke(callerKeyring.role, targetKeyring.role)) {\n throw new PermissionDeniedError(\n `Role \"${callerKeyring.role}\" cannot revoke role \"${targetKeyring.role}\"`,\n )\n }\n\n // Cascade-on-revoke. Only meaningful when the target is\n // an admin — operators/viewers/clients cannot grant other users so\n // they have no delegation subtree to walk.\n const cascadeMode = options.cascade ?? 'strict'\n const usersToRevoke: string[] = [options.userId]\n const affectedCollections = new Set(Object.keys(targetKeyring.deks))\n\n if (targetKeyring.role === 'admin') {\n const descendants = await findAdminDescendants(adapter, vault, options.userId)\n if (descendants.length > 0) {\n if (cascadeMode === 'warn') {\n // Diagnostic mode: leave the descendants in place but make\n // them visible. The owner / a different admin can clean up\n // manually. The single console.warn is intentionally noisy\n // (a list, not a count) so the operator sees exactly which\n // keyrings will become orphans.\n console.warn(\n `[noy-db] revoke(${options.userId}): cascade='warn' — leaving ` +\n `${descendants.length} descendant admin(s) in place: ` +\n `${descendants.join(', ')}. These admins were granted by the revoked user ` +\n `(transitively) and will become orphans in the delegation tree.`,\n )\n } else {\n // Strict mode (default): pull every descendant into the\n // revoke set. We collect their affected collections too so\n // the single rotation pass at the end covers everything.\n for (const userId of descendants) {\n const descEnv = await adapter.get(vault, '_keyring', userId)\n if (!descEnv) continue\n const descKf = JSON.parse(descEnv._data) as KeyringFile\n usersToRevoke.push(userId)\n for (const c of Object.keys(descKf.deks)) affectedCollections.add(c)\n }\n }\n }\n }\n\n // Delete every keyring in the revoke set. Order doesn't matter\n // because each keyring file is independent on disk; we don't have\n // referential integrity to maintain across deletes.\n for (const userId of usersToRevoke) {\n await adapter.delete(vault, '_keyring', userId)\n }\n\n // Single rotation pass at the end. The cost is O(records in\n // affected collections), NOT O(records × cascade depth) — every\n // descendant's collections were unioned into `affectedCollections`\n // before we got here, so the rotation re-encrypts each affected\n // record exactly once regardless of how deep the cascade went.\n if (options.rotateKeys !== false && affectedCollections.size > 0) {\n await rotateKeys(adapter, vault, callerKeyring, [...affectedCollections])\n }\n}\n\n// ─── Key Rotation ──────────────────────────────────────────────────────\n\n/**\n * Rotate DEKs for specified collections:\n * 1. Generate new DEKs\n * 2. Re-encrypt all records in affected collections\n * 3. Re-wrap new DEKs for all remaining users\n */\nexport async function rotateKeys(\n adapter: NoydbStore,\n vault: string,\n callerKeyring: UnlockedKeyring,\n collections: string[],\n): Promise<void> {\n // Generate new DEKs for each affected collection\n const newDeks = new Map<string, CryptoKey>()\n for (const collName of collections) {\n newDeks.set(collName, await generateDEK())\n }\n\n // Re-encrypt all records in affected collections\n for (const collName of collections) {\n const oldDek = callerKeyring.deks.get(collName)\n const newDek = newDeks.get(collName)!\n if (!oldDek) continue\n\n const ids = await adapter.list(vault, collName)\n for (const id of ids) {\n const envelope = await adapter.get(vault, collName, id)\n if (!envelope || !envelope._iv) continue\n\n // Decrypt with old DEK\n const plaintext = await decrypt(envelope._iv, envelope._data, oldDek)\n\n // Re-encrypt with new DEK\n const { iv, data } = await encrypt(plaintext, newDek)\n const newEnvelope: EncryptedEnvelope = {\n _noydb: NOYDB_FORMAT_VERSION,\n _v: envelope._v,\n _ts: new Date().toISOString(),\n _iv: iv,\n _data: data,\n }\n await adapter.put(vault, collName, id, newEnvelope)\n }\n }\n\n // Update caller's keyring with new DEKs\n for (const [collName, newDek] of newDeks) {\n callerKeyring.deks.set(collName, newDek)\n }\n await persistKeyring(adapter, vault, callerKeyring)\n\n // Update all remaining users' keyrings with re-wrapped new DEKs\n const userIds = await adapter.list(vault, '_keyring')\n for (const userId of userIds) {\n if (userId === callerKeyring.userId) continue\n\n const userEnvelope = await adapter.get(vault, '_keyring', userId)\n if (!userEnvelope) continue\n\n const userKeyringFile = JSON.parse(userEnvelope._data) as KeyringFile\n // Note: we can't derive other users' KEKs to re-wrap DEKs for them.\n // Rotation requires users to re-unlock and be re-granted after the caller\n // re-wraps with the raw DEKs held in memory. See rotation flow below.\n // The trick: import the user's KEK from their salt? No — we need their passphrase.\n //\n // Per the spec: the caller (owner/admin) wraps the new DEKs with each remaining\n // user's KEK. But we can't derive their KEK without their passphrase.\n //\n // Real solution from the spec: the caller wraps the DEK using the approach of\n // reading each user's existing wrapping. Since we can't derive their KEK,\n // we use a RE-KEYING approach: the new DEK is wrapped with a key-wrapping-key\n // that we CAN derive — we use the existing wrapped DEK as proof that the user\n // had access, and we replace it with the new wrapped DEK.\n //\n // Practical approach: Since the owner/admin has all raw DEKs in memory,\n // and each user's keyring contains their salt, we need the users to\n // re-authenticate to get the new wrapped keys. This is the standard approach.\n //\n // For NOYDB Phase 2: we'll update the keyring file to include a \"pending_rekey\"\n // flag. Users will get new DEKs on next login when the owner provides them.\n //\n // SIMPLER approach used here: Since the owner performed the rotation,\n // the owner has both old and new DEKs. We store a \"rekey token\" that the\n // user can use to unwrap: we wrap the new DEK with the OLD DEK (which the\n // user can still unwrap from their keyring, since their keyring has the old\n // wrapped DEK and their KEK can unwrap it).\n\n // Actually even simpler: we just need the user's KEK. We don't have it.\n // The spec says the owner wraps new DEKs for each remaining user.\n // This requires knowing each user's KEK (or having a shared secret).\n //\n // The CORRECT implementation from the spec: the owner/admin has all DEKs.\n // Each user's keyring stores DEKs wrapped with THAT USER's KEK.\n // To re-wrap, we need each user's KEK — which we can't get.\n //\n // Real-world solution: use a KEY ESCROW approach where the owner stores\n // each user's wrapping key (not their passphrase, but a key derived from\n // the grant process). During grant, the owner stores a copy of the new user's\n // KEK (wrapped with the owner's KEK) so they can re-wrap later.\n //\n // For now: mark the user's keyring as needing rekey. The user will need to\n // re-authenticate (owner provides new passphrase or re-grants).\n\n // Update: simplest correct approach — during grant, we store the user's KEK\n // wrapped with the owner's KEK in a separate escrow field. Then during rotation,\n // the owner unwraps the user's KEK from escrow and wraps the new DEKs.\n //\n // BUT: that means we need to change the KeyringFile format.\n // For Phase 2 MVP: just delete the user's old DEK entries and require re-grant.\n // This is secure (revoked keys are gone) but inconvenient (remaining users\n // need re-grant for rotated collections).\n\n // PHASE 2 APPROACH: Remove the affected collection DEKs from remaining users'\n // keyrings. The owner must re-grant access to those collections.\n // This is correct and secure — just requires the owner to re-run grant().\n\n const updatedDeks = { ...userKeyringFile.deks }\n for (const collName of collections) {\n delete updatedDeks[collName]\n }\n\n const updatedPermissions = { ...userKeyringFile.permissions }\n for (const collName of collections) {\n delete updatedPermissions[collName]\n }\n\n const updatedKeyring: KeyringFile = {\n ...userKeyringFile,\n deks: updatedDeks,\n permissions: updatedPermissions,\n }\n\n await writeKeyringFile(adapter, vault, userId, updatedKeyring)\n }\n}\n\n// ─── Change Secret ─────────────────────────────────────────────────────\n\n/** Change the user's passphrase. Re-wraps all DEKs with the new KEK. */\nexport async function changeSecret(\n adapter: NoydbStore,\n vault: string,\n keyring: UnlockedKeyring,\n newPassphrase: string,\n): Promise<UnlockedKeyring> {\n const newSalt = generateSalt()\n const newKek = await deriveKey(newPassphrase, newSalt)\n\n // Re-wrap all DEKs with the new KEK\n const wrappedDeks: Record<string, string> = {}\n for (const [collName, dek] of keyring.deks) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n\n const keyringFile: KeyringFile = {\n _noydb_keyring: NOYDB_KEYRING_VERSION,\n user_id: keyring.userId,\n display_name: keyring.displayName,\n role: keyring.role,\n permissions: keyring.permissions,\n deks: wrappedDeks,\n salt: bufferToBase64(newSalt),\n created_at: new Date().toISOString(),\n granted_by: keyring.userId,\n }\n\n await writeKeyringFile(adapter, vault, keyring.userId, keyringFile)\n\n return {\n userId: keyring.userId,\n displayName: keyring.displayName,\n role: keyring.role,\n permissions: keyring.permissions,\n deks: keyring.deks, // Same DEKs, different wrapping\n kek: newKek,\n salt: newSalt,\n }\n}\n\n// ─── Bundle recipients ──────────────────────────────────────────\n\n/**\n * Recipient slot in a re-keyed `.noydb` bundle. Each slot becomes its\n * own keyring file inside the bundle, sealed with its own passphrase.\n * Same role/permission semantics as `db.grant()` but no adapter side\n * effect — the slot only exists inside the bundle bytes.\n *\n * @public\n */\nexport interface BundleRecipient {\n /** User id stamped onto the keyring file in the bundle. */\n readonly id: string\n /** Optional display name. Defaults to `id`. */\n readonly displayName?: string\n /** Passphrase the recipient will type to unlock. */\n readonly passphrase: string\n /** Role on the destination vault. Defaults to `'viewer'`. */\n readonly role?: Role\n /**\n * Per-collection permissions. When omitted, role defaults apply.\n * Restricting permissions here ALSO restricts which DEKs are wrapped\n * into the slot — a slot with `{ invoices: 'ro' }` cannot decrypt\n * other collections even though their ciphertext sits in the bundle.\n */\n readonly permissions?: Permissions\n /**\n * Optional `as-*` export grants on the destination vault.\n * Mirrors the `exportCapability` field on a live keyring.\n */\n readonly exportCapability?: ExportCapability\n /**\n * Optional `as-*` import grants on the destination vault.\n * Mirrors the `importCapability` field on a live keyring.\n * Default-closed: no plaintext format granted, no bundle import.\n */\n readonly importCapability?: ImportCapability\n /**\n * Optional bundle-slot expiry. ISO-8601 timestamp; past the\n * cutoff this slot's keyring refuses to load with\n * `KeyringExpiredError`. Time-boxed audit access pattern: \"this\n * slot works for 30 days then becomes opaque to its holder.\"\n */\n readonly expiresAt?: string\n}\n\n/**\n * Build a `KeyringFile` for one bundle recipient, given the source\n * vault's unwrapped DEKs. Mirrors `grant()` minus the adapter write —\n * the produced file is meant to be embedded in the bundle's\n * `keyrings` map, never persisted to the source vault.\n *\n * Privilege-escalation check still runs: every DEK wrapped into the\n * recipient's keyring must come from the source's own DEK set.\n *\n * @internal\n */\nexport async function buildRecipientKeyringFile(\n callerKeyring: UnlockedKeyring,\n recipient: BundleRecipient,\n): Promise<KeyringFile> {\n const role: Role = recipient.role ?? 'viewer'\n const permissions = resolvePermissions(role, recipient.permissions)\n\n const newSalt = generateSalt()\n const newKek = await deriveKey(recipient.passphrase, newSalt)\n\n const wrappedDeks: Record<string, string> = {}\n\n // Collections the recipient was explicitly granted permission to.\n for (const collName of Object.keys(permissions)) {\n const dek = callerKeyring.deks.get(collName)\n if (dek) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n\n // owner / admin / viewer: wrap every known DEK (matches grant).\n if (role === 'owner' || role === 'admin' || role === 'viewer') {\n for (const [collName, dek] of callerKeyring.deks) {\n if (!(collName in wrappedDeks)) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n }\n\n // Always propagate system-prefixed collection DEKs (`_ledger`, etc.) —\n // the recipient needs them to verify the bundle on import.\n for (const [collName, dek] of callerKeyring.deks) {\n if (collName.startsWith('_') && !(collName in wrappedDeks)) {\n wrappedDeks[collName] = await wrapKey(dek, newKek)\n }\n }\n\n // Anti-privilege-escalation: every wrapped DEK must come from the\n // caller's own DEK set. Belt-and-braces with the lookups above.\n for (const collName of Object.keys(wrappedDeks)) {\n if (!callerKeyring.deks.has(collName)) {\n throw new PrivilegeEscalationError(collName)\n }\n }\n\n return {\n _noydb_keyring: NOYDB_KEYRING_VERSION,\n user_id: recipient.id,\n display_name: recipient.displayName ?? recipient.id,\n role,\n permissions,\n deks: wrappedDeks,\n salt: bufferToBase64(newSalt),\n created_at: new Date().toISOString(),\n granted_by: callerKeyring.userId,\n ...(recipient.exportCapability !== undefined\n ? { export_capability: recipient.exportCapability }\n : {}),\n ...(recipient.importCapability !== undefined\n ? { import_capability: recipient.importCapability }\n : {}),\n ...(recipient.expiresAt !== undefined\n ? { expires_at: recipient.expiresAt }\n : {}),\n }\n}\n\n// ─── List Users ────────────────────────────────────────────────────────\n\n/** List all users with access to a vault. */\nexport async function listUsers(\n adapter: NoydbStore,\n vault: string,\n): Promise<UserInfo[]> {\n const userIds = await adapter.list(vault, '_keyring')\n const users: UserInfo[] = []\n\n for (const userId of userIds) {\n const envelope = await adapter.get(vault, '_keyring', userId)\n if (!envelope) continue\n const kf = JSON.parse(envelope._data) as KeyringFile\n users.push({\n userId: kf.user_id,\n displayName: kf.display_name,\n role: kf.role,\n permissions: kf.permissions,\n createdAt: kf.created_at,\n grantedBy: kf.granted_by,\n })\n }\n\n return users\n}\n\n// ─── DEK Management ────────────────────────────────────────────────────\n\n/** Ensure a DEK exists for a collection. Generates one if new. */\nexport async function ensureCollectionDEK(\n adapter: NoydbStore,\n vault: string,\n keyring: UnlockedKeyring,\n): Promise<(collectionName: string) => Promise<CryptoKey>> {\n // Dedupe concurrent first-time DEK creates per collection. Without\n // this, two concurrent `getDEK('foo')` calls both pass the `existing`\n // check (the Map is empty), both generate fresh DEKs, and the second\n // `set` overwrites the first — making any envelope encrypted with\n // the discarded DEK fail to decrypt later (TamperedError on read).\n // Pre-existing race exposed by the multi-writer ledger work in #296.\n const inFlight = new Map<string, Promise<CryptoKey>>()\n return async (collectionName: string): Promise<CryptoKey> => {\n const existing = keyring.deks.get(collectionName)\n if (existing) return existing\n const pending = inFlight.get(collectionName)\n if (pending) return pending\n\n const promise = (async () => {\n const dek = await generateDEK()\n keyring.deks.set(collectionName, dek)\n await persistKeyring(adapter, vault, keyring)\n return dek\n })()\n inFlight.set(collectionName, promise)\n try {\n return await promise\n } finally {\n inFlight.delete(collectionName)\n }\n }\n}\n\n// ─── Permission Checks ─────────────────────────────────────────────────\n\n/** Check if a user has write permission for a collection. */\nexport function hasWritePermission(keyring: UnlockedKeyring, collectionName: string): boolean {\n if (keyring.role === 'owner' || keyring.role === 'admin') return true\n if (keyring.role === 'viewer' || keyring.role === 'client') return false\n return keyring.permissions[collectionName] === 'rw'\n}\n\n/** Check if a user has any access to a collection. */\nexport function hasAccess(keyring: UnlockedKeyring, collectionName: string): boolean {\n if (keyring.role === 'owner' || keyring.role === 'admin' || keyring.role === 'viewer') return true\n return collectionName in keyring.permissions\n}\n\n// ─── Helpers ───────────────────────────────────────────────────────────\n\n/** Persist a keyring file to the adapter. */\nexport async function persistKeyring(\n adapter: NoydbStore,\n vault: string,\n keyring: UnlockedKeyring,\n): Promise<void> {\n const wrappedDeks: Record<string, string> = {}\n for (const [collName, dek] of keyring.deks) {\n wrappedDeks[collName] = await wrapKey(dek, keyring.kek)\n }\n\n const keyringFile: KeyringFile = {\n _noydb_keyring: NOYDB_KEYRING_VERSION,\n user_id: keyring.userId,\n display_name: keyring.displayName,\n role: keyring.role,\n permissions: keyring.permissions,\n deks: wrappedDeks,\n salt: bufferToBase64(keyring.salt),\n created_at: new Date().toISOString(),\n granted_by: keyring.userId,\n ...(keyring.exportCapability !== undefined && { export_capability: keyring.exportCapability }),\n ...(keyring.importCapability !== undefined && { import_capability: keyring.importCapability }),\n }\n\n await writeKeyringFile(adapter, vault, keyring.userId, keyringFile)\n}\n\n// ─── Export capability ──────────────────────────────────────\n\n/**\n * Role-based default policy for the encrypted-bundle capability.\n *\n * Applied when `keyring.exportCapability` is absent or\n * `exportCapability.bundle` is undefined:\n *\n * - `owner` / `admin` → `true` (happy-path backup without friction)\n * - `operator` / `viewer` / `client` → `false` (explicit grant required)\n *\n * Rationale: a bundle is inert without the KEK, so an owner backing up\n * their own vault doesn't need friction; a non-admin role producing a\n * bundle for an external party does, because the bundle outlives\n * keyring revocation.\n */\nfunction defaultBundleCapability(role: Role): boolean {\n return role === 'owner' || role === 'admin'\n}\n\n/**\n * Check whether a keyring is authorised for a given `@noy-db/as-*`\n * export tier.\n *\n * - `tier: 'plaintext'` — returns true iff `exportCapability.plaintext`\n * contains the requested `format` or the `'*'` wildcard. Default for\n * every role is empty — no grant, no plaintext export.\n * - `tier: 'bundle'` — returns `exportCapability.bundle` if present, or\n * the role-based default otherwise (owner/admin → true, else false).\n *\n * `@noy-db/as-*` packages MUST call this before invoking the underlying\n * export primitive. Rogue forks that skip the check are caught by code\n * review — the single-entry-point contract is a convention, not a\n * runtime invariant. Vault-level gated wrappers\n * (`vault.exportRecords` / `exportBlobs` / `writeBundle`) will land in a\n * follow-up PR to enforce at the primitive level.\n */\nexport function hasExportCapability(\n keyring: UnlockedKeyring,\n tier: 'plaintext',\n format: ExportFormat,\n): boolean\nexport function hasExportCapability(\n keyring: UnlockedKeyring,\n tier: 'bundle',\n): boolean\nexport function hasExportCapability(\n keyring: UnlockedKeyring,\n tier: 'plaintext' | 'bundle',\n format?: ExportFormat,\n): boolean {\n const cap = keyring.exportCapability\n if (tier === 'plaintext') {\n const allowed = cap?.plaintext ?? []\n return allowed.includes('*') || (format !== undefined && allowed.includes(format))\n }\n // tier === 'bundle'\n return cap?.bundle ?? defaultBundleCapability(keyring.role)\n}\n\n/**\n * Same-shape inspector for an `ExportCapability` value that isn't yet\n * attached to a keyring (e.g. for previewing a grant before applying).\n * Role must be supplied separately so bundle defaults can be computed.\n */\nexport function evaluateExportCapability(\n capability: ExportCapability | undefined,\n role: Role,\n tier: 'plaintext',\n format: ExportFormat,\n): boolean\nexport function evaluateExportCapability(\n capability: ExportCapability | undefined,\n role: Role,\n tier: 'bundle',\n): boolean\nexport function evaluateExportCapability(\n capability: ExportCapability | undefined,\n role: Role,\n tier: 'plaintext' | 'bundle',\n format?: ExportFormat,\n): boolean {\n if (tier === 'plaintext') {\n const allowed = capability?.plaintext ?? []\n return allowed.includes('*') || (format !== undefined && allowed.includes(format))\n }\n return capability?.bundle ?? defaultBundleCapability(role)\n}\n\n// ─── Import capability (issue ) ────────────────────────────────────\n\n/**\n * Check whether a keyring is authorised for a given `@noy-db/as-*`\n * import tier (issue ).\n *\n * - `tier: 'plaintext'` — true iff `importCapability.plaintext`\n * contains the requested `format` or the `'*'` wildcard.\n * - `tier: 'bundle'` — true iff `importCapability.bundle === true`.\n *\n * **Default-closed for every role on every dimension** — including\n * owner. Import is more dangerous than export (corrupts vs leaks), so\n * the policy refuses to assume intent. Owners must positively grant\n * the capability via `vault.grant({ importCapability: ... })`.\n */\nexport function hasImportCapability(\n keyring: UnlockedKeyring,\n tier: 'plaintext',\n format: ExportFormat,\n): boolean\nexport function hasImportCapability(\n keyring: UnlockedKeyring,\n tier: 'bundle',\n): boolean\nexport function hasImportCapability(\n keyring: UnlockedKeyring,\n tier: 'plaintext' | 'bundle',\n format?: ExportFormat,\n): boolean {\n const cap = keyring.importCapability\n if (tier === 'plaintext') {\n const allowed = cap?.plaintext ?? []\n return allowed.includes('*') || (format !== undefined && allowed.includes(format))\n }\n // tier === 'bundle' — closed default for every role\n return cap?.bundle === true\n}\n\n/**\n * Same-shape inspector for an `ImportCapability` value that isn't yet\n * attached to a keyring (e.g. previewing a grant before applying).\n * `role` is accepted for symmetry with `evaluateExportCapability` even\n * though the import policy ignores it — bundle defaults are\n * role-agnostic and closed.\n */\nexport function evaluateImportCapability(\n capability: ImportCapability | undefined,\n role: Role,\n tier: 'plaintext',\n format: ExportFormat,\n): boolean\nexport function evaluateImportCapability(\n capability: ImportCapability | undefined,\n role: Role,\n tier: 'bundle',\n): boolean\nexport function evaluateImportCapability(\n capability: ImportCapability | undefined,\n _role: Role,\n tier: 'plaintext' | 'bundle',\n format?: ExportFormat,\n): boolean {\n if (tier === 'plaintext') {\n const allowed = capability?.plaintext ?? []\n return allowed.includes('*') || (format !== undefined && allowed.includes(format))\n }\n return capability?.bundle === true\n}\n\nfunction resolvePermissions(role: Role, explicit?: Permissions): Permissions {\n if (role === 'owner' || role === 'admin' || role === 'viewer') return {}\n return explicit ?? {}\n}\n\nasync function writeKeyringFile(\n adapter: NoydbStore,\n vault: string,\n userId: string,\n keyringFile: KeyringFile,\n): Promise<void> {\n const envelope = {\n _noydb: 1 as const,\n _v: 1,\n _ts: new Date().toISOString(),\n _iv: '',\n _data: JSON.stringify(keyringFile),\n }\n await adapter.put(vault, '_keyring', userId, envelope)\n}\n"],"mappings":";;;;;;;;;;;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,WAAW;AAChC;AAEA,SAAS,mBAAmB,MAAY,UAAqC;AAC3E,MAAI,SAAS,WAAW,SAAS,WAAW,SAAS,SAAU,QAAO,CAAC;AACvE,SAAO,YAAY,CAAC;AACtB;AAEA,eAAe,iBACb,SACA,OACA,QACA,aACe;AACf,QAAM,WAAW;AAAA,IACf,QAAQ;AAAA,IACR,IAAI;AAAA,IACJ,MAAK,oBAAI,KAAK,GAAE,YAAY;AAAA,IAC5B,KAAK;AAAA,IACL,OAAO,KAAK,UAAU,WAAW;AAAA,EACnC;AACA,QAAM,QAAQ,IAAI,OAAO,YAAY,QAAQ,QAAQ;AACvD;","names":[]}
@@ -0,0 +1,84 @@
1
+ // src/query/predicate.ts
2
+ function readPath(record, path) {
3
+ if (record === null || record === void 0) return void 0;
4
+ if (!path.includes(".")) {
5
+ return record[path];
6
+ }
7
+ const segments = path.split(".");
8
+ let cursor = record;
9
+ for (const segment of segments) {
10
+ if (cursor === null || cursor === void 0) return void 0;
11
+ cursor = cursor[segment];
12
+ }
13
+ return cursor;
14
+ }
15
+ function evaluateFieldClause(record, clause) {
16
+ const actual = readPath(record, clause.field);
17
+ const { op, value } = clause;
18
+ switch (op) {
19
+ case "==":
20
+ return actual === value;
21
+ case "!=":
22
+ return actual !== value;
23
+ case "<":
24
+ return isComparable(actual, value) && actual < value;
25
+ case "<=":
26
+ return isComparable(actual, value) && actual <= value;
27
+ case ">":
28
+ return isComparable(actual, value) && actual > value;
29
+ case ">=":
30
+ return isComparable(actual, value) && actual >= value;
31
+ case "in":
32
+ return Array.isArray(value) && value.includes(actual);
33
+ case "contains":
34
+ if (typeof actual === "string") return typeof value === "string" && actual.includes(value);
35
+ if (Array.isArray(actual)) return actual.includes(value);
36
+ return false;
37
+ case "startsWith":
38
+ return typeof actual === "string" && typeof value === "string" && actual.startsWith(value);
39
+ case "between": {
40
+ if (!Array.isArray(value) || value.length !== 2) return false;
41
+ const [lo, hi] = value;
42
+ if (!isComparable(actual, lo) || !isComparable(actual, hi)) return false;
43
+ return actual >= lo && actual <= hi;
44
+ }
45
+ default: {
46
+ const _exhaustive = op;
47
+ void _exhaustive;
48
+ return false;
49
+ }
50
+ }
51
+ }
52
+ function isComparable(a, b) {
53
+ if (typeof a === "number" && typeof b === "number") return true;
54
+ if (typeof a === "string" && typeof b === "string") return true;
55
+ if (a instanceof Date && b instanceof Date) return true;
56
+ return false;
57
+ }
58
+ function evaluateClause(record, clause) {
59
+ switch (clause.type) {
60
+ case "field":
61
+ return evaluateFieldClause(record, clause);
62
+ case "filter":
63
+ return clause.fn(record);
64
+ case "group":
65
+ if (clause.op === "and") {
66
+ for (const child of clause.clauses) {
67
+ if (!evaluateClause(record, child)) return false;
68
+ }
69
+ return true;
70
+ } else {
71
+ for (const child of clause.clauses) {
72
+ if (evaluateClause(record, child)) return true;
73
+ }
74
+ return false;
75
+ }
76
+ }
77
+ }
78
+
79
+ export {
80
+ readPath,
81
+ evaluateFieldClause,
82
+ evaluateClause
83
+ };
84
+ //# sourceMappingURL=chunk-M5INGEFC.js.map