|
41 | 41 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
42 | 42 | import type { PermissionSet } from '@objectstack/spec/security'; |
43 | 43 | import { parsePlatformAdminEmails } from '@objectstack/core'; |
| 44 | +import { TENANT_SCOPE_FIELD_DEF } from '@objectstack/metadata-core'; |
44 | 45 | import { SecurityPlugin } from './security-plugin.js'; |
45 | 46 | import { RLS_DENY_FILTER } from './rls-compiler.js'; |
46 | 47 | import { |
@@ -91,16 +92,19 @@ async function boot(opts: { |
91 | 92 | users?: Record<string, any>; |
92 | 93 | tenancy?: { posture: string }; |
93 | 94 | permissionSets?: PermissionSet[]; |
| 95 | + /** The one object schema served; {@link TENANT_SCHEMA} unless a case names another. */ |
| 96 | + schema?: Record<string, unknown>; |
94 | 97 | } = {}) { |
95 | 98 | const users = opts.users ?? {}; |
| 99 | + const schema = opts.schema ?? TENANT_SCHEMA; |
96 | 100 | const findOne = vi.fn(async (object: string, o: any) => |
97 | 101 | object === 'sys_user' ? (users[o?.where?.id] ?? null) : null, |
98 | 102 | ); |
99 | 103 | const warn = vi.fn(); |
100 | 104 | const services: Record<string, unknown> = { |
101 | 105 | manifest: { register: vi.fn() }, |
102 | | - objectql: { registerMiddleware: vi.fn(), getSchema: () => TENANT_SCHEMA, findOne }, |
103 | | - metadata: { get: async () => TENANT_SCHEMA, list: async () => opts.permissionSets ?? [PLAIN_MEMBER] }, |
| 106 | + objectql: { registerMiddleware: vi.fn(), getSchema: () => schema, findOne }, |
| 107 | + metadata: { get: async () => schema, list: async () => opts.permissionSets ?? [PLAIN_MEMBER] }, |
104 | 108 | 'org-scoping': { name: 'com.objectstack.org-scoping' }, |
105 | 109 | }; |
106 | 110 | if (opts.tenancy) services['tenancy'] = opts.tenancy; |
@@ -310,6 +314,106 @@ describe('[#12974] audit — the ruled floor', () => { |
310 | 314 | }); |
311 | 315 | }); |
312 | 316 |
|
| 317 | +describe('[ADR-0015 §10] an external object that states no organization scope reads as empty for every caller, the platform owner included', () => { |
| 318 | + /** |
| 319 | + * What the registry hands the plugin for an external object registered after |
| 320 | + * the boot that states nothing: its remote columns plus the platform's |
| 321 | + * injected `organization_id` anchor (the spread `applySystemFields` makes), |
| 322 | + * which is no declaration — `federated-tenant-layer0.test.ts` pins the |
| 323 | + * member's half of the same verdict. |
| 324 | + */ |
| 325 | + const UNDECLARED_EXTERNAL = { |
| 326 | + name: 'ext_ledger', |
| 327 | + external: { remoteName: 'ledger' }, |
| 328 | + fields: { |
| 329 | + organization_id: { ...TENANT_SCOPE_FIELD_DEF }, |
| 330 | + name: { type: 'text', label: 'Name' }, |
| 331 | + }, |
| 332 | + }; |
| 333 | + /** The same remote, declaring its own `organization_id`: walled by organization. */ |
| 334 | + const DECLARED_WALLED_EXTERNAL = { |
| 335 | + name: 'ext_ledger', |
| 336 | + external: { remoteName: 'ledger' }, |
| 337 | + fields: { |
| 338 | + organization_id: { type: 'text', label: 'Organization (remote column)' }, |
| 339 | + name: { type: 'text', label: 'Name' }, |
| 340 | + }, |
| 341 | + }; |
| 342 | + const OWNER_ROW = { u_owner: { id: 'u_owner', email: OWNER_EMAIL, email_verified: true } }; |
| 343 | + const MEMBER = { userId: 'u_m', email: 'member@corp.example' }; |
| 344 | + const layered = (plugin: unknown, ctx: unknown) => |
| 345 | + (plugin as any).computeLayeredRlsFilter([PLAIN_MEMBER], 'ext_ledger', 'find', ctx); |
| 346 | + const bypassEvents = (warn: ReturnType<typeof vi.fn>) => |
| 347 | + warn.mock.calls.filter(([, meta]) => meta?.event === PLATFORM_OWNER_WALL_BYPASS_EVENT); |
| 348 | + |
| 349 | + it('`isolated`: the verified owner reads the deny sentinel, the recorded verdict is `deny`, no crossing is logged and the owner probe is never asked', async () => { |
| 350 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 351 | + const { plugin, warn, findOne } = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL }); |
| 352 | + // The spec's frozen deny sentinel, by identity (ADR-0139 D2 step 2). |
| 353 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx())).toBe(RLS_DENY_FILTER); |
| 354 | + const split = await layered(plugin, sessionCtx()); |
| 355 | + expect(split.layer0).toBe(RLS_DENY_FILTER); |
| 356 | + // The verdict as RECORDED for the engine: the wall still stands, so `deny`, never `none`. |
| 357 | + expect(split.tenantLayer0Verdict).toEqual({ kind: 'deny' }); |
| 358 | + expect(split.crossedByPlatformAdmin).toBe(false); |
| 359 | + expect(bypassEvents(warn)).toHaveLength(0); |
| 360 | + // The verdict judges the object, not the reader: no `sys_user` row is read to reach it. |
| 361 | + expect(findOne.mock.calls.filter(([object]) => object === 'sys_user')).toHaveLength(0); |
| 362 | + }); |
| 363 | + |
| 364 | + it('`group`: the verified owner reads the deny sentinel there too', async () => { |
| 365 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 366 | + const { plugin, warn } = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL, tenancy: { posture: 'group' } }); |
| 367 | + const ctx = sessionCtx({ accessible_org_ids: ['org-1', 'org-2'] }); |
| 368 | + expect(await (plugin as any).getReadFilter('ext_ledger', ctx)).toBe(RLS_DENY_FILTER); |
| 369 | + expect((await layered(plugin, ctx)).tenantLayer0Verdict).toEqual({ kind: 'deny' }); |
| 370 | + expect(bypassEvents(warn)).toHaveLength(0); |
| 371 | + }); |
| 372 | + |
| 373 | + it('an org-less verified owner reads the deny sentinel on it as well: the crossing of the org-less deny does not reach this one', async () => { |
| 374 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 375 | + const { plugin } = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL }); |
| 376 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx({ tenantId: undefined }))).toBe(RLS_DENY_FILTER); |
| 377 | + }); |
| 378 | + |
| 379 | + it('CONTROL: the verified owner still crosses the wall on an external object that declares its own `organization_id`', async () => { |
| 380 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 381 | + const { plugin, warn } = await boot({ users: OWNER_ROW, schema: DECLARED_WALLED_EXTERNAL }); |
| 382 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx())).toBeUndefined(); |
| 383 | + const split = await layered(plugin, sessionCtx()); |
| 384 | + expect(split.layer0).toBeNull(); |
| 385 | + expect(split.tenantLayer0Verdict).toEqual({ kind: 'none' }); |
| 386 | + expect(bypassEvents(warn).length).toBeGreaterThan(0); |
| 387 | + // …and a member of that object keeps the organization equality. |
| 388 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx(MEMBER))).toEqual({ organization_id: 'org-1' }); |
| 389 | + }); |
| 390 | + |
| 391 | + it('CONTROL: the verified owner still crosses the wall, and the org-less deny, on a local object', async () => { |
| 392 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 393 | + const { plugin } = await boot({ users: OWNER_ROW }); |
| 394 | + expect(await readFilter(plugin, sessionCtx())).toBeUndefined(); |
| 395 | + expect(await readFilter(plugin, sessionCtx({ tenantId: undefined }))).toBeUndefined(); |
| 396 | + }); |
| 397 | + |
| 398 | + it('CONTROL: a member is unchanged — the deny sentinel, under `isolated` and `group`', async () => { |
| 399 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 400 | + const { plugin } = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL }); |
| 401 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx(MEMBER))).toBe(RLS_DENY_FILTER); |
| 402 | + const group = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL, tenancy: { posture: 'group' } }); |
| 403 | + expect( |
| 404 | + await (group.plugin as any).getReadFilter('ext_ledger', sessionCtx({ ...MEMBER, accessible_org_ids: ['org-1'] })), |
| 405 | + ).toBe(RLS_DENY_FILTER); |
| 406 | + }); |
| 407 | + |
| 408 | + it('CONTROL: `single` is unchanged — no wall, so the owner and a member both read it unwalled', async () => { |
| 409 | + process.env.OS_PLATFORM_OWNER_EMAIL = OWNER_EMAIL; |
| 410 | + const { plugin } = await boot({ users: OWNER_ROW, schema: UNDECLARED_EXTERNAL, tenancy: { posture: 'single' } }); |
| 411 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx())).toBeUndefined(); |
| 412 | + expect(await (plugin as any).getReadFilter('ext_ledger', sessionCtx(MEMBER))).toBeUndefined(); |
| 413 | + expect((await layered(plugin, sessionCtx())).tenantLayer0Verdict).toEqual({ kind: 'none' }); |
| 414 | + }); |
| 415 | +}); |
| 416 | + |
313 | 417 | describe('[#12974] the shared row predicate (the per-request derivation’s twin)', () => { |
314 | 418 | it('matchesDeclaredOwnerEmail — canonical comparison: trimmed, case-insensitive', () => { |
315 | 419 | expect(matchesDeclaredOwnerEmail({ email: 'Operator@Corp.Example' }, SOLO)).toBe(true); |
|
0 commit comments