@stonyx/orm 0.3.2-beta.153 → 0.3.2-beta.154
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 +186 -8
- package/dist/index.d.ts +1 -0
- package/dist/main.d.ts +116 -0
- package/dist/main.js +119 -0
- package/dist/orm-request.d.ts +121 -3
- package/dist/orm-request.js +151 -1
- package/dist/setup-rest-server.js +51 -5
- package/dist/types/orm-types.d.ts +101 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/main.ts +123 -0
- package/src/orm-request.ts +156 -6
- package/src/setup-rest-server.ts +59 -6
- package/src/types/orm-types.ts +106 -0
- package/src/types/stonyx-rest-server.d.ts +14 -1
package/README.md
CHANGED
|
@@ -381,6 +381,174 @@ export default class GlobalAccess {
|
|
|
381
381
|
}
|
|
382
382
|
```
|
|
383
383
|
|
|
384
|
+
|
|
385
|
+
### The access context (second argument)
|
|
386
|
+
|
|
387
|
+
`access()` is called with **two** arguments:
|
|
388
|
+
|
|
389
|
+
```js
|
|
390
|
+
access(request, { model, operation })
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
The second is the **access context** — the structural facts about the request,
|
|
394
|
+
which the framework already holds at authorization time. Read these instead of
|
|
395
|
+
parsing anything.
|
|
396
|
+
|
|
397
|
+
| Key | Value |
|
|
398
|
+
|---|---|
|
|
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 *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
|
+
|
|
402
|
+
So a predicate can be written without reference to any URL:
|
|
403
|
+
|
|
404
|
+
```js
|
|
405
|
+
export default class OwnerAccess {
|
|
406
|
+
models = ['owner'];
|
|
407
|
+
|
|
408
|
+
access(request, { model, operation }) {
|
|
409
|
+
if (model === 'owner' && operation === 'read') {
|
|
410
|
+
return record => record.id !== 'angela';
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return ['read'];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
There is no string to parse, no variant to miss, and no way to fail open through
|
|
419
|
+
a URL shape nobody anticipated. `model` is fixed at mount time and no request
|
|
420
|
+
can influence it — not a mount prefix, not a query string, not a case-varied
|
|
421
|
+
path, not an absolute-form request target.
|
|
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
|
+
|
|
466
|
+
The four `operation` values are the same four strings the permission-array
|
|
467
|
+
return shape is written in (`['read', 'create', 'update', 'delete']`), because
|
|
468
|
+
both come from one method map inside the framework. The two forms cannot
|
|
469
|
+
disagree about the same request.
|
|
470
|
+
|
|
471
|
+
**`operation` is `undefined`, never defaulted, for an unmapped method.** Express
|
|
472
|
+
delivers `HEAD` to the `GET` handler, so this is reachable. It is deliberately
|
|
473
|
+
not defaulted to `'read'`: a fabricated operation would turn an unclassified
|
|
474
|
+
request into an authorized one. Treat `undefined` as *not classified* and deny.
|
|
475
|
+
|
|
476
|
+
**The second argument is additive.** JavaScript ignores extra arguments, so an
|
|
477
|
+
existing `access(request)` predicate keeps working exactly as it did. Nothing
|
|
478
|
+
needs to be migrated to keep running — but note that argument **one** is still
|
|
479
|
+
the raw request, so the warning in
|
|
480
|
+
[Identifying the collection](#identifying-the-collection) still applies to any
|
|
481
|
+
predicate that reads it.
|
|
482
|
+
|
|
483
|
+
#### `record` is not in the context
|
|
484
|
+
|
|
485
|
+
Deliberately, and it is not an oversight. `auth()` runs after route matching but
|
|
486
|
+
**before any handler executes**, so nothing has been fetched yet. Supplying a
|
|
487
|
+
record would force a pre-fetch on every request — a second store hit, a new
|
|
488
|
+
failure mode, and an ordering change in the middle of an authorization path.
|
|
489
|
+
|
|
490
|
+
It is also unnecessary: the **function** return shape already *is* the
|
|
491
|
+
per-record hook. Return `(record) => boolean` and the handlers apply it to every
|
|
492
|
+
record the request touches. Auth-time and record-time are separate decision
|
|
493
|
+
points, and the contract keeps them separate.
|
|
494
|
+
|
|
495
|
+
#### Reaching another model's predicate
|
|
496
|
+
|
|
497
|
+
The model → predicate map is published on the ORM instance at boot, before any
|
|
498
|
+
route is mounted, so a predicate can be resolved by model name and asked about a
|
|
499
|
+
request routed to a *different* model:
|
|
500
|
+
|
|
501
|
+
```js
|
|
502
|
+
import Orm from '@stonyx/orm';
|
|
503
|
+
|
|
504
|
+
const predicate = Orm.instance.getAccess('animal');
|
|
505
|
+
if (!predicate) return deny;
|
|
506
|
+
|
|
507
|
+
const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
508
|
+
```
|
|
509
|
+
|
|
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).
|
|
548
|
+
|
|
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.
|
|
551
|
+
|
|
384
552
|
### Return values
|
|
385
553
|
|
|
386
554
|
| `access()` returns | Effect |
|
|
@@ -512,14 +680,24 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
512
680
|
one collection is writable and another is filtered on a field the first can
|
|
513
681
|
set. Blocking it requires checking animal 21 against the **animal** model's
|
|
514
682
|
predicate while servicing an **owners** route — cross-model access resolution,
|
|
515
|
-
which the
|
|
516
|
-
|
|
517
|
-
`setup-rest-server.ts`
|
|
518
|
-
|
|
519
|
-
[
|
|
520
|
-
(
|
|
521
|
-
|
|
522
|
-
|
|
683
|
+
which the contract could not express before
|
|
684
|
+
[#202](https://github.com/abofs/stonyx-orm/issues/202): `access()` never
|
|
685
|
+
received the model structurally and `setup-rest-server.ts` discarded the
|
|
686
|
+
model→predicate map at boot. **#202 has landed and both halves now exist** —
|
|
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
|
|
696
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196) and
|
|
697
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207), which were blocked on
|
|
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
|
|
700
|
+
the hidden ones.
|
|
523
701
|
- **Authorization by identifying the collection is a consumer-side
|
|
524
702
|
reconstruction of information the framework already holds.** `access()`
|
|
525
703
|
receives a transport artifact and is asked to work out which model, which
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { count, avg, sum, min, max } from './aggregates.js';
|
|
|
9
9
|
export { default } from './main.js';
|
|
10
10
|
export { store, relationships } from './main.js';
|
|
11
11
|
export type { PersistErrorDetail } from './main.js';
|
|
12
|
+
export type { AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
12
13
|
export { Model, View, Serializer };
|
|
13
14
|
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
14
15
|
export { count, avg, sum, min, max };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Store from './store.js';
|
|
2
|
+
import type { AccessFunction } from './types/orm-types.js';
|
|
2
3
|
interface OrmOptions {
|
|
3
4
|
dbType?: string;
|
|
4
5
|
}
|
|
@@ -32,12 +33,127 @@ export default class Orm {
|
|
|
32
33
|
views: Record<string, unknown>;
|
|
33
34
|
transforms: Record<string, (value: unknown) => unknown>;
|
|
34
35
|
warnings: Set<string>;
|
|
36
|
+
/**
|
|
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.
|
|
56
|
+
*
|
|
57
|
+
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
58
|
+
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
59
|
+
* and reachable before the first request can be served. The mapping is
|
|
60
|
+
* one-to-one by construction: setup-rest-server throws if two access classes
|
|
61
|
+
* claim the same model.
|
|
62
|
+
*
|
|
63
|
+
* Keys are model names as declared and stored (kebab-case, e.g.
|
|
64
|
+
* `'phone-number'`), NOT pluralised or mount-prefixed route names.
|
|
65
|
+
*
|
|
66
|
+
* WHY THIS EXISTS AS A FIELD. It used to be a function-local in
|
|
67
|
+
* setup-rest-server that was discarded when that function returned, so at
|
|
68
|
+
* request time there was no way to get from a model name to that model's
|
|
69
|
+
* predicate at all. Each `OrmRequest` held only its OWN model's predicate.
|
|
70
|
+
* That made cross-model authorization -- asking model X's predicate about a
|
|
71
|
+
* request routed to model Y -- inexpressible, which is the capability
|
|
72
|
+
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
73
|
+
*
|
|
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.
|
|
79
|
+
*/
|
|
80
|
+
accessFunctions: Record<string, AccessFunction>;
|
|
35
81
|
options: OrmOptions;
|
|
36
82
|
sqlDb?: SqlDb;
|
|
37
83
|
db?: OrmDB | SqlDb;
|
|
38
84
|
private _persistErrorHandler;
|
|
39
85
|
constructor(options?: OrmOptions);
|
|
40
86
|
init(): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Resolve the `access` predicate registered for a model name
|
|
89
|
+
* (abofs/stonyx-orm#202).
|
|
90
|
+
*
|
|
91
|
+
* This is the supported way to reach another model's predicate while
|
|
92
|
+
* servicing a request routed to a different model. Call it with the model
|
|
93
|
+
* name and invoke the result with the live request and an explicit context
|
|
94
|
+
* naming THAT model:
|
|
95
|
+
*
|
|
96
|
+
* ```js
|
|
97
|
+
* const predicate = Orm.instance.getAccess('animal');
|
|
98
|
+
* if (!predicate) return deny;
|
|
99
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
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.
|
|
137
|
+
*
|
|
138
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
139
|
+
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
140
|
+
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
141
|
+
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
142
|
+
* returned a TRUTHY value (`Object(request)` is the request), bypassing the
|
|
143
|
+
* `undefined`-means-deny contract entirely. Nothing in the ORM calls
|
|
144
|
+
* `getAccess` yet, so it was not exploitable as shipped -- but #207 takes the
|
|
145
|
+
* model name from the REQUEST BODY (`data.relationships.<key>.data.type`),
|
|
146
|
+
* which would have made a one-field body an authorization bypass. Guarded
|
|
147
|
+
* here at the read point rather than by constructing the map with a null
|
|
148
|
+
* prototype, because the field is public and reassignable and the guard has
|
|
149
|
+
* to hold whatever object it is holding.
|
|
150
|
+
*
|
|
151
|
+
* @param modelName - Model name as declared and stored (kebab-case).
|
|
152
|
+
* @returns The predicate, or `undefined` when no predicate could be resolved
|
|
153
|
+
* for that name. `undefined` is NOT "this model is unrestricted" -- see the
|
|
154
|
+
* note above. Treat it as deny.
|
|
155
|
+
*/
|
|
156
|
+
getAccess(modelName: string): AccessFunction | undefined;
|
|
41
157
|
startup(): Promise<void>;
|
|
42
158
|
shutdown(): Promise<void>;
|
|
43
159
|
static get db(): OrmDB | SqlDb;
|
package/dist/main.js
CHANGED
|
@@ -38,6 +38,51 @@ export default class Orm {
|
|
|
38
38
|
views = {};
|
|
39
39
|
transforms = { ...baseTransforms };
|
|
40
40
|
warnings = new Set();
|
|
41
|
+
/**
|
|
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.
|
|
61
|
+
*
|
|
62
|
+
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
63
|
+
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
64
|
+
* and reachable before the first request can be served. The mapping is
|
|
65
|
+
* one-to-one by construction: setup-rest-server throws if two access classes
|
|
66
|
+
* claim the same model.
|
|
67
|
+
*
|
|
68
|
+
* Keys are model names as declared and stored (kebab-case, e.g.
|
|
69
|
+
* `'phone-number'`), NOT pluralised or mount-prefixed route names.
|
|
70
|
+
*
|
|
71
|
+
* WHY THIS EXISTS AS A FIELD. It used to be a function-local in
|
|
72
|
+
* setup-rest-server that was discarded when that function returned, so at
|
|
73
|
+
* request time there was no way to get from a model name to that model's
|
|
74
|
+
* predicate at all. Each `OrmRequest` held only its OWN model's predicate.
|
|
75
|
+
* That made cross-model authorization -- asking model X's predicate about a
|
|
76
|
+
* request routed to model Y -- inexpressible, which is the capability
|
|
77
|
+
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
78
|
+
*
|
|
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.
|
|
84
|
+
*/
|
|
85
|
+
accessFunctions = {};
|
|
41
86
|
options;
|
|
42
87
|
sqlDb;
|
|
43
88
|
db;
|
|
@@ -145,6 +190,80 @@ export default class Orm {
|
|
|
145
190
|
Orm.ready = await Promise.all(promises);
|
|
146
191
|
Orm.initialized = true;
|
|
147
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Resolve the `access` predicate registered for a model name
|
|
195
|
+
* (abofs/stonyx-orm#202).
|
|
196
|
+
*
|
|
197
|
+
* This is the supported way to reach another model's predicate while
|
|
198
|
+
* servicing a request routed to a different model. Call it with the model
|
|
199
|
+
* name and invoke the result with the live request and an explicit context
|
|
200
|
+
* naming THAT model:
|
|
201
|
+
*
|
|
202
|
+
* ```js
|
|
203
|
+
* const predicate = Orm.instance.getAccess('animal');
|
|
204
|
+
* if (!predicate) return deny;
|
|
205
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
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.
|
|
243
|
+
*
|
|
244
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
245
|
+
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
246
|
+
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
247
|
+
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
248
|
+
* returned a TRUTHY value (`Object(request)` is the request), bypassing the
|
|
249
|
+
* `undefined`-means-deny contract entirely. Nothing in the ORM calls
|
|
250
|
+
* `getAccess` yet, so it was not exploitable as shipped -- but #207 takes the
|
|
251
|
+
* model name from the REQUEST BODY (`data.relationships.<key>.data.type`),
|
|
252
|
+
* which would have made a one-field body an authorization bypass. Guarded
|
|
253
|
+
* here at the read point rather than by constructing the map with a null
|
|
254
|
+
* prototype, because the field is public and reassignable and the guard has
|
|
255
|
+
* to hold whatever object it is holding.
|
|
256
|
+
*
|
|
257
|
+
* @param modelName - Model name as declared and stored (kebab-case).
|
|
258
|
+
* @returns The predicate, or `undefined` when no predicate could be resolved
|
|
259
|
+
* for that name. `undefined` is NOT "this model is unrestricted" -- see the
|
|
260
|
+
* note above. Treat it as deny.
|
|
261
|
+
*/
|
|
262
|
+
getAccess(modelName) {
|
|
263
|
+
if (!Object.hasOwn(this.accessFunctions, modelName))
|
|
264
|
+
return undefined;
|
|
265
|
+
return this.accessFunctions[modelName];
|
|
266
|
+
}
|
|
148
267
|
async startup() {
|
|
149
268
|
if (this.sqlDb)
|
|
150
269
|
await this.sqlDb.startup();
|
package/dist/orm-request.d.ts
CHANGED
|
@@ -2,6 +2,124 @@
|
|
|
2
2
|
* REST request handling and access enforcement for @stonyx/orm.
|
|
3
3
|
*
|
|
4
4
|
* ---------------------------------------------------------------------------
|
|
5
|
+
* THE `access()` CONTRACT: `access(request, { model, operation })`
|
|
6
|
+
* ---------------------------------------------------------------------------
|
|
7
|
+
* `auth()` calls your predicate with TWO arguments. The second is the access
|
|
8
|
+
* CONTEXT -- the structural facts about the request, which the framework
|
|
9
|
+
* already holds and which you should read INSTEAD of parsing anything:
|
|
10
|
+
*
|
|
11
|
+
* context.model The model this route was mounted for, as a model name:
|
|
12
|
+
* kebab-case, exactly as declared under
|
|
13
|
+
* `config.orm.paths.model` and keyed in the store --
|
|
14
|
+
* `'owner'`, `'animal'`, `'phone-number'`. NOT the
|
|
15
|
+
* pluralised, dasherized, mount-prefixed ROUTE name. It is
|
|
16
|
+
* read from the OrmRequest instance, fixed at mount time,
|
|
17
|
+
* and no request can influence it.
|
|
18
|
+
*
|
|
19
|
+
* context.operation The operation being authorised. Exactly one of the four
|
|
20
|
+
* verbs `'read'`, `'create'`, `'update'`, `'delete'` --
|
|
21
|
+
* no second vocabulary ON THIS PATH, and never an HTTP
|
|
22
|
+
* method name like `'GET'`. These are the same four
|
|
23
|
+
* strings the permission-array return shape is written in
|
|
24
|
+
* (`['read', 'create']`), because both come from the one
|
|
25
|
+
* `methodAccessMap` below.
|
|
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
|
+
*
|
|
41
|
+
* `undefined` when the dispatched method has no entry in
|
|
42
|
+
* that map. Express delivers `HEAD` to the `GET` handler,
|
|
43
|
+
* so this is reachable. It is left undefined rather than
|
|
44
|
+
* defaulted on purpose -- a fabricated `'read'` would turn
|
|
45
|
+
* an unclassified request into an authorised one. Treat
|
|
46
|
+
* `undefined` as "not classified" and deny.
|
|
47
|
+
*
|
|
48
|
+
* So a consumer writes `if (model === 'owner' && operation === 'read')`. There
|
|
49
|
+
* is no string to parse, no variant to miss, and no way to fail open through a
|
|
50
|
+
* URL shape nobody anticipated.
|
|
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
|
+
*
|
|
72
|
+
* `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
|
|
73
|
+
* matching but BEFORE any handler executes (`@stonyx/rest-server`
|
|
74
|
+
* `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
|
|
75
|
+
* record would force a pre-fetch on every request, a second store hit and an
|
|
76
|
+
* ordering change in the middle of an authorization path. It is also
|
|
77
|
+
* unnecessary: the FUNCTION return shape already is the per-record hook. Return
|
|
78
|
+
* `(record) => boolean` and the handlers apply it to every record the request
|
|
79
|
+
* touches. Auth-time and record-time are separate decision points.
|
|
80
|
+
*
|
|
81
|
+
* THE SECOND ARGUMENT IS ADDITIVE. JavaScript ignores extra arguments, so an
|
|
82
|
+
* existing `access(request)` predicate keeps working exactly as before. The
|
|
83
|
+
* warning immediately below is therefore still live: `request` is still
|
|
84
|
+
* argument ONE, and reading it is still how predicates fail open.
|
|
85
|
+
*
|
|
86
|
+
* To reach ANOTHER model's predicate -- e.g. to check an animal while servicing
|
|
87
|
+
* an owners route -- use the boot-time registry:
|
|
88
|
+
*
|
|
89
|
+
* const predicate = Orm.instance.getAccess('animal');
|
|
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.
|
|
121
|
+
*
|
|
122
|
+
* ---------------------------------------------------------------------------
|
|
5
123
|
* DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
|
|
6
124
|
* ---------------------------------------------------------------------------
|
|
7
125
|
* `auth()` below hands your `access(request)` a raw transport artifact and asks
|
|
@@ -59,6 +177,7 @@
|
|
|
59
177
|
* See `### Known limitations` in README.
|
|
60
178
|
*/
|
|
61
179
|
import { Request } from '@stonyx/rest-server';
|
|
180
|
+
import type { AccessFunction } from './types/orm-types.js';
|
|
62
181
|
interface OrmRequest$ extends Request {
|
|
63
182
|
protocol?: string;
|
|
64
183
|
method: string;
|
|
@@ -73,13 +192,12 @@ interface OrmRequest$ extends Request {
|
|
|
73
192
|
};
|
|
74
193
|
get(header: string): string;
|
|
75
194
|
}
|
|
76
|
-
type AccessMethod = string | boolean | string[] | ((record: unknown) => boolean);
|
|
77
195
|
type HandlerFn = (request: OrmRequest$, state: {
|
|
78
196
|
[key: string]: unknown;
|
|
79
197
|
}) => unknown | Promise<unknown>;
|
|
80
198
|
export default class OrmRequest extends Request {
|
|
81
199
|
model: string;
|
|
82
|
-
access:
|
|
200
|
+
access: AccessFunction;
|
|
83
201
|
handlers: {
|
|
84
202
|
[key: string]: {
|
|
85
203
|
[key: string]: HandlerFn;
|
|
@@ -87,7 +205,7 @@ export default class OrmRequest extends Request {
|
|
|
87
205
|
};
|
|
88
206
|
constructor({ model, access }: {
|
|
89
207
|
model: string;
|
|
90
|
-
access:
|
|
208
|
+
access: AccessFunction;
|
|
91
209
|
});
|
|
92
210
|
private _withHooks;
|
|
93
211
|
private _generateRelationshipRoutes;
|