@proteinjs/db 1.50.0 → 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/CHANGELOG.md +22 -0
- package/dist/generated/test/index.js +1 -1
- package/dist/src/auth/TableAuth.d.ts +9 -0
- package/dist/src/auth/TableAuth.d.ts.map +1 -1
- package/dist/src/auth/TableAuth.js +39 -2
- package/dist/src/auth/TableAuth.js.map +1 -1
- package/dist/test/TableAuthIdentity.test.js +211 -0
- package/dist/test/TableAuthIdentity.test.js.map +1 -1
- package/generated/test/index.ts +1 -1
- package/package.json +2 -2
- package/src/auth/TableAuth.ts +41 -2
- package/test/TableAuthIdentity.test.ts +215 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@proteinjs/db",
|
|
3
|
-
"version": "1.50.
|
|
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": "
|
|
69
|
+
"gitHead": "6a487f9e29bc1ab5e3a7f79e1cce178cd2512beb"
|
|
70
70
|
}
|
package/src/auth/TableAuth.ts
CHANGED
|
@@ -132,8 +132,47 @@ export class TableAuth {
|
|
|
132
132
|
|
|
133
133
|
// Within a declared block an undeclared operation stays closed to everyone (the block is
|
|
134
134
|
// the whole declaration) — `allows` reads undefined as false, so the two doors are exactly
|
|
135
|
-
// the ones the table wrote down
|
|
136
|
-
|
|
135
|
+
// the ones the table wrote down...
|
|
136
|
+
if (this.allows(tableAuth.all) || this.allows(tableAuth[operation])) {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ...except that a db door the block leaves UNDECLARED mirrors the door the table's SERVICE
|
|
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.
|
|
152
|
+
return api === 'db' && tableAuth.all === undefined && tableAuth[operation] === undefined
|
|
153
|
+
? this.mirrorsAdminOnlyServiceDoor(table, operation)
|
|
154
|
+
: false;
|
|
155
|
+
}
|
|
156
|
+
|
|
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 {
|
|
164
|
+
const serviceAuth = table.auth?.service;
|
|
165
|
+
if (!serviceAuth || Object.keys(serviceAuth).length == 0) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
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';
|
|
137
176
|
}
|
|
138
177
|
|
|
139
178
|
/** One identity against the current user; undefined (nothing declared) is not a grant. */
|
|
@@ -11,8 +11,10 @@ import { TableAuth } from '../src/auth/TableAuth';
|
|
|
11
11
|
* - each identity form resolves against the current user; 'admin' is break-glass throughout;
|
|
12
12
|
* - an UNDECLARED identity is admin-only (default-deny made explicit), never open;
|
|
13
13
|
* - it fails CLOSED when no user can be resolved;
|
|
14
|
-
* - the operation doors (`canPerform`)
|
|
15
|
-
*
|
|
14
|
+
* - the operation doors (`canPerform`) keep the declared-block semantics: within a declared block
|
|
15
|
+
* an undeclared operation stays closed to everyone, admin included — unless the table's SERVICE
|
|
16
|
+
* block names that door for the admin role alone, which the db door then mirrors (the last two
|
|
17
|
+
* describes).
|
|
16
18
|
*
|
|
17
19
|
* `UserAuth` reads from a static repo; tests stub it directly per identity — no server needed.
|
|
18
20
|
*/
|
|
@@ -113,3 +115,214 @@ describe('TableAuth operation doors keep their declared-block semantics', () =>
|
|
|
113
115
|
expect(new TableAuth().canPerform(new QueryOnlyTable(), 'insert')).toBe(false);
|
|
114
116
|
});
|
|
115
117
|
});
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The RPC runs BOTH doors — the service gate (`TableServiceAuth`) and then the inner `Db`'s
|
|
121
|
+
* db-api re-check as the calling user — so a db door a table leaves UNDECLARED mirrors the door
|
|
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.
|
|
131
|
+
*/
|
|
132
|
+
class ReadOpenWriteAdminTable extends Table<Doc> {
|
|
133
|
+
public name = 'identity_read_open_write_admin_test';
|
|
134
|
+
public auth: Table<Doc>['auth'] = {
|
|
135
|
+
db: { query: 'authenticated' },
|
|
136
|
+
service: { query: 'authenticated', insert: ['admin'], update: ['admin'], delete: ['admin'] },
|
|
137
|
+
};
|
|
138
|
+
public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class LockedLedgerTable extends Table<Doc> {
|
|
142
|
+
public name = 'identity_locked_ledger_test';
|
|
143
|
+
public auth: Table<Doc>['auth'] = {
|
|
144
|
+
db: { query: { permission: 'ledger' } },
|
|
145
|
+
service: { query: { permission: 'ledger' } },
|
|
146
|
+
};
|
|
147
|
+
public columns = withRecordColumns<Doc>({ title: new StringColumn('title') });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
describe("TableAuth — an undeclared db door mirrors the declared ['admin'] service door", () => {
|
|
151
|
+
beforeEach(() => {
|
|
152
|
+
setMapping({ ledger: ['ledger'] });
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
afterEach(() => {
|
|
156
|
+
(UserAuth as any).userRepo = undefined;
|
|
157
|
+
(UserAuth as any).permissionRolesMapping = undefined;
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("an admin's write clears the ['admin'] service door AND the db-api re-check behind it", () => {
|
|
161
|
+
setUser(['admin']);
|
|
162
|
+
const table = new ReadOpenWriteAdminTable();
|
|
163
|
+
expect(() => new TableAuth().canUpdate(table, 'service')).not.toThrow();
|
|
164
|
+
expect(() => new TableAuth().canUpdate(table)).not.toThrow();
|
|
165
|
+
expect(() => new TableAuth().canInsert(table)).not.toThrow();
|
|
166
|
+
expect(() => new TableAuth().canDelete(table)).not.toThrow();
|
|
167
|
+
expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(true);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('a signed-in non-admin is refused by name on both apis; the read door stays open', () => {
|
|
171
|
+
setUser(['ledger']);
|
|
172
|
+
const table = new ReadOpenWriteAdminTable();
|
|
173
|
+
expect(() => new TableAuth().canUpdate(table, 'service')).toThrow(
|
|
174
|
+
'User is not authorized to update records in table: identity_read_open_write_admin_test'
|
|
175
|
+
);
|
|
176
|
+
expect(() => new TableAuth().canUpdate(table)).toThrow(
|
|
177
|
+
'User is not authorized to update records in table: identity_read_open_write_admin_test'
|
|
178
|
+
);
|
|
179
|
+
expect(() => new TableAuth().canQuery(table)).not.toThrow();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('a guest is refused every door', () => {
|
|
183
|
+
setGuest();
|
|
184
|
+
const table = new ReadOpenWriteAdminTable();
|
|
185
|
+
expect(() => new TableAuth().canQuery(table)).toThrow(
|
|
186
|
+
'User is not authorized to query table: identity_read_open_write_admin_test'
|
|
187
|
+
);
|
|
188
|
+
expect(() => new TableAuth().canUpdate(table)).toThrow();
|
|
189
|
+
expect(() => new TableAuth().canInsert(table, 'service')).toThrow();
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test('one-way: a service block that omits the door keeps the lock on both apis, break-glass included', () => {
|
|
193
|
+
setUser(['admin']);
|
|
194
|
+
const table = new LockedLedgerTable();
|
|
195
|
+
expect(new TableAuth().canPerform(table, 'update', 'service')).toBe(false);
|
|
196
|
+
expect(new TableAuth().canPerform(table, 'update', 'db')).toBe(false);
|
|
197
|
+
expect(() => new TableAuth().canUpdate(table)).toThrow(
|
|
198
|
+
'User is not authorized to update records in table: identity_locked_ledger_test'
|
|
199
|
+
);
|
|
200
|
+
expect(() => new TableAuth().canInsert(table, 'service')).toThrow();
|
|
201
|
+
expect(() => new TableAuth().canQuery(table)).not.toThrow();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('a table with no service block keeps its db block as written — admin included', () => {
|
|
205
|
+
setUser(['admin']);
|
|
206
|
+
expect(new TableAuth().canPerform(new QueryOnlyTable(), 'update', 'db')).toBe(false);
|
|
207
|
+
expect(() => new TableAuth().canUpdate(new QueryOnlyTable())).toThrow(
|
|
208
|
+
'User is not authorized to update records in table: identity_query_only_test'
|
|
209
|
+
);
|
|
210
|
+
});
|
|
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
|
+
});
|