diff --git a/.changeset/20001-scim-row-scope.md b/.changeset/20001-scim-row-scope.md new file mode 100644 index 00000000000..4542823d2d9 --- /dev/null +++ b/.changeset/20001-scim-row-scope.md @@ -0,0 +1,48 @@ +--- +'@objectstack/plugin-security': minor +--- + +fix(plugin-security)!: the SCIM projection tables are row-scoped in every shipped permission set — no principal below platform admin reads a SCIM row another organization provisioned (#20001) + +Clause-②: no (narrowing) + +**BREAKING** — shipped as `minor` under the launch-window convention +(`check-changeset-no-major` refuses `major` until GA; breaking-ness is carried by +this banner and the ADR-0087 disposition below, never by the level). + +The seven `@better-auth/scim` tables carry no tenant column, so the organization +wall is inert on them, and the read that the shipped permission sets grant on +every better-auth-managed object had no row policy behind it for any of the +seven. Any authenticated member could therefore read the users, groups and +memberships every organization's identity provider had provisioned. + +**What a principal can no longer read.** Every shipped set that grants that read +and carries row-level security — `member_default`, `viewer_readonly`, +`organization_admin` and its wall-less variant `organization_admin_no_bypass` — +now declares a row policy on each table: + +- `sys_scim_user`, `sys_scim_subject`, `sys_scim_projection_grant` and + `sys_scim_identity_tombstone`: only the rows whose `user_id` is the caller + (the `_self` policies); +- `sys_scim_group`, `sys_scim_group_member` and `sys_scim_connection_binding`: + no row at all (the `
_none` policies). + +This binds organization admins as well: an org admin no longer reads their own +organization's SCIM users or groups, only the rows about themselves. An MCP +agent acting for a user is bounded by that user's sets and reads the same. No +organization-scoped read replaces the old one, because none of the seven tables +has a column naming the organization and a row policy cannot follow +`connection_id` to the connection's organization. `admin_full_access` is +unchanged and still reads every row. + +**Remedy.** A deployment that needs a principal below platform admin to read +these tables grants it in a permission set of its own, with a row-level-security +policy on each table that names the rows it may see. Do not reach for a policy +that admits every row: no organization wall bounds these tables, so such a +policy admits every organization's rows. + +Unchanged: SCIM provisioning itself (its reads and writes run through +better-auth's adapter under system context, which no row policy reaches), and +every other managed object. + + diff --git a/packages/plugins/plugin-security/src/objects/default-permission-sets.ts b/packages/plugins/plugin-security/src/objects/default-permission-sets.ts index f62e5e9ea8e..2ae6cfa3ab1 100644 --- a/packages/plugins/plugin-security/src/objects/default-permission-sets.ts +++ b/packages/plugins/plugin-security/src/objects/default-permission-sets.ts @@ -102,6 +102,96 @@ const denyWritesOnManagedObjects = (): Record [ + { name: 'sys_scim_user_self', object: 'sys_scim_user', operation: 'select', using: 'user_id == current_user.id' }, + { name: 'sys_scim_subject_self', object: 'sys_scim_subject', operation: 'select', using: 'user_id == current_user.id' }, + { + name: 'sys_scim_projection_grant_self', + object: 'sys_scim_projection_grant', + operation: 'select', + using: 'user_id == current_user.id', + }, + { + name: 'sys_scim_identity_tombstone_self', + object: 'sys_scim_identity_tombstone', + operation: 'select', + using: 'user_id == current_user.id', + }, + { name: 'sys_scim_group_none', object: 'sys_scim_group', operation: 'select', using: 'id == null' }, + { name: 'sys_scim_group_member_none', object: 'sys_scim_group_member', operation: 'select', using: 'id == null' }, + { + name: 'sys_scim_connection_binding_none', + object: 'sys_scim_connection_binding', + operation: 'select', + using: 'id == null', + }, +]; + /** * Default permission sets seeded by the platform. * @@ -385,6 +475,11 @@ const baseDefaultPermissionSets: PermissionSet[] = [ operation: 'select', using: 'organization_id == current_user.organization_id', }, + // [#20001] The SCIM projection tables: self-only / no row, and deliberately + // no org-admin admission — Layer 0 is inert on them, so an `id != null` + // admission shaped like `sys_invitation_org_admin` would read every + // organization's rows. See `scimProjectionRowScope` above. + ...scimProjectionRowScope(), ], }), PermissionSetSchema.parse({ @@ -926,6 +1021,9 @@ const baseDefaultPermissionSets: PermissionSet[] = [ using: 'id != null', positions: ['org_member'], }, + // [#20001] The SCIM projection tables: a member reads the rows about + // themselves and no other — see `scimProjectionRowScope` above. + ...scimProjectionRowScope(), ], }), PermissionSetSchema.parse({ @@ -1045,6 +1143,11 @@ const baseDefaultPermissionSets: PermissionSet[] = [ operation: 'select', using: 'email == current_user.email', }, + // [#20001] Repeated here rather than inherited from `member_default`: this + // set names the SCIM projection tables through the blanket, and wherever + // the platform baseline is not composed a set with no policy for them + // leaves them unfiltered. See `scimProjectionRowScope` above. + ...scimProjectionRowScope(), ], }), @@ -1148,7 +1251,7 @@ const baseDefaultPermissionSets: PermissionSet[] = [ * ({@link ORGANIZATION_ADMIN_NO_BYPASS}) from `organization_admin` by dropping * the wildcard `viewAllRecords`/`modifyAllRecords` bits — everything else * (object grants, managed-write denies, anti-escalation RBAC read-only rules, - * system permissions, the 15 identity RLS carve-outs) is carried over verbatim. + * system permissions, the identity RLS carve-outs) is carried over verbatim. * * DERIVED, never a second literal: two hand-maintained copies of a * high-privilege set are a drift waiting to happen, and the drift would be a diff --git a/packages/plugins/plugin-security/src/objects/rbac-objects.test.ts b/packages/plugins/plugin-security/src/objects/rbac-objects.test.ts index d8c106dd311..74e78e24395 100644 --- a/packages/plugins/plugin-security/src/objects/rbac-objects.test.ts +++ b/packages/plugins/plugin-security/src/objects/rbac-objects.test.ts @@ -135,6 +135,19 @@ describe('default permission sets', () => { 'sys_oauth_consent_self', 'sys_oauth_refresh_token_self', 'sys_organization_self', + // [#20001] The SCIM projection tables' row scope — re-judged here, not a + // drift: none of the seven carries a tenant column, so Layer 0 is inert + // on them and the managed-object blanket read every organization's rows. + // `_self` on the four that name a user; `_none` (`id == null`, no row) on + // the three that name none. Their behaviour is pinned end to end in + // `scim-projection-row-scope.test.ts`; this list only records presence. + 'sys_scim_connection_binding_none', + 'sys_scim_group_member_none', + 'sys_scim_group_none', + 'sys_scim_identity_tombstone_self', + 'sys_scim_projection_grant_self', + 'sys_scim_subject_self', + 'sys_scim_user_self', 'sys_session_self', 'sys_team_member_self', 'sys_two_factor_self', diff --git a/packages/plugins/plugin-security/src/scim-projection-row-scope.test.ts b/packages/plugins/plugin-security/src/scim-projection-row-scope.test.ts new file mode 100644 index 00000000000..eb34a837fef --- /dev/null +++ b/packages/plugins/plugin-security/src/scim-projection-row-scope.test.ts @@ -0,0 +1,345 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * The seven `@better-auth/scim` projection tables are row-scoped in every + * shipped permission set that holds the managed-object read grant — so no + * principal below platform admin reads a row another organization's identity + * provider provisioned. + * + * The class (see the header of `objects/default-permission-sets.ts`): every + * object on `BETTER_AUTH_MANAGED_OBJECTS` gets an object-level `allowRead` from + * `denyWritesOnManagedObjects()`, and what narrows that read is whatever row + * policy the holder's set declares for it. None of the `sys_scim_*` tables + * carries a tenant column, so the organization wall (Layer 0) is inert on them + * and the engine scopes nothing by organization; with no row policy either, an + * object-level read there is a read of every organization's rows. + * + * The fix is the header's own instrument, the per-object row scope: + * - a `_self` policy (`user_id == current_user.id`) on the four tables that + * name the platform user the row is about — a principal reads the rows + * about themselves and nothing else; + * - a `_none` policy (`id == null`) on the three that name no user — no row + * is readable through a shipped set at all. + * The platform admin (`admin_full_access`) keeps every row through its + * superuser bypass, which is the only principal the SCIM administration + * surface (`sys_scim_connection_credential`, `manage_platform_settings`) is + * gated to. + * + * Driven on the real `SecurityPlugin` + `ObjectQL` over a real `SqlDriver`, with + * the SHIPPED permission sets (no copy): rows for two organizations' SCIM + * connections are written straight into the tables, past every scope, and each + * shipped persona of organization X reads every table through the generic data + * path. Three deployment shapes: + * - `isolated` with the platform baseline composed — the stock walled shape; + * - `isolated` with NO baseline — each persona resolves only the set it + * names, so a set that carried no scope of its own would read every row + * here even while the stock shape stayed green through `member_default`; + * - `single` — the policies carry no tenant token, so the posture's strip of + * the platform's tenant policies (ADR-0105 D3) must leave them in force. + */ + +import { describe, it, expect, afterAll, beforeAll, vi } from 'vitest'; +import { ObjectQL } from '@objectstack/objectql'; +import { SqlDriver } from '@objectstack/driver-sql'; +import { + SysMember, + SysScimConnectionBinding, + SysScimGroup, + SysScimGroupMember, + SysScimIdentityTombstone, + SysScimProjectionGrant, + SysScimSubject, + SysScimUser, + SysUser, +} from '@objectstack/platform-objects/identity'; +import { SecurityPlugin } from './security-plugin.js'; + +/** The SCIM tables that name the platform user each row is about. */ +const USER_KEYED = [ + 'sys_scim_user', + 'sys_scim_subject', + 'sys_scim_projection_grant', + 'sys_scim_identity_tombstone', +] as const; + +/** The SCIM tables that name no user. */ +const CONNECTION_KEYED = ['sys_scim_group', 'sys_scim_group_member', 'sys_scim_connection_binding'] as const; + +const SCIM_OBJECTS = [...USER_KEYED, ...CONNECTION_KEYED] as const; +type ScimObject = (typeof SCIM_OBJECTS)[number]; + +const SCHEMAS = [ + SysUser, + SysMember, + SysScimUser, + SysScimSubject, + SysScimProjectionGrant, + SysScimIdentityTombstone, + SysScimGroup, + SysScimGroupMember, + SysScimConnectionBinding, +]; + +/** + * The people in the fixture. `u_x`, `u_x2`, `v_x` and `a_x` belong to + * organization X; `u_y` belongs to organization Y. Every user-keyed SCIM table + * holds one row per person, each written by that person's organization's + * connection (`conn_x` / `conn_y`). + */ +const PEOPLE = { member: 'u_x', peer: 'u_x2', viewer: 'v_x', admin: 'a_x', otherOrg: 'u_y' } as const; +const ORG_OF: Record = { u_x: 'org_x', u_x2: 'org_x', v_x: 'org_x', a_x: 'org_x', u_y: 'org_y' }; +const CONN_OF: Record = { org_x: 'conn_x', org_y: 'conn_y' }; + +/** Row id of the `object` row about `user`, e.g. `sys_scim_user:u_y`. */ +const rowAbout = (object: string, user: string) => `${object}:${user}`; +/** Row id of the connection-keyed `object` row written by `org`'s connection. */ +const rowOf = (object: string, org: string) => `${object}:${org}`; + +/** A plausible value for a required column the fixture does not care about. */ +function filler(type: string, key: string): unknown { + switch (type) { + case 'boolean': + return true; + case 'number': + return 0; + case 'datetime': + return '2026-01-01T00:00:00.000Z'; + default: + return key; + } +} + +/** + * One row of `object`: every required column filled, then those of `values` + * the table declares (`sys_scim_subject` names no connection, and + * `sys_scim_group_member` no provisioning domain). + */ +function row(object: ScimObject, values: Record): Record { + const schema = SCHEMAS.find((s) => s.name === object)!; + const fields = schema.fields as Record; + const out: Record = {}; + for (const [name, def] of Object.entries(fields)) { + if (def.required) out[name] = filler(def.type, `${name}-${String(values.id)}`); + } + for (const [name, value] of Object.entries(values)) if (name in fields) out[name] = value; + return out; +} + +function fixtureRows(): Record>> { + const rows: Record>> = {}; + for (const object of USER_KEYED) { + rows[object] = Object.values(PEOPLE).map((user) => { + const org = ORG_OF[user]; + return row(object, { + id: rowAbout(object, user), + user_id: user, + connection_id: CONN_OF[org], + provisioning_domain_id: CONN_OF[org], + }); + }); + } + for (const object of CONNECTION_KEYED) { + rows[object] = ['org_x', 'org_y'].map((org) => + row(object, { id: rowOf(object, org), connection_id: CONN_OF[org], provisioning_domain_id: CONN_OF[org] }), + ); + } + return rows; +} + +/** The driver's own query builder, reached past its `protected` modifier. */ +type Table = (name: string) => { insert(rows: Array>): Promise }; + +/** + * One deployment shape. `composedBaseline: false` boots with no platform + * baseline (`fallbackPermissionSet: null`), so each persona resolves ONLY the + * set it names — which is what proves every shipped set carries the scope + * itself, rather than borrowing it from `member_default`. + */ +interface Shape { + label: string; + posture: 'isolated' | 'single'; + composedBaseline: boolean; +} + +const SHAPES: Shape[] = [ + { label: 'isolated posture, platform baseline composed (the stock shape)', posture: 'isolated', composedBaseline: true }, + { label: 'isolated posture, no platform baseline (each set on its own)', posture: 'isolated', composedBaseline: false }, + { label: 'single posture, platform baseline composed', posture: 'single', composedBaseline: true }, +]; + +async function boot(shape: Shape): Promise { + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); + const engine = new ObjectQL(); + engine.registerDriver(driver as never, true); + await engine.init(); + engine.registerApp({ + id: 'com.objectstack.qa.scim-projection-row-scope', + name: 'SCIM projection row scope', + version: '1.0.0', + type: 'plugin', + scope: 'system', + objects: SCHEMAS, + } as never); + await engine.syncSchemas(); + + const services: Record = { + ...(shape.posture === 'isolated' ? { 'org-scoping': { name: 'com.objectstack.org-scoping' } } : {}), + tenancy: { posture: shape.posture }, + manifest: { register: vi.fn() }, + objectql: engine, + // `list` answers nothing, so every set name resolves from the plugin's + // bootstrap list: the SHIPPED `defaultPermissionSets`, not a copy. + metadata: { get: async (_t: string, name: string) => engine.getSchema(name) ?? null, list: async () => [] }, + }; + const ctx = { + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, + registerService: vi.fn(), + getService: (name: string) => { + if (!(name in services)) throw new Error(`service not registered: ${name}`); + return services[name]; + }, + }; + const plugin = new SecurityPlugin(shape.composedBaseline ? {} : { fallbackPermissionSet: null }); + await plugin.init(ctx as never); + await plugin.start(ctx as never); + vi.spyOn((engine as unknown as { logger: { warn: () => void } }).logger, 'warn').mockImplementation(() => undefined); + + // Straight into the tables, past every scope: the fixture is not the subject. + const table = (driver as unknown as { knex: Table }).knex; + for (const [object, rows] of Object.entries(fixtureRows())) await table(object).insert(rows); + await table('sys_user').insert( + Object.values(PEOPLE).map((id) => ({ id, name: id, email: `${id}@example.test` })), + ); + await table('sys_member').insert( + Object.values(PEOPLE).map((id) => ({ + id: `sys_member:${id}`, + user_id: id, + organization_id: ORG_OF[id], + role: id === PEOPLE.admin ? 'admin' : 'member', + })), + ); + return engine; +} + +/** Organization X's co-members, as the authz resolver pre-resolves them. */ +const ORG_X_USERS = Object.values(PEOPLE).filter((id) => ORG_OF[id] === 'org_x'); + +/** + * Each shipped set that holds the managed-object read grant, carried by a + * principal of organization X exactly as the authz resolver shapes it — each + * naming its set explicitly, so it resolves even where no baseline is composed. + */ +const PERSONAS = { + member_default: { + userId: PEOPLE.member, tenantId: 'org_x', positions: ['org_member'], permissions: ['member_default'], + posture: 'MEMBER', org_user_ids: ORG_X_USERS, email: `${PEOPLE.member}@example.test`, + }, + viewer_readonly: { + userId: PEOPLE.viewer, tenantId: 'org_x', positions: ['org_member'], permissions: ['viewer_readonly'], + posture: 'MEMBER', org_user_ids: ORG_X_USERS, email: `${PEOPLE.viewer}@example.test`, + }, + organization_admin: { + userId: PEOPLE.admin, tenantId: 'org_x', positions: ['org_admin'], permissions: ['organization_admin'], + posture: 'TENANT_ADMIN', org_user_ids: ORG_X_USERS, email: `${PEOPLE.admin}@example.test`, + }, + organization_admin_no_bypass: { + userId: PEOPLE.admin, tenantId: 'org_x', positions: ['org_admin'], permissions: ['organization_admin_no_bypass'], + posture: 'TENANT_ADMIN', org_user_ids: ORG_X_USERS, email: `${PEOPLE.admin}@example.test`, + }, + // The MCP write ceiling holds the grant too; it carries no row policy of its + // own by design (ADR-0090 D10), so its bound is the delegating member's sets. + // Those resolve from the baseline, so this persona runs where one is composed. + mcp_agent_data_write: { + userId: 'agent_x', tenantId: 'org_x', positions: [], permissions: ['mcp_agent_data_write'], + principalKind: 'agent', onBehalfOf: { userId: PEOPLE.member }, org_user_ids: ORG_X_USERS, + }, +} as const; +type Persona = keyof typeof PERSONAS; + +/** Whose rows each persona reads through its self scope. */ +const SELF_OF: Record = { + member_default: PEOPLE.member, + viewer_readonly: PEOPLE.viewer, + organization_admin: PEOPLE.admin, + organization_admin_no_bypass: PEOPLE.admin, + mcp_agent_data_write: PEOPLE.member, +}; + +const PLATFORM_ADMIN = { + userId: 'p_admin', tenantId: 'org_x', positions: [], permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN', +}; + +async function idsRead(engine: ObjectQL, object: string, context: object): Promise { + const rows = (await engine.find(object, { context } as never)) as Array<{ id: string }>; + return rows.map((r) => r.id).sort(); +} + +/** Everything `context` reads of the seven SCIM tables, one sorted id list per table. */ +async function scimTable(engine: ObjectQL, context: object): Promise> { + const out = {} as Record; + for (const object of SCIM_OBJECTS) out[object] = await idsRead(engine, object, context); + return out; +} + +describe.each(SHAPES)('SCIM projection tables — $label', (shape) => { + let engine: ObjectQL; + beforeAll(async () => { + engine = await boot(shape); + }); + afterAll(async () => { + try { await engine?.destroy(); } catch { /* noop */ } + }); + + const personas = (Object.keys(PERSONAS) as Persona[]).filter( + (p) => shape.composedBaseline || p !== 'mcp_agent_data_write', + ); + + for (const persona of personas) { + it(`${persona}: reads the SCIM rows about itself, and nothing another organization provisioned`, async () => { + const seen = await scimTable(engine, PERSONAS[persona]); + + const self = SELF_OF[persona]; + const expected = {} as Record; + for (const object of USER_KEYED) expected[object] = [rowAbout(object, self)]; + for (const object of CONNECTION_KEYED) expected[object] = []; + expect(seen).toEqual(expected); + }); + + it(`${persona}: a by-id read of another organization’s SCIM row finds nothing`, async () => { + const context = PERSONAS[persona]; + for (const object of USER_KEYED) { + const other = await engine.findOne(object, { where: { id: rowAbout(object, PEOPLE.otherOrg) }, context } as never); + expect(other, object).toBeNull(); + } + for (const object of CONNECTION_KEYED) { + const other = await engine.findOne(object, { where: { id: rowOf(object, 'org_y') }, context } as never); + expect(other, object).toBeNull(); + } + }); + } + + it('CONTROL: the fixture really holds both organizations’ rows — the platform admin reads every one', async () => { + const seen = await scimTable(engine, PLATFORM_ADMIN); + const rows = fixtureRows(); + const expected = {} as Record; + for (const object of SCIM_OBJECTS) expected[object] = rows[object].map((r) => String(r.id)).sort(); + expect(seen).toEqual(expected); + // Both organizations are present in what that read returned. + expect(seen.sys_scim_user).toContain(rowAbout('sys_scim_user', PEOPLE.otherOrg)); + expect(seen.sys_scim_group).toContain(rowOf('sys_scim_group', 'org_y')); + }); + + it('CONTROL: a co-member’s own SCIM rows are readable to that co-member — the scope is per person, not a blanket deny', async () => { + const peer = { ...PERSONAS.member_default, userId: PEOPLE.peer, email: `${PEOPLE.peer}@example.test` }; + const seen = await scimTable(engine, peer); + for (const object of USER_KEYED) expect(seen[object], object).toEqual([rowAbout(object, PEOPLE.peer)]); + }); + + if (shape.posture === 'isolated') { + it('CONTROL: an unrelated managed object keeps its own scope — `sys_member` reads organization X’s memberships', async () => { + const orgX = ORG_X_USERS.map((id) => `sys_member:${id}`).sort(); + expect(await idsRead(engine, 'sys_member', PERSONAS.member_default)).toEqual(orgX); + expect(await idsRead(engine, 'sys_member', PERSONAS.organization_admin)).toEqual(orgX); + }); + } +});