Repository navigation
Expand file tree
/
Copy pathoperator-id-write-gate-33928.test.ts
More file actions
185 lines (165 loc) · 7.57 KB
/
Copy pathoperator-id-write-gate-33928.test.ts
File metadata and controls
185 lines (165 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* The declared narrowing of the sharing write gate, with plugin-security composed in.
*
* Under `multi: true`, a scalar `where.id` beside another `where` key is a write
* the engine runs as a predicate. The sharing write gate judges it by the bulk
* write scope, as it judges every predicate write, and not by id.
*
* Two answers are pinned on the same row, side by side:
*
* - the narrowed answer: a row the caller may write only because an
* application row-level policy widens the sharing scope for it is not
* written by that shape, and the write answers no error;
* - the remedy: the same row, addressed by id alone, is judged by id and
* written.
*
* Real stack: an `ObjectKernel` with `ObjectQLPlugin` over better-sqlite3, the
* platform permission sets plus an application writer set with a row-level
* update/delete policy, `SecurityPlugin` and `SharingServicePlugin`.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core';
import { ObjectQL, ObjectQLPlugin } from '@objectstack/objectql';
import { SqlDriver } from '@objectstack/driver-sql';
import { SharingServicePlugin } from '@objectstack/plugin-sharing';
import { SysMember, SysOrganization, SysUser } from '@objectstack/platform-objects/identity';
import { PermissionSetSchema } from '@objectstack/spec/security';
import { SecurityPlugin } from './security-plugin.js';
import { defaultPermissionSets } from './objects/default-permission-sets.js';
const ORG = 'org_opidc';
const ME = 'usr_opidc_me';
const OTHER = 'usr_opidc_other';
/** Owner-scoped (`private` OWD with an `owner_id`): record sharing enforces on it. */
const DEAL = 'qa_opidc_deal';
const CRUD = { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true } as const;
/** The application's writer set: a row-level policy admits an update or delete of any `open` deal. */
const WRITER = PermissionSetSchema.parse({
name: 'qa_opidc_writer',
objects: { [DEAL]: CRUD },
rowLevelSecurity: [
{ name: 'qa_opidc_open_updates', object: DEAL, operation: 'update', using: "stage == 'open'" },
{ name: 'qa_opidc_open_deletes', object: DEAL, operation: 'delete', using: "stage == 'open'" },
],
});
const SYSTEM = { isSystem: true, tenantId: ORG } as const;
const SYS = { context: SYSTEM } as never;
const asCaller = () => ({ userId: ME, tenantId: ORG, positions: [], permissions: [WRITER.name] });
type Outcome = { kind: 'landed' } | { kind: 'refused'; code?: string; status?: number };
const outcomeOf = (p: Promise<unknown>): Promise<Outcome> =>
p.then(
() => ({ kind: 'landed' as const }),
(e: { code?: unknown; status?: unknown; statusCode?: unknown } | null) => {
const status = e?.statusCode ?? e?.status;
return {
kind: 'refused' as const,
code: e?.code == null ? undefined : String(e.code),
status: status == null ? undefined : Number(status),
};
},
);
function sqliteDriverPlugin(): Plugin {
return {
name: 'test.driver.sqlite',
type: 'standard',
version: '1.0.0',
async init(ctx: PluginContext) {
ctx.registerService(
'driver.default',
new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }),
);
},
};
}
function objectsPlugin(): Plugin {
return {
name: 'test.objects',
type: 'standard',
version: '1.0.0',
dependencies: ['com.objectstack.engine.objectql'],
async init(ctx: PluginContext) {
ctx.getService<{ register(m: unknown): unknown }>('manifest').register({
id: 'test.operator-id-write-gate-composed',
namespace: 'qa_opidc',
version: '1.0.0',
type: 'plugin',
name: 'Operator id write gate, composed',
objects: [
SysUser, SysMember, SysOrganization,
{
name: DEAL,
label: 'Deal',
sharingModel: 'private',
fields: {
name: { type: 'text', label: 'Name' },
stage: { type: 'text', label: 'Stage' },
owner_id: { type: 'lookup', reference: 'sys_user', label: 'Owner' },
},
},
],
});
},
};
}
describe('sharing write gate with plugin-security: a scalar id beside another key under multi meets the bulk scope; by id alone it is judged by id', () => {
let kernel: ObjectKernel;
let ql: ObjectQL;
beforeAll(async () => {
kernel = new ObjectKernel({ logger: { level: 'silent' } } as never);
await kernel.use(sqliteDriverPlugin());
await kernel.use(new ObjectQLPlugin());
await kernel.use(objectsPlugin());
await kernel.use(new SecurityPlugin({
defaultPermissionSets: [...defaultPermissionSets, WRITER],
fallbackPermissionSet: 'member_default',
}));
await kernel.use(new SharingServicePlugin({ registerShareLinkRoutes: false }));
await kernel.bootstrap();
ql = kernel.getService<ObjectQL>('objectql');
await ql.insert('sys_organization', { id: ORG, name: 'Acme', slug: 'acme' }, SYS);
for (const userId of [ME, OTHER]) {
await ql.insert('sys_user', { id: userId, name: userId, email: `${userId}@acme.test`, email_verified: true }, SYS);
await ql.insert('sys_member', { user_id: userId, organization_id: ORG, role: 'member' }, SYS);
}
}, 120_000);
afterAll(async () => {
if (kernel) await Promise.race([kernel.shutdown(), new Promise<void>((r) => setTimeout(r, 10_000))]);
}, 30_000);
const stored = async (id: string) =>
((await ql.find(DEAL, { where: { id }, context: SYSTEM } as never)) as Array<{ name?: string }>)[0] ?? null;
/**
* Another member's `open` deal, shared to the caller for reading only. Record
* sharing does not let the caller write it; the application's row-level
* policy does.
*/
const seedPolicyAdmittedRow = async (id: string) => {
await ql.insert(DEAL, { id, owner_id: OTHER, created_by: OTHER, stage: 'open', name: 'seed' }, SYS);
await ql.insert('sys_record_share', {
object_name: DEAL, record_id: id, recipient_type: 'user', recipient_id: ME,
access_level: 'read', source: 'manual',
}, SYS);
expect(await ql.findOne(DEAL, { where: { id }, context: asCaller() } as never)).toMatchObject({ id });
};
it('update: the two-key predicate answers no error and leaves the row; the same row by id alone is written', async () => {
await seedPolicyAdmittedRow('u_1');
const narrowed = await outcomeOf(
ql.update(DEAL, { name: 'by_predicate' }, { where: { id: 'u_1', stage: 'open' }, multi: true, context: asCaller() } as never),
);
expect(narrowed).toEqual({ kind: 'landed' });
expect(await stored('u_1')).toMatchObject({ name: 'seed' });
const byId = await outcomeOf(ql.update(DEAL, { name: 'by_id' }, { where: { id: 'u_1' }, context: asCaller() } as never));
expect(byId).toEqual({ kind: 'landed' });
expect(await stored('u_1')).toMatchObject({ name: 'by_id' });
});
it('delete: the two-key predicate answers no error and leaves the row; the same row by id alone is deleted', async () => {
await seedPolicyAdmittedRow('d_1');
const narrowed = await outcomeOf(
ql.delete(DEAL, { where: { id: 'd_1', stage: 'open' }, multi: true, context: asCaller() } as never),
);
expect(narrowed).toEqual({ kind: 'landed' });
expect(await stored('d_1')).not.toBeNull();
const byId = await outcomeOf(ql.delete(DEAL, { where: { id: 'd_1' }, context: asCaller() } as never));
expect(byId).toEqual({ kind: 'landed' });
expect(await stored('d_1')).toBeNull();
});
});