Skip to content

Commit ee912fb

Browse files
committed
fix(plugin-security): a refused grant holder the caller cannot read is answered as a holder that does not exist
The grant-holder membership refusal asks the caller's own read door about a holder the write names, the way the engine's reference check asks it, and leaves a holder that door does not return to that check. A holder the caller can read is judged on membership as before; the refusal set and envelope are unchanged. Claude-Session: https://claude.ai/code/session_012dn2AdqWnb3ndnEacKvJvF Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9f61e06 commit ee912fb

2 files changed

Lines changed: 314 additions & 14 deletions

File tree

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

Lines changed: 200 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { ObjectQL } from '@objectstack/objectql';
2727
import { SqlDriver } from '@objectstack/driver-sql';
2828
import { resolveThrownHttpError } from '@objectstack/types';
2929
import type { PermissionSet } from '@objectstack/spec/security';
30+
import { ORGANIZATION_ADMIN } from '@objectstack/spec/identity';
31+
import { defaultPermissionSets } from './objects/default-permission-sets.js';
3032

3133
import { SecurityPlugin } from './security-plugin.js';
3234
import { SysUser, SysMember } from '@objectstack/platform-objects/identity';
@@ -74,6 +76,28 @@ const QA_READ_ONLY_ON_GRANTS = {
7476
},
7577
} as unknown as PermissionSet;
7678

79+
/** The shipped organization administrator's `sys_user` read: its object entry and its row policies. */
80+
const SHIPPED_ORG_ADMIN = defaultPermissionSets.find((s) => s.name === ORGANIZATION_ADMIN)!;
81+
const SHIPPED_USER_READ_POLICIES = (SHIPPED_ORG_ADMIN.rowLevelSecurity ?? []).filter((p) => p.object === 'sys_user');
82+
83+
/** The superuser wildcard, with the shipped organization administrator's `sys_user` read (its own row and its organization's members). */
84+
const QA_ADMIN_SHIPPED_USER_READ = {
85+
name: 'qa_admin_shipped_user_read',
86+
label: 'QA Admin, shipped user read',
87+
objects: { ...(QA_ADMIN as any).objects, sys_user: (SHIPPED_ORG_ADMIN.objects as any).sys_user },
88+
rowLevelSecurity: SHIPPED_USER_READ_POLICIES,
89+
} as unknown as PermissionSet;
90+
91+
/** The same, with a `sys_user` read NARROWER than organization membership: its own row only. */
92+
const QA_ADMIN_OWN_USER_ROW = {
93+
name: 'qa_admin_own_user_row',
94+
label: 'QA Admin, own user row',
95+
objects: { ...(QA_ADMIN as any).objects, sys_user: (SHIPPED_ORG_ADMIN.objects as any).sys_user },
96+
rowLevelSecurity: SHIPPED_USER_READ_POLICIES.filter((p) => p.name === 'sys_user_self'),
97+
} as unknown as PermissionSet;
98+
99+
const PERMISSION_SETS = [MEMBER_DEFAULT, QA_ADMIN, QA_READ_ONLY_ON_GRANTS, QA_ADMIN_SHIPPED_USER_READ, QA_ADMIN_OWN_USER_ROW];
100+
77101
const SYS = { isSystem: true } as const;
78102
const ORG_A = 'org_a';
79103
const ORG_B = 'org_b';
@@ -119,6 +143,8 @@ interface BootOptions {
119143
* every membership read fails.
120144
*/
121145
members?: boolean | 'unprovisioned';
146+
/** `false` registers no `sys_user` object: the engine's reference check has no holder to read. */
147+
users?: boolean;
122148
}
123149

