@proteinjs/db 1.50.1 → 1.50.2

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": "@proteinjs/db",
3
- "version": "1.50.1",
3
+ "version": "1.50.2",
4
4
  "main": "./dist/generated/index.js",
5
5
  "types": "./dist/generated/index.d.ts",
6
6
  "exports": {
@@ -66,5 +66,5 @@
66
66
  "ts-jest": "29.1.1",
67
67
  "typescript": "5.2.2"
68
68
  },
69
- "gitHead": "a324a92e60161ddf69fec4e17b646882fe830f85"
69
+ "gitHead": "6a487f9e29bc1ab5e3a7f79e1cce178cd2512beb"
70
70
  }
@@ -138,26 +138,41 @@ export class TableAuth {
138
138
  }
139
139
 
140
140
  // ...except that a db door the block leaves UNDECLARED mirrors the door the table's SERVICE
141
- // block declares for the same operation. The RPC runs both doors — the service gate
142
- // (TableServiceAuth), then the inner Db's db-api re-check as the calling user — so a service
143
- // door with no db mirror admitted the caller at the gate and refused them behind it, in the
144
- // gate's own words ("db doors mirror service doors" was a rule tables hand-copied; the owner
145
- // derives it). One-way: a service block that omits the door keeps the lock (the session /
146
- // audit-trail pattern — no write door on either api, break-glass included), and a table with
147
- // no service block keeps its db block as written.
141
+ // block declares for the same operation — and only where that door admits the admin role
142
+ // ALONE. The RPC runs both doors — the service gate (TableServiceAuth), then the inner Db's
143
+ // db-api re-check as the calling user — so an ['admin'] service write door with no db mirror
144
+ // admitted the admin at the gate and refused them behind it, in the gate's own words ("db
145
+ // doors mirror service doors" was a rule tables hand-copied; the owner derives it). Any wider
146
+ // service door — a permission, a non-admin role, 'authenticated', 'public' — leaves the
147
+ // undeclared db door shut: role privilege and the reach of a permission are different things,
148
+ // so a table that means a wider identity to write through the db api declares that door at
149
+ // the db block itself. One-way: a service block that omits the door keeps the lock (the
150
+ // session / audit-trail pattern — no write door on either api, break-glass included), and a
151
+ // table with no service block keeps its db block as written.
148
152
  return api === 'db' && tableAuth.all === undefined && tableAuth[operation] === undefined
149
- ? this.allowsServiceDoor(table, operation)
153
+ ? this.mirrorsAdminOnlyServiceDoor(table, operation)
150
154
  : false;
151
155
  }
152
156
 
