Skip to content

Commit 6602814

Browse files
committed
test(plugin-security): suites name users their callers read
The engine's reference check now reads a user or lookup target through the caller's own read door, so suites whose subject is another predicate on the same write provision sys_user with a row for each user they name, and give their callers the org_user_ids the runtime fills from sys_member. The write-preview parity controls read the related account, and a new block drives the editor who cannot: both doors answer the lookup's reference_not_found, never the rule. The tenant-scope suite counts the rule's related reads alone, and a user the caller cannot read is now answered on the reference check. Claude-Session: https://claude.ai/code/session_01Utp6CYQjMkN514R78sCrTT Co-authored-by: Claude <noreply@anthropic.com>
1 parent 129000b commit 6602814

9 files changed

Lines changed: 227 additions & 50 deletions

‎packages/plugins/plugin-security/src/delegable-scope-no-identity-positions.posture.test.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { SysUserPermissionSet } from './objects/sys-user-permission-set.object.j
3737
import { RESERVED_IDENTITY_NAMES } from './objects/reserved-identity-names.js';
3838
import { defaultPermissionSets } from './objects/index.js';
3939
import { BUILTIN_IDENTITY_ORG_EXTERNAL } from '@objectstack/spec';
40+
import { provisionRigUsers } from './rig-users.testkit.js';
4041

