@fabrika/db 0.9.0-dev.7 → 0.9.0-dev.71
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 +9 -5
- package/src/authority.ts +560 -0
- package/src/builtins.ts +21 -0
- package/src/capability.ts +51 -2
- package/src/codegen-sqlite.ts +69 -13
- package/src/codegen.ts +181 -42
- package/src/condition-nodes.ts +119 -0
- package/src/conditions.ts +683 -0
- package/src/description.ts +67 -9
- package/src/errors.ts +9 -5
- package/src/file-columns.ts +52 -10
- package/src/framework.ts +5 -2
- package/src/guard.ts +365 -15
- package/src/index.ts +8 -0
- package/src/job-leases.ts +19 -2
- package/src/job-wakes.ts +183 -33
- package/src/json.ts +187 -0
- package/src/list-cursor.ts +82 -0
- package/src/migrations-sqlite.ts +93 -0
- package/src/migrations.ts +99 -0
- package/src/nodes.ts +33 -14
- package/src/notifications.ts +152 -0
- package/src/orm-types.ts +216 -77
- package/src/orm.ts +417 -158
- package/src/policy.ts +2043 -131
- package/src/push-subscriptions.ts +287 -0
- package/src/registry.ts +50 -1
- package/src/request-receipts.ts +107 -0
- package/src/sql.ts +39 -11
- package/src/sqlite-migrate.ts +102 -1
- package/src/sqlite.ts +46 -1
- package/src/store.ts +1958 -167
- package/src/tables.ts +156 -6
- package/src/temporal.ts +39 -4
- package/src/testing-shared.ts +872 -148
- package/src/testing-sqlite.ts +215 -40
- package/src/testing.ts +151 -22
- package/src/vocabulary-tables.ts +172 -26
- package/types/src/authority.d.ts +147 -0
- package/types/src/builtins.d.ts +2 -0
- package/types/src/capability.d.ts +50 -2
- package/types/src/codegen-sqlite.d.ts +8 -1
- package/types/src/codegen.d.ts +54 -10
- package/types/src/condition-nodes.d.ts +9 -0
- package/types/src/conditions.d.ts +151 -0
- package/types/src/description.d.ts +40 -9
- package/types/src/errors.d.ts +8 -5
- package/types/src/file-columns.d.ts +6 -4
- package/types/src/framework.d.ts +5 -2
- package/types/src/guard.d.ts +24 -0
- package/types/src/index.d.ts +4 -3
- package/types/src/job-leases.d.ts +8 -1
- package/types/src/job-wakes.d.ts +28 -5
- package/types/src/json.d.ts +35 -0
- package/types/src/list-cursor.d.ts +13 -0
- package/types/src/migrations.d.ts +5 -0
- package/types/src/nodes.d.ts +5 -1
- package/types/src/notifications.d.ts +37 -0
- package/types/src/orm-types.d.ts +167 -75
- package/types/src/orm.d.ts +21 -0
- package/types/src/policy.d.ts +378 -27
- package/types/src/push-subscriptions.d.ts +49 -0
- package/types/src/registry.d.ts +36 -1
- package/types/src/request-receipts.d.ts +22 -0
- package/types/src/sql.d.ts +2 -2
- package/types/src/sqlite-migrate.d.ts +32 -0
- package/types/src/store.d.ts +118 -31
- package/types/src/tables.d.ts +8 -2
- package/types/src/temporal.d.ts +7 -1
- package/types/src/testing-shared.d.ts +125 -26
- package/types/src/testing-sqlite.d.ts +17 -2
- package/types/src/testing.d.ts +1 -1
- package/types/src/vocabulary-tables.d.ts +176 -22
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/db",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.71",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"sideEffects": [
|
|
6
|
+
"./src/testing.ts",
|
|
7
|
+
"./src/testing-sqlite.ts"
|
|
8
|
+
],
|
|
5
9
|
"repository": {
|
|
6
10
|
"type": "git",
|
|
7
11
|
"url": "git+https://github.com/contember/fabrika-platform.git",
|
|
@@ -88,13 +92,13 @@
|
|
|
88
92
|
}
|
|
89
93
|
},
|
|
90
94
|
"scripts": {
|
|
91
|
-
"typecheck": "
|
|
95
|
+
"typecheck": "tsrs --build tsconfig.test.json",
|
|
92
96
|
"test": "bun test"
|
|
93
97
|
},
|
|
94
98
|
"dependencies": {
|
|
95
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
96
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
97
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
99
|
+
"@fabrika/auth-core": "0.9.0-dev.71",
|
|
100
|
+
"@fabrika/platform": "0.9.0-dev.71",
|
|
101
|
+
"@fabrika/platform-node": "0.9.0-dev.71",
|
|
98
102
|
"kysely": "0.29.6",
|
|
99
103
|
"kysely-postgres-js": "5.0.1",
|
|
100
104
|
"zod": "^3.25.76"
|
package/src/authority.ts
ADDED
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
// What a capability admits of a table's rows beyond its containment: the relations it is held through and the
|
|
2
|
+
// condition its action holds a row under. Every predicate a capability scopes is built through `refScope`,
|
|
3
|
+
// `viewScope` or `conditioned`, so neither is left out of a statement by a builder that forgot it.
|
|
4
|
+
|
|
5
|
+
import { AndNode, BinaryOperationNode, type OperationNode, OperatorNode, ParensNode, ValueNode } from 'kysely'
|
|
6
|
+
import { ownEntry } from './builtins'
|
|
7
|
+
import type { ContainerGrant, RefGrant, RelationHold, WithinGrant } from './capability'
|
|
8
|
+
import { conditionNode, subquery } from './condition-nodes'
|
|
9
|
+
import type { ConditionModel } from './conditions'
|
|
10
|
+
import { CapabilityError, PolicyError } from './errors'
|
|
11
|
+
import type { ViewParentLink } from './guard'
|
|
12
|
+
import { linksPredicate, linkValues } from './links'
|
|
13
|
+
import { allOf, anyOf, column, equals, valueList } from './nodes'
|
|
14
|
+
import {
|
|
15
|
+
type Container,
|
|
16
|
+
includes,
|
|
17
|
+
type MemberRevealModel,
|
|
18
|
+
nodeColumnOf,
|
|
19
|
+
type RelationSource,
|
|
20
|
+
type RevealModel,
|
|
21
|
+
type SecondaryPath,
|
|
22
|
+
type TableModel,
|
|
23
|
+
} from './policy'
|
|
24
|
+
|
|
25
|
+
/** A relation of the layer: its source table, and the tables whose rows it reaches reveal an ancestor node. */
|
|
26
|
+
export interface LayerRelation extends RelationSource {
|
|
27
|
+
readonly kind: 'source'
|
|
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>>>>
|
|
36
|
+
}
|
|
37
|
+
|
|
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>>
|
|
48
|
+
|
|
49
|
+
/** Every member reveal of the layer, by the dimension whose grants it reveals to. */
|
|
50
|
+
export type MemberReveals = Readonly<Record<string, readonly MemberRevealModel[]>>
|
|
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
|
+
|
|
58
|
+
export function relationSource(relations: RelationSources, relation: string): LayerRelation {
|
|
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`)
|
|
62
|
+
}
|
|
63
|
+
return source
|
|
64
|
+
}
|
|
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
|
+
|
|
106
|
+
/** A source row that confers its relation now: unexpired, and admitted by the relation's own condition. */
|
|
107
|
+
export function activeRow(source: RelationSource, now: Date): readonly OperationNode[] {
|
|
108
|
+
const terms: OperationNode[] = []
|
|
109
|
+
if (source.expires !== undefined) {
|
|
110
|
+
const open = BinaryOperationNode.create(column(source.table, source.expires), OperatorNode.create('is'), ValueNode.createImmediate(null))
|
|
111
|
+
const ahead = BinaryOperationNode.create(column(source.table, source.expires), OperatorNode.create('>'), ValueNode.create(now))
|
|
112
|
+
terms[terms.length] = anyOf([open, ahead])
|
|
113
|
+
}
|
|
114
|
+
if (source.when !== undefined) {
|
|
115
|
+
terms[terms.length] = conditionNode(source.table, source.when, now)
|
|
116
|
+
}
|
|
117
|
+
return terms
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A source row naming `principal`: `S.<principal> = $me`, or through its path `S.<column> in (select P.<key> from P where
|
|
122
|
+
* P.<column> = $me)`. Every check of a source row's principal is built here.
|
|
123
|
+
*/
|
|
124
|
+
export function namesPrincipal(source: RelationSource, principal: string): OperationNode {
|
|
125
|
+
const path = source.principalPath
|
|
126
|
+
if (path === undefined) {
|
|
127
|
+
return equals(source.table, source.principal, principal)
|
|
128
|
+
}
|
|
129
|
+
return isIn(source.table, source.principal, subquery(path.table, path.key, equals(path.table, path.column, principal)))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `select S.<node> from S where <names principal> and <active>`: the nodes a relation holds `principal` at now. */
|
|
133
|
+
function relationNodes(source: RelationSource, principal: string, now: Date): OperationNode {
|
|
134
|
+
return subquery(source.table, source.node, allOf([namesPrincipal(source, principal), ...activeRow(source, now)]))
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function isIn(table: string, name: string, nodes: OperationNode): OperationNode {
|
|
138
|
+
return BinaryOperationNode.create(column(table, name), OperatorNode.create('in'), nodes)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The rows of `table` whose `path` column names one of `nodes` of `dimension`: `T.<column> in (nodes)` at the path's own
|
|
143
|
+
* dimension, `T.<column> in (select D.<key> from D where D.<link> in (nodes))` at one above it; `undefined` elsewhere. A
|
|
144
|
+
* path through the nearest container reads the container's: `T.<link> in (select C.<key> from C where <C's term>)`.
|
|
145
|
+
*/
|
|
146
|
+
export function throughPath(table: string, path: SecondaryPath, dimension: string, nodes: OperationNode): OperationNode | undefined {
|
|
147
|
+
const via = path.via
|
|
148
|
+
const term = pathTerm(via === undefined ? table : via.table, path, dimension, nodes)
|
|
149
|
+
return via === undefined || term === undefined ? term : isIn(table, via.column, subquery(via.table, via.key, term))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function pathTerm(table: string, path: SecondaryPath, dimension: string, nodes: OperationNode): OperationNode | undefined {
|
|
153
|
+
if (path.dimension === dimension) {
|
|
154
|
+
return isIn(table, path.column, nodes)
|
|
155
|
+
}
|
|
156
|
+
for (let index = 0; index < path.chain.length; index++) {
|
|
157
|
+
const link = path.chain[index]
|
|
158
|
+
if (link !== undefined && link.dimension === dimension) {
|
|
159
|
+
return isIn(table, path.column, subquery(path.table, path.key, isIn(path.table, link.column, nodes)))
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return undefined
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** `select R.<ancestor> from R where R.<key> in (<the relation's nodes>) and <when>`: the nodes a reveal holds `principal` at. */
|
|
166
|
+
export function revealedNodes(source: RelationSource, reveal: RevealModel, principal: string, now: Date): OperationNode {
|
|
167
|
+
const nodes = relationNodes(source, principal, now)
|
|
168
|
+
const through = reveal.path === undefined ? undefined : throughPath(reveal.table, reveal.path, source.dimension, nodes)
|
|
169
|
+
const reached = through ?? isIn(reveal.table, reveal.key, nodes)
|
|
170
|
+
const where = reveal.when === undefined ? reached : AndNode.create(reached, ParensNode.create(conditionNode(reveal.table, reveal.when, now)))
|
|
171
|
+
return subquery(reveal.table, reveal.column, where)
|
|
172
|
+
}
|
|
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
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The rows of `model` a relation reaches for `principal` under `action`: `T.<node> in (select S.<node> from S where
|
|
187
|
+
* S.<principal> = $me and <active>)` where `model` lies at a node of the relation's dimension, and one
|
|
188
|
+
* `T.<ancestor> in (<revealed nodes>)` per reveal of `action` to an ancestor `model` lies at. Empty where neither holds.
|
|
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.
|
|
194
|
+
*/
|
|
195
|
+
export function relationTerms(
|
|
196
|
+
relations: RelationSources,
|
|
197
|
+
model: TableModel,
|
|
198
|
+
hold: RelationHold,
|
|
199
|
+
principal: string,
|
|
200
|
+
action: string,
|
|
201
|
+
now: Date,
|
|
202
|
+
paths: PathRule,
|
|
203
|
+
): readonly OperationNode[] {
|
|
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[] = []
|
|
220
|
+
const at = nodeColumnOf(model, source.dimension)
|
|
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++) {
|
|
301
|
+
const path = model.readThrough[index]
|
|
302
|
+
const through = path === undefined || !paths(path)
|
|
303
|
+
? undefined
|
|
304
|
+
: throughPath(model.name, path, source.dimension, relationNodes(source, principal, now))
|
|
305
|
+
if (through !== undefined) terms[terms.length] = through
|
|
306
|
+
}
|
|
307
|
+
for (let index = 0; index < source.reveals.length; index++) {
|
|
308
|
+
const reveal = source.reveals[index]
|
|
309
|
+
const held = reveal !== undefined && includes(reveal.actions, action) && (reveal.path === undefined || paths(reveal.path))
|
|
310
|
+
const placed = reveal === undefined || !held ? undefined : nodeColumnOf(model, reveal.dimension)
|
|
311
|
+
if (reveal !== undefined && placed !== undefined) {
|
|
312
|
+
terms[terms.length] = isIn(model.name, placed, revealedNodes(source, reveal, principal, now))
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return terms
|
|
316
|
+
}
|
|
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
|
+
|
|
339
|
+
/**
|
|
340
|
+
* The rows of `model` a `Within`'s read reaches through member reveals: `revealingTerms` of its grants at revealing
|
|
341
|
+
* dimensions.
|
|
342
|
+
*/
|
|
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[] {
|
|
363
|
+
const terms: OperationNode[] = []
|
|
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) ?? []
|
|
368
|
+
let named = false
|
|
369
|
+
for (let position = 0; position < reveals.length; position++) {
|
|
370
|
+
const reveal = reveals[position]
|
|
371
|
+
if (reveal === undefined || !includes(reveal.actions, action) || (reveal.path !== undefined && !paths(reveal.path))) continue
|
|
372
|
+
named = true
|
|
373
|
+
const placed = nodeColumnOf(model, reveal.dimension)
|
|
374
|
+
if (placed === undefined || grant.values.length === 0) continue
|
|
375
|
+
terms[terms.length] = isIn(model.name, placed, memberRevealed(reveal, grant.values, now))
|
|
376
|
+
}
|
|
377
|
+
if (!named) {
|
|
378
|
+
throw new CapabilityError(
|
|
379
|
+
'unenforceable',
|
|
380
|
+
`${holder} carries a grant on ${grant.dimension}, whose rows reveal no node for ${action}`,
|
|
381
|
+
)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return terms
|
|
385
|
+
}
|
|
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
|
+
|
|
394
|
+
/** The condition `action` holds a row of `model` under, or `undefined` when the action is not narrowed there. */
|
|
395
|
+
export function actionCondition(model: TableModel, action: string, now: Date): OperationNode | undefined {
|
|
396
|
+
const condition = ownEntry(model.when, action)
|
|
397
|
+
return condition === undefined ? undefined : conditionNode(model.name, condition, now)
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* `branch`, narrowed by the condition of `action`: `<reach> and (<when>)`, for a global grant `true and (<when>)`, the
|
|
402
|
+
* one shape the guard admits a condition in, so a condition never stands in for the reach.
|
|
403
|
+
*/
|
|
404
|
+
export function conditioned(model: TableModel, action: string, branch: OperationNode | 'all', now: Date): OperationNode {
|
|
405
|
+
return conditionedAny(model, [action], branch, now)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* `branch`, narrowed as one role holding `actions` is: `<reach> and ((<when a>) or (<when b>))`, a row passing where any
|
|
410
|
+
* action's condition admits it, and unnarrowed when one of them has none. `actions` are those that allow the operation,
|
|
411
|
+
* at least one; for one action this is `conditioned`.
|
|
412
|
+
*/
|
|
413
|
+
export function conditionedAny(model: TableModel, actions: readonly string[], branch: OperationNode | 'all', now: Date): OperationNode {
|
|
414
|
+
const reach = branch === 'all' ? ValueNode.createImmediate(true) : branch
|
|
415
|
+
if (actions.length === 0) {
|
|
416
|
+
throw new CapabilityError('unenforceable', `no action allows this operation on ${model.name}`)
|
|
417
|
+
}
|
|
418
|
+
const conditions: OperationNode[] = []
|
|
419
|
+
for (let index = 0; index < actions.length; index++) {
|
|
420
|
+
const action = actions[index]
|
|
421
|
+
if (action === undefined) continue
|
|
422
|
+
const condition = actionCondition(model, action, now)
|
|
423
|
+
if (condition === undefined) {
|
|
424
|
+
return reach
|
|
425
|
+
}
|
|
426
|
+
conditions[conditions.length] = ParensNode.create(condition)
|
|
427
|
+
}
|
|
428
|
+
return AndNode.create(reach, anyOf(conditions))
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* A Ref's predicate over `links` of `model`: one equality per link of its chain (after `row`, the equality that
|
|
433
|
+
* addresses one row by its key), then, when the Ref is held through relations or member reveals, a row of one of them
|
|
434
|
+
* at the node, and its action's condition. A Ref whose relations, revealing grants and paths together reach no row of
|
|
435
|
+
* `model` is unenforceable there, never unconditioned.
|
|
436
|
+
*/
|
|
437
|
+
export function refScope(
|
|
438
|
+
reach: LayerReach,
|
|
439
|
+
model: TableModel,
|
|
440
|
+
links: readonly Container[],
|
|
441
|
+
grant: RefGrant,
|
|
442
|
+
now: Date,
|
|
443
|
+
row?: OperationNode,
|
|
444
|
+
paths: PathRule = heldThrough(grant.action),
|
|
445
|
+
): OperationNode {
|
|
446
|
+
const linked = linksPredicate(model.name, linkValues(model.name, links, grant))
|
|
447
|
+
const chain = row === undefined ? linked : allOf([row, linked])
|
|
448
|
+
const gate = relationGate(reach, model, grant, now, paths)
|
|
449
|
+
return conditioned(model, grant.action, gate === undefined ? chain : allOf([chain, gate]), now)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** A child view's parent reach, split by the relations the view narrows: the rest's reach, and each such relation's own. */
|
|
453
|
+
export interface SplitReach {
|
|
454
|
+
readonly rest: OperationNode | undefined
|
|
455
|
+
readonly relations: readonly { readonly reached: OperationNode; readonly condition: ConditionModel }[]
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* The rows of a child view whose parent row `reached` admits: `V.<column> in (select P.<key> from P where <reached>)`,
|
|
460
|
+
* after `row` when it addresses one row, then the view's own condition of `action`. For `all`, every parent row, so a
|
|
461
|
+
* row whose parent column names none is still read by no capability. Split by the relations the view narrows
|
|
462
|
+
* (`relationWhen`), one such membership per part, each narrowed relation's ANDed with the view's condition for it. No
|
|
463
|
+
* column of the view places a row: its containment rests on the parent's foreign keys (ADR-0072).
|
|
464
|
+
*/
|
|
465
|
+
export function viewScope(
|
|
466
|
+
model: TableModel,
|
|
467
|
+
parent: ViewParentLink,
|
|
468
|
+
action: string,
|
|
469
|
+
reached: OperationNode | 'all' | SplitReach | undefined,
|
|
470
|
+
now: Date,
|
|
471
|
+
row?: OperationNode,
|
|
472
|
+
access: readonly OperationNode[] = [],
|
|
473
|
+
): OperationNode {
|
|
474
|
+
const membership = (where: OperationNode | undefined) => isIn(model.name, parent.column, subquery(parent.table, parent.key, where))
|
|
475
|
+
const parts: OperationNode[] = []
|
|
476
|
+
if (reached === 'all') {
|
|
477
|
+
parts[parts.length] = membership(undefined)
|
|
478
|
+
} else if (reached !== undefined && 'rest' in reached) {
|
|
479
|
+
if (reached.rest !== undefined) parts[parts.length] = membership(reached.rest)
|
|
480
|
+
for (let index = 0; index < reached.relations.length; index++) {
|
|
481
|
+
const part = reached.relations[index]
|
|
482
|
+
if (part !== undefined) parts[parts.length] = narrowedTerm(membership(part.reached), conditionNode(model.name, part.condition, now))
|
|
483
|
+
}
|
|
484
|
+
} else if (reached !== undefined) {
|
|
485
|
+
parts[parts.length] = membership(reached)
|
|
486
|
+
}
|
|
487
|
+
for (let index = 0; index < access.length; index++) {
|
|
488
|
+
const term = access[index]
|
|
489
|
+
if (term !== undefined) parts[parts.length] = term
|
|
490
|
+
}
|
|
491
|
+
const member = anyOf(parts)
|
|
492
|
+
return conditioned(model, action, row === undefined ? member : allOf([row, member]), now)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* The rows a Ref held through relations, member reveals or secondary paths needs at `model`: a relation row at the node,
|
|
497
|
+
* a revealing row the Ref's grants reveal it through, or a path `paths` admits naming a node its grants hold;
|
|
498
|
+
* `undefined` for a Ref an IAM grant holds on its chain. A part that reaches no row of `model` adds nothing; when none
|
|
499
|
+
* does, the Ref is unenforceable there, never unconditioned.
|
|
500
|
+
*/
|
|
501
|
+
export function relationGate(
|
|
502
|
+
reach: LayerReach,
|
|
503
|
+
model: TableModel,
|
|
504
|
+
grant: RefGrant,
|
|
505
|
+
now: Date,
|
|
506
|
+
paths: PathRule = heldThrough(grant.action),
|
|
507
|
+
): OperationNode | undefined {
|
|
508
|
+
if (grant.relations.length === 0 && grant.revealing.length === 0 && grant.paths.length === 0) {
|
|
509
|
+
return undefined
|
|
510
|
+
}
|
|
511
|
+
const terms = gateTerms(reach, model, grant, now, paths)
|
|
512
|
+
if (terms.length === 0) {
|
|
513
|
+
throw new CapabilityError(
|
|
514
|
+
'unenforceable',
|
|
515
|
+
`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}`,
|
|
516
|
+
)
|
|
517
|
+
}
|
|
518
|
+
return anyOf(terms)
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/** Every term of `grant`'s relations, member reveals and paths at `model`; empty where none reaches it. */
|
|
522
|
+
export function gateTerms(reach: LayerReach, model: TableModel, grant: RefGrant, now: Date, paths: PathRule): readonly OperationNode[] {
|
|
523
|
+
const terms: OperationNode[] = []
|
|
524
|
+
const add = (more: readonly OperationNode[]): void => {
|
|
525
|
+
for (let index = 0; index < more.length; index++) {
|
|
526
|
+
const term = more[index]
|
|
527
|
+
if (term !== undefined) terms[terms.length] = term
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
for (let index = 0; index < grant.relations.length; index++) {
|
|
531
|
+
const hold = grant.relations[index]
|
|
532
|
+
if (hold !== undefined) add(relationTerms(reach.relations, model, hold, grant.principal, grant.action, now, paths))
|
|
533
|
+
}
|
|
534
|
+
if (grant.revealing.length > 0) {
|
|
535
|
+
add(revealingTerms(reach.members, model, `the Ref of ${grant.dimension}`, grant.action, grant.revealing, now, paths))
|
|
536
|
+
}
|
|
537
|
+
if (grant.paths.length > 0) {
|
|
538
|
+
add(pathTerms(model, grant.paths, paths))
|
|
539
|
+
}
|
|
540
|
+
return terms
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* The rows of `model` whose path `paths` admits names a node `grants` hold: `T.<column> in (values)` at the path's own
|
|
545
|
+
* dimension, `T.<column> in (select D.<key> from D where D.<link> in (values))` above it, through the container's column
|
|
546
|
+
* for a path through the container.
|
|
547
|
+
*/
|
|
548
|
+
export function pathTerms(model: TableModel, grants: readonly ContainerGrant[], paths: PathRule): readonly OperationNode[] {
|
|
549
|
+
const terms: OperationNode[] = []
|
|
550
|
+
for (let index = 0; index < model.readThrough.length; index++) {
|
|
551
|
+
const path = model.readThrough[index]
|
|
552
|
+
if (path === undefined || !paths(path)) continue
|
|
553
|
+
for (let position = 0; position < grants.length; position++) {
|
|
554
|
+
const grant = grants[position]
|
|
555
|
+
const term = grant === undefined || grant.values.length === 0 ? undefined : throughPath(model.name, path, grant.dimension, valueList(grant.values))
|
|
556
|
+
if (term !== undefined) terms[terms.length] = term
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return terms
|
|
560
|
+
}
|
package/src/builtins.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { PolicyError } from './errors'
|
|
2
|
+
|
|
1
3
|
// Captured when the module loads, before application code: a later patch to Object, Array, Map or their prototypes
|
|
2
4
|
// does not change what these references do. Every policy and allowlist check goes through them and index loops,
|
|
3
5
|
// never through an iterator, a spread or a method the checked value (or a mutable prototype) supplies.
|
|
@@ -92,3 +94,22 @@ export function defineValue(target: object, key: string, value: unknown): void {
|
|
|
92
94
|
export function ownEntry<V>(record: Readonly<Record<string, V>>, key: string): V | undefined {
|
|
93
95
|
return hasOwn(record, key) ? record[key] : undefined
|
|
94
96
|
}
|
|
97
|
+
|
|
98
|
+
/** A copy of a plain object's own data properties; anything else (an instance, a symbol key, an accessor) is refused. */
|
|
99
|
+
export function plainRecord(value: unknown, what: string): Readonly<Record<string, unknown>> {
|
|
100
|
+
const prototype: unknown = typeof value === 'object' && value !== null && !isArray(value) ? prototypeOf(value) : undefined
|
|
101
|
+
if ((prototype !== OBJECT_PROTOTYPE && prototype !== null) || typeof value !== 'object' || value === null || ownSymbols(value).length > 0) {
|
|
102
|
+
throw new PolicyError(`${what} must be a plain object`)
|
|
103
|
+
}
|
|
104
|
+
const copy: Record<string, unknown> = {}
|
|
105
|
+
const keys = ownNames(value)
|
|
106
|
+
for (let index = 0; index < keys.length; index++) {
|
|
107
|
+
const key = keys[index] ?? ''
|
|
108
|
+
const descriptor = ownDescriptor(value, key)
|
|
109
|
+
if (descriptor === undefined || !hasOwn(descriptor, 'value')) {
|
|
110
|
+
throw new PolicyError(`${what}: ${key} is an accessor; a policy holds plain values`)
|
|
111
|
+
}
|
|
112
|
+
setOwn(copy, key, descriptor.value)
|
|
113
|
+
}
|
|
114
|
+
return freeze(copy)
|
|
115
|
+
}
|