153
- /** The service block's door for `operation`, as the RPC gate reads it; a table with no service block declares none. */
154
- private allowsServiceDoor(table: Table<any>, operation: 'query' | 'insert' | 'update' | 'delete'): boolean {
157
+ /**
158
+ * The mirror: the service block's door for `operation` (its `all` and its own entry, as the RPC
159
+ * gate reads them) where every identity it declares is exactly `['admin']` admits the admin
160
+ * here too; a door declared any wider, or not at all, mirrors nothing. A table with no service
161
+ * block declares no door to mirror.
162
+ */
163
+ private mirrorsAdminOnlyServiceDoor(table: Table<any>, operation: 'query' | 'insert' | 'update' | 'delete'): boolean {
155
164
  const serviceAuth = table.auth?.service;
156
165
  if (!serviceAuth || Object.keys(serviceAuth).length == 0) {
157
166
  return false;
158
167
  }
159
168
 
160
- return this.allows(serviceAuth.all) || this.allows(serviceAuth[operation]);
169
+ const door = [serviceAuth.all, serviceAuth[operation]].filter((identity) => identity !== undefined);
170
+ return door.length > 0 && door.every((identity) => this.isAdminOnly(identity)) && UserAuth.hasRole('admin');
171
+ }
172
+
173
+ /** A roles list naming the admin role and nothing else. */
174
+ private isAdminOnly(identity: Identity | undefined): boolean {
175
+ return Array.isArray(identity) && identity.length === 1 && identity[0] === 'admin';
161
176
  }
162
177
 
163
178
  /** One identity against the current user; undefined (nothing declared) is not a grant. */
@@ -13,7 +13,8 @@ import { TableAuth } from '../src/auth/TableAuth';
13
13
  * - it fails CLOSED when no user can be resolved;
14
14
  * - the operation doors (`canPerform`) keep the declared-block semantics: within a declared block
15
15
  * an undeclared operation stays closed to everyone, admin included — unless the table's SERVICE
16
- * block names that door, which the db door then mirrors (the last describe).
16
+ * block names that door for the admin role alone, which the db door then mirrors (the last two
17
+ * describes).
17
18
  *
18
19
  * `UserAuth` reads from a static repo; tests stub it directly per identity — no server needed.
19
20
  */
@@ -118,13 +119,15 @@ describe('TableAuth operation doors keep their declared-block semantics', () =>
118
119
  /**
119
120
  * The RPC runs BOTH doors — the service gate (`TableServiceAuth`) and then the inner `Db`'s
120
121
  * db-api re-check as the calling user — so a db door a table leaves UNDECLARED mirrors the door
121
- * its service block declares for the same operation. "db doors mirror service doors" was a rule
122
- * tables hand-copied; the owner derives it now: fifteen consumer tables opened `['admin']` writes
123
- * on the service api over a query-only db block, and every one refused the admin at the inner
124
- * check in the gate's own words — the gate admitted them, the re-check behind it did not, two
125
- * verdicts for one caller. One-way: a service block that omits the door keeps the lock (the
126
- * session / audit-trail pattern — no write door on either api, break-glass included), and a table
127
- * with no service block keeps its db block as written (the pin above).
122
+ * its service block declares for the same operation, where that door admits the admin role ALONE
123
+ * (declared exactly `['admin']`). "db doors mirror service doors" was a rule tables hand-copied;
124
+ * the owner derives it now: consumer tables opened `['admin']` writes on the service api over a
125
+ * query-only db block, and every one refused the admin at the inner check in the gate's own words
126
+ * — the gate admitted them, the re-check behind it did not, two verdicts for one caller. One-way:
127
+ * a service block that omits the door keeps the lock (the session / audit-trail pattern — no
128
+ * write door on either api, break-glass included), and a table with no service block keeps its
129
+ * db block as written (the pin above). A service door declared any wider than the admin role is
130
+ * the next describe's: it mirrors nothing.
128
131
  */
129
132
  class ReadOpenWriteAdminTable extends Table<Doc> {
130
133
  public name = 'identity_read_open_write_admin_test';
@@ -144,7 +147,7 @@ class LockedLedgerTable extends Table<Doc> {
144
147
  public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
145
148
  }
146
149
 
147
- describe('TableAuth — an undeclared db door mirrors the declared service door', () => {
150
+ describe("TableAuth — an undeclared db door mirrors the declared ['admin'] service door", () => {
148
151
  beforeEach(() => {
149
152
  setMapping({ ledger: ['ledger'] });
150
153
  });
@@ -206,3 +209,120 @@ describe('TableAuth — an undeclared db door mirrors the declared service door'
206
209
  );
207
210
  });
208
211
  });
212
+
213
+ /**
214
+ * Role privilege and the reach of a permission are different things: the mirror carries the
215
+ * admin's break-glass across the two apis, never a wider identity. The service block here opens
216
+ * three writes to identities wider than the admin role — a permission, a non-admin role, every
217
+ * signed-in user — over a query-only db block, and none of them is mirrored: the gate admits the
218
+ * holder and the db door behind it stays shut, exactly as before the mirror existed. A table that
219
+ * means a wider identity to write through the db api declares that door at the db block itself.
220
+ */
221
+ class WiderServiceDoorsTable extends Table<Doc> {
222
+ public name = 'identity_wider_service_doors_test';
223
+ public auth: Table<Doc>['auth'] = {
224
+ db: { query: { permission: 'ledger' } },
225
+ service: {
226
+ query: { permission: 'ledger' },
227
+ update: { permission: 'ledger' },
228
+ insert: ['ledger'],
229
+ delete: 'authenticated',
230
+ },
231
+ };
232
+ public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
233
+ }
234
+
235
+ /** A service write door the admin role shares with another role: not the admin role alone. */
236
+ class SharedRoleDoorTable extends Table<Doc> {
237
+ public name = 'identity_shared_role_door_test';
238
+ public auth: Table<Doc>['auth'] = {
239
+ db: { query: 'authenticated' },
240
+ service: { query: 'authenticated', update: ['admin', 'ledger'] },
241
+ };
242
+ public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
243
+ }
244
+
245
+ /** The admin-only door named by `all`: the same door, read the way the gate reads it. */
246
+ class AllAdminServiceTable extends Table<Doc> {
247
+ public name = 'identity_all_admin_service_test';
248
+ public auth: Table<Doc>['auth'] = {
249
+ db: { query: 'authenticated' },
250
+ service: { all: ['admin'] },
251
+ };
252
+ public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
253
+ }
254
+
255
+ /** An `['admin']` entry beside an `all` that widens the door to every signed-in user: not admin alone. */
256
+ class WidenedByAllTable extends Table<Doc> {
257
+ public name = 'identity_widened_by_all_test';
258
+ public auth: Table<Doc>['auth'] = {
259
+ db: { query: 'authenticated' },
260
+ service: { all: 'authenticated', update: ['admin'] },
261
+ };
262
+ public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
263
+ }
264
+
265
+ describe('TableAuth — the mirror reaches only a service door that admits the admin role alone', () => {
266
+ beforeEach(() => {
267
+ setMapping({ ledger: ['ledger'] });
268
+ });
269
+
270
+ afterEach(() => {
271
+ (UserAuth as any).userRepo = undefined;
272
+ (UserAuth as any).permissionRolesMapping = undefined;
273
+ });
274
+
275
+ test('a permission-declared service write with no db door stays shut for the permission holder — the gate admits, the db door does not', () => {
276
+ setUser(['ledger']);
277
+ const table = new WiderServiceDoorsTable();
278
+ expect(new TableAuth().canPerform(table, 'update', 'service')).toBe(true);
279
+ expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(false);
280
+ expect(() => new TableAuth().canUpdate(table)).toThrow(
281
+ 'User is not authorized to update records in table: identity_wider_service_doors_test'
282
+ );
283
+ expect(() => new TableAuth().canQuery(table)).not.toThrow();
284
+ });
285
+
286
+ test("a non-admin role door and an 'authenticated' door are as shut on the db api as the permission door", () => {
287
+ setUser(['ledger']);
288
+ const table = new WiderServiceDoorsTable();
289
+ expect(new TableAuth().canPerform(table, 'insert', 'service')).toBe(true);
290
+ expect(new TableAuth().canPerform(table, 'insert', 'db')).toBe(false);
291
+ expect(new TableAuth().canPerform(table, 'delete', 'service')).toBe(true);
292
+ expect(new TableAuth().canPerform(table, 'delete', 'db')).toBe(false);
293
+ expect(() => new TableAuth().canInsert(table)).toThrow(
294
+ 'User is not authorized to insert records into table: identity_wider_service_doors_test'
295
+ );
296
+ });
297
+
298
+ test('a wider service door opens nothing on the db api for the admin either — shut as it was before the mirror', () => {
299
+ setUser(['admin']);
300
+ const table = new WiderServiceDoorsTable();
301
+ expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(false);
302
+ expect(new TableAuth().canPerform(table, 'insert', 'db')).toBe(false);
303
+ expect(new TableAuth().canPerform(table, 'delete', 'db')).toBe(false);
304
+ expect(() => new TableAuth().canUpdate(table)).toThrow(
305
+ 'User is not authorized to update records in table: identity_wider_service_doors_test'
306
+ );
307
+ });
308
+
309
+ test('a door the admin role shares with another role is not the admin role alone: shut on the db api for both', () => {
310
+ const table = new SharedRoleDoorTable();
311
+ setUser(['admin']);
312
+ expect(new TableAuth().canPerform(table, 'update', 'service')).toBe(true);
313
+ expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(false);
314
+ setUser(['ledger']);
315
+ expect(new TableAuth().canPerform(table, 'update', 'service')).toBe(true);
316
+ expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(false);
317
+ });
318
+
319
+ test("the admin-only door may be named by `all`; an `all` that widens the door beside an ['admin'] entry mirrors nothing", () => {
320
+ setUser(['admin']);
321
+ expect(new TableAuth().canPerform(new AllAdminServiceTable(), 'update', 'db')).toBe(true);
322
+ expect(() => new TableAuth().canUpdate(new AllAdminServiceTable())).not.toThrow();
323
+ expect(new TableAuth().canPerform(new WidenedByAllTable(), 'update', 'service')).toBe(true);
324
+ expect(new TableAuth().canPerform(new WidenedByAllTable(), 'update', 'db')).toBe(false);
325
+ setUser(['ledger']);
326
+ expect(new TableAuth().canPerform(new AllAdminServiceTable(), 'update', 'db')).toBe(false);
327
+ });
328
+ });