124150
/** The positions the fixture's assignment rows name, as their definitions. */
@@ -154,8 +180,10 @@ async function boot(opts: BootOptions = {}) {
154180
// name: the engine's own `user_id` reference check reads the holder through
155181
// the caller's read door and finds it, so what is measured here is the
156182
// membership predicate alone.
157-
if (!engine.registry.getObject(SysUser.name)) engine.registry.registerObject(SysUser as never, 'qa.authz-read-set');
158-
await provisionRigUsers(engine, RIG_USERS);
183+
if (opts.users !== false) {
184+
if (!engine.registry.getObject(SysUser.name)) engine.registry.registerObject(SysUser as never, 'qa.authz-read-set');
185+
await provisionRigUsers(engine, RIG_USERS);
186+
}
159187
if (opts.members === 'unprovisioned') engine.registry.registerObject(SysMember as never, 'qa.authz-read-set');
160188
engines.push(engine);
161189

@@ -167,7 +195,7 @@ async function boot(opts: BootOptions = {}) {
167195
metadata: {
168196
get: async (type: string, name: string) =>
169197
(type === 'position' ? QA_POSITIONS.find((p) => p.name === name) : engine.getSchema(name)) ?? null,
170-
list: async (type: string) => (type === 'position' ? QA_POSITIONS : [MEMBER_DEFAULT, QA_ADMIN, QA_READ_ONLY_ON_GRANTS]),
198+
list: async (type: string) => (type === 'position' ? QA_POSITIONS : PERMISSION_SETS),
171199
},
172200
...(opts.walled
173201
? { 'org-scoping': { name: 'com.objectstack.org-scoping' }, tenancy: { posture: 'isolated' } }
@@ -446,6 +474,175 @@ describe('single posture', () => {
446474
});
447475
});
448476

477+
// ---------------------------------------------------------------------------
478+
// A holder the caller cannot read
479+
// ---------------------------------------------------------------------------
480+
481+
/**
482+
* A refused grant holder the caller cannot read is answered as a holder that
483+
* does not exist — one answer, the engine's own reference refusal, whatever
484+
* that holder is. One id is named in three stores (a member of the row's
485+
* organization, a member of another organization only, no user at all) by a
486+
* caller whose `sys_user` read is narrower than organization membership, and
487+
* the whole answer an HTTP door serves is compared byte for byte. A holder the
488+
* caller CAN read keeps the membership refusal, and the refusal SET is the
489+
* ruled one on every path.
490+
*/
491+
describe('a refused grant holder the caller cannot read is answered as a holder that does not exist', () => {
492+
const HOLDER = 'u_x';
493+
type Store = 'member here' | 'member elsewhere' | 'no such user';
494+
const STORES: readonly Store[] = ['member here', 'member elsewhere', 'no such user'];
495+
496+
/** May write both grant tables; reads its own `sys_user` row only. Not a member of org_a itself. */
497+
const OWN_ROW_ADMIN_A = { userId: 'u_admin_a', positions: [], permissions: ['qa_admin_own_user_row'], tenantId: ORG_A };
498+
/** The same grants, held by a member of org_a. */
499+
const OWN_ROW_MEMBER_A = { userId: 'u_member_a', positions: [], permissions: ['qa_admin_own_user_row'], tenantId: ORG_A };
500+
501+
/** `boot`, then the holder as `store` says: a `sys_user` row and one membership, or nothing at all. */
502+
async function bootWith(store: Store, opts: BootOptions = { walled: true }) {
503+
const h = await boot(opts);
504+
if (store !== 'no such user') {
505+
await provisionRigUsers(h.engine, [HOLDER]);
506+
const org = store === 'member here' ? ORG_A : ORG_B;
507+
await h.engine.insert(
508+
'sys_member', { id: 'm_x', user_id: HOLDER, organization_id: org, role: 'member' },
509+
{ context: { isSystem: true, tenantId: org } } as any,
510+
);
511+
}
512+
return h;
513+
}
514+
515+
/** Everything an HTTP door serves for this throw: status, codes, message and every field entry. */
516+
const wireOf = (e: unknown): string => JSON.stringify(resolveThrownHttpError(e));
517+
518+
/** One answer across the stores, and that answer the reference refusal at `user_id`. */
519+
function expectOneHolderAnswer(answers: ReadonlyMap<Store, unknown>) {
520+
const wires = STORES.map((s) => wireOf(answers.get(s)));
521+
for (const [i, s] of STORES.entries()) expect(wires[i], s).toBe(wires[0]);
522+
const env = envelopeOf(answers.get(STORES[0]));
523+
expectMembershipRefusal(answers.get(STORES[0]));
524+
expect(env.fields[0]?.constraint?.target).toBe('sys_user');
525+
}
526+
527+
it('CONTROL: the caller reads its own sys_user row and never the holder, which exists in two of the stores', async () => {
528+
for (const store of STORES) {
529+
const h = await bootWith(store);
530+
const seen = await h.engine.find('sys_user', { where: { id: { $in: ['u_admin_a', HOLDER] } }, context: OWN_ROW_ADMIN_A } as any);
531+
expect((seen as any[]).map((r) => r.id), store).toEqual(['u_admin_a']);
532+
expect(await rowsOf(h, 'sys_user', { id: HOLDER }), store).toHaveLength(store === 'no such user' ? 0 : 1);
533+
expect(await rowsOf(h, 'sys_member', { user_id: HOLDER }), store).toHaveLength(store === 'no such user' ? 0 : 1);
534+
}
535+
});
536+
537+
for (const [object, row] of [
538+
['sys_user_position', { position: 'qa_rep' }],
539+
['sys_user_permission_set', { permission_set: 'qa_rep_set' }],
540+
] as const) {
541+
it(`${object}: an insert naming it gets one answer in every store, and stores nothing`, async () => {
542+
const answers = new Map<Store, unknown>();
543+
for (const store of STORES) {
544+
const h = await bootWith(store);
545+
answers.set(store, await refusalOf(() => h.engine.insert(
546+
object, { user_id: HOLDER, ...row }, { context: OWN_ROW_ADMIN_A } as any,
547+
)));
548+
expect(await rowsOf(h, object, { user_id: HOLDER }), store).toHaveLength(0);
549+
}
550+
expectOneHolderAnswer(answers);
551+
});
552+
553+
it(`${object}: an update by id that moves the holder to it gets one answer in every store; the stored row is untouched`, async () => {
554+
const answers = new Map<Store, unknown>();
555+
for (const store of STORES) {
556+
const h = await bootWith(store);
557+
await h.engine.insert(
558+
object, { id: 'g1', user_id: 'u_member_a', organization_id: ORG_A, ...row },
559+
{ context: { isSystem: true, tenantId: ORG_A } } as any,
560+
);
561+
answers.set(store, await refusalOf(() => h.engine.update(
562+
object, { id: 'g1', user_id: HOLDER }, { context: OWN_ROW_ADMIN_A } as any,
563+
)));
564+
const [stored] = await rowsOf(h, object, { id: 'g1' });
565+
expect(stored?.user_id, store).toBe('u_member_a');
566+
}
567+
expectOneHolderAnswer(answers);
568+
});
569+
}
570+
571+
it('a holder the caller CAN read keeps the membership refusal; a readable member is admitted', async () => {
572+
const h = await bootWith('member elsewhere');
573+
// The caller's own row is readable, and the caller is no member of org_a.
574+
const err = await refusalOf(() => h.engine.insert(
575+
'sys_user_position', { user_id: 'u_admin_a', position: 'qa_rep' }, { context: OWN_ROW_ADMIN_A } as any,
576+
));
577+
expectMembershipRefusal(err);
578+
expect(envelopeOf(err).fields[0]?.constraint?.target).toBe('sys_member');
579+
expect(await rowsOf(h, 'sys_user_position', { user_id: 'u_admin_a' })).toHaveLength(0);
580+
581+
await h.engine.insert(
582+
'sys_user_position', { user_id: 'u_member_a', position: 'qa_rep' }, { context: OWN_ROW_MEMBER_A } as any,
583+
);
584+
const [row] = await rowsOf(h, 'sys_user_position', { user_id: 'u_member_a' });
585+
expect(row?.organization_id).toBe(ORG_A);
586+
});
587+
588+
it('the refusal set is unchanged: an update that moves only the organization still judges a holder the caller cannot read on membership', async () => {
589+
const h = await bootWith('member here', {});
590+
await h.engine.insert(
591+
'sys_user_position', { id: 'mv', user_id: HOLDER, position: 'qa_rep', organization_id: ORG_A },
592+
{ context: { isSystem: true, tenantId: ORG_A } } as any,
593+
);
594+
const err = await refusalOf(() => h.engine.update(
595+
'sys_user_position', { id: 'mv', organization_id: ORG_B }, { context: OWN_ROW_ADMIN_A } as any,
596+
));
597+
expectMembershipRefusal(err);
598+
expect((await rowsOf(h, 'sys_user_position', { id: 'mv' }))[0]?.organization_id).toBe(ORG_A);
599+
600+
// Positive control: once the holder is a member there, the same move lands.
601+
await h.engine.insert(
602+
'sys_member', { id: 'm_x_b', user_id: HOLDER, organization_id: ORG_B, role: 'member' },
603+
{ context: { isSystem: true, tenantId: ORG_B } } as any,
604+
);
605+
await h.engine.update('sys_user_position', { id: 'mv', organization_id: ORG_B }, { context: OWN_ROW_ADMIN_A } as any);
606+
expect((await rowsOf(h, 'sys_user_position', { id: 'mv' }))[0]?.organization_id).toBe(ORG_B);
607+
});
608+
609+
it('under the shipped user read, a member is admitted, and a user outside the organization gets the answer a missing user gets', async () => {
610+
const orgAPeers = (store: Store) => [
611+
'u_admin_a', ...MEMBERSHIPS.filter((m) => m.organization_id === ORG_A).map((m) => m.user_id),
612+
...(store === 'member here' ? [HOLDER] : []),
613+
];
614+
const caller = (store: Store) => ({
615+
userId: 'u_admin_a', positions: [], permissions: ['qa_admin_shipped_user_read'], tenantId: ORG_A,
616+
org_user_ids: orgAPeers(store),
617+
});
618+
const answers = new Map<Store, unknown>();
619+
for (const store of STORES) {
620+
const h = await bootWith(store);
621+
const write = () => h.engine.insert(
622+
'sys_user_position', { user_id: HOLDER, position: 'qa_rep' }, { context: caller(store) } as any,
623+
);
624+
if (store === 'member here') {
625+
await write();
626+
expect(await rowsOf(h, 'sys_user_position', { user_id: HOLDER })).toHaveLength(1);
627+
} else {
628+
answers.set(store, await refusalOf(write));
629+
}
630+
}
631+
expect(wireOf(answers.get('member elsewhere'))).toBe(wireOf(answers.get('no such user')));
632+
expectMembershipRefusal(answers.get('no such user'));
633+
});
634+
635+
it('a composition with no sys_user object leaves the reference check nothing to read: membership is judged here', async () => {
636+
const h = await boot({ walled: true, users: false });
637+
expectMembershipRefusal(await refusalOf(() => h.engine.insert(
638+
'sys_user_position', { user_id: 'u_member_b', position: 'qa_rep' }, { context: ADMIN_A } as any,
639+
)));
640+
expect(await rowsOf(h, 'sys_user_position', { user_id: 'u_member_b' })).toHaveLength(0);
641+
await h.engine.insert('sys_user_position', { user_id: 'u_member_a', position: 'qa_rep' }, { context: ADMIN_A } as any);
642+
expect(await rowsOf(h, 'sys_user_position', { user_id: 'u_member_a' })).toHaveLength(1);
643+
});
644+
});
645+
449646
// ---------------------------------------------------------------------------
450647
// Registration
451648
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)