|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A package-declared sharing rule grants in an organization created AFTER the |
| 5 | + * server booted, exactly as in one present at boot — without a restart. |
| 6 | + * |
| 7 | + * ## The defect this pins |
| 8 | + * |
| 9 | + * The per-record rule hooks (`bindRuleHooks`) bind on the objects of the rules |
| 10 | + * the service serves. Under a walled posture a declared rule is served once per |
| 11 | + * organization (ADR-0131 D2), so a kernel that booted with no organization |
| 12 | + * served no declared rule and bound no hook on its object. The middleware that |
| 13 | + * serves a newly created organization (`addDeclaredRuleScope`) added the scope |
| 14 | + * and bound nothing, so a record written in that organization reached no |
| 15 | + * hook: no `sys_record_share` row, and the recipient could not read it until |
| 16 | + * the next boot. That is a fresh environment's first sign-up. |
| 17 | + * |
| 18 | + * The hooks now bind on every object a DECLARATION names, whatever |
| 19 | + * organizations it is served in at the moment of binding; the organization is |
| 20 | + * resolved when a record is evaluated. |
| 21 | + * |
| 22 | + * ## Tenant isolation, both directions |
| 23 | + * |
| 24 | + * Binding on the object is organization-agnostic; evaluation is not. A rule of |
| 25 | + * organization A must never grant on a record of organization B — neither an |
| 26 | + * organization-authored row nor A's projection of the declared rule. The |
| 27 | + * second case pins both, beside the control that proves the organization-A row |
| 28 | + * is live at all. |
| 29 | + * |
| 30 | + * Real stack, ONE boot: an `ObjectKernel` with `ObjectQLPlugin` over |
| 31 | + * better-sqlite3 and `SharingServicePlugin`, under the `isolated` posture — |
| 32 | + * the `tenancy` service the sharing plugin reads, and the engine's posture |
| 33 | + * accessor the security plugin hands it in a deployment, so a rule's criteria |
| 34 | + * sweep runs inside its organization at the driver. Every organization is |
| 35 | + * created after `bootstrap()`. |
| 36 | + */ |
| 37 | + |
| 38 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 39 | +import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core'; |
| 40 | +import { ObjectQL, ObjectQLPlugin } from '@objectstack/objectql'; |
| 41 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 42 | +import { SysMember, SysOrganization, SysUser } from '@objectstack/platform-objects/identity'; |
| 43 | +import { SharingServicePlugin } from './sharing-plugin.js'; |
| 44 | + |
| 45 | +const TASK = 'qa_lateorg_task'; |
| 46 | +const RULE = 'qa_lateorg_task_assignee'; |
| 47 | +const ORG_A = 'org_lateorg_a'; |
| 48 | +const ORG_B = 'org_lateorg_b'; |
| 49 | +const CREATOR_A = 'usr_lateorg_creator_a'; |
| 50 | +const ASSIGNEE_A = 'usr_lateorg_assignee_a'; |
| 51 | +const CREATOR_B = 'usr_lateorg_creator_b'; |
| 52 | +const ASSIGNEE_B = 'usr_lateorg_assignee_b'; |
| 53 | +/** The recipient of organization A's own authored rule. */ |
| 54 | +const WATCHER_A = 'usr_lateorg_watcher_a'; |
| 55 | +const PEOPLE = [CREATOR_A, ASSIGNEE_A, CREATOR_B, ASSIGNEE_B, WATCHER_A]; |
| 56 | + |
| 57 | +const SYSTEM = { isSystem: true } as const; |
| 58 | +const as = (userId: string, organizationId: string) => ({ userId, tenantId: organizationId, positions: [], permissions: [] }); |
| 59 | + |
| 60 | +function sqliteDriverPlugin(): Plugin { |
| 61 | + return { |
| 62 | + name: 'test.driver.sqlite', |
| 63 | + type: 'standard', |
| 64 | + version: '1.0.0', |
| 65 | + async init(ctx: PluginContext) { |
| 66 | + ctx.registerService( |
| 67 | + 'driver.default', |
| 68 | + new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), |
| 69 | + ); |
| 70 | + }, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +/** The `isolated` posture, read by the sharing plugin and the engine alike. */ |
| 75 | +function isolatedPosturePlugin(): Plugin { |
| 76 | + return { |
| 77 | + name: 'test.lateorg.posture', |
| 78 | + type: 'standard', |
| 79 | + version: '1.0.0', |
| 80 | + dependencies: ['com.objectstack.engine.objectql'], |
| 81 | + async init(ctx: PluginContext) { |
| 82 | + ctx.registerService('tenancy', { posture: 'isolated' }); |
| 83 | + ctx.getService<ObjectQL>('objectql').setTenancyPostureProvider(() => 'isolated'); |
| 84 | + }, |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +/** The identity objects, a private task object, and the package's declared `field` rule on it. */ |
| 89 | +function packagePlugin(): Plugin { |
| 90 | + return { |
| 91 | + name: 'test.lateorg.package', |
| 92 | + type: 'standard', |
| 93 | + version: '1.0.0', |
| 94 | + dependencies: ['com.objectstack.engine.objectql'], |
| 95 | + async init(ctx: PluginContext) { |
| 96 | + ctx.getService<{ register(m: unknown): unknown }>('manifest').register({ |
| 97 | + id: 'test.declared-rule-late-organization', |
| 98 | + namespace: 'qa_lateorg', |
| 99 | + version: '1.0.0', |
| 100 | + type: 'plugin', |
| 101 | + name: 'Declared rule, late organization', |
| 102 | + objects: [ |
| 103 | + SysUser, |
| 104 | + SysMember, |
| 105 | + SysOrganization, |
| 106 | + { |
| 107 | + name: TASK, |
| 108 | + label: 'Task', |
| 109 | + sharingModel: 'private', |
| 110 | + fields: { |
| 111 | + title: { type: 'text', label: 'Title' }, |
| 112 | + owner_id: { type: 'lookup', reference: 'sys_user', label: 'Owner' }, |
| 113 | + assignee_id: { type: 'lookup', reference: 'sys_user', label: 'Assignee' }, |
| 114 | + // The tenant column: the wall and a rule's criteria sweep scope by it. |
| 115 | + organization_id: { type: 'lookup', reference: 'sys_organization', label: 'Organization' }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + ], |
| 119 | + sharingRules: [ |
| 120 | + { |
| 121 | + name: RULE, |
| 122 | + label: 'Task assignee', |
| 123 | + object: TASK, |
| 124 | + type: 'criteria', |
| 125 | + condition: 'record.assignee_id != null', |
| 126 | + sharedWith: { type: 'field', value: 'assignee_id' }, |
| 127 | + accessLevel: 'edit', |
| 128 | + }, |
| 129 | + ], |
| 130 | + }); |
| 131 | + }, |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +type Grant = { record_id: string; recipient_id: string; access_level: string; source: string; source_id: string; organization_id?: string | null }; |
| 136 | + |
| 137 | +describe('a declared sharing rule grants in an organization created after boot (one boot, walled posture)', () => { |
| 138 | + let kernel: ObjectKernel | undefined; |
| 139 | + let ql: ObjectQL; |
| 140 | + |
| 141 | + beforeAll(async () => { |
| 142 | + kernel = new ObjectKernel({ logger: { level: 'silent' } } as never); |
| 143 | + await kernel.use(sqliteDriverPlugin()); |
| 144 | + await kernel.use(new ObjectQLPlugin()); |
| 145 | + await kernel.use(isolatedPosturePlugin()); |
| 146 | + await kernel.use(packagePlugin()); |
| 147 | + await kernel.use(new SharingServicePlugin({ registerShareLinkRoutes: false })); |
| 148 | + await kernel.bootstrap(); |
| 149 | + ql = kernel.getService<ObjectQL>('objectql'); |
| 150 | + for (const userId of PEOPLE) { |
| 151 | + await ql.insert('sys_user', { id: userId, name: userId, email: `${userId}@lateorg.test`, email_verified: true }, { context: SYSTEM } as never); |
| 152 | + } |
| 153 | + }, 120_000); |
| 154 | + |
| 155 | + afterAll(async () => { |
| 156 | + if (kernel) await Promise.race([kernel.shutdown(), new Promise<void>((r) => setTimeout(r, 10_000))]); |
| 157 | + }, 30_000); |
| 158 | + |
| 159 | + /** Every rule grant on one record, read unscoped. */ |
| 160 | + const grantsOn = async (recordId: string): Promise<Grant[]> => |
| 161 | + (await ql.find('sys_record_share', { where: { object_name: TASK, record_id: recordId }, context: SYSTEM } as never)) as Grant[]; |
| 162 | + const readAs = async (userId: string, organizationId: string, id: string) => |
| 163 | + (await ql.findOne(TASK, { where: { id }, context: as(userId, organizationId) } as never)) as Record<string, unknown> | null; |
| 164 | + const createOrganization = async (id: string) => { |
| 165 | + await ql.insert('sys_organization', { id, name: id, slug: id.replace(/_/g, '-') }, { context: SYSTEM } as never); |
| 166 | + }; |
| 167 | + const createTask = async (id: string, organizationId: string, creator: string, assignee: string, title = `task ${id}`) => { |
| 168 | + await ql.insert(TASK, { id, title, assignee_id: assignee }, { context: as(creator, organizationId) } as never); |
| 169 | + // The record is stamped with its organization, so the isolation below is about organizations at all. |
| 170 | + const stored = (await ql.find(TASK, { where: { id }, context: SYSTEM } as never)) as Array<Record<string, unknown>>; |
| 171 | + expect(stored[0]?.organization_id).toBe(organizationId); |
| 172 | + return id; |
| 173 | + }; |
| 174 | + |
| 175 | + it('grants the assignee edit on a record of the first organization, created after bootstrap, with no rule write and no restart', async () => { |
| 176 | + await createOrganization(ORG_A); |
| 177 | + const id = await createTask('task_lateorg_a1', ORG_A, CREATOR_A, ASSIGNEE_A); |
| 178 | + |
| 179 | + expect(await grantsOn(id)).toEqual([ |
| 180 | + expect.objectContaining({ recipient_id: ASSIGNEE_A, access_level: 'edit', source: 'rule', source_id: RULE }), |
| 181 | + ]); |
| 182 | + expect(await readAs(ASSIGNEE_A, ORG_A, id)).toMatchObject({ id, assignee_id: ASSIGNEE_A }); |
| 183 | + // Under-grant fixed, no over-grant: a member the rule does not name still reads nothing. |
| 184 | + expect(await readAs(WATCHER_A, ORG_A, id)).toBeNull(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('keeps every grant inside its own organization: organization A\'s rules grant nothing on organization B\'s records', async () => { |
| 188 | + await createOrganization(ORG_B); |
| 189 | + // Organization A authors a rule of its own on the same object. |
| 190 | + await ql.insert('sys_sharing_rule', { |
| 191 | + name: 'qa_lateorg_a_shared_to_watcher', |
| 192 | + label: 'Shared tasks to the watcher', |
| 193 | + object_name: TASK, |
| 194 | + criteria_json: JSON.stringify({ title: 'shared' }), |
| 195 | + recipient_type: 'user', |
| 196 | + recipient_id: WATCHER_A, |
| 197 | + access_level: 'read', |
| 198 | + active: true, |
| 199 | + organization_id: ORG_A, |
| 200 | + }, { context: { isSystem: true, tenantId: ORG_A } } as never); |
| 201 | + |
| 202 | + // Control: the organization-A row is live on organization A's records. |
| 203 | + const inA = await createTask('task_lateorg_a2', ORG_A, CREATOR_A, ASSIGNEE_A, 'shared'); |
| 204 | + expect((await grantsOn(inA)).map((g) => g.recipient_id).sort()).toEqual([ASSIGNEE_A, WATCHER_A].sort()); |
| 205 | + |
| 206 | + // Organization B's record matches A's criteria and names an assignee. |
| 207 | + const inB = await createTask('task_lateorg_b1', ORG_B, CREATOR_B, ASSIGNEE_B, 'shared'); |
| 208 | + const grants = await grantsOn(inB); |
| 209 | + // One grant: B's projection of the declared rule. Neither A's authored row |
| 210 | + // nor A's projection of the declared rule wrote one here. |
| 211 | + expect(grants).toEqual([ |
| 212 | + expect.objectContaining({ recipient_id: ASSIGNEE_B, access_level: 'edit', source: 'rule', source_id: RULE }), |
| 213 | + ]); |
| 214 | + expect(grants[0]?.organization_id).toBe(ORG_B); |
| 215 | + expect(await readAs(ASSIGNEE_B, ORG_B, inB)).toMatchObject({ id: inB }); |
| 216 | + expect(await readAs(WATCHER_A, ORG_A, inB)).toBeNull(); |
| 217 | + }); |
| 218 | +}); |
0 commit comments