@stonyx/orm 0.3.2-alpha.57 → 0.3.2-alpha.59
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/README.md +98 -17
- package/dist/main.d.ts +65 -11
- package/dist/main.js +67 -13
- package/dist/orm-request.d.ts +66 -4
- package/dist/orm-request.js +65 -3
- package/dist/setup-rest-server.js +6 -6
- package/package.json +1 -1
- package/src/main.ts +67 -13
- package/src/orm-request.ts +65 -3
- package/src/setup-rest-server.ts +6 -6
package/README.md
CHANGED
|
@@ -397,7 +397,7 @@ parsing anything.
|
|
|
397
397
|
| Key | Value |
|
|
398
398
|
|---|---|
|
|
399
399
|
| `model` | The model this route was mounted for, as a **model name**: kebab-case, exactly as declared under `config.orm.paths.model` and keyed in the store — `'owner'`, `'animal'`, `'phone-number'`. **Not** the pluralized, dasherized, mount-prefixed *route* name. |
|
|
400
|
-
| `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary
|
|
400
|
+
| `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary *on this path*. Never an HTTP method name like `'GET'`, and **not** the hook vocabulary either (see [below](#operation-is-not-the-hook-operation)). `undefined` when the dispatched method has no entry in the framework's method map. |
|
|
401
401
|
|
|
402
402
|
So a predicate can be written without reference to any URL:
|
|
403
403
|
|
|
@@ -420,6 +420,49 @@ a URL shape nobody anticipated. `model` is fixed at mount time and no request
|
|
|
420
420
|
can influence it — not a mount prefix, not a query string, not a case-varied
|
|
421
421
|
path, not an absolute-form request target.
|
|
422
422
|
|
|
423
|
+
#### What the context does not tell you: which surface
|
|
424
|
+
|
|
425
|
+
It names **which model and which verb**, not **which route**. Measured over the
|
|
426
|
+
live router, six surfaces produce one identical context:
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
GET /owners { model: 'owner', operation: 'read' }
|
|
430
|
+
GET /owners/gina { model: 'owner', operation: 'read' }
|
|
431
|
+
GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
432
|
+
GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
433
|
+
GET /owners/archived { model: 'owner', operation: 'read' }
|
|
434
|
+
GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
So a rule that depends on the **sub-path** still needs `request.path` —
|
|
438
|
+
mount-relative and query-free, and the one read of argument one that
|
|
439
|
+
[Identifying the collection](#identifying-the-collection) sanctions. The sample
|
|
440
|
+
access class shipped with this repo has such a rule: its `/archived` deny
|
|
441
|
+
**cannot be expressed from the context alone**, and a predicate migrated to
|
|
442
|
+
context-only would silently drop it — a deny becoming an allow.
|
|
443
|
+
|
|
444
|
+
Note also that the related-resource and `?include=` surfaces serve *another
|
|
445
|
+
model's* records under `model: 'owner'`, and the context gives a predicate no
|
|
446
|
+
signal that it is authorizing a related-resource route. That is
|
|
447
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196).
|
|
448
|
+
|
|
449
|
+
#### `operation` is not the hook `operation`
|
|
450
|
+
|
|
451
|
+
This module exposes a **second** `operation` vocabulary, on an identically-named
|
|
452
|
+
key of an identically-shaped context object:
|
|
453
|
+
[hook contexts](#hook-context-object) carry `list` / `get` / `create` /
|
|
454
|
+
`update` / `delete`. The access vocabulary collapses `list` and `get` into
|
|
455
|
+
`'read'`, so for one `GET /animals/1` a hook sees `'get'` while `access()` sees
|
|
456
|
+
`'read'` — and a predicate cannot distinguish a collection read from a
|
|
457
|
+
record read.
|
|
458
|
+
|
|
459
|
+
"No second vocabulary" above is a statement about the **access path**, where
|
|
460
|
+
both the context and the permission array come from one method map. It is not a
|
|
461
|
+
statement about the module. Writing `operation === 'get'` in a predicate never
|
|
462
|
+
matches, and a predicate that stops matching falls through to the permission
|
|
463
|
+
array — so the misreading is fail-open shaped. In TypeScript the exported
|
|
464
|
+
`AccessOperation` union makes it a compile error.
|
|
465
|
+
|
|
423
466
|
The four `operation` values are the same four strings the permission-array
|
|
424
467
|
return shape is written in (`['read', 'create', 'update', 'delete']`), because
|
|
425
468
|
both come from one method map inside the framework. The two forms cannot
|
|
@@ -459,18 +502,52 @@ request routed to a *different* model:
|
|
|
459
502
|
import Orm from '@stonyx/orm';
|
|
460
503
|
|
|
461
504
|
const predicate = Orm.instance.getAccess('animal');
|
|
462
|
-
|
|
505
|
+
if (!predicate) return deny;
|
|
506
|
+
|
|
507
|
+
const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
463
508
|
```
|
|
464
509
|
|
|
465
|
-
`
|
|
466
|
-
|
|
467
|
-
|
|
510
|
+
**`undefined` means no predicate could be resolved — not that the model is
|
|
511
|
+
unrestricted. Treat it as deny.** It covers a model with no access class *and* a
|
|
512
|
+
model whose access class failed to **load**: a load failure is caught and warned
|
|
513
|
+
about, and the partial map is published anyway, so a missing key is not evidence
|
|
514
|
+
of an unrestricted model. This is the same rule as `operation === undefined`
|
|
515
|
+
above, and for the same reason.
|
|
516
|
+
|
|
517
|
+
The raw map is `Orm.instance.accessFunctions`, keyed by model name; prefer
|
|
518
|
+
`getAccess()` — it is guarded against inherited `Object.prototype` members and a
|
|
519
|
+
direct index is not. Note that it maps a model name to the predicate of the
|
|
520
|
+
access *class* that claims it, which may claim many models: against this repo's
|
|
521
|
+
sample, `getAccess('owner') === getAccess('animal')`.
|
|
522
|
+
|
|
523
|
+
#### Passing the context makes a model-correct answer *possible*
|
|
524
|
+
|
|
525
|
+
It does not make the answer model-correct on its own. **The resolved predicate
|
|
526
|
+
has to read the context.** Measured against the access class shipped with this
|
|
527
|
+
repo, on a request Express dispatched to `GET /owners/angela`, asked about
|
|
528
|
+
**animals**:
|
|
529
|
+
|
|
530
|
+
```
|
|
531
|
+
getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
532
|
+
-> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
That is the **owners** filter, and it returns `true` for animal 21 — the record
|
|
536
|
+
hidden on every animal surface. Under a mount that predicate recognizes neither
|
|
537
|
+
way it is worse still: it falls through to
|
|
538
|
+
`['read', 'create', 'update', 'delete']`, a full CRUD grant.
|
|
539
|
+
|
|
540
|
+
Either way the context was supplied and the answer is not the animal answer, and
|
|
541
|
+
it is wrong in the direction that **grants**. That predicate is single-argument
|
|
542
|
+
and identifies its collection from the request, so it answered about the
|
|
543
|
+
collection the request is *addressed to* while being asked about another one.
|
|
544
|
+
Every predicate in this repo, and in every consumer tree, is single-argument on
|
|
545
|
+
the day this ships, and a caller has no supported way to tell which kind it
|
|
546
|
+
resolved. The boot-time arity warning that would surface it is
|
|
547
|
+
[#213](https://github.com/abofs/stonyx-orm/issues/213).
|
|
468
548
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
about the collection the request is *addressed to* while being asked about
|
|
472
|
-
another one — and per the five variants below, it answers wrong in the direction
|
|
473
|
-
that grants access.
|
|
549
|
+
So: pass the context, and do not treat a resolved predicate's answer as
|
|
550
|
+
model-specific until that predicate has been migrated to read it.
|
|
474
551
|
|
|
475
552
|
### Return values
|
|
476
553
|
|
|
@@ -607,15 +684,19 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
607
684
|
[#202](https://github.com/abofs/stonyx-orm/issues/202): `access()` never
|
|
608
685
|
received the model structurally and `setup-rest-server.ts` discarded the
|
|
609
686
|
model→predicate map at boot. **#202 has landed and both halves now exist** —
|
|
610
|
-
see [The access context](#the-access-context-second-argument):
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
687
|
+
see [The access context](#the-access-context-second-argument):
|
|
688
|
+
`Orm.instance.getAccess(modelName)` makes another model's predicate
|
|
689
|
+
**reachable**, and `context.model` makes a **model-correct answer possible** —
|
|
690
|
+
possible, not guaranteed: the resolved predicate has to read the context, and
|
|
691
|
+
every predicate in tree is still single-argument
|
|
692
|
+
([#213](https://github.com/abofs/stonyx-orm/issues/213)), so today it answers
|
|
693
|
+
about the collection the request is addressed to. **The mechanism exists; the
|
|
694
|
+
ORM does not yet use it on this path.** The re-parenting write above is still
|
|
695
|
+
**not refused** — that enforcement is
|
|
615
696
|
[#196](https://github.com/abofs/stonyx-orm/issues/196) and
|
|
616
697
|
[#207](https://github.com/abofs/stonyx-orm/issues/207), which were blocked on
|
|
617
|
-
#202 and are now
|
|
618
|
-
record unmodifiable; keep the *writable* collections' predicates as tight as
|
|
698
|
+
#202 and are now free to proceed. Until they land, do not rely on a filter to
|
|
699
|
+
keep a record unmodifiable; keep the *writable* collections' predicates as tight as
|
|
619
700
|
the hidden ones.
|
|
620
701
|
- **Authorization by identifying the collection is a consumer-side
|
|
621
702
|
reconstruction of information the framework already holds.** `access()`
|
package/dist/main.d.ts
CHANGED
|
@@ -34,7 +34,25 @@ export default class Orm {
|
|
|
34
34
|
transforms: Record<string, (value: unknown) => unknown>;
|
|
35
35
|
warnings: Set<string>;
|
|
36
36
|
/**
|
|
37
|
-
* Model name ->
|
|
37
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
38
|
+
* model (abofs/stonyx-orm#202).
|
|
39
|
+
*
|
|
40
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
41
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
42
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
43
|
+
* the same function object is registered under every one of those keys.
|
|
44
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
45
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
46
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
47
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
48
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
49
|
+
*
|
|
50
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
51
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
52
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
53
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
54
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
55
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
38
56
|
*
|
|
39
57
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
40
58
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -53,10 +71,13 @@ export default class Orm {
|
|
|
53
71
|
* request routed to model Y -- inexpressible, which is the capability
|
|
54
72
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
55
73
|
*
|
|
56
|
-
* Empty when the REST server is disabled,
|
|
57
|
-
*
|
|
74
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
75
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
76
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
77
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
78
|
+
* against the prototype chain and this is not.
|
|
58
79
|
*/
|
|
59
|
-
|
|
80
|
+
accessFunctions: Record<string, AccessFunction>;
|
|
60
81
|
options: OrmOptions;
|
|
61
82
|
sqlDb?: SqlDb;
|
|
62
83
|
db?: OrmDB | SqlDb;
|
|
@@ -64,7 +85,8 @@ export default class Orm {
|
|
|
64
85
|
constructor(options?: OrmOptions);
|
|
65
86
|
init(): Promise<void>;
|
|
66
87
|
/**
|
|
67
|
-
* Resolve
|
|
88
|
+
* Resolve the `access` predicate registered for a model name
|
|
89
|
+
* (abofs/stonyx-orm#202).
|
|
68
90
|
*
|
|
69
91
|
* This is the supported way to reach another model's predicate while
|
|
70
92
|
* servicing a request routed to a different model. Call it with the model
|
|
@@ -73,15 +95,47 @@ export default class Orm {
|
|
|
73
95
|
*
|
|
74
96
|
* ```js
|
|
75
97
|
* const predicate = Orm.instance.getAccess('animal');
|
|
76
|
-
*
|
|
98
|
+
* if (!predicate) return deny;
|
|
99
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
77
100
|
* ```
|
|
78
101
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
102
|
+
* WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
|
|
103
|
+
* which is not necessarily specific to it: one class may claim many models
|
|
104
|
+
* and declares one `access` method, so
|
|
105
|
+
* `getAccess('owner') === getAccess('animal')` is `true` against this repo's
|
|
106
|
+
* fixture. See {@link Orm#accessFunctions}.
|
|
107
|
+
*
|
|
108
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
|
|
109
|
+
* includes a model whose access class failed to LOAD -- `setup-rest-server`
|
|
110
|
+
* catches, warns and publishes the partial map -- so it is not the same claim
|
|
111
|
+
* as "this model is unrestricted". Treat it as DENY, the same way
|
|
112
|
+
* `AccessContext.operation === undefined` is treated.
|
|
113
|
+
*
|
|
114
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
|
|
115
|
+
* its own, make the answer model-correct: the resolved predicate has to READ
|
|
116
|
+
* the context. Measured against this repo's shipped access class on a request
|
|
117
|
+
* express dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
118
|
+
*
|
|
119
|
+
* ```
|
|
120
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
121
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* The OWNERS filter, which returns `true` for animal 21 -- the record hidden
|
|
125
|
+
* on every animal surface. Under a mount that predicate recognises neither
|
|
126
|
+
* way it falls through to `['read', 'create', 'update', 'delete']`, a full
|
|
127
|
+
* CRUD grant. Either way: context supplied, answer not the animal answer,
|
|
128
|
+
* wrong in the GRANTING direction, because that predicate is arity-1 and
|
|
129
|
+
* identifies its collection from the request. AC9 asserts the first case on a
|
|
130
|
+
* live dispatch.
|
|
131
|
+
*
|
|
132
|
+
* Every predicate in this repo and in every consumer tree is arity-1 today,
|
|
133
|
+
* and there is no supported way for the caller to tell which kind it got; the
|
|
134
|
+
* boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
|
|
135
|
+
* the context, and do not treat a resolved predicate's answer as
|
|
136
|
+
* model-specific until that predicate reads it.
|
|
83
137
|
*
|
|
84
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
138
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
85
139
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
86
140
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
87
141
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
package/dist/main.js
CHANGED
|
@@ -39,7 +39,25 @@ export default class Orm {
|
|
|
39
39
|
transforms = { ...baseTransforms };
|
|
40
40
|
warnings = new Set();
|
|
41
41
|
/**
|
|
42
|
-
* Model name ->
|
|
42
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
43
|
+
* model (abofs/stonyx-orm#202).
|
|
44
|
+
*
|
|
45
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
46
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
47
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
48
|
+
* the same function object is registered under every one of those keys.
|
|
49
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
50
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
51
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
52
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
53
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
54
|
+
*
|
|
55
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
56
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
57
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
58
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
59
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
60
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
43
61
|
*
|
|
44
62
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
45
63
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -58,10 +76,13 @@ export default class Orm {
|
|
|
58
76
|
* request routed to model Y -- inexpressible, which is the capability
|
|
59
77
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
60
78
|
*
|
|
61
|
-
* Empty when the REST server is disabled,
|
|
62
|
-
*
|
|
79
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
80
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
81
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
82
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
83
|
+
* against the prototype chain and this is not.
|
|
63
84
|
*/
|
|
64
|
-
|
|
85
|
+
accessFunctions = {};
|
|
65
86
|
options;
|
|
66
87
|
sqlDb;
|
|
67
88
|
db;
|
|
@@ -170,7 +191,8 @@ export default class Orm {
|
|
|
170
191
|
Orm.initialized = true;
|
|
171
192
|
}
|
|
172
193
|
/**
|
|
173
|
-
* Resolve
|
|
194
|
+
* Resolve the `access` predicate registered for a model name
|
|
195
|
+
* (abofs/stonyx-orm#202).
|
|
174
196
|
*
|
|
175
197
|
* This is the supported way to reach another model's predicate while
|
|
176
198
|
* servicing a request routed to a different model. Call it with the model
|
|
@@ -179,15 +201,47 @@ export default class Orm {
|
|
|
179
201
|
*
|
|
180
202
|
* ```js
|
|
181
203
|
* const predicate = Orm.instance.getAccess('animal');
|
|
182
|
-
*
|
|
204
|
+
* if (!predicate) return deny;
|
|
205
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
183
206
|
* ```
|
|
184
207
|
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
208
|
+
* WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
|
|
209
|
+
* which is not necessarily specific to it: one class may claim many models
|
|
210
|
+
* and declares one `access` method, so
|
|
211
|
+
* `getAccess('owner') === getAccess('animal')` is `true` against this repo's
|
|
212
|
+
* fixture. See {@link Orm#accessFunctions}.
|
|
213
|
+
*
|
|
214
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
|
|
215
|
+
* includes a model whose access class failed to LOAD -- `setup-rest-server`
|
|
216
|
+
* catches, warns and publishes the partial map -- so it is not the same claim
|
|
217
|
+
* as "this model is unrestricted". Treat it as DENY, the same way
|
|
218
|
+
* `AccessContext.operation === undefined` is treated.
|
|
219
|
+
*
|
|
220
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
|
|
221
|
+
* its own, make the answer model-correct: the resolved predicate has to READ
|
|
222
|
+
* the context. Measured against this repo's shipped access class on a request
|
|
223
|
+
* express dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
224
|
+
*
|
|
225
|
+
* ```
|
|
226
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
227
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
228
|
+
* ```
|
|
229
|
+
*
|
|
230
|
+
* The OWNERS filter, which returns `true` for animal 21 -- the record hidden
|
|
231
|
+
* on every animal surface. Under a mount that predicate recognises neither
|
|
232
|
+
* way it falls through to `['read', 'create', 'update', 'delete']`, a full
|
|
233
|
+
* CRUD grant. Either way: context supplied, answer not the animal answer,
|
|
234
|
+
* wrong in the GRANTING direction, because that predicate is arity-1 and
|
|
235
|
+
* identifies its collection from the request. AC9 asserts the first case on a
|
|
236
|
+
* live dispatch.
|
|
237
|
+
*
|
|
238
|
+
* Every predicate in this repo and in every consumer tree is arity-1 today,
|
|
239
|
+
* and there is no supported way for the caller to tell which kind it got; the
|
|
240
|
+
* boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
|
|
241
|
+
* the context, and do not treat a resolved predicate's answer as
|
|
242
|
+
* model-specific until that predicate reads it.
|
|
189
243
|
*
|
|
190
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
244
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
191
245
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
192
246
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
193
247
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
@@ -206,9 +260,9 @@ export default class Orm {
|
|
|
206
260
|
* note above. Treat it as deny.
|
|
207
261
|
*/
|
|
208
262
|
getAccess(modelName) {
|
|
209
|
-
if (!Object.hasOwn(this.
|
|
263
|
+
if (!Object.hasOwn(this.accessFunctions, modelName))
|
|
210
264
|
return undefined;
|
|
211
|
-
return this.
|
|
265
|
+
return this.accessFunctions[modelName];
|
|
212
266
|
}
|
|
213
267
|
async startup() {
|
|
214
268
|
if (this.sqlDb)
|
package/dist/orm-request.d.ts
CHANGED
|
@@ -18,12 +18,26 @@
|
|
|
18
18
|
*
|
|
19
19
|
* context.operation The operation being authorised. Exactly one of the four
|
|
20
20
|
* verbs `'read'`, `'create'`, `'update'`, `'delete'` --
|
|
21
|
-
*
|
|
21
|
+
* no second vocabulary ON THIS PATH, and never an HTTP
|
|
22
22
|
* method name like `'GET'`. These are the same four
|
|
23
23
|
* strings the permission-array return shape is written in
|
|
24
24
|
* (`['read', 'create']`), because both come from the one
|
|
25
25
|
* `methodAccessMap` below.
|
|
26
26
|
*
|
|
27
|
+
* NOT the hook vocabulary. `HookContext.operation`
|
|
28
|
+
* (`src/hooks.ts`, documented under "Hook Context Object"
|
|
29
|
+
* in the README) carries `'list' | 'get' | 'create' |
|
|
30
|
+
* 'update' | 'delete'` on an identically-named key of an
|
|
31
|
+
* identically-shaped context object, and the access
|
|
32
|
+
* vocabulary collapses `list` and `get` into `'read'`. For
|
|
33
|
+
* one `GET /animals/1` a hook sees `'get'` and `access()`
|
|
34
|
+
* sees `'read'`, so a predicate cannot tell a collection
|
|
35
|
+
* read from a record read. `AccessOperation` makes
|
|
36
|
+
* `operation === 'get'` a compile error for a TypeScript
|
|
37
|
+
* consumer, because a predicate that stops matching falls
|
|
38
|
+
* through to the permission array -- the misreading is
|
|
39
|
+
* fail-open shaped.
|
|
40
|
+
*
|
|
27
41
|
* `undefined` when the dispatched method has no entry in
|
|
28
42
|
* that map. Express delivers `HEAD` to the `GET` handler,
|
|
29
43
|
* so this is reachable. It is left undefined rather than
|
|
@@ -35,6 +49,26 @@
|
|
|
35
49
|
* is no string to parse, no variant to miss, and no way to fail open through a
|
|
36
50
|
* URL shape nobody anticipated.
|
|
37
51
|
*
|
|
52
|
+
* WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
|
|
53
|
+
* the verb, not the route. Measured over the live router, six surfaces produce
|
|
54
|
+
* one identical context:
|
|
55
|
+
*
|
|
56
|
+
* GET /owners { model: 'owner', operation: 'read' }
|
|
57
|
+
* GET /owners/gina { model: 'owner', operation: 'read' }
|
|
58
|
+
* GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
59
|
+
* GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
60
|
+
* GET /owners/archived { model: 'owner', operation: 'read' }
|
|
61
|
+
* GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
62
|
+
*
|
|
63
|
+
* So a rule that depends on the SUB-PATH still needs `request.path` -- which is
|
|
64
|
+
* mount-relative and query-free, and is the one read of argument one the
|
|
65
|
+
* warning below sanctions. This repo's own fixture has such a rule: its
|
|
66
|
+
* `/archived` deny cannot be expressed from the context alone, and a predicate
|
|
67
|
+
* migrated to context-only would silently drop it, turning a deny into an
|
|
68
|
+
* allow. The related-resource and `?include=` surfaces serve ANOTHER model's
|
|
69
|
+
* records under `model: 'owner'`, and the context gives no signal of that
|
|
70
|
+
* (abofs/stonyx-orm#196).
|
|
71
|
+
*
|
|
38
72
|
* `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
|
|
39
73
|
* matching but BEFORE any handler executes (`@stonyx/rest-server`
|
|
40
74
|
* `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
|
|
@@ -53,9 +87,37 @@
|
|
|
53
87
|
* an owners route -- use the boot-time registry:
|
|
54
88
|
*
|
|
55
89
|
* const predicate = Orm.instance.getAccess('animal');
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
90
|
+
* if (!predicate) return deny;
|
|
91
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
92
|
+
*
|
|
93
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
|
|
94
|
+
* includes the case where the model has an access class that failed to load,
|
|
95
|
+
* because `setup-rest-server.ts` catches a load failure, warns, and publishes
|
|
96
|
+
* whatever partial map it had. It does NOT mean the model is unrestricted.
|
|
97
|
+
* Treat it as DENY, the same way `operation === undefined` is treated above.
|
|
98
|
+
*
|
|
99
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
|
|
100
|
+
* the answer model-correct on its own -- the resolved predicate has to READ it.
|
|
101
|
+
* Measured against this repo's own shipped access class, on a request express
|
|
102
|
+
* dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
103
|
+
*
|
|
104
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
105
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
106
|
+
*
|
|
107
|
+
* That is the OWNERS filter, and it returns `true` for animal 21 -- the record
|
|
108
|
+
* hidden on every animal surface. Under a mount that predicate recognises
|
|
109
|
+
* neither way it is worse: it falls through to
|
|
110
|
+
* `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
|
|
111
|
+
* context was supplied and the answer is not the animal answer, and it is wrong
|
|
112
|
+
* in the GRANTING direction, because that predicate is arity-1 and identifies
|
|
113
|
+
* its collection from the request. (The first of these is asserted on a live
|
|
114
|
+
* dispatch by AC9 in test/integration/orm-test.ts.)
|
|
115
|
+
*
|
|
116
|
+
* Every predicate in this repo and in every consumer tree is arity-1 on the day
|
|
117
|
+
* this ships, and the caller has no supported way to tell which kind it got --
|
|
118
|
+
* the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
|
|
119
|
+
* So: pass the context, and do not treat a resolved predicate's answer as
|
|
120
|
+
* model-specific until that predicate has been migrated to read the context.
|
|
59
121
|
*
|
|
60
122
|
* ---------------------------------------------------------------------------
|
|
61
123
|
* DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
|
package/dist/orm-request.js
CHANGED
|
@@ -18,12 +18,26 @@
|
|
|
18
18
|
*
|
|
19
19
|
* context.operation The operation being authorised. Exactly one of the four
|
|
20
20
|
* verbs `'read'`, `'create'`, `'update'`, `'delete'` --
|
|
21
|
-
*
|
|
21
|
+
* no second vocabulary ON THIS PATH, and never an HTTP
|
|
22
22
|
* method name like `'GET'`. These are the same four
|
|
23
23
|
* strings the permission-array return shape is written in
|
|
24
24
|
* (`['read', 'create']`), because both come from the one
|
|
25
25
|
* `methodAccessMap` below.
|
|
26
26
|
*
|
|
27
|
+
* NOT the hook vocabulary. `HookContext.operation`
|
|
28
|
+
* (`src/hooks.ts`, documented under "Hook Context Object"
|
|
29
|
+
* in the README) carries `'list' | 'get' | 'create' |
|
|
30
|
+
* 'update' | 'delete'` on an identically-named key of an
|
|
31
|
+
* identically-shaped context object, and the access
|
|
32
|
+
* vocabulary collapses `list` and `get` into `'read'`. For
|
|
33
|
+
* one `GET /animals/1` a hook sees `'get'` and `access()`
|
|
34
|
+
* sees `'read'`, so a predicate cannot tell a collection
|
|
35
|
+
* read from a record read. `AccessOperation` makes
|
|
36
|
+
* `operation === 'get'` a compile error for a TypeScript
|
|
37
|
+
* consumer, because a predicate that stops matching falls
|
|
38
|
+
* through to the permission array -- the misreading is
|
|
39
|
+
* fail-open shaped.
|
|
40
|
+
*
|
|
27
41
|
* `undefined` when the dispatched method has no entry in
|
|
28
42
|
* that map. Express delivers `HEAD` to the `GET` handler,
|
|
29
43
|
* so this is reachable. It is left undefined rather than
|
|
@@ -35,6 +49,26 @@
|
|
|
35
49
|
* is no string to parse, no variant to miss, and no way to fail open through a
|
|
36
50
|
* URL shape nobody anticipated.
|
|
37
51
|
*
|
|
52
|
+
* WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
|
|
53
|
+
* the verb, not the route. Measured over the live router, six surfaces produce
|
|
54
|
+
* one identical context:
|
|
55
|
+
*
|
|
56
|
+
* GET /owners { model: 'owner', operation: 'read' }
|
|
57
|
+
* GET /owners/gina { model: 'owner', operation: 'read' }
|
|
58
|
+
* GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
59
|
+
* GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
60
|
+
* GET /owners/archived { model: 'owner', operation: 'read' }
|
|
61
|
+
* GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
62
|
+
*
|
|
63
|
+
* So a rule that depends on the SUB-PATH still needs `request.path` -- which is
|
|
64
|
+
* mount-relative and query-free, and is the one read of argument one the
|
|
65
|
+
* warning below sanctions. This repo's own fixture has such a rule: its
|
|
66
|
+
* `/archived` deny cannot be expressed from the context alone, and a predicate
|
|
67
|
+
* migrated to context-only would silently drop it, turning a deny into an
|
|
68
|
+
* allow. The related-resource and `?include=` surfaces serve ANOTHER model's
|
|
69
|
+
* records under `model: 'owner'`, and the context gives no signal of that
|
|
70
|
+
* (abofs/stonyx-orm#196).
|
|
71
|
+
*
|
|
38
72
|
* `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
|
|
39
73
|
* matching but BEFORE any handler executes (`@stonyx/rest-server`
|
|
40
74
|
* `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
|
|
@@ -53,9 +87,37 @@
|
|
|
53
87
|
* an owners route -- use the boot-time registry:
|
|
54
88
|
*
|
|
55
89
|
* const predicate = Orm.instance.getAccess('animal');
|
|
56
|
-
*
|
|
90
|
+
* if (!predicate) return deny;
|
|
91
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
92
|
+
*
|
|
93
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
|
|
94
|
+
* includes the case where the model has an access class that failed to load,
|
|
95
|
+
* because `setup-rest-server.ts` catches a load failure, warns, and publishes
|
|
96
|
+
* whatever partial map it had. It does NOT mean the model is unrestricted.
|
|
97
|
+
* Treat it as DENY, the same way `operation === undefined` is treated above.
|
|
98
|
+
*
|
|
99
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
|
|
100
|
+
* the answer model-correct on its own -- the resolved predicate has to READ it.
|
|
101
|
+
* Measured against this repo's own shipped access class, on a request express
|
|
102
|
+
* dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
103
|
+
*
|
|
104
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
105
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
106
|
+
*
|
|
107
|
+
* That is the OWNERS filter, and it returns `true` for animal 21 -- the record
|
|
108
|
+
* hidden on every animal surface. Under a mount that predicate recognises
|
|
109
|
+
* neither way it is worse: it falls through to
|
|
110
|
+
* `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
|
|
111
|
+
* context was supplied and the answer is not the animal answer, and it is wrong
|
|
112
|
+
* in the GRANTING direction, because that predicate is arity-1 and identifies
|
|
113
|
+
* its collection from the request. (The first of these is asserted on a live
|
|
114
|
+
* dispatch by AC9 in test/integration/orm-test.ts.)
|
|
57
115
|
*
|
|
58
|
-
*
|
|
116
|
+
* Every predicate in this repo and in every consumer tree is arity-1 on the day
|
|
117
|
+
* this ships, and the caller has no supported way to tell which kind it got --
|
|
118
|
+
* the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
|
|
119
|
+
* So: pass the context, and do not treat a resolved predicate's answer as
|
|
120
|
+
* model-specific until that predicate has been migrated to read the context.
|
|
59
121
|
*
|
|
60
122
|
* ---------------------------------------------------------------------------
|
|
61
123
|
* DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
|
|
@@ -8,7 +8,7 @@ import { dbKey } from './db.js';
|
|
|
8
8
|
import { getPluralName } from './plural-registry.js';
|
|
9
9
|
import log from 'stonyx/log';
|
|
10
10
|
export default async function (route, accessPath, metaRoute) {
|
|
11
|
-
const
|
|
11
|
+
const accessFunctions = {};
|
|
12
12
|
try {
|
|
13
13
|
await forEachFileImport(accessPath, (accessClass) => {
|
|
14
14
|
const accessInstance = new accessClass();
|
|
@@ -25,9 +25,9 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
25
25
|
continue;
|
|
26
26
|
if (!store.data.has(model))
|
|
27
27
|
throw new Error(`Unable to define access for Invalid Model "${model}". Model does not exist`);
|
|
28
|
-
if (
|
|
28
|
+
if (accessFunctions[model])
|
|
29
29
|
throw new Error(`Access for model "${model}" has already been defined by another access class.`);
|
|
30
|
-
|
|
30
|
+
accessFunctions[model] = accessInstance.access;
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
33
|
}
|
|
@@ -38,7 +38,7 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
38
38
|
// -------------------------------------------------------------------------
|
|
39
39
|
// #202 -- the registry has to survive this function.
|
|
40
40
|
//
|
|
41
|
-
// `
|
|
41
|
+
// `accessFunctions` used to be a function-local that was discarded at the return
|
|
42
42
|
// below, so the only thing that ever saw it was the mount loop. Each mounted
|
|
43
43
|
// OrmRequest then held its OWN model's predicate and nothing held the map, so
|
|
44
44
|
// at request time there was no route from a model NAME to that model's
|
|
@@ -56,12 +56,12 @@ export default async function (route, accessPath, metaRoute) {
|
|
|
56
56
|
// set of predicates that is actually enforcing. A guard here that skipped the
|
|
57
57
|
// assignment would let the registry go silently missing, which is precisely
|
|
58
58
|
// the failure #202's AC8 exists to catch.
|
|
59
|
-
Orm.instance.
|
|
59
|
+
Orm.instance.accessFunctions = accessFunctions;
|
|
60
60
|
await waitForModule('rest-server');
|
|
61
61
|
// Remove "/" prefix and name mount point accordingly
|
|
62
62
|
const name = route === '/' ? 'index' : (route[0] === '/' ? route.slice(1) : route);
|
|
63
63
|
// Configure endpoints for models and views with access configuration
|
|
64
|
-
for (const [model, access] of Object.entries(
|
|
64
|
+
for (const [model, access] of Object.entries(accessFunctions)) {
|
|
65
65
|
const pluralizedModel = getPluralName(model);
|
|
66
66
|
const modelName = name === 'index' ? pluralizedModel : `${name}/${pluralizedModel}`;
|
|
67
67
|
RestServer.instance.mountRoute(OrmRequest, { name: modelName, options: { model, access } });
|
package/package.json
CHANGED
package/src/main.ts
CHANGED
|
@@ -71,7 +71,25 @@ export default class Orm {
|
|
|
71
71
|
warnings: Set<string> = new Set();
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
|
-
* Model name ->
|
|
74
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
75
|
+
* model (abofs/stonyx-orm#202).
|
|
76
|
+
*
|
|
77
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
78
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
79
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
80
|
+
* the same function object is registered under every one of those keys.
|
|
81
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
82
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
83
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
84
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
85
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
86
|
+
*
|
|
87
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
88
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
89
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
90
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
91
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
92
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
75
93
|
*
|
|
76
94
|
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
77
95
|
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
@@ -90,10 +108,13 @@ export default class Orm {
|
|
|
90
108
|
* request routed to model Y -- inexpressible, which is the capability
|
|
91
109
|
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
92
110
|
*
|
|
93
|
-
* Empty when the REST server is disabled,
|
|
94
|
-
*
|
|
111
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
112
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
113
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
114
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
115
|
+
* against the prototype chain and this is not.
|
|
95
116
|
*/
|
|
96
|
-
|
|
117
|
+
accessFunctions: Record<string, AccessFunction> = {};
|
|
97
118
|
|
|
98
119
|
options!: OrmOptions;
|
|
99
120
|
sqlDb?: SqlDb;
|
|
@@ -223,7 +244,8 @@ export default class Orm {
|
|
|
223
244
|
}
|
|
224
245
|
|
|
225
246
|
/**
|
|
226
|
-
* Resolve
|
|
247
|
+
* Resolve the `access` predicate registered for a model name
|
|
248
|
+
* (abofs/stonyx-orm#202).
|
|
227
249
|
*
|
|
228
250
|
* This is the supported way to reach another model's predicate while
|
|
229
251
|
* servicing a request routed to a different model. Call it with the model
|
|
@@ -232,15 +254,47 @@ export default class Orm {
|
|
|
232
254
|
*
|
|
233
255
|
* ```js
|
|
234
256
|
* const predicate = Orm.instance.getAccess('animal');
|
|
235
|
-
*
|
|
257
|
+
* if (!predicate) return deny;
|
|
258
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
236
259
|
* ```
|
|
237
260
|
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
261
|
+
* WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
|
|
262
|
+
* which is not necessarily specific to it: one class may claim many models
|
|
263
|
+
* and declares one `access` method, so
|
|
264
|
+
* `getAccess('owner') === getAccess('animal')` is `true` against this repo's
|
|
265
|
+
* fixture. See {@link Orm#accessFunctions}.
|
|
266
|
+
*
|
|
267
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
|
|
268
|
+
* includes a model whose access class failed to LOAD -- `setup-rest-server`
|
|
269
|
+
* catches, warns and publishes the partial map -- so it is not the same claim
|
|
270
|
+
* as "this model is unrestricted". Treat it as DENY, the same way
|
|
271
|
+
* `AccessContext.operation === undefined` is treated.
|
|
272
|
+
*
|
|
273
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
|
|
274
|
+
* its own, make the answer model-correct: the resolved predicate has to READ
|
|
275
|
+
* the context. Measured against this repo's shipped access class on a request
|
|
276
|
+
* express dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
277
|
+
*
|
|
278
|
+
* ```
|
|
279
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
280
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* The OWNERS filter, which returns `true` for animal 21 -- the record hidden
|
|
284
|
+
* on every animal surface. Under a mount that predicate recognises neither
|
|
285
|
+
* way it falls through to `['read', 'create', 'update', 'delete']`, a full
|
|
286
|
+
* CRUD grant. Either way: context supplied, answer not the animal answer,
|
|
287
|
+
* wrong in the GRANTING direction, because that predicate is arity-1 and
|
|
288
|
+
* identifies its collection from the request. AC9 asserts the first case on a
|
|
289
|
+
* live dispatch.
|
|
290
|
+
*
|
|
291
|
+
* Every predicate in this repo and in every consumer tree is arity-1 today,
|
|
292
|
+
* and there is no supported way for the caller to tell which kind it got; the
|
|
293
|
+
* boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
|
|
294
|
+
* the context, and do not treat a resolved predicate's answer as
|
|
295
|
+
* model-specific until that predicate reads it.
|
|
242
296
|
*
|
|
243
|
-
* OWN PROPERTIES ONLY. A bare `this.
|
|
297
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
244
298
|
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
245
299
|
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
246
300
|
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
@@ -259,9 +313,9 @@ export default class Orm {
|
|
|
259
313
|
* note above. Treat it as deny.
|
|
260
314
|
*/
|
|
261
315
|
getAccess(modelName: string): AccessFunction | undefined {
|
|
262
|
-
if (!Object.hasOwn(this.
|
|
316
|
+
if (!Object.hasOwn(this.accessFunctions, modelName)) return undefined;
|
|
263
317
|
|
|
264
|
-
return this.
|
|
318
|
+
return this.accessFunctions[modelName];
|
|
265
319
|
}
|
|
266
320
|
|
|
267
321
|
async startup(): Promise<void> {
|
package/src/orm-request.ts
CHANGED
|
@@ -18,12 +18,26 @@
|
|
|
18
18
|
*
|
|
19
19
|
* context.operation The operation being authorised. Exactly one of the four
|
|
20
20
|
* verbs `'read'`, `'create'`, `'update'`, `'delete'` --
|
|
21
|
-
*
|
|
21
|
+
* no second vocabulary ON THIS PATH, and never an HTTP
|
|
22
22
|
* method name like `'GET'`. These are the same four
|
|
23
23
|
* strings the permission-array return shape is written in
|
|
24
24
|
* (`['read', 'create']`), because both come from the one
|
|
25
25
|
* `methodAccessMap` below.
|
|
26
26
|
*
|
|
27
|
+
* NOT the hook vocabulary. `HookContext.operation`
|
|
28
|
+
* (`src/hooks.ts`, documented under "Hook Context Object"
|
|
29
|
+
* in the README) carries `'list' | 'get' | 'create' |
|
|
30
|
+
* 'update' | 'delete'` on an identically-named key of an
|
|
31
|
+
* identically-shaped context object, and the access
|
|
32
|
+
* vocabulary collapses `list` and `get` into `'read'`. For
|
|
33
|
+
* one `GET /animals/1` a hook sees `'get'` and `access()`
|
|
34
|
+
* sees `'read'`, so a predicate cannot tell a collection
|
|
35
|
+
* read from a record read. `AccessOperation` makes
|
|
36
|
+
* `operation === 'get'` a compile error for a TypeScript
|
|
37
|
+
* consumer, because a predicate that stops matching falls
|
|
38
|
+
* through to the permission array -- the misreading is
|
|
39
|
+
* fail-open shaped.
|
|
40
|
+
*
|
|
27
41
|
* `undefined` when the dispatched method has no entry in
|
|
28
42
|
* that map. Express delivers `HEAD` to the `GET` handler,
|
|
29
43
|
* so this is reachable. It is left undefined rather than
|
|
@@ -35,6 +49,26 @@
|
|
|
35
49
|
* is no string to parse, no variant to miss, and no way to fail open through a
|
|
36
50
|
* URL shape nobody anticipated.
|
|
37
51
|
*
|
|
52
|
+
* WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
|
|
53
|
+
* the verb, not the route. Measured over the live router, six surfaces produce
|
|
54
|
+
* one identical context:
|
|
55
|
+
*
|
|
56
|
+
* GET /owners { model: 'owner', operation: 'read' }
|
|
57
|
+
* GET /owners/gina { model: 'owner', operation: 'read' }
|
|
58
|
+
* GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
59
|
+
* GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
60
|
+
* GET /owners/archived { model: 'owner', operation: 'read' }
|
|
61
|
+
* GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
62
|
+
*
|
|
63
|
+
* So a rule that depends on the SUB-PATH still needs `request.path` -- which is
|
|
64
|
+
* mount-relative and query-free, and is the one read of argument one the
|
|
65
|
+
* warning below sanctions. This repo's own fixture has such a rule: its
|
|
66
|
+
* `/archived` deny cannot be expressed from the context alone, and a predicate
|
|
67
|
+
* migrated to context-only would silently drop it, turning a deny into an
|
|
68
|
+
* allow. The related-resource and `?include=` surfaces serve ANOTHER model's
|
|
69
|
+
* records under `model: 'owner'`, and the context gives no signal of that
|
|
70
|
+
* (abofs/stonyx-orm#196).
|
|
71
|
+
*
|
|
38
72
|
* `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
|
|
39
73
|
* matching but BEFORE any handler executes (`@stonyx/rest-server`
|
|
40
74
|
* `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
|
|
@@ -53,9 +87,37 @@
|
|
|
53
87
|
* an owners route -- use the boot-time registry:
|
|
54
88
|
*
|
|
55
89
|
* const predicate = Orm.instance.getAccess('animal');
|
|
56
|
-
*
|
|
90
|
+
* if (!predicate) return deny;
|
|
91
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
92
|
+
*
|
|
93
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
|
|
94
|
+
* includes the case where the model has an access class that failed to load,
|
|
95
|
+
* because `setup-rest-server.ts` catches a load failure, warns, and publishes
|
|
96
|
+
* whatever partial map it had. It does NOT mean the model is unrestricted.
|
|
97
|
+
* Treat it as DENY, the same way `operation === undefined` is treated above.
|
|
98
|
+
*
|
|
99
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
|
|
100
|
+
* the answer model-correct on its own -- the resolved predicate has to READ it.
|
|
101
|
+
* Measured against this repo's own shipped access class, on a request express
|
|
102
|
+
* dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
103
|
+
*
|
|
104
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
105
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
106
|
+
*
|
|
107
|
+
* That is the OWNERS filter, and it returns `true` for animal 21 -- the record
|
|
108
|
+
* hidden on every animal surface. Under a mount that predicate recognises
|
|
109
|
+
* neither way it is worse: it falls through to
|
|
110
|
+
* `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
|
|
111
|
+
* context was supplied and the answer is not the animal answer, and it is wrong
|
|
112
|
+
* in the GRANTING direction, because that predicate is arity-1 and identifies
|
|
113
|
+
* its collection from the request. (The first of these is asserted on a live
|
|
114
|
+
* dispatch by AC9 in test/integration/orm-test.ts.)
|
|
57
115
|
*
|
|
58
|
-
*
|
|
116
|
+
* Every predicate in this repo and in every consumer tree is arity-1 on the day
|
|
117
|
+
* this ships, and the caller has no supported way to tell which kind it got --
|
|
118
|
+
* the boot-time arity warning that would surface it is abofs/stonyx-orm#213.
|
|
119
|
+
* So: pass the context, and do not treat a resolved predicate's answer as
|
|
120
|
+
* model-specific until that predicate has been migrated to read the context.
|
|
59
121
|
*
|
|
60
122
|
* ---------------------------------------------------------------------------
|
|
61
123
|
* DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
|
package/src/setup-rest-server.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface AccessInstance {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export default async function(route: string, accessPath: string, metaRoute: boolean): Promise<void> {
|
|
23
|
-
const
|
|
23
|
+
const accessFunctions: Record<string, AccessFunction> = {};
|
|
24
24
|
|
|
25
25
|
try {
|
|
26
26
|
await forEachFileImport(accessPath, (accessClass: unknown) => {
|
|
@@ -37,9 +37,9 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
37
37
|
for (const model of models === '*' ? availableModels : models) {
|
|
38
38
|
if (model === dbKey) continue;
|
|
39
39
|
if (!store.data.has(model)) throw new Error(`Unable to define access for Invalid Model "${model}". Model does not exist`);
|
|
40
|
-
if (
|
|
40
|
+
if (accessFunctions![model]) throw new Error(`Access for model "${model}" has already been defined by another access class.`);
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
accessFunctions![model] = accessInstance.access;
|
|
43
43
|
}
|
|
44
44
|
});
|
|
45
45
|
} catch (error) {
|
|
@@ -50,7 +50,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
50
50
|
// -------------------------------------------------------------------------
|
|
51
51
|
// #202 -- the registry has to survive this function.
|
|
52
52
|
//
|
|
53
|
-
// `
|
|
53
|
+
// `accessFunctions` used to be a function-local that was discarded at the return
|
|
54
54
|
// below, so the only thing that ever saw it was the mount loop. Each mounted
|
|
55
55
|
// OrmRequest then held its OWN model's predicate and nothing held the map, so
|
|
56
56
|
// at request time there was no route from a model NAME to that model's
|
|
@@ -68,7 +68,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
68
68
|
// set of predicates that is actually enforcing. A guard here that skipped the
|
|
69
69
|
// assignment would let the registry go silently missing, which is precisely
|
|
70
70
|
// the failure #202's AC8 exists to catch.
|
|
71
|
-
Orm.instance.
|
|
71
|
+
Orm.instance.accessFunctions = accessFunctions;
|
|
72
72
|
|
|
73
73
|
await waitForModule('rest-server');
|
|
74
74
|
|
|
@@ -76,7 +76,7 @@ export default async function(route: string, accessPath: string, metaRoute: bool
|
|
|
76
76
|
const name = route === '/' ? 'index' : (route[0] === '/' ? route.slice(1) : route);
|
|
77
77
|
|
|
78
78
|
// Configure endpoints for models and views with access configuration
|
|
79
|
-
for (const [model, access] of Object.entries(
|
|
79
|
+
for (const [model, access] of Object.entries(accessFunctions!)) {
|
|
80
80
|
const pluralizedModel = getPluralName(model);
|
|
81
81
|
const modelName = name === 'index' ? pluralizedModel : `${name}/${pluralizedModel}`;
|
|
82
82
|
RestServer.instance.mountRoute(OrmRequest, { name: modelName, options: { model, access } });
|