4142
const SYS = { isSystem: true } as const;
4243
const ORG_A = 'org_a';
@@ -53,7 +54,12 @@ const PERMISSIONS = [
5354
{
5455
name: 'east_admin',
5556
label: 'East delegate',
56-
objects: { sys_user_position: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true } },
57+
// [#29029] With read on `sys_user`, the companion grant a delegate holds
58+
// to pick people: the write names a holder its caller reads.
59+
objects: {
60+
sys_user_position: { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true },
61+
sys_user: { allowRead: true },
62+
},
5763
adminScope: { businessUnit: 'east', includeSubtree: true, manageAssignments: true, assignablePermissionSets: ['field_user'] },
5864
},
5965
];
@@ -62,20 +68,26 @@ const POSITIONS = [{ name: CONTROL, label: 'Field rep', permissionSets: ['field_
6268
type Posture = 'single' | 'group' | 'isolated';
6369
const POSTURES: Posture[] = ['single', 'group', 'isolated'];
6470

71+
/**
72+
* [#29029] Organization A's users as `org_user_ids`, which the runtime fills
73+
* from `sys_member`: what the shipped sets' `sys_user` read rule reads.
74+
*/
75+
const ORG_USERS = ['u_delegate', 'u_org_admin', 'u_platform', 'u_target'];
76+
6577
/** One caller per branch of the report, and how the report must name the branch it took. */
6678
const BRANCHES = {
6779
'tenant-level': {
68-
context: { userId: 'u_platform', positions: [], permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN', tenantId: ORG_A, accessible_org_ids: [ORG_A] },
80+
context: { userId: 'u_platform', positions: [], permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN', tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_USERS },
6981
isTenantAdmin: true,
7082
scopes: [] as string[],
7183
},
7284
'organization-level': {
73-
context: { userId: 'u_org_admin', positions: [], permissions: ['organization_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A] },
85+
context: { userId: 'u_org_admin', positions: [], permissions: ['organization_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_USERS },
7486
isTenantAdmin: false,
7587
scopes: [] as string[],
7688
},
7789
delegated: {
78-
context: { userId: 'u_delegate', positions: [], permissions: ['east_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A] },
90+
context: { userId: 'u_delegate', positions: [], permissions: ['east_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_USERS },
7991
isTenantAdmin: false,
8092
scopes: ['east_admin'],
8193
},
@@ -89,6 +101,9 @@ const MEMBERSHIPS = [
89101
{ id: 'm_target', user_id: 'u_target', role: 'member' },
90102
];
91103

104+
/** [#29029] Every user this suite's writes name or act as: each gets a `sys_user` row. */
105+
const RIG_USERS = ORG_USERS;
106+
92107
const engines: ObjectQL[] = [];
93108
afterEach(async () => {
94109
vi.restoreAllMocks();
@@ -113,8 +128,10 @@ async function boot(posture: Posture) {
113128
objects: [SysUserPosition, SysUserPermissionSet, SysMember, SysBusinessUnit, SysBusinessUnitMember],
114129
} as any);
115130
await engine.syncSchemas();
116-
// `sys_user` registered and unprovisioned, as in the sibling posture suites.
131+
// [#29029] `sys_user` provisioned with a row for every user the writes name,
132+
// as in the sibling posture suites.
117133
if (!engine.registry.getObject(SysUser.name)) engine.registry.registerObject(SysUser as never, 'qa.delegable-scope-identity');
134+
await provisionRigUsers(engine, RIG_USERS);
118135
engines.push(engine);
119136

120137
const services: Record<string, unknown> = {

‎packages/plugins/plugin-security/src/grant-holder-membership-refusal.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
registerGrantHolderMembershipRefusal,
4141
} from './grant-holder-membership-refusal.js';
4242
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
43+
import { provisionRigUsers } from './rig-users.testkit.js';
4344

4445
// ---------------------------------------------------------------------------
4546
// Fixture
@@ -95,6 +96,12 @@ const MEMBERSHIPS = [
9596
{ id: 'm_both_b', user_id: 'u_both', organization_id: ORG_B, role: 'member' },
9697
];
9798

99+
/** [#29029] Every user this suite's writes name or act as: each gets a `sys_user` row. */
100+
const RIG_USERS = [
101+
'u_admin', 'u_admin_a', 'u_both', 'u_member_a', 'u_member_a2', 'u_member_b', 'u_nobody',
102+
'u_platform', 'u_ro_a',
103+
];
104+
98105
const engines: ObjectQL[] = [];
99106
afterEach(async () => {
100107
vi.restoreAllMocks();
@@ -143,10 +150,12 @@ async function boot(opts: BootOptions = {}) {
143150
// The organization-less holdings and rows below exist only on a store built
144151
// before 18.x (ADR-0131 D1 makes a fresh tenant column NOT NULL): this is it.
145152
await relaxTenantColumns(engine, ['sys_position', 'sys_user_position', 'sys_user_permission_set']);
146-
// `sys_user` stays registered and unprovisioned, as in the sibling suites:
147-
// the engine's own `user_id` existence probe then cannot run and stands
148-
// down, so what is measured here is the membership predicate alone.
153+
// [#29029] `sys_user` is provisioned with a row for every user the writes
154+
// name: the engine's own `user_id` reference check reads the holder through
155+
// the caller's read door and finds it, so what is measured here is the
156+
// membership predicate alone.
149157
if (!engine.registry.getObject(SysUser.name)) engine.registry.registerObject(SysUser as never, 'qa.authz-read-set');
158+
await provisionRigUsers(engine, RIG_USERS);
150159
if (opts.members === 'unprovisioned') engine.registry.registerObject(SysMember as never, 'qa.authz-read-set');
151160
engines.push(engine);
152161

‎packages/plugins/plugin-security/src/grant-permission-set-name.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
registerGrantPermissionSetNameHooks,
4545
} from './grant-permission-set-name.js';
4646
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
47+
import { provisionRigUsers } from './rig-users.testkit.js';
4748

4849
// ---------------------------------------------------------------------------
4950
// Fixture
@@ -70,6 +71,12 @@ const CATALOG = [MEMBER_DEFAULT, QA_ADMIN, QA_ALPHA, QA_BETA];
7071
const SYS = { isSystem: true } as const;
7172
const ADMIN = { userId: 'u_admin', positions: [], permissions: ['qa_admin'] };
7273

74+
/** [#29029] Every user this suite's writes name or act as: each gets a `sys_user` row. */
75+
const RIG_USERS = [
76+
'u_1', 'u_2', 'u_3', 'u_3b', 'u_4', 'u_5', 'u_6', 'u_7', 'u_admin', 'u_b1', 'u_c1', 'u_c2',
77+
'u_c3', 'u_c4', 'u_d1', 'u_p1', 'u_r1', 'u_s1', 'u_s2', 'u_u1',
78+
];
79+
7380
const engines: ObjectQL[] = [];
7481
afterEach(async () => {
7582
vi.restoreAllMocks();
@@ -103,6 +110,9 @@ async function boot() {
103110
for (const o of [SysUser, SysMember]) {
104111
if (!engine.registry.getObject(o.name)) engine.registry.registerObject(o as never, 'qa.authz-read-set');
105112
}
113+
// [#29029] Each grant's holder is a user the writer reads, so the grant-name
114+
// check is what is measured.
115+
await provisionRigUsers(engine, RIG_USERS);
106116
engines.push(engine);
107117

108118
const services: Record<string, unknown> = {

‎packages/plugins/plugin-security/src/org-admin-assignment-boundary.posture.test.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import { SysUserPosition } from './objects/sys-user-position.object.js';
4444
import { SysUserPermissionSet } from './objects/sys-user-permission-set.object.js';
4545
import { defaultPermissionSets } from './objects/index.js';
4646
import { maskFieldValue } from './field-masker.js';
47+
import { provisionRigUsers } from './rig-users.testkit.js';
4748

4849
const SYS = { isSystem: true } as const;
4950
const ORG_A = 'org_a';
@@ -109,11 +110,30 @@ const PERMISSIONS = [...defaultPermissionSets, QA_IN_SET, ...DELEGATE_SETS, ORG_
109110
type Posture = 'single' | 'group' | 'isolated';
110111
const POSTURES: Posture[] = ['single', 'group', 'isolated'];
111112

113+
/** [#29029] Every user this suite's writes name or act as: each gets a `sys_user` row. */
114+
const RIG_USERS = [
115+
'u_delegate', 'u_holder_hidden_delegate', 'u_in_0', 'u_in_1', 'u_in_2', 'u_in_3', 'u_in_4',
116+
'u_in_5', 'u_member', 'u_org_admin', 'u_org_admin_columns_hidden', 'u_org_admin_delegate',
117+
'u_org_admin_hiding', 'u_org_admin_nb', 'u_org_admin_organization_hidden',
118+
'u_org_admin_reads_masked', 'u_other_org', 'u_platform', 'u_position_hidden_delegate',
119+
'u_seeded', 'u_set_hidden_delegate', 'u_target', 'u_unit_hidden_delegate',
120+
'u_write_only_delegate',
121+
];
122+
123+
/**
124+
* [#29029] The users organization A seats — every rig user but the one of
125+
* organization B — as `org_user_ids`, which the runtime fills from
126+
* `sys_member`: an organization-A caller's `sys_user` read returns them, as
127+
* the shipped sets' `id in current_user.org_user_ids` rule answers in a
128+
* deployment.
129+
*/
130+
const ORG_A_USERS = RIG_USERS.filter((id) => id !== 'u_other_org');
131+
112132
const CALLERS = {
113-
platform: { userId: 'u_platform', positions: [], permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN', tenantId: ORG_A, accessible_org_ids: [ORG_A] },
114-
orgAdmin: { userId: 'u_org_admin', positions: [], permissions: ['organization_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A] },
115-
orgAdminNoBypass: { userId: 'u_org_admin_nb', positions: [], permissions: ['organization_admin_no_bypass'], tenantId: ORG_A, accessible_org_ids: [ORG_A] },
116-
member: { userId: 'u_member', positions: [], permissions: ['member_default'], tenantId: ORG_A, accessible_org_ids: [ORG_A] },
133+
platform: { userId: 'u_platform', positions: [], permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN', tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_A_USERS },
134+
orgAdmin: { userId: 'u_org_admin', positions: [], permissions: ['organization_admin'], tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_A_USERS },
135+
orgAdminNoBypass: { userId: 'u_org_admin_nb', positions: [], permissions: ['organization_admin_no_bypass'], tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_A_USERS },
136+
member: { userId: 'u_member', positions: [], permissions: ['member_default'], tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_A_USERS },
117137
} as const;
118138
type Caller = keyof typeof CALLERS;
119139

@@ -171,10 +191,12 @@ async function boot(posture: Posture, options: BootOptions = {}) {
171191
],
172192
} as any);
173193
await engine.syncSchemas();
174-
// `sys_user` registered and unprovisioned, as in the sibling suites: the
175-
// engine's `user_id` existence probe stands down and the membership hook is
176-
// what judges the holder.
194+
// [#29029] `sys_user` is provisioned with a row for every user the writes
195+
// name: the engine's `user_id` reference check reads the holder through the
196+
// caller's read door and finds it, and the membership hook is what judges
197+
// the holder.
177198
if (!engine.registry.getObject(SysUser.name)) engine.registry.registerObject(SysUser as never, 'qa.org-admin-boundary');
199+
await provisionRigUsers(engine, RIG_USERS);
178200
engines.push(engine);
179201

180202
const services: Record<string, unknown> = {
@@ -328,11 +350,8 @@ describe('[ADR-0090 D12, amended 2026-10-17] the organization administrator assi
328350
it('the delegable-scope report offers the org admin its in-boundary positions only, and explain admits it for its member', async () => {
329351
const h = await boot(posture);
330352
const security = h.plugin as any;
331-
// [#26641] Explain answers only for a user a `sys_user` row carries, so
332-
// the explained member gets one here (the boot leaves the table
333-
// unprovisioned for the write cases above).
334-
await h.engine.syncObjectSchema(SysUser.name);
335-
await h.engine.insert('sys_user', { id: 'u_target', name: 'Target', email: 'u_target@example.test' }, { context: { isSystem: true } } as any);
353+
// [#26641] Explain answers only for a user a `sys_user` row carries;
354+
// the boot gives the explained member one (`RIG_USERS`).
336355
for (const context of [CALLERS.orgAdmin, CALLERS.orgAdminNoBypass]) {
337356
const sets = await security.resolvePermissionSetsForContext(context);
338357
const report = await security.delegatedAdminGate.describeDelegableScope(sets, context);
@@ -511,7 +530,7 @@ describe("[ADR-0090 D12; ruling of record 5985885287] an organization administra
511530
// ── The delegated branch ────────────────────────────────────────────────────
512531

513532
const delegateContext = (userId: string, permissions: string[]) =>
514-
({ userId, positions: [] as string[], permissions, tenantId: ORG_A, accessible_org_ids: [ORG_A] });
533+
({ userId, positions: [] as string[], permissions, tenantId: ORG_A, accessible_org_ids: [ORG_A], org_user_ids: ORG_A_USERS });
515534
type DelegateContext = ReturnType<typeof delegateContext>;
516535

517536
/** Three principal classes the delegated walk judges, each holding the `a_ops` scope of organization A. */

‎packages/plugins/plugin-security/src/position-catalog-refusal.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { SysUserPosition } from './objects/sys-user-position.object.js';
3737
import { SysUserPermissionSet } from './objects/sys-user-permission-set.object.js';
3838
import { namesWithoutCatalogRow, positionNotInCatalogMessage } from './position-catalog-refusal.js';
3939
import { relaxTenantColumns } from './pre-18-tenant-column.testkit.js';
40+
import { provisionRigUsers } from './rig-users.testkit.js';
4041

4142
// ---------------------------------------------------------------------------
4243
// Fixture
@@ -104,6 +105,14 @@ const ADMIN_IN_GRANT_ORG = { ...ADMIN, tenantId: GRANT_ORG };
104105
/** A position record id — the spelling the measured authors wrote where the position's NAME belongs. */
105106
const AUDITOR_POSITION_ID = 'position_mtur8hz5jp7ptm3r';
106107

108+
/** [#29029] Every user this suite's writes name or act as: each gets a `sys_user` row. */
109+
const RIG_USERS = [
110+
'u_admin', 'u_b1', 'u_b2', 'u_bx', 'u_echo', 'u_fossil', 'u_holder', 'u_member', 'u_multi',
111+
'u_named', 'u_ns', 'u_nsu', 'u_ob', 'u_ob2', 'u_op', 'u_org_admin', 'u_plain', 'u_reg',
112+
'u_retired', 'u_sys', 'u_tok', 'u_tok_ok', 'u_up', 'u_wa', 'u_wb', 'u_wc', 'u_wf', 'u_wm',
113+
'u_wo', 'u_x',
114+
];
115+
107116
const engines: ObjectQL[] = [];
108117
afterEach(async () => {
109118
vi.restoreAllMocks();
@@ -143,6 +152,11 @@ async function boot(opts: { walled?: boolean } = {}) {
143152
for (const o of [SysUser, SysMember, SysUserPosition, SysUserPermissionSet]) {
144153
if (!engine.registry.getObject(o.name)) engine.registry.registerObject(o as never, 'qa.authz-read-set');
145154
}
155+
// [#29029] …except `sys_user`, which is provisioned with a row for every
156+
// holder the writes name: the engine's `user_id` reference check reads the
157+
// holder through the caller's read door, so the position check is what is
158+
// measured.
159+
await provisionRigUsers(engine, RIG_USERS);
146160
engines.push(engine);
147161

148162
const warn = vi.fn();

‎packages/plugins/plugin-security/src/predicate-related-read-tenant-scope.test.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
* real middleware: a caller bound to org X writes a reference to org Y's row,
1616
* once with that row `secret` and once `public`. The two must end identically,
1717
* the read must return nothing, and the org X control must reach the rule.
18+
*
19+
* [#29029] A write that is not a system write may name only a record its
20+
* caller can read, and that reference check runs before the record's rules
21+
* (ruling records 6573561091 and 6663849489): a reference to org Y's row, or to
22+
* a user the caller cannot read, is answered the lookup's `reference_not_found`.
23+
* The reads counted below are the rule's related reads alone — a read that
24+
* selects a column the predicate names; the reference check reads `id` only.
1825
*/
1926

2027
import { describe, it, expect, afterEach, vi } from 'vitest';
@@ -173,7 +180,9 @@ async function boot(kind: 'secret' | 'public', posture?: 'single' | 'group' | 'i
173180
const find = driver.find.bind(driver);
174181
vi.spyOn(driver, 'find').mockImplementation(async (object, ast, options) => {
175182
const rows = await find(object, ast, options);
176-
if (object === 'qa_line') readsOfLine.push(rows as unknown[]);
183+
const columns = (ast as { fields?: unknown[] }).fields;
184+
const ruleRead = Array.isArray(columns) && columns.some((column) => column !== 'id');
185+
if (object === 'qa_line' && ruleRead) readsOfLine.push(rows as unknown[]);
177186
if (object === 'sys_user') userColumnsRead.push(...((ast as { fields?: unknown[] }).fields ?? ['*']));
178187
return rows;
179188
});
@@ -303,12 +312,13 @@ describe('#18682 — under `isolated`, an org-less writer of an unwalled object
303312
/**
304313
* `sys_user` has no tenant column, so no organization wall scopes the related
305314
* read; its own wall is the shipped `member_default` row-level security. A user
306-
* the caller's own read of `sys_user` does not return is NOT READABLE: the rule
307-
* faults loudly, is never evaluated on that user, and never reads its columns.
315+
* the caller's own read of `sys_user` does not return may not be named
316+
* (#29029): the write answers the field's `reference_not_found`, before the
317+
* rule, which is never evaluated on that user and never reads its columns.
308318
*/
309-
describe('#18682 — a related user the caller cannot read makes the rule fault loudly', () => {
319+
describe('#18682, #29029 — a user the caller cannot read cannot be named', () => {
310320
const PEERS = { ...CALLER, org_user_ids: ['u_x', 'u_x2'] };
311-
const NOT_READABLE = "could not read 'sys_user'";
321+
const NOT_READABLE = 'reviewer: no sys_user record has id "u_y"';
312322

313323
it('CONTROL: the caller’s own read of sys_user returns org X’s users, never org Y’s', async () => {
314324
const h = await boot('secret', 'isolated');
@@ -379,12 +389,12 @@ describe('#18682 — a caller with no userId that is not system is bound like a
379389
expect(secret.readsOfLine).toEqual([]);
380390
});
381391

382-
it('a user only org Y holds, under no wall: every door refuses, whatever its value — the write as not readable, the preview at its write gate', async () => {
392+
it('a user only org Y holds, under no wall: every door refuses, whatever its value — the write on the reference check, the preview at its write gate', async () => {
383393
const banned = await observe('secret', 'u_y', PUBLIC_FORM('qa_review'), 'single', 'qa_review');
384394
const clear = await observe('public', 'u_y', PUBLIC_FORM('qa_review'), 'single', 'qa_review');
385395

386396
expect(clear.seen).toEqual(banned.seen);
387-
expect(banned.seen.refusal).toMatchObject({ code: 'VALIDATION_FAILED', message: expect.stringContaining("could not read 'sys_user'") });
397+
expect(banned.seen.refusal).toMatchObject({ code: 'VALIDATION_FAILED', message: expect.stringContaining('reviewer: no sys_user record has id "u_y"') });
388398
expect(banned.seen.committed).toBe(0);
389399
// [#21079] The preview asks the engine's write gate before it reads anything
390400
// related. This grant context names a set the deployment does not register,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* Provision `sys_user` and give each user a suite's writes name a row.
5+
*
6+
* [#29029] A write that is not a system write may name, in a `user` or
7+
* `lookup` field, only a record its caller's own read door returns (ruling
8+
* records 6573561091 and 6663849489), and a read door that fails — a
9+
* registered `sys_user` with no table behind it among them — answers
10+
* `reference_not_found`. A suite whose subject is another predicate on the
11+
* same write (a membership, a catalog name, a boundary) provisions the users
12+
* its writes name, so that predicate is the one it measures.
13+
*
14+
* Call it after `sys_user` is registered. The rows are written as the system;
15+
* each suite's callers read them through their own grants, as in a deployment.
16+
*/
17+
export async function provisionRigUsers(
18+
engine: {
19+
syncObjectSchema(object: string): Promise<unknown>;
20+
insert(object: string, data: Record<string, unknown>, options: unknown): Promise<unknown>;
21+
},
22+
ids: readonly string[],
23+
): Promise<void> {
24+
await engine.syncObjectSchema('sys_user');
25+
for (const id of new Set(ids)) {
26+
await engine.insert('sys_user', { id, name: id, email: `${id}@example.test` }, { context: { isSystem: true } });
27+
}
28+
}

0 commit comments

Comments
 (0)