Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/39339-owner-beside-delete-policy.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
---
'@objectstack/plugin-security': patch
---
Expand All @@ -10,4 +10,4 @@
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.
- **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, the ones they created and the ones they own, 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.
13 changes: 13 additions & 0 deletions .changeset/39464-modify-all-owner-delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@objectstack/plugin-security': patch
---

fix(security): a Modify All holder deletes the records they own beside an app's own delete rule, as any owner does

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`), and a user who holds Modify All (`modifyAllRecords`) on that object, typically a manager.

- **Holding Modify All no longer takes a delete away from an owner.** Before, beside such a policy a Modify All holder was refused (403) a record assigned to them that someone else entered, while a plain owner in the same position deleted it. Now the Modify All holder deletes the records they own as well, by id and by a predicate (bulk) delete alike, and `security/explain` agrees.
- **Unchanged:** Modify All itself still does not lift an authored delete policy on a business object. Its holder deletes the records the policy admits, the ones they created and the ones they own, and no others: a record of another owner, or one with no owner, stays refused. An object with no owner column keeps the creator rule. Edit (update) is not changed by this release.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* [ruling of record 7403225870, route A] The owner term a Modify All holder
* keeps beside an app-authored DELETE row policy.
*
* Beside an authored delete policy the OR term the platform floor keeps is the
* creator/owner term (ruling of record 7063130499, A), and its owner half is
* the one the declared write authority's `allow` speaks for (ruling of record
* 7288570890, C). For a caller who holds no Modify All on the object that
* `allow` IS ownership, so it admits the row whole
* (`security-plugin-rls-filter.ts`, `ownershipAllowAdmitsRow`). For a Modify
* All holder it is not: the write depth the bit manufactures is `org`, which
* matches every owned row, and the authority answers its bypass `allow` on the
* rest, so admitting on it would lift the authored policy for every row, which
* ADR-0066 ① forbids. Until this module that holder kept the creator half
* alone, and the owner of a row they did not create was refused a delete a
* plain owner gets: holding more permission gave less.
*
* The ruling: the owner term admits delete for every owner, Modify All holders
* included, and the bypass stays excluded. So the term handed back here is the
* record's OWNER, read off the row, `owner_id == current_user.id`, and nothing
* the bit or the authority's `allow` adds: never a row with no owner (the
* bypass's own reach, `ISharingService.checkDelete` step 7), never a row of
* another owner. It joins the OR beside the floor's creator term and the
* authored terms, so it only ever adds the caller's own rows.
*
* What it does NOT carry: someone above the owner by write depth. A Modify All
* set declares no depth of its own (the spec refuses a `writeScope` beside
* `modifyAllRecords`), so a holder's depth apart from the bit is `own` unless a
* second set declares one, and reading that depth's owner set takes the
* hierarchy resolution that lives behind the sharing service.
*
* Asked only where the composition is about to keep the floor beside an
* authored term for a Modify All holder whose by-id delete the declared write
* authority answered `allow` (`dropPlatformOwnershipFloor`), which it does only
* on an object whose delete ownership decides (an owner column, a model that
* does not open delete, a principal, no on-behalf-of identity). The owner
* column must also be provable from the object's declaration: a federated
* object whose `owner_id` is only the platform's injected anchor has no owner a
* row can be matched on (`isOwnerAnchorUnprovable`), so no term is handed back
* there and the holder keeps the composition as it stood.
*/

import type { RowLevelSecurityPolicy } from '@objectstack/spec/security';
import { isOwnerAnchorUnprovable } from '@objectstack/spec/data';

/** The column the platform stamps a record's owner in. */
const OWNER_FIELD = 'owner_id';

/** The owner half of the creator/owner term, as a row predicate. */
export const MODIFY_ALL_OWNER_DELETE_PREDICATE = `${OWNER_FIELD} == current_user.id`;

/**
* The name the term compiles under. Not a shipped policy: the floor's
* provenance test (`isPlatformOwnershipFloorPolicy`) never recognises it, so no
* floor replacement can take it out of the OR it was added to.
*/
export const MODIFY_ALL_OWNER_DELETE_TERM_NAME = 'platform_modify_all_owner_delete';

/**
* The owner term for `object`, or `null` where none applies. The caller has
* already decided the composition keeps the floor beside an authored term for
* a Modify All holder under an `allow`; this answers only whether the row can
* name its owner.
*/
export function modifyAllOwnerDeleteTerm(
object: string,
schema: unknown,
objectFields: ReadonlySet<string> | null | undefined,
): RowLevelSecurityPolicy | null {
// A schema that cannot be read, or a field set that could not be loaded,
// proves no owner column: no term, the composition as it stood.
if (!schema || !objectFields || !objectFields.has(OWNER_FIELD)) return null;
if (isOwnerAnchorUnprovable(schema)) return null;
return {
name: MODIFY_ALL_OWNER_DELETE_TERM_NAME,
object,
operation: 'delete',
using: MODIFY_ALL_OWNER_DELETE_PREDICATE,
} as RowLevelSecurityPolicy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
* Modify All stays out of that OR (ADR-0066 ①: it does not lift an authored
* write policy on a business object).
*
* Ruling of record 7403225870 (route A): the owner term admits delete for
* every owner, Modify All holders included, and the Modify All bypass stays
* excluded. A Modify All holder therefore deletes the rows they own beside the
* authored policy, and still no row they neither own nor created.
*
* 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
Expand Down Expand Up @@ -163,7 +168,7 @@ function fixturePlugin(): Plugin {
};
}

describe('an authored delete policy on an owned object: ownership admits the row beside it, Modify All does not', () => {
describe('an authored delete policy on an owned object: ownership admits the row beside it, the Modify All bypass does not', () => {
let kernel: ObjectKernel;
let ql: ObjectQL;
let security: Required<Pick<ISecurityService, 'explain'>>;
Expand Down Expand Up @@ -220,14 +225,17 @@ describe('an authored delete policy on an owned object: ownership admits the row
});

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)],
for (const [what, context, owner] of [
['the owner (not the creator)', member(OWNER, REP.name), OWNER],
['the manager above the owner (own_and_reports)', member(MANAGER, LEAD.name), OWNER],
['a holder of write depth org, without Modify All', member(ORG_DEPTH, WIDE.name), OWNER],
// [ruling of record 7403225870] Holding Modify All takes nothing away
// from what owning the row gives.
['a Modify All holder who owns the row (not the creator)', member(MODIFY_ALL, ADMIN.name), MODIFY_ALL],
] 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);
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);
Expand Down Expand Up @@ -256,6 +264,28 @@ describe('an authored delete policy on an owned object: ownership admits the row
}
});

describe('by id: the Modify All bypass stays out of the OR beside the authored policy (ADR-0066 ①)', () => {
for (const object of [DEAL, SECRET]) {
it(`${object}: a Modify All holder who owns other rows is refused a row someone else owns`, async () => {
const mine = await seed(object, MODIFY_ALL, CREATOR);
const theirs = await seed(object, OWNER, CREATOR);
const context = member(MODIFY_ALL, ADMIN.name);
expect(await explainDelete(object, theirs, context), 'explain agrees with the door').toBe(false);
expect(await deleteById(object, theirs, context)).toMatchObject({ kind: 'refused', status: 403 });
expect(await exists(object, theirs), 'the row stays').toBe(true);
expect(await exists(object, mine), 'nothing else moved').toBe(true);
});

it(`${object}: a Modify All holder is refused a row that has no owner, which only the bypass would admit`, async () => {
const id = await seed(object, null, CREATOR);
const context = member(MODIFY_ALL, ADMIN.name);
expect(await explainDelete(object, id, context), 'explain agrees with the door').toBe(false);
expect(await deleteById(object, id, context)).toMatchObject({ kind: 'refused', status: 403 });
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);
Expand All @@ -271,6 +301,14 @@ describe('an authored delete policy on an owned object: ownership admits the row
expect(await deleteById(MEMO, closed, member(STRANGER, REP.name))).toEqual({ kind: 'landed' });
});

it('an object with no owner column: a Modify All holder keeps the creator rule beside the authored policy', async () => {
const theirs = await seed(MEMO, null, CREATOR);
expect(await deleteById(MEMO, theirs, member(MODIFY_ALL, ADMIN.name))).toMatchObject({ kind: 'refused', status: 403 });
expect(await exists(MEMO, theirs)).toBe(true);
const mine = await seed(MEMO, null, MODIFY_ALL);
expect(await deleteById(MEMO, mine, member(MODIFY_ALL, ADMIN.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 () => {
Expand Down Expand Up @@ -300,18 +338,24 @@ describe('an authored delete policy on an owned object: ownership admits the row
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);
});
for (const object of [DEAL, SECRET]) {
it(`${object}: a Modify All holder's predicate delete removes the closed row, the one it created and the one it owns, and no other`, async () => {
const closed = await seed(object, OWNER, CREATOR, 'closed');
const created = await seed(object, OWNER, MODIFY_ALL, 'bulk');
const owned = await seed(object, MODIFY_ALL, CREATOR, 'bulk');
const other = await seed(object, OWNER, CREATOR, 'bulk');
const ownerless = await seed(object, null, CREATOR, 'bulk');
const out = await outcomeOf(ql.delete(object, {
where: { id: { $in: [closed, created, owned, other, ownerless] } }, multi: true, context: member(MODIFY_ALL, ADMIN.name),
} as never));
expect(out).toEqual({ kind: 'landed' });
expect(await exists(object, closed)).toBe(false);
expect(await exists(object, created)).toBe(false);
expect(await exists(object, owned), 'the row it owns is gone, as by id').toBe(false);
expect(await exists(object, other), 'Modify All does not lift the authored policy').toBe(true);
expect(await exists(object, ownerless), 'nor admit a row that has no owner').toBe(true);
});
}
});

it('edit is untouched by this composition: the authored policy is delete-only, and every member edits the deal', async () => {
Expand Down
Loading
Loading