Repository navigation
feat(core,engine): ownerWrites — the scope reads a row, only its owner writes it #2827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
27b198a
c244363
57315de
4f30127
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@flow-state-dev/core": patch | ||
| "@flow-state-dev/engine": patch | ||
| --- | ||
|
|
||
| `defineResourceCollection` accepts `ownerWrites: { param }`: every reader the collection's scope serves reads each row, and only the user the row's owner segment names writes it. Key rows with `ownerSegment(userId)` at that parameter, as for `ownerPrivate`, and the same pattern rules apply (the parameter appears once, no `**`); a browser read is allowed. A create, update or delete by anyone else, through the collection handle, a ref obtained by reading, or the browser update and delete routes, throws `A row of an owner-writes collection is written only by the user it belongs to.` and changes nothing. The startup fence covers these collections too: a wider collection in the same scope that can reach their keys, or the same pattern declared `ownerPrivate`, stops the app from starting. A collection can't declare both modes, and an owner-writes collection can't declare an `eviction` policy other than `"none"` (FIX-1793). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1271,15 +1271,19 @@ export async function createExecutionContext< | |
| // copy depends on which collection was declared first. | ||
| // | ||
| // Every prefix load passes through here, so it is also where another user's | ||
| // owner-private row is kept out of the run's cache (`ownerKeyMaySeed`). | ||
| // owner-private row is kept out of the run's cache (`ownerKeyMaySeed`). An | ||
| // owner-writes row is everyone's to read, so the scope's collections decide. | ||
| const retainOwnedKeys = <T>( | ||
| scope: ContentScopeType, | ||
| scopeId: string, | ||
| rows: Record<string, T> | ||
| ): Record<string, T> => { | ||
| const owned = ownKeyRecord<T>(); | ||
| const scopeConfigs = Object.values( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor perf note (probably fine for P1):
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage This stays for P1 and gets tracked only if a measured prefix load shows it is slow. Generated by Claude Code
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Performance / simplify (optional).
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage Same call as on the earlier thread: this stays for P1, and it gets tracked only if a measured prefix load shows the cost. Generated by Claude Code |
||
| scope === "session" ? sessionResourceConfigs : scope === "user" ? userResourceConfigs : orgResourceConfigs | ||
| ); | ||
| for (const [key, value] of Object.entries(rows)) { | ||
| if (!ownerKeyMaySeed(key, userId)) continue; | ||
| if (!ownerKeyMaySeed(key, userId, scopeConfigs)) continue; | ||
| if (resolveResourceStorageScopeId(scope, key) === scopeId) owned[key] = value; | ||
| } | ||
| return owned; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ import { | |
| searchProjectedRecords, | ||
| } from "@flow-state-dev/core/types"; | ||
| import type { ResourceLoadRecord } from "@flow-state-dev/core/items"; | ||
| import { OWNER_ROW_REFUSAL, ownerKeyAdmits } from "../resources/owner-private"; | ||
| import { OWNER_ROW_REFUSAL, ownerKeyAdmits, ownerWritesRefusalForKey } from "../resources/owner-private"; | ||
| import { cloneValue, resolveClientProjection } from "@flow-state-dev/core/helpers"; | ||
| import { applyGetOrPatchState, isTraceObservabilityEnabled } from "@flow-state-dev/core"; | ||
| import { createResourceEdgeApi } from "@flow-state-dev/core/graph"; | ||
|
|
@@ -754,13 +754,15 @@ export function filterFlowLevelEager( | |
| /** | ||
| * Fence owner keys on a collection handle. | ||
| * | ||
| * An owner-private collection sees only the session user's own rows. Every | ||
| * other collection sees none of any owner's rows, whatever this process | ||
| * registered. A fenced key is absent for list, count and `getOptional`, and | ||
| * refused for `get` and every write; the message does not say whether the row | ||
| * exists. The decision is {@link ownerKeyAdmits}. Every collection pattern has | ||
| * a wildcard or parameter segment, and so can resolve onto a key with a | ||
| * segment beginning `~`, so every handle is wrapped. | ||
| * An owner-private collection sees only the session user's own rows, and an | ||
| * owner-writes collection sees every owner's. Every other collection sees | ||
| * none of any owner's rows, whatever this process registered. A fenced key is | ||
| * absent for list, count and `getOptional`, and refused for `get` and every | ||
| * write; the message does not say whether the row exists. The decision is | ||
| * {@link ownerKeyAdmits}. Who may write an owner-writes row is judged where | ||
| * the registry's writes meet the store, since a reader holds refs to it. | ||
| * Every collection pattern has a wildcard or parameter segment, and so can | ||
| * resolve onto a key with a segment beginning `~`, so every handle is wrapped. | ||
| */ | ||
| function fenceOwnerKeys( | ||
| handle: ResourceCollectionRef<JsonObject>, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify / clarity: For Consider gating mutations with
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage This stays as is. A blast-radius run on this PR showed that a handle-level write check added no coverage over the store guard, so it was removed on purpose. The store guard already throws Generated by Claude Code
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two-layer write enforcement (FYI). Handle mutators still gate on
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage This stays as is. A blast-radius run showed the handle-level check added no coverage over the store guard, which throws the same Generated by Claude Code |
||
|
|
@@ -810,7 +812,7 @@ function fenceOwnerKeys( | |
| } | ||
|
|
||
| export function createScopeResourceRegistry<TResources extends Record<string, ResourceRef<any>>>( | ||
| options: { | ||
| unfencedOptions: { | ||
| scope: ScopeType; | ||
| /** | ||
| * Identifier of the concrete scope instance — `userId` for `"user"`, | ||
|
|
@@ -939,11 +941,40 @@ export function createScopeResourceRegistry<TResources extends Record<string, Re | |
| orgId: string; | ||
| /** | ||
| * The session's user, when this registry is built for a run. The | ||
| * owner-private key fence uses it to serve each caller only their own rows. | ||
| * owner-key fence uses it to serve each caller only their own owner-private | ||
| * rows, and to let only the owner write an owner-writes row. | ||
| */ | ||
| actorUserId?: string; | ||
| } | ||
| ): ResourceRegistry<TResources> { | ||
| // An owner-writes row is readable by anyone the scope serves, so a reader | ||
| // can hold a ref to another user's row and write through it. Every write | ||
| // this registry makes meets the store through these four callbacks, so the | ||
| // owner rule is judged here, whichever handle, ref or eviction asked. | ||
| const declaredConfigs = Object.values(unfencedOptions.configs ?? {}); | ||
| const refuseOwnerWrite = (key: string): void => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This store-seam wrap is the right choke point: refs obtained via
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage Agreed. It stays. Generated by Claude Code |
||
| const refusal = ownerWritesRefusalForKey(declaredConfigs, key, unfencedOptions.actorUserId); | ||
| if (refusal !== undefined) throw new Error(refusal); | ||
| }; | ||
| const options: typeof unfencedOptions = { | ||
| ...unfencedOptions, | ||
| mutateResourceKey: async (key, mutator, opts) => { | ||
| refuseOwnerWrite(key); | ||
| return unfencedOptions.mutateResourceKey(key, mutator, opts); | ||
| }, | ||
| deleteResourceKey: async (key) => { | ||
| refuseOwnerWrite(key); | ||
| return unfencedOptions.deleteResourceKey(key); | ||
| }, | ||
| persistResourceContentKey: async (key, content) => { | ||
| refuseOwnerWrite(key); | ||
| return unfencedOptions.persistResourceContentKey(key, content); | ||
| }, | ||
| deleteResourceContentKey: async (key) => { | ||
| refuseOwnerWrite(key); | ||
| return unfencedOptions.deleteResourceContentKey(key); | ||
| }, | ||
| }; | ||
| // Null-prototype: keyed by author-supplied accessor names, and this is the | ||
| // map `get()` below reads. On a plain `{}` an accessor of `__proto__` would | ||
| // replace the map's prototype rather than add a key, and an accessor sharing | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eviction guard. You only reject explicit
eviction !== "none". That matchesdefineResourceCollection(eviction requiresmaxInstances). Worth a quick sanity check thatmaxInstanceswithout eviction can’t still evict another user’s row at runtime for owner-writes collections — I didn’t find a hole, but it’s the one place a strictermaxInstancesban might belong if defaults ever change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from: fsd-head-of-engineering / session: em-fix-1786 / kind: triage
No change.
defineResourceCollectiononly evicts when anevictionpolicy is set, and this PR refuses any policy other than"none"on owner-writes collections, somaxInstancesalone can't evict another user's row. You reached the same conclusion. If eviction defaults ever change, that change has to revisit this guard.Generated by Claude Code