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
4 changes: 3 additions & 1 deletion .changeset/pre/35802-predicate-write-floor-deferral.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ A predicate write (`update(object, data, { where, multi: true })`, `delete(objec

The predicate write now makes the by-id decision for each matched row the floor would refuse. Such a row is written when a write of that row by id would be, because the sharing service answers `allow` for it: ownership at the caller's write depth, Modify All, or, for an update only, an `edit` share or the row's queue. A row the service abstains on keeps the floor: on an object with no `owner_id`, and on a delete under `public_read_write` (an update there has no floor to keep, because that OWD opens row updates). A row it denies keeps the floor too. The row-level `check` on each updated row follows the same decision.

The by-id decision costs one sharing verdict, a few store reads, for each matched row the floor does not already admit. Measured on SQLite in memory:
The by-id decision is one sharing verdict for each matched row the floor does not already admit. The bundled sharing service (`@objectstack/plugin-sharing`) answers all of them through its set form, `checkEditMany` / `checkDeleteMany`, in chunks of 1,000 rows: one read of the chunk's rows and, for an update, one read of the caller's write-level shares on them, so the store reads grow per 1,000 rows rather than per row. Measured on SQLite over 10,000 rows owned by another user: a member's predicate update takes 24 store reads and about 0.27 s, the same delete 13 reads, and a Modify All holder's update of all 10,000 rows 14 reads, and it now writes those rows.

A sharing service that does not implement the set form is asked `checkEdit` / `checkDelete` once per row, 25 at a time, with the same answers, and then each verdict costs a few store reads. Measured on SQLite in memory with the per-row form:

- A member's predicate update matching 1,000 readable rows owned by other people, every verdict `deny`: 1,000 verdicts and 2,004 store reads, about 1.1 s, against 44 ms and 3 reads before.
- The same at 10,000 rows: 10,000 verdicts and 20,004 store reads, about 5.3 s, against 47 ms before.
Expand Down
46 changes: 44 additions & 2 deletions content/docs/kernel/runtime-services/sharing-service.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: services.sharing — the record share API
navTitle: services.sharing
description: Check and manage record-level sharing with services.sharing — read filters, canEdit and canDelete checks, and granting or revoking record shares.
description: Check and manage record-level sharing with services.sharing — read filters, the tri-state checkEdit and checkDelete verdicts and their canEdit and canDelete projections, the optional set form, and granting or revoking record shares.
---

- **Stability:** `stable`
Expand All @@ -12,8 +12,12 @@ description: Check and manage record-level sharing with services.sharing — rea

```ts
services.sharing.buildReadFilter(object: string, context: ExecutionContext): Promise<unknown | null>
services.sharing.checkEdit(object: string, recordId: string, context: ExecutionContext): Promise<SharingWriteVerdict>
services.sharing.checkDelete(object: string, recordId: string, context: ExecutionContext): Promise<SharingWriteVerdict>
services.sharing.canEdit(object: string, recordId: string, context: ExecutionContext): Promise<boolean>
services.sharing.canDelete(object: string, recordId: string, context: ExecutionContext): Promise<boolean>
services.sharing.checkEditMany?(object: string, recordIds: readonly string[], context: ExecutionContext): Promise<SharingWriteVerdict[]>
services.sharing.checkDeleteMany?(object: string, recordIds: readonly string[], context: ExecutionContext): Promise<SharingWriteVerdict[]>
services.sharing.canManageShares(object: string, recordId: string, context: ExecutionContext): Promise<boolean>
services.sharing.grant(input: GrantShareInput, context: ExecutionContext): Promise<RecordShare>
services.sharing.revoke(shareId: string, context: ExecutionContext, scope?: { object: string; recordId: string }): Promise<void>
Expand All @@ -26,6 +30,42 @@ Every method above **adjudicates access**, so each takes the complete
these signatures used to name was retired in #7218; pass the whole context you
were handed, unchanged.

## The write verdicts: tri-state, two-state and set form

Each write gate has a primary **tri-state** form, `checkEdit` (update) and
`checkDelete` (delete), which answers a `SharingWriteVerdict`:

- `allow` — a positive basis to permit: ownership (widened by write DEPTH), the
`modifyAllRecords` super-user bypass or a system context, and, for `checkEdit`
only, an `edit`-level share or a record sitting in a queue the caller belongs to.
- `abstain` — record sharing does not enforce on this row at all: a `public`
object, an object with no owner field, a bypass-listed platform internal, or an
object with no resolvable schema. The decision belongs to whatever else guards
the row. `checkDelete` abstains on exactly the rows `checkEdit` abstains on.
- `deny` — refused: a sharing-enforced row where the caller has none of the
bases above, a principal-less context, or a lookup that failed. A failure is
always `deny`, never `abstain`.

`canEdit` / `canDelete` are the **two-state projection** of those verdicts:
`true` for anything that is not `deny`, so `abstain` and `allow` both read as
`true`. That is right for a caller that only adds this gate to what already
guards the row, and wrong for one that would let the answer override another
authority's floor: such a caller must read `checkEdit` / `checkDelete`, so that
"sharing has no opinion" is never taken as permission.

`checkEditMany` / `checkDeleteMany` are the optional **set form** of the
tri-state verdicts: `answer[i]` is the verdict `checkEdit` / `checkDelete`
answers for `recordIds[i]` under the same context, with the same bases and the
same verb boundary, and the answer is exactly as long as `recordIds`. Only the
cost differs: the number of store reads grows with the number of chunks, not the
number of records. The bundled `SharingService` (`@objectstack/plugin-sharing`)
implements both, reading 1,000 ids per chunk. They are optional: a caller
feature-detects them (`typeof sharing.checkEditMany === 'function'`) and, where
a service does not implement them, asks `checkEdit` / `checkDelete` once per
record, with the same answers. The platform's predicate update and delete do
exactly that. A chunk whose lookup fails answers `deny` for each of its records.
A custom `ISharingService` that does not add the set form keeps working.

## Management authority (ADR-0111)

`grant` / `revoke` / `listShares` are **management operations**, enforced in the
Expand Down Expand Up @@ -54,7 +94,9 @@ mask AND-ed with object CRUD, not a fourth `access_level`.
## Returns

- `buildReadFilter`: `null` means unrestricted read; otherwise returns an engine filter
- `canEdit` / `canDelete` / `canManageShares`: boolean decisions (they return `false` rather than throwing)
- `checkEdit` / `checkDelete`: a `SharingWriteVerdict` (`'allow' | 'abstain' | 'deny'`); a failed lookup answers `deny` rather than throwing
- `checkEditMany` / `checkDeleteMany` (optional): one `SharingWriteVerdict` per id, in the order of `recordIds`
- `canEdit` / `canDelete` / `canManageShares`: boolean decisions (they return `false` rather than throwing); `canEdit` / `canDelete` are `true` for every verdict that is not `deny`
- `grant`/`listShares`: normalized `RecordShare` rows

## Typical Errors
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/plugin-security/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@

The predicate write now makes the by-id decision for each matched row the floor would refuse. Such a row is written when a write of that row by id would be, because the sharing service answers `allow` for it: ownership at the caller's write depth, Modify All, or, for an update only, an `edit` share or the row's queue. A row the service abstains on keeps the floor: on an object with no `owner_id`, and on a delete under `public_read_write` (an update there has no floor to keep, because that OWD opens row updates). A row it denies keeps the floor too. The row-level `check` on each updated row follows the same decision.

The by-id decision costs one sharing verdict, a few store reads, for each matched row the floor does not already admit. Measured on SQLite in memory:
The by-id decision is one sharing verdict for each matched row the floor does not already admit. The bundled sharing service (`@objectstack/plugin-sharing`) answers all of them through its set form, `checkEditMany` / `checkDeleteMany`, in chunks of 1,000 rows: one read of the chunk's rows and, for an update, one read of the caller's write-level shares on them, so the store reads grow per 1,000 rows rather than per row. Measured on SQLite over 10,000 rows owned by another user: a member's predicate update takes 24 store reads and about 0.27 s, the same delete 13 reads, and a Modify All holder's update of all 10,000 rows 14 reads, and it now writes those rows.

A sharing service that does not implement the set form is asked `checkEdit` / `checkDelete` once per row, 25 at a time, with the same answers, and then each verdict costs a few store reads. Measured on SQLite in memory with the per-row form:

- A member's predicate update matching 1,000 readable rows owned by other people, every verdict `deny`: 1,000 verdicts and 2,004 store reads, about 1.1 s, against 44 ms and 3 reads before.
- The same at 10,000 rows: 10,000 verdicts and 20,004 store reads, about 5.3 s, against 47 ms before.
Expand Down
Loading