@fabrika/db 0.9.0-dev.13 → 0.9.0-dev.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/src/authority.ts +339 -54
- package/src/capability.ts +32 -4
- package/src/framework.ts +1 -1
- package/src/guard.ts +68 -5
- package/src/index.ts +1 -0
- package/src/job-leases.ts +3 -2
- package/src/nodes.ts +12 -0
- package/src/orm.ts +23 -13
- package/src/policy.ts +438 -39
- package/src/registry.ts +30 -1
- package/src/sql.ts +1 -2
- package/src/sqlite.ts +46 -1
- package/src/store.ts +853 -78
- package/src/tables.ts +42 -0
- package/src/vocabulary-tables.ts +14 -2
- package/types/src/authority.d.ts +94 -19
- package/types/src/capability.d.ts +31 -4
- package/types/src/framework.d.ts +1 -1
- package/types/src/guard.d.ts +8 -0
- package/types/src/index.d.ts +1 -1
- package/types/src/job-leases.d.ts +2 -1
- package/types/src/nodes.d.ts +2 -0
- package/types/src/policy.d.ts +79 -6
- package/types/src/registry.d.ts +18 -1
- package/types/src/store.d.ts +32 -7
- package/types/src/vocabulary-tables.d.ts +26 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"test": "bun test"
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
95
|
+
"@fabrika/auth-core": "0.9.0-dev.15",
|
|
96
|
+
"@fabrika/platform": "0.9.0-dev.15",
|
|
97
|
+
"@fabrika/platform-node": "0.9.0-dev.15",
|
|
98
98
|
"kysely": "0.29.6",
|
|
99
99
|
"kysely-postgres-js": "5.0.1",
|
|
100
100
|
"zod": "^3.25.76"
|
package/src/authority.ts
CHANGED
|
@@ -4,9 +4,10 @@
|
|
|
4
4
|
|
|
5
5
|
import { AndNode, BinaryOperationNode, type OperationNode, OperatorNode, ParensNode, ValueNode } from 'kysely'
|
|
6
6
|
import { ownEntry } from './builtins'
|
|
7
|
-
import type { RefGrant, WithinGrant } from './capability'
|
|
7
|
+
import type { ContainerGrant, RefGrant, RelationHold, WithinGrant } from './capability'
|
|
8
8
|
import { conditionNode, subquery } from './condition-nodes'
|
|
9
|
-
import {
|
|
9
|
+
import type { ConditionModel } from './conditions'
|
|
10
|
+
import { CapabilityError, PolicyError } from './errors'
|
|
10
11
|
import type { ViewParentLink } from './guard'
|
|
11
12
|
import { linksPredicate, linkValues } from './links'
|
|
12
13
|
import { allOf, anyOf, column, equals, valueList } from './nodes'
|
|
@@ -23,23 +24,85 @@ import {
|
|
|
23
24
|
|
|
24
25
|
/** A relation of the layer: its source table, and the tables whose rows it reaches reveal an ancestor node. */
|
|
25
26
|
export interface LayerRelation extends RelationSource {
|
|
27
|
+
readonly kind: 'source'
|
|
26
28
|
readonly reveals: readonly RevealModel[]
|
|
29
|
+
/** The actions some table's `relationWhen` narrows the relation for: it holds them only on the rows that condition admits. */
|
|
30
|
+
readonly narrowed: readonly string[]
|
|
31
|
+
/**
|
|
32
|
+
* The source's and each reveal table's column for every dimension a row of it lies at, by table: where a hold inside
|
|
33
|
+
* a node finds whether a row that holds a destination lies inside that node.
|
|
34
|
+
*/
|
|
35
|
+
readonly placed: Readonly<Record<string, Readonly<Record<string, string>>>>
|
|
27
36
|
}
|
|
28
37
|
|
|
29
|
-
/**
|
|
30
|
-
export
|
|
38
|
+
/** A relation no table confers: only access views decide what it reaches, so it has no source row to check or lock. */
|
|
39
|
+
export interface AccessRelation {
|
|
40
|
+
readonly kind: 'access'
|
|
41
|
+
readonly relation: string
|
|
42
|
+
readonly dimension: string
|
|
43
|
+
readonly narrowed: readonly string[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Every relation of the layer, by name, with its source table, or the mark that only access views serve it. */
|
|
47
|
+
export type RelationSources = Readonly<Record<string, LayerRelation | AccessRelation>>
|
|
31
48
|
|
|
32
49
|
/** Every member reveal of the layer, by the dimension whose grants it reveals to. */
|
|
33
50
|
export type MemberReveals = Readonly<Record<string, readonly MemberRevealModel[]>>
|
|
34
51
|
|
|
52
|
+
/** What a capability is held through beyond its grants: the layer's relations and its member reveals. */
|
|
53
|
+
export interface LayerReach {
|
|
54
|
+
readonly relations: RelationSources
|
|
55
|
+
readonly members: MemberReveals
|
|
56
|
+
}
|
|
57
|
+
|
|
35
58
|
export function relationSource(relations: RelationSources, relation: string): LayerRelation {
|
|
36
|
-
const source =
|
|
37
|
-
if (source ===
|
|
38
|
-
throw new CapabilityError('unenforceable', `the
|
|
59
|
+
const source = knownRelation(relations, relation)
|
|
60
|
+
if (source.kind === 'access') {
|
|
61
|
+
throw new CapabilityError('unenforceable', `the relation ${relation} is served by access views only: it has no source row to check or lock`)
|
|
39
62
|
}
|
|
40
63
|
return source
|
|
41
64
|
}
|
|
42
65
|
|
|
66
|
+
/** The relation by name, conferred or served by access views; one the layer does not know is unenforceable, never ignored. */
|
|
67
|
+
export function knownRelation(relations: RelationSources, relation: string): LayerRelation | AccessRelation {
|
|
68
|
+
const known = ownEntry(relations, relation)
|
|
69
|
+
if (known === undefined) {
|
|
70
|
+
throw new CapabilityError('unenforceable', `the capability is held through the relation ${relation}, which no table of the layer confers`)
|
|
71
|
+
}
|
|
72
|
+
return known
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The rows of `model` the relation's access views list for the caller: `T.<key> in (select V.<row> from V where
|
|
77
|
+
* V.<principal> = $me [and V.<member> in (<nodes>)])`. A hold inside a node binds `member` to the node; a view with only
|
|
78
|
+
* a member binding lists nothing for a hold anywhere, and one with only a principal binding is narrowed to the node by
|
|
79
|
+
* the table's own column as every relation term is. The view's SQL decides the rest; nothing of a handler is bound.
|
|
80
|
+
*/
|
|
81
|
+
function accessTerms(
|
|
82
|
+
model: TableModel,
|
|
83
|
+
key: string,
|
|
84
|
+
hold: RelationHold,
|
|
85
|
+
principal: string,
|
|
86
|
+
): { readonly bound: readonly OperationNode[]; readonly free: readonly OperationNode[] } {
|
|
87
|
+
const bound: OperationNode[] = []
|
|
88
|
+
const free: OperationNode[] = []
|
|
89
|
+
for (let index = 0; index < model.access.length; index++) {
|
|
90
|
+
const access = model.access[index]
|
|
91
|
+
if (access === undefined || access.relation !== hold.relation) continue
|
|
92
|
+
const parts: OperationNode[] = []
|
|
93
|
+
if (access.member !== undefined) {
|
|
94
|
+
// A node of another dimension (a grant at an ancestor) is no key the member column holds.
|
|
95
|
+
if (hold.inside === null || hold.inside.dimension !== access.dimension || hold.inside.values.length === 0) continue
|
|
96
|
+
parts[parts.length] = isIn(access.view, access.member, valueList(hold.inside.values))
|
|
97
|
+
}
|
|
98
|
+
if (access.principal !== undefined) parts[parts.length] = equals(access.view, access.principal, principal)
|
|
99
|
+
const term = isIn(model.name, key, subquery(access.view, access.row, allOf(parts)))
|
|
100
|
+
if (access.member === undefined) free[free.length] = term
|
|
101
|
+
else bound[bound.length] = term
|
|
102
|
+
}
|
|
103
|
+
return { bound, free }
|
|
104
|
+
}
|
|
105
|
+
|
|
43
106
|
/** A source row that confers its relation now: unexpired, and admitted by the relation's own condition. */
|
|
44
107
|
export function activeRow(source: RelationSource, now: Date): readonly OperationNode[] {
|
|
45
108
|
const terms: OperationNode[] = []
|
|
@@ -71,7 +134,7 @@ function relationNodes(source: RelationSource, principal: string, now: Date): Op
|
|
|
71
134
|
return subquery(source.table, source.node, allOf([namesPrincipal(source, principal), ...activeRow(source, now)]))
|
|
72
135
|
}
|
|
73
136
|
|
|
74
|
-
function isIn(table: string, name: string, nodes: OperationNode): OperationNode {
|
|
137
|
+
export function isIn(table: string, name: string, nodes: OperationNode): OperationNode {
|
|
75
138
|
return BinaryOperationNode.create(column(table, name), OperatorNode.create('in'), nodes)
|
|
76
139
|
}
|
|
77
140
|
|
|
@@ -108,33 +171,142 @@ export function revealedNodes(source: RelationSource, reveal: RevealModel, princ
|
|
|
108
171
|
return subquery(reveal.table, reveal.column, where)
|
|
109
172
|
}
|
|
110
173
|
|
|
174
|
+
/** Which secondary paths a statement follows: every one for a `Within`'s read, `heldThrough` for a `Ref`, none for a move. */
|
|
175
|
+
export type PathRule = (path: SecondaryPath) => boolean
|
|
176
|
+
|
|
177
|
+
export const everyPath: PathRule = () => true
|
|
178
|
+
export const noPath: PathRule = () => false
|
|
179
|
+
|
|
180
|
+
/** The paths that hold `action` for a `Ref` (`writeThrough`). */
|
|
181
|
+
export function heldThrough(action: string): PathRule {
|
|
182
|
+
return (path) => includes(path.writes, action)
|
|
183
|
+
}
|
|
184
|
+
|
|
111
185
|
/**
|
|
112
186
|
* The rows of `model` a relation reaches for `principal` under `action`: `T.<node> in (select S.<node> from S where
|
|
113
187
|
* S.<principal> = $me and <active>)` where `model` lies at a node of the relation's dimension, and one
|
|
114
188
|
* `T.<ancestor> in (<revealed nodes>)` per reveal of `action` to an ancestor `model` lies at. Empty where neither holds.
|
|
115
|
-
*
|
|
116
|
-
*
|
|
189
|
+
* It also follows the secondary paths `paths` admits: the column `model` reaches the relation's dimension through, and
|
|
190
|
+
* the reveals matched through one. A hold inside a node narrows each term to the rows inside it, by `model`'s own
|
|
191
|
+
* column for the hold's dimension, and reaches none where `model` has no such column. Where `model`'s `relationWhen`
|
|
192
|
+
* narrows the relation for `action`, each term carries that condition too: `(<term> and (<narrowing>))`, so no builder
|
|
193
|
+
* of a relation's reach leaves either out.
|
|
117
194
|
*/
|
|
118
195
|
export function relationTerms(
|
|
119
196
|
relations: RelationSources,
|
|
120
197
|
model: TableModel,
|
|
121
|
-
|
|
198
|
+
hold: RelationHold,
|
|
122
199
|
principal: string,
|
|
123
200
|
action: string,
|
|
124
201
|
now: Date,
|
|
125
|
-
|
|
202
|
+
paths: PathRule,
|
|
126
203
|
): readonly OperationNode[] {
|
|
127
|
-
const
|
|
204
|
+
const relation = hold.relation
|
|
205
|
+
const known = knownRelation(relations, relation)
|
|
206
|
+
const access = accessReach(model, hold, principal, action, now)
|
|
207
|
+
if (known.kind === 'access') {
|
|
208
|
+
return access
|
|
209
|
+
}
|
|
210
|
+
const narrowing: OperationNode[] = []
|
|
211
|
+
if (hold.inside !== null) {
|
|
212
|
+
const column = nodeColumnOf(model, hold.inside.dimension)
|
|
213
|
+
if (column === undefined || hold.inside.values.length === 0) {
|
|
214
|
+
return access
|
|
215
|
+
}
|
|
216
|
+
narrowing[narrowing.length] = isIn(model.name, column, valueList(hold.inside.values))
|
|
217
|
+
}
|
|
218
|
+
const source = known
|
|
219
|
+
const rest: OperationNode[] = []
|
|
128
220
|
const at = nodeColumnOf(model, source.dimension)
|
|
129
|
-
|
|
130
|
-
|
|
221
|
+
if (at !== undefined) rest[rest.length] = isIn(model.name, at, relationNodes(source, principal, now))
|
|
222
|
+
const others = relationTermsOf(source, model, principal, action, now, paths, rest)
|
|
223
|
+
return [...access, ...narrowedTerms(model, relation, action, now, others, narrowing)]
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The rows of `model` its access views list for `hold`, each carrying the relation's `relationWhen` for `action`. A view
|
|
228
|
+
* bound to the hold's node decides which rows lie inside it, so the table's own column does not narrow it; a view bound
|
|
229
|
+
* to the principal alone is narrowed to the node by that column, and reaches nothing where `model` has none.
|
|
230
|
+
*/
|
|
231
|
+
export function accessReach(model: TableModel, hold: RelationHold, principal: string, action: string, now: Date): readonly OperationNode[] {
|
|
232
|
+
const { bound, free } = accessTerms(model, keyColumnOf(model), hold, principal)
|
|
233
|
+
const terms = narrowedByRelation(model, hold.relation, action, now, bound)
|
|
234
|
+
if (free.length === 0) {
|
|
235
|
+
return terms
|
|
236
|
+
}
|
|
237
|
+
if (hold.inside === null) {
|
|
238
|
+
return [...terms, ...narrowedByRelation(model, hold.relation, action, now, free)]
|
|
239
|
+
}
|
|
240
|
+
const column = nodeColumnOf(model, hold.inside.dimension)
|
|
241
|
+
if (column === undefined || hold.inside.values.length === 0) {
|
|
242
|
+
return terms
|
|
243
|
+
}
|
|
244
|
+
return [...terms, ...narrowedTerms(model, hold.relation, action, now, free, [isIn(model.name, column, valueList(hold.inside.values))])]
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** The key column a row of `model` is named by: a child's `key`, a dimension table's own key. */
|
|
248
|
+
function keyColumnOf(model: TableModel): string {
|
|
249
|
+
if (model.scope.mode === 'child' || model.scope.mode === 'dimension') return model.scope.key
|
|
250
|
+
throw new PolicyError(`${model.name}: an access view names a row of a dimension or child table only`)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** `terms` with the relation's `relationWhen` for `action`, when the table declares one. */
|
|
254
|
+
function narrowedByRelation(
|
|
255
|
+
model: TableModel,
|
|
256
|
+
relation: string,
|
|
257
|
+
action: string,
|
|
258
|
+
now: Date,
|
|
259
|
+
terms: readonly OperationNode[],
|
|
260
|
+
): readonly OperationNode[] {
|
|
261
|
+
return narrowedTerms(model, relation, action, now, terms, [])
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function narrowedTerms(
|
|
265
|
+
model: TableModel,
|
|
266
|
+
relation: string,
|
|
267
|
+
action: string,
|
|
268
|
+
now: Date,
|
|
269
|
+
terms: readonly OperationNode[],
|
|
270
|
+
inside: readonly OperationNode[],
|
|
271
|
+
): readonly OperationNode[] {
|
|
272
|
+
const narrowing: OperationNode[] = [...inside]
|
|
273
|
+
const condition = relationCondition(model, relation, action)
|
|
274
|
+
if (condition !== undefined) {
|
|
275
|
+
narrowing[narrowing.length] = narrowing.length === 0
|
|
276
|
+
? conditionNode(model.name, condition, now)
|
|
277
|
+
: ParensNode.create(conditionNode(model.name, condition, now))
|
|
278
|
+
}
|
|
279
|
+
if (narrowing.length === 0) {
|
|
280
|
+
return terms
|
|
281
|
+
}
|
|
282
|
+
const narrowed: OperationNode[] = []
|
|
283
|
+
for (let index = 0; index < terms.length; index++) {
|
|
284
|
+
const term = terms[index]
|
|
285
|
+
if (term !== undefined) narrowed[narrowed.length] = narrowedTerm(term, allOf(narrowing))
|
|
286
|
+
}
|
|
287
|
+
return narrowed
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** The secondary-path and reveal terms of a conferred relation, appended to `terms`. */
|
|
291
|
+
function relationTermsOf(
|
|
292
|
+
source: LayerRelation,
|
|
293
|
+
model: TableModel,
|
|
294
|
+
principal: string,
|
|
295
|
+
action: string,
|
|
296
|
+
now: Date,
|
|
297
|
+
paths: PathRule,
|
|
298
|
+
terms: OperationNode[],
|
|
299
|
+
): readonly OperationNode[] {
|
|
300
|
+
for (let index = 0; index < model.readThrough.length; index++) {
|
|
131
301
|
const path = model.readThrough[index]
|
|
132
|
-
const through = path === undefined
|
|
302
|
+
const through = path === undefined || !paths(path)
|
|
303
|
+
? undefined
|
|
304
|
+
: throughPath(model.name, path, source.dimension, relationNodes(source, principal, now))
|
|
133
305
|
if (through !== undefined) terms[terms.length] = through
|
|
134
306
|
}
|
|
135
307
|
for (let index = 0; index < source.reveals.length; index++) {
|
|
136
308
|
const reveal = source.reveals[index]
|
|
137
|
-
const held = reveal !== undefined && includes(reveal.actions, action) && (
|
|
309
|
+
const held = reveal !== undefined && includes(reveal.actions, action) && (reveal.path === undefined || paths(reveal.path))
|
|
138
310
|
const placed = reveal === undefined || !held ? undefined : nodeColumnOf(model, reveal.dimension)
|
|
139
311
|
if (reveal !== undefined && placed !== undefined) {
|
|
140
312
|
terms[terms.length] = isIn(model.name, placed, revealedNodes(source, reveal, principal, now))
|
|
@@ -143,39 +315,82 @@ export function relationTerms(
|
|
|
143
315
|
return terms
|
|
144
316
|
}
|
|
145
317
|
|
|
318
|
+
/** `holds` without those of a relation `narrowed` names: the holds no `relationWhen` narrows. */
|
|
319
|
+
export function holdsBesides(holds: readonly RelationHold[], narrowed: readonly string[]): readonly RelationHold[] {
|
|
320
|
+
const kept: RelationHold[] = []
|
|
321
|
+
for (let index = 0; index < holds.length; index++) {
|
|
322
|
+
const hold = holds[index]
|
|
323
|
+
if (hold !== undefined && !includes(narrowed, hold.relation)) kept[kept.length] = hold
|
|
324
|
+
}
|
|
325
|
+
return kept
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** The condition `model`'s `relationWhen` puts on the rows `relation` reaches for `action`, if any. */
|
|
329
|
+
export function relationCondition(model: TableModel, relation: string, action: string): ConditionModel | undefined {
|
|
330
|
+
const conditions = ownEntry(model.relationWhen, relation)
|
|
331
|
+
return conditions === undefined ? undefined : ownEntry(conditions, action)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** `(<term> and (<narrowing>))`: one branch of a reach, narrowed; the one shape the guard admits a branch's condition in. */
|
|
335
|
+
export function narrowedTerm(term: OperationNode, narrowing: OperationNode): OperationNode {
|
|
336
|
+
return ParensNode.create(AndNode.create(term, ParensNode.create(narrowing)))
|
|
337
|
+
}
|
|
338
|
+
|
|
146
339
|
/**
|
|
147
|
-
* The rows of `model` a `Within`'s read reaches through member reveals:
|
|
148
|
-
*
|
|
149
|
-
* ancestor `model` lies at. A grant at a dimension that reveals no node for `action` is refused: the framework passes
|
|
150
|
-
* none.
|
|
340
|
+
* The rows of `model` a `Within`'s read reaches through member reveals: `revealingTerms` of its grants at revealing
|
|
341
|
+
* dimensions.
|
|
151
342
|
*/
|
|
152
343
|
export function memberTerms(members: MemberReveals, model: TableModel, grant: WithinGrant, now: Date): readonly OperationNode[] {
|
|
344
|
+
return revealingTerms(members, model, `the Within of ${grant.dimension}`, grant.action, grant.revealing, now, everyPath)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* The rows of `model` that `revealing`, grants of `action` at nodes of revealing dimensions, reach through member
|
|
349
|
+
* reveals: per grant, `T.<ancestor> in (select R.<ancestor> from R where R.<key> in (values) and <when>)` for each reveal
|
|
350
|
+
* of `action` to an ancestor `model` lies at, `R.<key>` the row's key or its path's column. A reveal through a path
|
|
351
|
+
* counts where `paths` admits it. A grant at a dimension that reveals no node for `action` is refused: the framework
|
|
352
|
+
* passes none.
|
|
353
|
+
*/
|
|
354
|
+
export function revealingTerms(
|
|
355
|
+
members: MemberReveals,
|
|
356
|
+
model: TableModel,
|
|
357
|
+
holder: string,
|
|
358
|
+
action: string,
|
|
359
|
+
revealing: readonly ContainerGrant[],
|
|
360
|
+
now: Date,
|
|
361
|
+
paths: PathRule,
|
|
362
|
+
): readonly OperationNode[] {
|
|
153
363
|
const terms: OperationNode[] = []
|
|
154
|
-
for (let index = 0; index <
|
|
155
|
-
const
|
|
156
|
-
if (
|
|
157
|
-
const reveals = ownEntry(members,
|
|
364
|
+
for (let index = 0; index < revealing.length; index++) {
|
|
365
|
+
const grant = revealing[index]
|
|
366
|
+
if (grant === undefined) continue
|
|
367
|
+
const reveals = ownEntry(members, grant.dimension) ?? []
|
|
158
368
|
let named = false
|
|
159
369
|
for (let position = 0; position < reveals.length; position++) {
|
|
160
370
|
const reveal = reveals[position]
|
|
161
|
-
if (reveal === undefined || !includes(reveal.actions,
|
|
371
|
+
if (reveal === undefined || !includes(reveal.actions, action) || (reveal.path !== undefined && !paths(reveal.path))) continue
|
|
162
372
|
named = true
|
|
163
373
|
const placed = nodeColumnOf(model, reveal.dimension)
|
|
164
|
-
if (placed === undefined ||
|
|
165
|
-
|
|
166
|
-
const where = reveal.when === undefined ? reached : AndNode.create(reached, ParensNode.create(conditionNode(reveal.table, reveal.when, now)))
|
|
167
|
-
terms[terms.length] = isIn(model.name, placed, subquery(reveal.table, reveal.column, where))
|
|
374
|
+
if (placed === undefined || grant.values.length === 0) continue
|
|
375
|
+
terms[terms.length] = isIn(model.name, placed, memberRevealed(reveal, grant.values, now))
|
|
168
376
|
}
|
|
169
377
|
if (!named) {
|
|
170
378
|
throw new CapabilityError(
|
|
171
379
|
'unenforceable',
|
|
172
|
-
|
|
380
|
+
`${holder} carries a grant on ${grant.dimension}, whose rows reveal no node for ${action}`,
|
|
173
381
|
)
|
|
174
382
|
}
|
|
175
383
|
}
|
|
176
384
|
return terms
|
|
177
385
|
}
|
|
178
386
|
|
|
387
|
+
/** `select R.<ancestor> from R where R.<key> in (values) and <when>`: the nodes a member reveal holds at for `values`. */
|
|
388
|
+
export function memberRevealed(reveal: MemberRevealModel, values: readonly string[], now: Date): OperationNode {
|
|
389
|
+
const reached = isIn(reveal.table, reveal.key, valueList(values))
|
|
390
|
+
const where = reveal.when === undefined ? reached : AndNode.create(reached, ParensNode.create(conditionNode(reveal.table, reveal.when, now)))
|
|
391
|
+
return subquery(reveal.table, reveal.column, where)
|
|
392
|
+
}
|
|
393
|
+
|
|
179
394
|
/** The condition `action` holds a row of `model` under, or `undefined` when the action is not narrowed there. */
|
|
180
395
|
export function actionCondition(model: TableModel, action: string, now: Date): OperationNode | undefined {
|
|
181
396
|
const condition = ownEntry(model.when, action)
|
|
@@ -194,61 +409,131 @@ export function conditioned(model: TableModel, action: string, branch: Operation
|
|
|
194
409
|
|
|
195
410
|
/**
|
|
196
411
|
* A Ref's predicate over `links` of `model`: one equality per link of its chain (after `row`, the equality that
|
|
197
|
-
* addresses one row by its key), then, when the Ref is held through relations, a row of one of them
|
|
198
|
-
* its action's condition. A
|
|
199
|
-
* unconditioned.
|
|
412
|
+
* addresses one row by its key), then, when the Ref is held through relations or member reveals, a row of one of them
|
|
413
|
+
* at the node, and its action's condition. A Ref whose relations, revealing grants and paths together reach no row of
|
|
414
|
+
* `model` is unenforceable there, never unconditioned.
|
|
200
415
|
*/
|
|
201
416
|
export function refScope(
|
|
202
|
-
|
|
417
|
+
reach: LayerReach,
|
|
203
418
|
model: TableModel,
|
|
204
419
|
links: readonly Container[],
|
|
205
420
|
grant: RefGrant,
|
|
206
421
|
now: Date,
|
|
207
422
|
row?: OperationNode,
|
|
423
|
+
paths: PathRule = heldThrough(grant.action),
|
|
208
424
|
): OperationNode {
|
|
209
425
|
const linked = linksPredicate(model.name, linkValues(model.name, links, grant))
|
|
210
426
|
const chain = row === undefined ? linked : allOf([row, linked])
|
|
211
|
-
const gate = relationGate(
|
|
427
|
+
const gate = relationGate(reach, model, grant, now, paths)
|
|
212
428
|
return conditioned(model, grant.action, gate === undefined ? chain : allOf([chain, gate]), now)
|
|
213
429
|
}
|
|
214
430
|
|
|
431
|
+
/** A child view's parent reach, split by the relations the view narrows: the rest's reach, and each such relation's own. */
|
|
432
|
+
export interface SplitReach {
|
|
433
|
+
readonly rest: OperationNode | undefined
|
|
434
|
+
readonly relations: readonly { readonly reached: OperationNode; readonly condition: ConditionModel }[]
|
|
435
|
+
}
|
|
436
|
+
|
|
215
437
|
/**
|
|
216
438
|
* The rows of a child view whose parent row `reached` admits: `V.<column> in (select P.<key> from P where <reached>)`,
|
|
217
439
|
* after `row` when it addresses one row, then the view's own condition of `action`. For `all`, every parent row, so a
|
|
218
|
-
* row whose parent column names none is still read by no capability.
|
|
219
|
-
*
|
|
440
|
+
* row whose parent column names none is still read by no capability. Split by the relations the view narrows
|
|
441
|
+
* (`relationWhen`), one such membership per part, each narrowed relation's ANDed with the view's condition for it. No
|
|
442
|
+
* column of the view places a row: its containment rests on the parent's foreign keys (ADR-0072).
|
|
220
443
|
*/
|
|
221
444
|
export function viewScope(
|
|
222
445
|
model: TableModel,
|
|
223
446
|
parent: ViewParentLink,
|
|
224
447
|
action: string,
|
|
225
|
-
reached: OperationNode | 'all',
|
|
448
|
+
reached: OperationNode | 'all' | SplitReach | undefined,
|
|
226
449
|
now: Date,
|
|
227
450
|
row?: OperationNode,
|
|
451
|
+
access: readonly OperationNode[] = [],
|
|
228
452
|
): OperationNode {
|
|
229
|
-
const
|
|
453
|
+
const membership = (where: OperationNode | undefined) => isIn(model.name, parent.column, subquery(parent.table, parent.key, where))
|
|
454
|
+
const parts: OperationNode[] = []
|
|
455
|
+
if (reached === 'all') {
|
|
456
|
+
parts[parts.length] = membership(undefined)
|
|
457
|
+
} else if (reached !== undefined && 'rest' in reached) {
|
|
458
|
+
if (reached.rest !== undefined) parts[parts.length] = membership(reached.rest)
|
|
459
|
+
for (let index = 0; index < reached.relations.length; index++) {
|
|
460
|
+
const part = reached.relations[index]
|
|
461
|
+
if (part !== undefined) parts[parts.length] = narrowedTerm(membership(part.reached), conditionNode(model.name, part.condition, now))
|
|
462
|
+
}
|
|
463
|
+
} else if (reached !== undefined) {
|
|
464
|
+
parts[parts.length] = membership(reached)
|
|
465
|
+
}
|
|
466
|
+
for (let index = 0; index < access.length; index++) {
|
|
467
|
+
const term = access[index]
|
|
468
|
+
if (term !== undefined) parts[parts.length] = term
|
|
469
|
+
}
|
|
470
|
+
const member = anyOf(parts)
|
|
230
471
|
return conditioned(model, action, row === undefined ? member : allOf([row, member]), now)
|
|
231
472
|
}
|
|
232
473
|
|
|
233
|
-
/**
|
|
234
|
-
|
|
235
|
-
|
|
474
|
+
/**
|
|
475
|
+
* The rows a Ref held through relations, member reveals or secondary paths needs at `model`: a relation row at the node,
|
|
476
|
+
* a revealing row the Ref's grants reveal it through, or a path `paths` admits naming a node its grants hold;
|
|
477
|
+
* `undefined` for a Ref an IAM grant holds on its chain. A part that reaches no row of `model` adds nothing; when none
|
|
478
|
+
* does, the Ref is unenforceable there, never unconditioned.
|
|
479
|
+
*/
|
|
480
|
+
export function relationGate(
|
|
481
|
+
reach: LayerReach,
|
|
482
|
+
model: TableModel,
|
|
483
|
+
grant: RefGrant,
|
|
484
|
+
now: Date,
|
|
485
|
+
paths: PathRule = heldThrough(grant.action),
|
|
486
|
+
): OperationNode | undefined {
|
|
487
|
+
if (grant.relations.length === 0 && grant.revealing.length === 0 && grant.paths.length === 0) {
|
|
236
488
|
return undefined
|
|
237
489
|
}
|
|
490
|
+
const terms = gateTerms(reach, model, grant, now, paths)
|
|
491
|
+
if (terms.length === 0) {
|
|
492
|
+
throw new CapabilityError(
|
|
493
|
+
'unenforceable',
|
|
494
|
+
`the Ref of ${grant.dimension} is held through relations, member reveals or secondary paths, none of which reaches a row of ${model.name} for ${grant.action}`,
|
|
495
|
+
)
|
|
496
|
+
}
|
|
497
|
+
return anyOf(terms)
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** Every term of `grant`'s relations, member reveals and paths at `model`; empty where none reaches it. */
|
|
501
|
+
export function gateTerms(reach: LayerReach, model: TableModel, grant: RefGrant, now: Date, paths: PathRule): readonly OperationNode[] {
|
|
238
502
|
const terms: OperationNode[] = []
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
throw new CapabilityError(
|
|
244
|
-
'unenforceable',
|
|
245
|
-
`the Ref of ${grant.dimension} is held through ${relation}, whose dimension ${model.name} carries no column for, and which reveals none of its nodes for ${grant.action}`,
|
|
246
|
-
)
|
|
503
|
+
const add = (more: readonly OperationNode[]): void => {
|
|
504
|
+
for (let index = 0; index < more.length; index++) {
|
|
505
|
+
const term = more[index]
|
|
506
|
+
if (term !== undefined) terms[terms.length] = term
|
|
247
507
|
}
|
|
248
|
-
|
|
249
|
-
|
|
508
|
+
}
|
|
509
|
+
for (let index = 0; index < grant.relations.length; index++) {
|
|
510
|
+
const hold = grant.relations[index]
|
|
511
|
+
if (hold !== undefined) add(relationTerms(reach.relations, model, hold, grant.principal, grant.action, now, paths))
|
|
512
|
+
}
|
|
513
|
+
if (grant.revealing.length > 0) {
|
|
514
|
+
add(revealingTerms(reach.members, model, `the Ref of ${grant.dimension}`, grant.action, grant.revealing, now, paths))
|
|
515
|
+
}
|
|
516
|
+
if (grant.paths.length > 0) {
|
|
517
|
+
add(pathTerms(model, grant.paths, paths))
|
|
518
|
+
}
|
|
519
|
+
return terms
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* The rows of `model` whose path `paths` admits names a node `grants` hold: `T.<column> in (values)` at the path's own
|
|
524
|
+
* dimension, `T.<column> in (select D.<key> from D where D.<link> in (values))` above it, through the container's column
|
|
525
|
+
* for a path through the container.
|
|
526
|
+
*/
|
|
527
|
+
export function pathTerms(model: TableModel, grants: readonly ContainerGrant[], paths: PathRule): readonly OperationNode[] {
|
|
528
|
+
const terms: OperationNode[] = []
|
|
529
|
+
for (let index = 0; index < model.readThrough.length; index++) {
|
|
530
|
+
const path = model.readThrough[index]
|
|
531
|
+
if (path === undefined || !paths(path)) continue
|
|
532
|
+
for (let position = 0; position < grants.length; position++) {
|
|
533
|
+
const grant = grants[position]
|
|
534
|
+
const term = grant === undefined || grant.values.length === 0 ? undefined : throughPath(model.name, path, grant.dimension, valueList(grant.values))
|
|
250
535
|
if (term !== undefined) terms[terms.length] = term
|
|
251
536
|
}
|
|
252
537
|
}
|
|
253
|
-
return
|
|
538
|
+
return terms
|
|
254
539
|
}
|
package/src/capability.ts
CHANGED
|
@@ -40,7 +40,29 @@ export interface RefGrant {
|
|
|
40
40
|
* The relations the caller holds the action through on this object or a container, when no grant holds it: every
|
|
41
41
|
* statement of the Ref then also requires a row of one of them. Empty for a Ref an IAM grant holds.
|
|
42
42
|
*/
|
|
43
|
-
readonly relations: readonly
|
|
43
|
+
readonly relations: readonly RelationHold[]
|
|
44
|
+
/**
|
|
45
|
+
* The caller's grants of the action at nodes whose rows reveal the object, or a container on its chain, to their
|
|
46
|
+
* members (`memberReveals`), when no grant holds it: every statement of the Ref then also requires such a row, as
|
|
47
|
+
* for `relations`. Empty for a Ref an IAM grant holds.
|
|
48
|
+
*/
|
|
49
|
+
readonly revealing: readonly ContainerGrant[]
|
|
50
|
+
/**
|
|
51
|
+
* The caller's grants of the action at nodes of a dimension the object's table reads through a path that holds the
|
|
52
|
+
* action (`writeThrough`), or at containers of such a node, when no grant holds it on the chain: every statement then
|
|
53
|
+
* also requires the row to reach one of them through such a path. Empty for a Ref an IAM grant holds.
|
|
54
|
+
*/
|
|
55
|
+
readonly paths: readonly ContainerGrant[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A relation the caller holds the action through: anywhere (`inside: null`, a `~rel` entry), or only on rows that lie
|
|
60
|
+
* inside one of `inside`'s values (`~relin` entries of grants at those values): a row whose column for that dimension,
|
|
61
|
+
* its key at its own dimension, names one of them. A row with no such column is not inside, so the hold reaches none.
|
|
62
|
+
*/
|
|
63
|
+
export interface RelationHold {
|
|
64
|
+
readonly relation: string
|
|
65
|
+
readonly inside: ContainerGrant | null
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
/** The values of one container dimension in which the caller holds the action: `organization → [o1]`. */
|
|
@@ -73,10 +95,10 @@ export interface WithinGrant {
|
|
|
73
95
|
*/
|
|
74
96
|
readonly contained: readonly ContainerGrant[]
|
|
75
97
|
/**
|
|
76
|
-
* The relations through which the caller also holds the action (`~rel` entries of its token): a row lies
|
|
77
|
-
* `Within` too where a row of one of them names the caller at the row's node, read by the statement itself.
|
|
98
|
+
* The relations through which the caller also holds the action (`~rel` and `~relin` entries of its token): a row lies
|
|
99
|
+
* in the `Within` too where a row of one of them names the caller at the row's node, read by the statement itself.
|
|
78
100
|
*/
|
|
79
|
-
readonly relations: readonly
|
|
101
|
+
readonly relations: readonly RelationHold[]
|
|
80
102
|
/**
|
|
81
103
|
* The caller's grants of the action at nodes whose rows reveal the `Within`'s dimension or a container of it to their
|
|
82
104
|
* members (`memberReveals`): a row under a node such a row reveals is read too, the statement judging the revealing row.
|
|
@@ -137,4 +159,10 @@ export interface CapabilityAuthority {
|
|
|
137
159
|
* request. Every mutation checks it inside its own transaction.
|
|
138
160
|
*/
|
|
139
161
|
fence(): LeaseFence | null
|
|
162
|
+
/**
|
|
163
|
+
* What the caller of the work running now holds of `action` in `dimension`, as a `Within` of it would carry it, with
|
|
164
|
+
* no values when it holds none; `undefined` when the work has no caller (a job attempt, a webhook delivery). An
|
|
165
|
+
* insert that hands out access through a path judges its writer by it; an authority without the method has no caller.
|
|
166
|
+
*/
|
|
167
|
+
standing?(dimension: string, action: string): WithinGrant | undefined
|
|
140
168
|
}
|
package/src/framework.ts
CHANGED
|
@@ -24,7 +24,7 @@ export type { DeliveryClaim, NewWebhookBinding, WebhookBinding, WebhookTables }
|
|
|
24
24
|
export type { JobLeases, JobLeaseState, JobOutcome, LeaseChange, LeaseProjection, OverdueClaim } from './job-leases'
|
|
25
25
|
export type { FinishWakes, JobWakes, NewWake, Wake, WakeReason } from './job-wakes'
|
|
26
26
|
export type { DataAccess, DataHandles, DataSite } from './orm'
|
|
27
|
-
export type { FrameworkTables, Locator, ProvisionalBinding, RelationCheck, RevealCheck } from './registry'
|
|
27
|
+
export type { FrameworkTables, Locator, MemberRevealCheck, PathCheck, ProvisionalBinding, RelationCheck, RevealCheck } from './registry'
|
|
28
28
|
export type { SqlMode, SqlSite, SqlTag, SqlValue } from './sql'
|
|
29
29
|
export type { FileColumnRef, QuarantinedStoredFile, ServedFile, StagedFile, StoredFileMeta, StoredFiles, StoredFileStatus } from './stored-files'
|
|
30
30
|
|