Skip to content

Commit e86302c

Browse files
committed
test(plugin-security): pin the delegated answer about a user the caller cannot read in three postures
The write gate's four direct-grant shapes, security/explain and the effective-access read answer each of four subject states as an id naming no user; a readable subject inside the scope is served, and a tenant-level or organization administrator still removes the stored grant. Claude-Session: https://claude.ai/code/session_017xRBcT1w3zpekijraHJE4f Co-authored-by: Claude <noreply@anthropic.com>
1 parent 24e3a48 commit e86302c

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

‎packages/plugins/plugin-security/src/delegated-admin-own-name-set.posture.test.ts‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,3 +768,147 @@ describe("[ADR-0105 D6; ADR-0091 D3] a delegated business-unit walk never starts
768768
});
769769
}
770770
});
771+
772+
/**
773+
* [ADR-0090 D12; ruling on #26059, Q3] A delegated answer about a user the
774+
* caller cannot read does not depend on that user's existence or memberships.
775+
* One subject id in four states — no user; a user holding no business-unit
776+
* membership; a user whose unit is outside the delegate's scope; a user whose
777+
* unit is inside it whom the delegate cannot read — asked of every delegated
778+
* door that judges a target user: the write gate's four direct-grant shapes
779+
* (an insert, an update pointing a stored grant at the subject, an update of
780+
* the subject's stored grant leaving its `user_id` as stored, a removal of
781+
* it), `security/explain` for the subject, and the subject's effective access.
782+
* Every state answers exactly what the id naming no user answers; the CONTROL,
783+
* a user inside the scope whom the delegate reads, is served at every door.
784+
*
785+
* A user the delegate cannot read is one its context may not read: `sys_user`'s
786+
* row scope (`id == current_user.id || id in current_user.org_user_ids`), the
787+
* same in every posture. The write shapes are asked of the gate itself, as
788+
* `gateInsert` above asks it: the delegate holds no read of
789+
* `sys_user_permission_set`, so for a stored grant the write door would answer
790+
* the read door's refusal instead (`readDoorAnswerForGateRefusal`).
791+
*
792+
* Revocation does not leave the organization: a tenant-level administrator and
793+
* the organization administrator still remove the stored grant of the subject
794+
* the delegate cannot read, through the write door itself.
795+
*/
796+
describe("[ADR-0090 D12] a delegated answer about a user the caller cannot read does not depend on that user's existence or memberships — three postures", () => {
797+
const SUBJECT = 'u_subject';
798+
const STATES = [
799+
'an id naming no user',
800+
'a user with no business-unit membership',
801+
'a user whose business unit is outside the scope',
802+
'a user with a business unit inside the scope whom the delegate cannot read',
803+
] as const;
804+
const UNREADABLE_INSIDE = STATES[3];
805+
const CONTROL = 'a user with a business unit inside the scope whom the delegate reads';
806+
type State = (typeof STATES)[number] | typeof CONTROL;
807+
808+
/** The delegate's context: the subject is among the users it may read exactly when the subject is a user it reads. */
809+
const delegateFor = (state: State) => ({
810+
...NARROW,
811+
org_user_ids: state === 'an id naming no user' || state === UNREADABLE_INSIDE ? ORG_USER_IDS : [...ORG_USER_IDS, SUBJECT],
812+
});
813+
814+
/** A fresh rig with the subject in `state`, its stored grant `g_subject`, and `g_target`, a grant of a user inside the scope. */
815+
async function bootWith(posture: Posture, state: State) {
816+
const h = await boot(posture);
817+
const sys = { context: { isSystem: true, tenantId: ORG_A } } as any;
818+
if (state !== 'an id naming no user') {
819+
await h.engine.insert('sys_user', { id: SUBJECT, email: `${SUBJECT}@example.test`, name: SUBJECT }, sys);
820+
await h.engine.insert('sys_member', { id: `m_${SUBJECT}`, user_id: SUBJECT, organization_id: ORG_A, role: 'member' }, sys);
821+
}
822+
const unit = state === 'a user whose business unit is outside the scope' ? 'bu_hq'
823+
: state === UNREADABLE_INSIDE || state === CONTROL ? 'bu_east_field'
824+
: null;
825+
if (unit) {
826+
await h.engine.insert('sys_business_unit_member',
827+
{ id: 'bum_subject', user_id: SUBJECT, business_unit_id: unit, organization_id: ORG_A }, sys);
828+
}
829+
await h.engine.insert('sys_user_permission_set', [
830+
{ id: 'g_subject', user_id: SUBJECT, permission_set: 'field_user', organization_id: ORG_A },
831+
{ id: 'g_target', user_id: 'u_target', permission_set: 'field_user', organization_id: ORG_A },
832+
], sys);
833+
return h;
834+
}
835+
836+
/** Everything a caller is told: the envelope both HTTP doors answer with, the class, the message and the details. */
837+
async function answerOf(run: () => Promise<unknown>) {
838+
try {
839+
await run();
840+
return { status: 200 };
841+
} catch (e: any) {
842+
const r = resolveThrownHttpError(e);
843+
return { status: r.status, code: r.code, class: e?.constructor?.name, message: e?.message, details: e?.details };
844+
}
845+
}
846+
847+
/** Every delegated door's answer about the subject, asked by the delegate. */
848+
async function answersFor(posture: Posture, state: State) {
849+
const h = await bootWith(posture, state);
850+
const caller = delegateFor(state);
851+
const gate = (h.plugin as any).delegatedAdminGate;
852+
const write = (operation: string, data: Record<string, unknown> | undefined, id?: string) =>
853+
answerOf(() => gate.assert({
854+
object: 'sys_user_permission_set', operation, data, ...(id ? { options: { where: { id } } } : {}), context: caller,
855+
}));
856+
return {
857+
insert: await write('insert', { user_id: SUBJECT, permission_set: 'field_user' }),
858+
'update pointing a stored grant at the subject': await write('update', { id: 'g_target', user_id: SUBJECT }),
859+
"update of the subject's stored grant, its user_id left as stored": await write('update', { id: 'g_subject', reason: 'renewed' }),
860+
"removal of the subject's stored grant": await write('delete', undefined, 'g_subject'),
861+
'security/explain for the subject': await answerOf(() =>
862+
h.plugin.explainAccessForCaller({ object: 'sys_business_unit', operation: 'read', userId: SUBJECT }, caller)),
863+
"the subject's effective access": await answerOf(() => h.plugin.describeEffectiveAccess(SUBJECT, caller)),
864+
};
865+
}
866+
867+
for (const posture of POSTURES) {
868+
describe(`posture '${posture}'`, () => {
869+
it('ground truth: the delegate reads the subject exactly in the states where it is a user the delegate may read', async () => {
870+
for (const state of [...STATES, CONTROL]) {
871+
const h = await bootWith(posture, state);
872+
const rows = await h.engine.find('sys_user', { where: { id: SUBJECT }, fields: ['id'], context: delegateFor(state) } as any);
873+
const reads = (Array.isArray(rows) ? rows : []).some((r: any) => r.id === SUBJECT);
874+
expect(reads, state).toBe(state !== 'an id naming no user' && state !== UNREADABLE_INSIDE);
875+
}
876+
});
877+
878+
it(`the ${STATES.length} states get, at every door, the answer the id naming no user gets`, async () => {
879+
const answers = [];
880+
for (const state of STATES) answers.push(await answersFor(posture, state));
881+
for (const [i, answer] of answers.entries()) expect(answer, STATES[i]).toEqual(answers[0]);
882+
// The one answer: the write gate's scope violation, and each explain door's refusal of a user the caller may not describe.
883+
const [none] = answers;
884+
for (const [door, answer] of Object.entries(none)) {
885+
const expected = door.startsWith('security/explain') || door.startsWith("the subject's effective")
886+
? [403, 'PERMISSION_DENIED', 'PermissionDeniedError']
887+
: [403, 'DELEGATION_SCOPE_VIOLATION', 'DelegationScopeViolationError'];
888+
expect([answer.status, (answer as any).code, (answer as any).class], door).toEqual(expected);
889+
}
890+
expect((none['security/explain for the subject'] as any).details)
891+
.toEqual({ object: 'sys_business_unit', operation: 'read', targetUserId: SUBJECT });
892+
expect((none["the subject's effective access"] as any).details).toBeUndefined();
893+
});
894+
895+
it('CONTROL: the same subject with a business unit inside the scope, read by the delegate, is served at every door', async () => {
896+
const answers = await answersFor(posture, CONTROL);
897+
for (const [door, answer] of Object.entries(answers)) expect(answer, door).toEqual({ status: 200 });
898+
});
899+
900+
it("CONTROL: a tenant-level administrator and the organization administrator still remove the stored grant of the subject the delegate cannot read, through the write door", async () => {
901+
for (const [who, context] of [
902+
['tenant-level administrator', delegateCtx('u_org_admin', 'admin_full_access')],
903+
['organization administrator', delegateCtx('u_org_admin', 'organization_admin')],
904+
] as const) {
905+
const h = await bootWith(posture, UNREADABLE_INSIDE);
906+
const removed = await answerOf(() => h.engine.delete('sys_user_permission_set', { where: { id: 'g_subject' }, context } as any));
907+
expect(removed, who).toEqual({ status: 200 });
908+
const left = await h.engine.find('sys_user_permission_set', { where: { id: 'g_subject' }, context: SYS });
909+
expect(Array.isArray(left) ? left : [], who).toEqual([]);
910+
}
911+
});
912+
});
913+
}
914+
});

0 commit comments

Comments
 (0)