diff --git a/.changeset/39339-owner-beside-delete-policy.md b/.changeset/39339-owner-beside-delete-policy.md new file mode 100644 index 00000000000..8d9a7b4977e --- /dev/null +++ b/.changeset/39339-owner-beside-delete-policy.md @@ -0,0 +1,13 @@ +--- +'@objectstack/plugin-security': patch +--- + +fix(security): the owner of a record deletes it beside an app's own delete rule, whoever created it + +Clause-②: no +Metadata-protocol: none + +Who sees it: an app that authors a row-level `delete` policy (for example "anyone may delete a closed deal") on an object with an owner column (`owner_id`), on `private`, `public_read` and `public_read_write` objects alike. + +- **The record's owner deletes it again.** Before, beside such a policy the platform's own row gate admitted only the person who created the record, so the owner of a record they did not type in (assigned to them, imported, handed over) was refused with 403 on every record the policy does not admit: a rule written to let more people delete took the delete away from owners. Now the owner, someone above the owner by write depth (`writeScope`), and a holder of write depth `org` delete it, as they do on an object with no authored delete policy. A by-id delete and a predicate (bulk) delete reach the same records, and `security/explain` agrees. +- **Unchanged:** Modify All (`modifyAllRecords`) still does not lift an authored delete policy on a business object, so its holder deletes the records the policy admits and the ones they created, and no others. A creator who no longer owns a record is still refused unless the policy admits it. An object with no owner column keeps the creator rule. Edit (update) is not changed by this release. diff --git a/packages/plugins/plugin-security/src/delegated-write-never-exceeds-delegator.test.ts b/packages/plugins/plugin-security/src/delegated-write-never-exceeds-delegator.test.ts index fe050c9da5c..49420b1626f 100644 --- a/packages/plugins/plugin-security/src/delegated-write-never-exceeds-delegator.test.ts +++ b/packages/plugins/plugin-security/src/delegated-write-never-exceeds-delegator.test.ts @@ -209,8 +209,16 @@ async function boot(posture: TenancyPosture) { row('held_1', 'held', BOUNDED, OTHER), row('held_2', 'held', OTHER, OTHER), row('free_1', 'free', BOUNDED, BOUNDED), - row('held_del_direct', 'held', BOUNDED, OTHER), - row('held_del_agent', 'held', BOUNDED, OTHER), + // [ruling of record 7288570890 (C)] Owner-less: the delegator's write depth + // `org` reaches every OWNED row for delete, so the row its own delete + // refuses is one no owner holds. + { ...row('held_del_direct', 'held', OTHER, OTHER), owner_id: null }, + { ...row('held_del_agent', 'held', OTHER, OTHER), owner_id: null }, + // [ruling of record 7288570890 (C)] A held row the delegator OWNS and did + // not create: ownership admits the delegator's own delete beside the + // authored policy; the delegated delete keeps both floors and is refused. + row('held_owned_del_direct', 'held', BOUNDED, OTHER), + row('held_owned_del_agent', 'held', BOUNDED, OTHER), row('free_del_direct', 'free', BOUNDED, BOUNDED), row('free_del_agent', 'free', BOUNDED, BOUNDED), row('store_direct', 'free', BOUNDED, OTHER), @@ -429,6 +437,10 @@ const CELLS: Cell[] = [ }, // The bound only narrows: rows the delegator's own write admits and the // delegated write has always refused stay refused. + { + delegator: BOUNDED, verb: 'delete', object: 'OBJ', rows: { direct: 'held_owned_del_direct', agent: 'held_owned_del_agent' }, + shape: 'unchanged on its behalf: a held row the delegator owns and did not create (ownership admits its own delete)', direct: ADMITTED, agent: DENIED, + }, { delegator: PLAIN, verb: 'update', object: 'OBJ', rows: { direct: 'plain_1', agent: 'plain_1' }, shape: 'unchanged: a row the delegator\'s own update admits and the delegated update refuses', direct: ADMITTED, agent: DENIED, diff --git a/packages/plugins/plugin-security/src/owner-beside-authored-delete-policy.test.ts b/packages/plugins/plugin-security/src/owner-beside-authored-delete-policy.test.ts new file mode 100644 index 00000000000..090706a5a0f --- /dev/null +++ b/packages/plugins/plugin-security/src/owner-beside-authored-delete-policy.test.ts @@ -0,0 +1,324 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * An app-authored DELETE row policy on an object that has an owner column, + * read beside the two rulings that govern it: + * + * - ruling of record 7063130499 (A): under a sharing `allow`, the + * creator/owner floor stays an OR term beside an authored update/delete + * `using` policy, and the by-id and predicate writes give one answer; + * - ruling of record 7288570890 (C): delete on an owned object follows the + * record's owner, someone above the owner by write depth, or a Modify All + * holder; a creator who no longer owns the row cannot delete. + * + * Before this file the OR term beside the authored policy was only the + * creator half (`created_by == current_user.id`, `owner_only_deletes`), so the + * owner of a row they did not create (assigned, imported, handed over) was + * refused on every row the authored policy does not admit: the policy, meant + * to widen delete, took it away from the owner. Here ownership admits the row + * beside the authored policy as it does where no authored policy stands, and + * Modify All stays out of that OR (ADR-0066 ①: it does not lift an authored + * write policy on a business object). + * + * One real stack, as `public-read-write-owner-delete.test.ts` builds it: an + * `ObjectKernel` with `ObjectQLPlugin` over better-sqlite3, the real + * `SecurityPlugin` (the real `member_default` floor) and the real + * `SharingServicePlugin`. Every caller holds the `org_member` position, the + * floor's domain. The authored policy admits a `closed` row to every member; + * every row the owner cases probe is `open`, so the policy never admits them. + */ + +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import { ObjectKernel, type Plugin, type PluginContext } from '@objectstack/core'; +import { ObjectQL, ObjectQLPlugin } from '@objectstack/objectql'; +import { SqlDriver } from '@objectstack/driver-sql'; +import { SharingServicePlugin } from '@objectstack/plugin-sharing'; +import { SysMember, SysOrganization, SysUser } from '@objectstack/platform-objects/identity'; +import { PermissionSetSchema } from '@objectstack/spec/security'; +import type { ISecurityService } from '@objectstack/spec/contracts'; +import { SecurityPlugin } from './security-plugin.js'; +import { defaultPermissionSets } from './objects/default-permission-sets.js'; + +const ORG = 'org_obadp'; +/** Owns rows it did not create. */ +const OWNER = 'usr_obadp_owner'; +/** Created rows it no longer owns. */ +const CREATOR = 'usr_obadp_creator'; +/** Neither owns nor created the probed rows. */ +const STRANGER = 'usr_obadp_stranger'; +/** Above {@link OWNER} by write depth (`own_and_reports`). */ +const MANAGER = 'usr_obadp_manager'; +/** Write depth `org`, declared, without Modify All. */ +const ORG_DEPTH = 'usr_obadp_org_depth'; +/** Holds Modify All on the probed objects. */ +const MODIFY_ALL = 'usr_obadp_modify_all'; +const USERS = [OWNER, CREATOR, STRANGER, MANAGER, ORG_DEPTH, MODIFY_ALL]; + +/** `public_read_write`, owner column. */ +const DEAL = 'qa_obadp_deal'; +/** `private`, otherwise identical. */ +const SECRET = 'qa_obadp_secret'; +/** `public_read_write` with NO owner column: the creator floor stays its row-level delete gate. */ +const MEMO = 'qa_obadp_memo'; + +const CRUD = { allowRead: true, allowCreate: true, allowEdit: true, allowDelete: true } as const; +/** The authored widener: any member deletes a `closed` row, on each probed object. */ +const CLOSED_DELETABLE = [DEAL, SECRET, MEMO].map((object) => ({ + name: `${object}_closed_deletable`, + object, + operation: 'delete' as const, + using: "stage == 'closed'", +})); +const setWith = (name: string, extra: Record = {}) => + PermissionSetSchema.parse({ + name, + objects: { [DEAL]: { ...CRUD, ...extra }, [SECRET]: { ...CRUD, ...extra }, [MEMO]: { ...CRUD, ...extra } }, + rowLevelSecurity: CLOSED_DELETABLE, + }); +/** Ordinary CRUD: every refusal below is row-level, never the object bit. */ +const REP = setWith('qa_obadp_rep'); +/** Depth `own_and_reports`, read too, so the `private` row is visible to the manager. */ +const LEAD = setWith('qa_obadp_lead', { readScope: 'own_and_reports', writeScope: 'own_and_reports' }); +/** Depth `org`, read too, and no Modify All. */ +const WIDE = setWith('qa_obadp_wide', { readScope: 'org', writeScope: 'org' }); +/** Modify All Data on the probed objects. */ +const ADMIN = setWith('qa_obadp_admin', { viewAllRecords: true, modifyAllRecords: true }); + +/** 403 where the caller reads the row, 404 where it cannot (the `private` row of another owner). */ +const REFUSED = (object: string) => ({ kind: 'refused', status: object === SECRET ? 404 : 403 }); + +const SYSTEM = { isSystem: true, tenantId: ORG } as const; +const SYS = { context: SYSTEM } as never; +const member = (userId: string, set: string) => ({ userId, tenantId: ORG, positions: ['org_member'], permissions: [set] }); + +type Outcome = { kind: 'landed' } | { kind: 'refused'; code?: string; status?: number }; +const outcomeOf = (p: Promise): Promise => + p.then( + () => ({ kind: 'landed' as const }), + (e: { code?: unknown; status?: unknown; statusCode?: unknown } | null) => { + const status = e?.statusCode ?? e?.status; + return { + kind: 'refused' as const, + code: e?.code == null ? undefined : String(e.code), + status: status == null ? undefined : Number(status), + }; + }, + ); + +function sqliteDriverPlugin(): Plugin { + return { + name: 'test.driver.sqlite', + type: 'standard', + version: '1.0.0', + async init(ctx: PluginContext) { + ctx.registerService( + 'driver.default', + new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), + ); + }, + }; +} + +const fields = () => ({ + name: { type: 'text', label: 'Name' }, + stage: { type: 'text', label: 'Stage' }, + owner_id: { type: 'lookup', reference: 'sys_user', label: 'Owner' }, +}); + +function fixturePlugin(): Plugin { + return { + name: 'test.obadp.fixture', + type: 'standard', + version: '1.0.0', + dependencies: ['com.objectstack.engine.objectql'], + async init(ctx: PluginContext) { + ctx.getService<{ register(m: unknown): unknown }>('manifest').register({ + id: 'test.owner-beside-authored-delete-policy', + namespace: 'qa_obadp', + version: '1.0.0', + type: 'plugin', + name: 'owner beside an authored delete policy', + objects: [ + SysUser, SysMember, SysOrganization, + { name: DEAL, label: 'Deal', sharingModel: 'public_read_write', fields: fields() }, + { name: SECRET, label: 'Secret', sharingModel: 'private', fields: fields() }, + { + name: MEMO, + label: 'Memo', + sharingModel: 'public_read_write', + ownership: 'none', + fields: { name: { type: 'text', label: 'Name' }, stage: { type: 'text', label: 'Stage' } }, + }, + ], + }); + // [ADR-0057] The manager's `own_and_reports` reaches the owner; nobody + // else reports to anyone. + ctx.registerService('hierarchy-scope-resolver', { + async resolveOwnerIds(scope: { userId: string }, depth: string) { + if (scope.userId === MANAGER && depth === 'own_and_reports') return [MANAGER, OWNER]; + return [scope.userId]; + }, + }); + }, + }; +} + +describe('an authored delete policy on an owned object: ownership admits the row beside it, Modify All does not', () => { + let kernel: ObjectKernel; + let ql: ObjectQL; + let security: Required>; + let seq = 0; + + beforeAll(async () => { + kernel = new ObjectKernel({ logger: { level: 'silent' } } as never); + await kernel.use(sqliteDriverPlugin()); + await kernel.use(new ObjectQLPlugin()); + await kernel.use(fixturePlugin()); + await kernel.use(new SecurityPlugin({ + defaultPermissionSets: [...defaultPermissionSets, REP, LEAD, WIDE, ADMIN], + fallbackPermissionSet: 'member_default', + })); + await kernel.use(new SharingServicePlugin({ registerShareLinkRoutes: false })); + await kernel.bootstrap(); + ql = kernel.getService('objectql'); + security = kernel.getService>>('security'); + + await ql.insert('sys_organization', { id: ORG, name: 'Acme', slug: 'acme' }, SYS); + for (const userId of USERS) { + await ql.insert('sys_user', { id: userId, name: userId, email: `${userId}@acme.test`, email_verified: true }, SYS); + await ql.insert('sys_member', { user_id: userId, organization_id: ORG, role: 'member' }, SYS); + } + }, 120_000); + + afterAll(async () => { + if (kernel) await Promise.race([kernel.shutdown(), new Promise((r) => setTimeout(r, 10_000))]); + }, 30_000); + + /** One fresh row, `owner_id` and `created_by` set apart. */ + const seed = async (object: string, owner: string | null, creator: string | null, stage = 'open') => { + const id = `obadp_${++seq}`; + await ql.insert(object, { + id, name: id, stage, + ...(object === MEMO ? {} : { owner_id: owner }), + created_by: creator, + }, SYS); + return id; + }; + const exists = async (object: string, id: string) => + (await ql.findOne(object, { where: { id }, context: SYSTEM } as never)) != null; + const deleteById = (object: string, id: string, context: unknown) => + outcomeOf(ql.delete(object, { where: { id }, context } as never)); + const explainDelete = async (object: string, id: string, context: unknown) => + (await security.explain({ object, operation: 'delete', recordId: id }, context as never))?.record?.visible; + + it('[integrity] the authored policy is in play: a stranger deletes a closed row and is refused an open one', async () => { + const closed = await seed(DEAL, OWNER, CREATOR, 'closed'); + expect(await deleteById(DEAL, closed, member(STRANGER, REP.name))).toEqual({ kind: 'landed' }); + const open = await seed(DEAL, OWNER, CREATOR); + expect(await deleteById(DEAL, open, member(STRANGER, REP.name))).toMatchObject({ kind: 'refused', status: 403 }); + expect(await exists(DEAL, open)).toBe(true); + }); + + describe('by id, on a row the authored policy does not admit: ownership deletes it', () => { + for (const [what, context] of [ + ['the owner (not the creator)', member(OWNER, REP.name)], + ['the manager above the owner (own_and_reports)', member(MANAGER, LEAD.name)], + ['a holder of write depth org, without Modify All', member(ORG_DEPTH, WIDE.name)], + ] as const) { + for (const object of [DEAL, SECRET]) { + it(`${object}: ${what} deletes the row, and security/explain says so first`, async () => { + const id = await seed(object, OWNER, CREATOR); + expect(await explainDelete(object, id, context), 'explain agrees with the door').toBe(true); + expect(await deleteById(object, id, context)).toEqual({ kind: 'landed' }); + expect(await exists(object, id), 'the row is gone').toBe(false); + }); + } + } + }); + + describe('by id, on a row the authored policy does not admit: everyone else is refused', () => { + for (const [what, context] of [ + ['the creator who no longer owns it', member(CREATOR, REP.name)], + ['a member who neither owns nor created it', member(STRANGER, REP.name)], + ['a Modify All holder who neither owns nor created it (ADR-0066 ①)', member(MODIFY_ALL, ADMIN.name)], + ] as const) { + for (const object of [DEAL, SECRET]) { + it(`${object}: ${what} is refused, the row stays, and security/explain says so first`, async () => { + const id = await seed(object, OWNER, CREATOR); + expect(await explainDelete(object, id, context), 'explain agrees with the door').toBe(false); + const out = await deleteById(object, id, context); + // The Modify All holder reads every row of the private object, so it + // meets the 403 there too. + expect(out).toMatchObject(context.userId === MODIFY_ALL ? { kind: 'refused', status: 403 } : REFUSED(object)); + expect(await exists(object, id), 'the row stays').toBe(true); + }); + } + } + }); + + it('unchanged: a Modify All holder who created a row it no longer owns deletes it through the creator term', async () => { + const id = await seed(DEAL, OWNER, MODIFY_ALL); + expect(await explainDelete(DEAL, id, member(MODIFY_ALL, ADMIN.name))).toBe(true); + expect(await deleteById(DEAL, id, member(MODIFY_ALL, ADMIN.name))).toEqual({ kind: 'landed' }); + }); + + it('an object with no owner column keeps the creator floor beside the authored policy', async () => { + const open = await seed(MEMO, null, CREATOR); + expect(await deleteById(MEMO, open, member(STRANGER, REP.name))).toMatchObject({ kind: 'refused', status: 403 }); + expect(await exists(MEMO, open)).toBe(true); + expect(await deleteById(MEMO, open, member(CREATOR, REP.name))).toEqual({ kind: 'landed' }); + const closed = await seed(MEMO, null, CREATOR, 'closed'); + expect(await deleteById(MEMO, closed, member(STRANGER, REP.name))).toEqual({ kind: 'landed' }); + }); + + describe('a predicate delete matches exactly the rows the by-id delete admits (ruling A)', () => { + for (const object of [DEAL, SECRET]) { + it(`${object}: the owner's predicate delete removes the row it owns and leaves the one it only created`, async () => { + const owned = await seed(object, OWNER, CREATOR, 'bulk'); + const created = await seed(object, STRANGER, OWNER, 'bulk'); + const other = await seed(object, STRANGER, STRANGER, 'bulk'); + const out = await outcomeOf(ql.delete(object, { + where: { id: { $in: [owned, created, other] } }, multi: true, context: member(OWNER, REP.name), + } as never)); + expect(out).toEqual({ kind: 'landed' }); + expect(await exists(object, owned), 'the row it owns is gone').toBe(false); + expect(await exists(object, created), 'the row it created but does not own stays').toBe(true); + expect(await exists(object, other), 'a stranger\'s row stays').toBe(true); + }); + } + + it(`${DEAL}: the manager's predicate delete removes the owner's row and the closed row the policy admits`, async () => { + const owners = await seed(DEAL, OWNER, CREATOR, 'bulk'); + const closed = await seed(DEAL, STRANGER, STRANGER, 'closed'); + const other = await seed(DEAL, STRANGER, STRANGER, 'bulk'); + const out = await outcomeOf(ql.delete(DEAL, { + where: { id: { $in: [owners, closed, other] } }, multi: true, context: member(MANAGER, LEAD.name), + } as never)); + expect(out).toEqual({ kind: 'landed' }); + expect(await exists(DEAL, owners)).toBe(false); + expect(await exists(DEAL, closed)).toBe(false); + expect(await exists(DEAL, other)).toBe(true); + }); + + it(`${DEAL}: a Modify All holder's predicate delete removes the closed row and the one it created, and no other`, async () => { + const closed = await seed(DEAL, OWNER, CREATOR, 'closed'); + const created = await seed(DEAL, OWNER, MODIFY_ALL, 'bulk'); + const other = await seed(DEAL, OWNER, CREATOR, 'bulk'); + const out = await outcomeOf(ql.delete(DEAL, { + where: { id: { $in: [closed, created, other] } }, multi: true, context: member(MODIFY_ALL, ADMIN.name), + } as never)); + expect(out).toEqual({ kind: 'landed' }); + expect(await exists(DEAL, closed)).toBe(false); + expect(await exists(DEAL, created)).toBe(false); + expect(await exists(DEAL, other), 'Modify All does not lift the authored policy').toBe(true); + }); + }); + + it('edit is untouched by this composition: the authored policy is delete-only, and every member edits the deal', async () => { + const id = await seed(DEAL, OWNER, CREATOR); + for (const userId of [OWNER, CREATOR, STRANGER]) { + const out = await outcomeOf(ql.update(DEAL, { name: `edited by ${userId}` }, { where: { id }, context: member(userId, REP.name) } as never)); + expect(out, `${userId} edits`).toEqual({ kind: 'landed' }); + } + }); +}); diff --git a/packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts b/packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts index 7ce0d4db4cf..799489c24a6 100644 --- a/packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts +++ b/packages/plugins/plugin-security/src/predicate-write-floor-deferral.test.ts @@ -850,10 +850,15 @@ describe('[#35917] beside an authored widener, the by-id write keeps the floor a }; describe('the owner-creator, ALICE, under the wideners', () => { - for (const verb of ['update', 'delete'] as const) { - it(`by-id ${verb} of her own closed r1 is admitted, and the predicate ${verb} agrees row for row`, async () => { - await expectPredicateMatchesById(verb, memberOf(ALICE, WIDENER_SET.name), ['r1'], ALL_CLOSED); + // [ruling of record 7288570890 (C)] On delete the owner half of the OR + // term admits r2 as well: she owns it, CAROL created it. On update the + // term is still the creator's alone. + for (const [verb, expected] of [['update', ['r1']], ['delete', ['r1', 'r2']]] as const) { + it(`by-id ${verb} of her own closed rows is admitted (${expected.join(', ')}), and the predicate ${verb} agrees row for row`, async () => { + await expectPredicateMatchesById(verb, memberOf(ALICE, WIDENER_SET.name), expected, ALL_CLOSED); }); + } + for (const verb of ['update', 'delete'] as const) { it(`${verb}: her own closed r1 by the floor, the open r2 she owns by the widener`, async () => { await expectPredicateMatchesById(verb, memberOf(ALICE, WIDENER_SET.name), ['r1', 'r2'], { ...ALL_CLOSED, r2: { stage: 'open' }, @@ -863,10 +868,15 @@ describe('[#35917] beside an authored widener, the by-id write keeps the floor a }); describe('the rest of the #35802 matrix under the wideners, every row closed', () => { - for (const verb of ['update', 'delete'] as const) { - it(`BOB (owner, created none), ${verb}: nothing, as before`, async () => { - await expectPredicateMatchesById(verb, memberOf(BOB, WIDENER_SET.name), [], ALL_CLOSED); + // [ruling of record 7288570890 (C)] BOB owns r3 and r4 and created + // neither: ownership admits his delete of both beside the widener, by id + // and by predicate alike; his update is still the creator term's, nothing. + for (const [verb, expected] of [['update', []], ['delete', ['r3', 'r4']]] as const) { + it(`BOB (owner, created none), ${verb}: ${expected.length ? expected.join(', ') : 'nothing'}`, async () => { + await expectPredicateMatchesById(verb, memberOf(BOB, WIDENER_SET.name), expected, ALL_CLOSED); }); + } + for (const verb of ['update', 'delete'] as const) { it(`CAROL (created r2 and r4, owns none), ${verb}: nothing — the floor admits them, record sharing does not`, async () => { await expectPredicateMatchesById(verb, memberOf(CAROL, WIDENER_SET.name), [], ALL_CLOSED); }); @@ -1469,7 +1479,7 @@ describe('[#35802] a predicate write of a controlled_by_parent detail writes the }); } - it('why the skip moves no row: no floor in the vouched Layer 1, the floor-only replacement is not empty beside a policy, and sharing abstains', async () => { + it('why the skip moves no row: no floor in the vouched Layer 1, the update replacement is not empty beside a policy, and sharing abstains', async () => { const plugin = securityPlugin as any; const rep = memberOf(ALICE, REP_SET.name); for (const verb of ['update', 'delete'] as const) { @@ -1478,15 +1488,20 @@ describe('[#35802] a predicate write of a controlled_by_parent detail writes the const bare = await plugin.computeLayeredRlsFilter([REP_SET, MEMBER_DEFAULT], LINE, verb, rep, { masterGateCoversThisWrite: true }); expect(bare.layer1 ?? null, `${verb}: the vouched Layer 1 without a policy`).toBeNull(); - // An authored `using`: the vouched Layer 1 is that policy alone, and - // replacing the floor alone (the relief's own test) keeps the authored - // term, so the relief answers before any verdict is asked. + // An authored `using`: the vouched Layer 1 is that policy alone, and on + // update replacing the floor alone (the relief's own test) keeps the + // authored term, so the relief answers before any verdict is asked. const set = policySetFor(verb); const authored = memberOf(ALICE, set.name); const vouched = await plugin.computeLayeredRlsFilter([set, MEMBER_DEFAULT], LINE_STAGED, verb, authored, { masterGateCoversThisWrite: true }); expect(vouched.layer1, `${verb}: the vouched Layer 1 beside a policy`).toEqual({ stage: 'open' }); + // On delete, a replacement for a caller without Modify All is an + // ownership `allow` (ruling of record 7288570890 (C)), which admits the + // row beside the policy, so that composition is empty and the relief + // would go on to ask; it never runs on a vouched write. const replaced = await plugin.computeLayeredRlsFilter([set, MEMBER_DEFAULT], LINE_STAGED, verb, authored, { dropPlatformOwnershipFloor: true }); - expect(replaced.layer1, `${verb}: the floor-only replacement beside a policy`).toBeTruthy(); + if (verb === 'update') expect(replaced.layer1, `${verb}: the floor-only replacement beside a policy`).toBeTruthy(); + else expect(replaced.layer1 ?? null, `${verb}: the ownership replacement beside a policy`).toBeNull(); // And an asked verdict would replace no floor: record sharing abstains // on every row of a controlled_by_parent detail. diff --git a/packages/plugins/plugin-security/src/security-plugin-rls-filter.ts b/packages/plugins/plugin-security/src/security-plugin-rls-filter.ts index 4d6728c41d2..2b4ebae9c70 100644 --- a/packages/plugins/plugin-security/src/security-plugin-rls-filter.ts +++ b/packages/plugins/plugin-security/src/security-plugin-rls-filter.ts @@ -439,9 +439,39 @@ export async function computeLayeredRlsFilter( ? collected.filter((p) => !isPlatformOwnershipFloorPolicy(p)) : collected; let allRlsPolicies = withoutFloor; + // [ruling of record 7288570890 (C), beside ruling of record 7063130499 + // (A)] The OR term the floor keeps beside an authored DELETE policy is + // the creator/owner term, and its owner half is the one the declared + // authority's `allow` speaks for. On `delete` that `allow` is ownership + // widened by write DEPTH, the Modify All bypass, or system context and + // nothing else (`ISharingService.checkDelete`: no share, no queue basis), + // so where the caller holds no Modify All on the object the `allow` IS + // ownership: the record's owner, someone above the owner by depth, a + // declared `writeScope: 'org'`. Ownership admits the row here exactly as + // it does where no authored term stands, so Layer 1 is null (the OR has + // a true term) and the authority's `allow` admits the row. Keeping only + // the `created_by` half refused the owner of a row they did not create on + // every row the authored policy does not admit — an authored widener + // narrowing the owner. + // + // Modify All stays OUT of the OR (ADR-0066 ①: it does not lift an + // authored write policy on a business object), so its holder keeps the + // composition above, floor and authored term both. An object with no + // owner column never gets an `allow` (the authority abstains there), so + // it keeps the `created_by` floor. Update is untouched: its `allow` also + // carries an `edit` share and the queue, which are not ownership. + // The predicate write reaches the same answer per row: its floor + // relief asks this composition with the floor replaced, finds it null, + // and hands each row its by-id verdict (`predicateWriteFloorRelief`). + const ownershipAllowAdmitsRow = + !!opts?.dropPlatformOwnershipFloor && + rlsOperation === 'delete' && + !this.permissionEvaluator.hasSuperuserWriteBypass(object, permissionSets, { isPrivate: meta.isPrivate }); if (withoutFloor.length < collected.length) { const rest = fieldExistencePassOne(withoutFloor); - if (rest.dropped > 0 || rest.compilable.some((p) => !!p.using)) allRlsPolicies = collected; + if (rest.dropped > 0 || rest.compilable.some((p) => !!p.using)) { + allRlsPolicies = ownershipAllowAdmitsRow ? [] : collected; + } } if (allRlsPolicies.length > 0) { // Field-existence safety, in TWO passes that answer two different