Skip to content
21 changes: 21 additions & 0 deletions .changeset/30906-cbp-null-master-write-gate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@objectstack/plugin-security': minor
'@objectstack/verify': minor
---

feat(security, verify): the `controlled_by_parent` write gate judges a detail row whose optional master is empty — its owner or Modify All on the detail writes it

Clause-②: yes
Metadata-protocol: semantic
Skills-docs: follow-up #30907

A `controlled_by_parent` object whose only `master_detail` is declared `required: false` (an optional master) could already be declared, and its rows that name no master were already read by their owner and by View All on the detail. Writing such a row was refused with `422 MISSING_REQUIRED_FIELD`. It is now judged:

- **Create.** An insert that names no master is admitted to anyone the detail's own `create` grant admits, and its creator becomes its owner. A batch is admitted the same way; every master its rows do name is still asked.
- **Update and delete.** A stored row that names no master is written by its owner (`owner_id` equal to the caller) and by a holder of `modifyAllRecords` on the detail, and refused `403 PERMISSION_DENIED` for everyone else, a holder of View All included. A row with no owner (a queue row), and every row of an object with no `owner_id` column, is Modify All's alone. The same rule judges each row a predicate update or delete (`multi: true`) selects, both principals of an on-behalf-of write, and `ISecurityService.checkControlledByParentWrite` (a refusal there is `deny` on the `master_chain` leg).
- **Setting a master** (none → M, M1 → M2) asks edit on the master the body names, as before. **Emptying one** (M → none) asks edit on the master the row leaves: the `sharing.controlledByParent.ownerMayEdit` opt-in, which lets an owner update a row with read on its master, does not cover a body that empties the master, so an owner who only reads the account cannot detach a contact and make it private.
- **Chains.** A master that is itself `controlled_by_parent` with an optional master of its own, and names none, ends the chain: writing its details asks the same rule of that row (its owner, or Modify All on its object).
- Ownership transfer is unchanged: a body that names another owner is still gated by `allowTransfer`.
- **`@objectstack/verify`.** The parent write reach family proves an optional-master detail in both cases: its records under a master, as every detail, and its records that name none, by the rule above (`ParentWriteReachReach.masters: 'none'`, with `masterRead: 'none'` and `unreadableDetails`).

A required master is unchanged: a row whose stored required master is empty still answers `422 MISSING_REQUIRED_FIELD`.

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion packages/plugins/plugin-security/src/explain-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1213,12 +1213,28 @@ const MASTER_CHECKED_BY_ID_WRITES: ReadonlySet<string> = new Set(['update', 'del
function masterWriteCheckRefusal(
outcome: ControlledByParentWriteOutcome,
verb: string,
/**
* [ADR-0055 §3 as amended · #30905 ruling A] The record's master reference
* key when its OPTIONAL master is empty ({@link nullMasterReferenceOf}):
* the check then judged the record by its owner rule, not by a master, and
* its `master_chain` refusal is narrated as that rule.
*/
nullMasterFk?: string,
): { outcome: ExplainRecordAttribution['outcome']; detail: string } | undefined {
switch (outcome?.outcome) {
case 'allow':
case 'not_applicable':
return undefined;
case 'deny':
if (nullMasterFk !== undefined && outcome.leg === 'master_chain') {
return {
outcome: 'excluded',
detail: `${MASTER_DERIVED} when it names one, and it names none: its master reference '${nullMasterFk}' is ` +
`empty, so the master-detail write check the by-id ${verb} runs admits only its owner and a holder of ` +
`Modify All on the object, and refuses this ${verb} on its 'master_chain' leg. ` +
`The ${verb} answers 403 PERMISSION_DENIED.`,
};
}
return {
outcome: 'excluded',
detail: `${MASTER_DERIVED}: the master-detail write check the by-id ${verb} runs refuses this ${verb} on its ` +
Expand Down Expand Up @@ -2011,7 +2027,7 @@ async function applyRecordAttribution(
const masterFault = masterOrFault === DEPENDENCY_FAULT;
const masterRefusal = masterOrFault === undefined || masterOrFault === DEPENDENCY_FAULT
? undefined
: masterWriteCheckRefusal(masterOrFault, engineOp);
: masterWriteCheckRefusal(masterOrFault, engineOp, nullMasterReferenceOf(deps, object, record as Record<string, unknown>));
// [ADR-0055] A record-grained read of a `controlled_by_parent` record meets
// a master leg too: the read door ANDs the record's master-derived read scope
// (the master ids the principal can read, the chain included) into the find,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ export function resolveCbpRelation(
// the validator's test it mirrors and cannot widen if the resolver
// changes — and it did: an OPTIONAL master (#30905 ruling A) resolves
// with `required: false`, so this is `false` there and the master gate
// keeps answering an insert that leaves the reference empty (the write
// gate is unchanged by that ruling: such an insert is still refused).
// keeps answering an insert that leaves the reference empty itself: it
// admits it, by the optional master's own rule (`CbpRelation.optional`
// in the write gate — the row is then its creator's), never by handing
// the omission to validation.
omissionRefusedByValidation:
def?.type === 'master_detail' && !!def?.required && !def?.readonly && !def?.system,
// [#25095] From the same def, asked of the parse's own predicate.
Expand Down
Loading
Loading