@proteinjs/user 1.12.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/dist/generated/index.d.ts +1 -0
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +2 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/MachineAccount.d.ts +5 -2
- package/dist/src/MachineAccount.d.ts.map +1 -1
- package/dist/src/MachineAccount.js +5 -2
- package/dist/src/MachineAccount.js.map +1 -1
- package/dist/src/RolesCatalog.d.ts +8 -0
- package/dist/src/RolesCatalog.d.ts.map +1 -1
- package/dist/src/RolesCatalog.js.map +1 -1
- package/dist/src/SharedRecord.d.ts.map +1 -1
- package/dist/src/SharedRecord.js +15 -0
- package/dist/src/SharedRecord.js.map +1 -1
- package/dist/src/tables/AccessGrantTable.d.ts +13 -0
- package/dist/src/tables/AccessGrantTable.d.ts.map +1 -1
- package/dist/src/tables/AccessGrantTable.js +48 -2
- package/dist/src/tables/AccessGrantTable.js.map +1 -1
- package/dist/test/AccessGrantWellFormed.test.d.ts +2 -0
- package/dist/test/AccessGrantWellFormed.test.d.ts.map +1 -0
- package/dist/test/AccessGrantWellFormed.test.js +353 -0
- package/dist/test/AccessGrantWellFormed.test.js.map +1 -0
- package/generated/index.ts +2 -1
- package/jest.config.js +7 -0
- package/package.json +3 -2
- package/src/MachineAccount.ts +5 -2
- package/src/RolesCatalog.ts +8 -0
- package/src/SharedRecord.ts +16 -0
- package/src/tables/AccessGrantTable.ts +37 -1
- package/test/AccessGrantWellFormed.test.ts +216 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { UserRepo } from '../src/UserRepo';
|
|
2
|
+
|
|
3
|
+
import moment from 'moment';
|
|
4
|
+
import { Db, getDb, getDbAsSystem, QueryBuilderFactory, Reference, StringColumn, Table } from '@proteinjs/db';
|
|
5
|
+
import { KnexDriver } from '@proteinjs/db-driver-knex';
|
|
6
|
+
import { getSharedDbAsSystem, SharedRecord, withSharedRecordColumns } from '../src/SharedRecord';
|
|
7
|
+
import { AccessGrant, AccessGrantTable, isMalformedAccessGrantError } from '../src/tables/AccessGrantTable';
|
|
8
|
+
import { Session, SessionData, SessionDataStorage } from '@proteinjs/server-api';
|
|
9
|
+
import { SourceRepository } from '@proteinjs/reflection';
|
|
10
|
+
import { tables } from '../src/tables/tables';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* AccessGrant well-formedness invariant (the malformed-grant class, 2026-08-23).
|
|
14
|
+
*
|
|
15
|
+
* The test environment held a non-owner `thought` grant whose principal was NULL; it crashed the
|
|
16
|
+
* deploy-gated OrphanContentReferenceSweep. Origin: `ReferenceColumn.serialize` stores NULL for a
|
|
17
|
+
* Reference with no `_id`, and nothing in the AccessGrant schema ever required one — so every
|
|
18
|
+
* grant write path inherited the hole. Two producers are known:
|
|
19
|
+
* - the SharedRecord owner-grant bootstrap in a context with NO session user (background
|
|
20
|
+
* executors, deploy-time migrations): `new UserRepo().getUser()` is `{}` there, so the mint
|
|
21
|
+
* wrote `principal = Reference('user', undefined)` → NULL;
|
|
22
|
+
* - any RPC-reachable insert that forwards a caller-supplied id unchecked (ThoughtSharing's
|
|
23
|
+
* re-level, a direct DbService insert by a resource admin).
|
|
24
|
+
*
|
|
25
|
+
* The invariant lives on the table (one owner): an AccessGrant insert without a principal id or a
|
|
26
|
+
* resource id is refused with a typed error, for system and caller context alike. The bootstrap
|
|
27
|
+
* then names its own no-session case explicitly instead of minting a row for nobody.
|
|
28
|
+
*
|
|
29
|
+
* Outcome bar: assertions read grant rows back as system — never a call merely "succeeding".
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
interface SharedItem extends SharedRecord {
|
|
33
|
+
name: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class TestSessionDataStorage implements SessionDataStorage {
|
|
37
|
+
environment = 'node' as 'node';
|
|
38
|
+
static SESSION_DATA: { [id: string]: SessionData } = {};
|
|
39
|
+
setData(data: SessionData) {
|
|
40
|
+
TestSessionDataStorage.SESSION_DATA['sessionData'] = data;
|
|
41
|
+
}
|
|
42
|
+
getData(): SessionData {
|
|
43
|
+
return TestSessionDataStorage.SESSION_DATA['sessionData'];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const users = [0, 1].map((i) => ({
|
|
48
|
+
name: `Test user${i}`,
|
|
49
|
+
email: `test.user${i}`,
|
|
50
|
+
password: 'test',
|
|
51
|
+
emailVerified: false,
|
|
52
|
+
roles: [] as string[],
|
|
53
|
+
created: moment(),
|
|
54
|
+
updated: moment(),
|
|
55
|
+
id: `user${i}`,
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
class SharedItemTable extends Table<SharedItem> {
|
|
59
|
+
name = 'user_test_wellformed_shared_item';
|
|
60
|
+
auth: Table<SharedItem>['auth'] = {
|
|
61
|
+
db: { all: 'authenticated' },
|
|
62
|
+
service: { all: 'authenticated' },
|
|
63
|
+
};
|
|
64
|
+
columns = withSharedRecordColumns<SharedItem>({
|
|
65
|
+
name: new StringColumn('name'),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const dbDriver = new KnexDriver({ host: 'localhost', user: 'root', password: '', dbName: 'test' });
|
|
70
|
+
const userRepo = new UserRepo();
|
|
71
|
+
const sharedItemTable = new SharedItemTable() as Table<SharedItem>;
|
|
72
|
+
|
|
73
|
+
const dropTable = async (table: Table<any>) => {
|
|
74
|
+
if (await dbDriver.getKnex().schema.withSchema(dbDriver.getDbName()).hasTable(table.name)) {
|
|
75
|
+
await dbDriver.getKnex().schema.withSchema(dbDriver.getDbName()).dropTable(table.name);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const runAs = (userIndex: number) => {
|
|
80
|
+
Session.setData({ sessionId: 'test-session', user: users[userIndex].email, data: {} });
|
|
81
|
+
userRepo.setUser(users[userIndex]);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** A context with no session user at all — what background executors and boot-time migrations see. */
|
|
85
|
+
const runSessionless = () => Session.setData({ sessionId: 'boot', user: undefined, data: {} });
|
|
86
|
+
|
|
87
|
+
const grantsOn = async (resourceId: string): Promise<AccessGrant[]> => {
|
|
88
|
+
const qb = new QueryBuilderFactory()
|
|
89
|
+
.getQueryBuilder(tables.AccessGrant)
|
|
90
|
+
.condition({ field: 'resource', operator: '=', value: resourceId })
|
|
91
|
+
.condition({ field: 'resourceTable', operator: '=', value: sharedItemTable.name });
|
|
92
|
+
return await getDbAsSystem().query(tables.AccessGrant, qb);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const allGrants = async (): Promise<AccessGrant[]> => await getDbAsSystem().query(tables.AccessGrant, {});
|
|
96
|
+
|
|
97
|
+
/** The typed refusal, by name tag (the house convention for cross-package error identity). */
|
|
98
|
+
const expectMalformedRefusal = async (write: Promise<unknown>) => {
|
|
99
|
+
let caught: unknown;
|
|
100
|
+
try {
|
|
101
|
+
await write;
|
|
102
|
+
} catch (error) {
|
|
103
|
+
caught = error;
|
|
104
|
+
}
|
|
105
|
+
expect(caught).toBeDefined();
|
|
106
|
+
expect(isMalformedAccessGrantError(caught)).toBe(true);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
describe('AccessGrant well-formedness invariant', () => {
|
|
110
|
+
beforeAll(async () => {
|
|
111
|
+
(SourceRepository.get() as any).objectCache['@proteinjs/db/DefaultDbDriverFactory'] = [
|
|
112
|
+
{ getDbDriver: () => dbDriver },
|
|
113
|
+
];
|
|
114
|
+
(SourceRepository.get() as any).objectCache['@proteinjs/server-api/SessionDataStorage'] = [
|
|
115
|
+
new TestSessionDataStorage(),
|
|
116
|
+
];
|
|
117
|
+
(SourceRepository.get() as any).objectCache['@proteinjs/db/Table'] = [tables.AccessGrant, sharedItemTable];
|
|
118
|
+
(SourceRepository.get() as any).objectCache['@proteinjs/user-auth/AuthenticatedUserRepo'] = [userRepo];
|
|
119
|
+
|
|
120
|
+
runAs(0);
|
|
121
|
+
|
|
122
|
+
if (dbDriver.start) {
|
|
123
|
+
await dbDriver.start();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
jest.spyOn(Db, 'getDefaultDbDriver').mockImplementation(() => dbDriver);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
beforeEach(async () => {
|
|
130
|
+
await dbDriver.getTableManager().loadTable(sharedItemTable);
|
|
131
|
+
await dbDriver.getTableManager().loadTable(tables.AccessGrant);
|
|
132
|
+
runAs(0);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
afterEach(async () => {
|
|
136
|
+
await dropTable(sharedItemTable);
|
|
137
|
+
await dropTable(new AccessGrantTable());
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
afterAll(() => {
|
|
141
|
+
if (dbDriver.stop) {
|
|
142
|
+
dbDriver.stop();
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('refuses a SYSTEM insert whose principal reference has no id — no row lands', async () => {
|
|
147
|
+
const item = await getSharedDbAsSystem().insert(sharedItemTable, { name: 'owned by user0' });
|
|
148
|
+
const before = (await grantsOn(item.id)).length;
|
|
149
|
+
|
|
150
|
+
await expectMalformedRefusal(
|
|
151
|
+
getDbAsSystem().insert(tables.AccessGrant, {
|
|
152
|
+
principal: new Reference('user', undefined),
|
|
153
|
+
resource: new Reference(sharedItemTable.name, item.id),
|
|
154
|
+
resourceTable: sharedItemTable.name,
|
|
155
|
+
accessLevel: 'read',
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const after = await grantsOn(item.id);
|
|
160
|
+
expect(after).toHaveLength(before);
|
|
161
|
+
expect(after.every((grant) => !!grant.principal?._id && !!grant.resource?._id)).toBe(true);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('refuses a SYSTEM insert whose resource reference has no id — no row lands', async () => {
|
|
165
|
+
const before = (await allGrants()).length;
|
|
166
|
+
|
|
167
|
+
await expectMalformedRefusal(
|
|
168
|
+
getDbAsSystem().insert(tables.AccessGrant, {
|
|
169
|
+
principal: new Reference('user', users[1].id),
|
|
170
|
+
resource: new Reference(sharedItemTable.name, undefined),
|
|
171
|
+
resourceTable: sharedItemTable.name,
|
|
172
|
+
accessLevel: 'write',
|
|
173
|
+
})
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
expect(await allGrants()).toHaveLength(before);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('refuses a CALLER insert with a null principal even from the resource owner (the DbService-reachable shape)', async () => {
|
|
180
|
+
const item = await getSharedDbAsSystem().insert(sharedItemTable, { name: 'owned by user0' });
|
|
181
|
+
const before = (await grantsOn(item.id)).length;
|
|
182
|
+
|
|
183
|
+
// user0 holds owner on the item (bootstrap) and would otherwise pass the admin/owner gate.
|
|
184
|
+
await expectMalformedRefusal(
|
|
185
|
+
getDb().insert(tables.AccessGrant, {
|
|
186
|
+
principal: null as any,
|
|
187
|
+
resource: new Reference(sharedItemTable.name, item.id),
|
|
188
|
+
resourceTable: sharedItemTable.name,
|
|
189
|
+
accessLevel: 'read',
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
expect(await grantsOn(item.id)).toHaveLength(before);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('a root created with NO session user lands without a grant — never a principal-less owner grant', async () => {
|
|
197
|
+
runSessionless();
|
|
198
|
+
expect(new UserRepo().getUser().id).toBeUndefined();
|
|
199
|
+
|
|
200
|
+
const item = await getSharedDbAsSystem().insert(sharedItemTable, { name: 'created by a background executor' });
|
|
201
|
+
|
|
202
|
+
// The row itself lands (system creators are legitimate) …
|
|
203
|
+
expect(await getSharedDbAsSystem().get(sharedItemTable, { id: item.id })).toBeDefined();
|
|
204
|
+
// … with nothing minted for nobody: no grant at all on it, and no malformed row anywhere.
|
|
205
|
+
expect(await grantsOn(item.id)).toHaveLength(0);
|
|
206
|
+
expect((await allGrants()).every((grant) => !!grant.principal?._id && !!grant.resource?._id)).toBe(true);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('a root created WITH a session user still gets exactly its owner grant (the bootstrap is intact)', async () => {
|
|
210
|
+
runAs(1);
|
|
211
|
+
const item = await getSharedDbAsSystem().insert(sharedItemTable, { name: 'owned by user1' });
|
|
212
|
+
|
|
213
|
+
const grants = await grantsOn(item.id);
|
|
214
|
+
expect(grants.map((grant) => [grant.principal._id, grant.accessLevel])).toEqual([[users[1].id, 'owner']]);
|
|
215
|
+
});
|
|
216
|
+
});
|