@stonyx/orm 0.3.2-alpha.71 → 0.3.2-alpha.73
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 +155 -52
- package/dist/access-verdict.d.ts +26 -0
- package/dist/access-verdict.js +63 -1
- package/dist/hooks.d.ts +15 -1
- package/dist/orm-request.d.ts +48 -0
- package/dist/orm-request.js +91 -0
- package/dist/record.js +128 -36
- package/dist/types/orm-types.d.ts +95 -0
- package/package.json +1 -1
- package/src/access-verdict.ts +65 -1
- package/src/hooks.ts +15 -1
- package/src/orm-request.ts +91 -0
- package/src/record.ts +137 -37
- package/src/types/orm-types.ts +96 -0
package/README.md
CHANGED
|
@@ -325,6 +325,19 @@ Access classes define models and provide custom filtering/authorization logic.
|
|
|
325
325
|
> deny — which folds case but does not decode, so `GET /owners/%61rchived` steps
|
|
326
326
|
> past it ([#228](https://github.com/abofs/stonyx-orm/issues/228)).
|
|
327
327
|
>
|
|
328
|
+
> **Superseded 2026-09-01 by [#236](https://github.com/abofs/stonyx-orm/issues/236) /
|
|
329
|
+
> [#237](https://github.com/abofs/stonyx-orm/issues/237), and left standing rather
|
|
330
|
+
> than rewritten.** Both claims above are now false: `#228` is **closed**, and the
|
|
331
|
+
> one string comparison variant 3 lived in is gone — the sample below compares the
|
|
332
|
+
> **decoded `recordId`** the access context supplies, so there is no comparison
|
|
333
|
+
> left to step around. The paragraph about `request.path` further down is
|
|
334
|
+
> superseded the same way. Nothing here is deleted because the same "variant 3
|
|
335
|
+
> survives" wording sits at four sites (this file twice, `src/orm-request.ts`, and
|
|
336
|
+
> the test fixture) and retiring one of four leaves the shipped copies
|
|
337
|
+
> contradicting each other; retiring all four **with the measurement that retires
|
|
338
|
+
> them** is [#238](https://github.com/abofs/stonyx-orm/issues/238), which also owns
|
|
339
|
+
> this blockquote and the reference section below.
|
|
340
|
+
>
|
|
328
341
|
> That is still a stopgap. **The real fix is
|
|
329
342
|
> [#202](https://github.com/abofs/stonyx-orm/issues/202)** — `access()` should
|
|
330
343
|
> receive the model, the operation and the record, so there is nothing to
|
|
@@ -337,6 +350,14 @@ Access classes define models and provide custom filtering/authorization logic.
|
|
|
337
350
|
> context alone** and a context-only rewrite would silently turn it into an
|
|
338
351
|
> allow.
|
|
339
352
|
>
|
|
353
|
+
> **Superseded 2026-09-01 by [#236](https://github.com/abofs/stonyx-orm/issues/236) /
|
|
354
|
+
> [#237](https://github.com/abofs/stonyx-orm/issues/237)** — see the dated note
|
|
355
|
+
> above. No read of argument **one** survives in the sample below: the context
|
|
356
|
+
> carries `recordId`, the decoded route-parameter id, so the `/archived` deny **is**
|
|
357
|
+
> expressible from the context alone. It still must not be dropped — expressible is
|
|
358
|
+
> not optional. Retirement of this wording:
|
|
359
|
+
> [#238](https://github.com/abofs/stonyx-orm/issues/238).
|
|
360
|
+
>
|
|
340
361
|
> The same warning is repeated at the top of `src/orm-request.ts`, which ships;
|
|
341
362
|
> the longer write-up in `docs/usage-patterns.md` does **not** ship, so this
|
|
342
363
|
> README and that source header are the two copies a consumer sees.
|
|
@@ -350,66 +371,73 @@ Access classes define models and provide custom filtering/authorization logic.
|
|
|
350
371
|
export default class GlobalAccess {
|
|
351
372
|
models = ['owner', 'animal'];
|
|
352
373
|
|
|
353
|
-
access(request, { model, operation }) {
|
|
374
|
+
access(request, { model, operation, recordId }) {
|
|
354
375
|
// `model` is the model this route was mounted for. It is assigned once, at
|
|
355
376
|
// mount time, and no request can influence it — not a mount prefix, not a
|
|
356
377
|
// query string, not a case-varied path, not an absolute-form request
|
|
357
|
-
// target.
|
|
358
|
-
//
|
|
359
|
-
//
|
|
360
|
-
//
|
|
361
|
-
//
|
|
362
|
-
//
|
|
378
|
+
// target. `recordId` is the record this route was ADDRESSED TO, decoded by
|
|
379
|
+
// the router and coerced to the key the store lookup uses. Nothing below
|
|
380
|
+
// parses anything, and since abofs/stonyx-orm#236 nothing below reads
|
|
381
|
+
// argument one AT ALL. Variants 1, 2, 4 and 5 were already unconstructible;
|
|
382
|
+
// the sub-path STRING COMPARISON that variant 3 lived in is gone too,
|
|
383
|
+
// replaced by a comparison against the decoded id. Retiring the "variant 3
|
|
384
|
+
// survives" wording at the four sites that still carry it — with the
|
|
385
|
+
// measurement that retires it, rather than by deletion — is
|
|
386
|
+
// abofs/stonyx-orm#238.
|
|
363
387
|
//
|
|
364
388
|
// `operation` is destructured to name the whole contract at the point of
|
|
365
|
-
// use. This sample's rules are per-model and per-
|
|
389
|
+
// use. This sample's rules are per-model and per-record rather than
|
|
366
390
|
// per-verb, so it does not branch on it; the permission array at the bottom
|
|
367
391
|
// is where the verb is answered.
|
|
368
392
|
|
|
369
|
-
// FAIL CLOSED ON
|
|
370
|
-
// resolved this predicate without supplying the context, and a request
|
|
371
|
-
// function cannot identify DENIES rather than falling through to the
|
|
372
|
-
// grant at the bottom. An unidentifiable input must never be the
|
|
373
|
-
// path.
|
|
374
|
-
// not cover it.
|
|
393
|
+
// FAIL CLOSED ON AN UNIDENTIFIABLE MODEL. `model` is absent for any caller
|
|
394
|
+
// that resolved this predicate without supplying the context, and a request
|
|
395
|
+
// this function cannot identify DENIES rather than falling through to the
|
|
396
|
+
// CRUD grant at the bottom. An unidentifiable input must never be the
|
|
397
|
+
// permissive path.
|
|
375
398
|
if (typeof model !== 'string' || model === '') return false;
|
|
376
399
|
|
|
377
400
|
if (model === 'owner') {
|
|
378
|
-
//
|
|
379
|
-
//
|
|
380
|
-
//
|
|
381
|
-
//
|
|
382
|
-
//
|
|
401
|
+
// FAIL CLOSED ON AN ABSENT `recordId` TOO, AND `undefined` IS THE ONLY
|
|
402
|
+
// SPELLING OF ABSENT. `auth()` ALWAYS sets the key — `null` on a
|
|
403
|
+
// collection route, which is addressed to no record — so `undefined`
|
|
404
|
+
// means the context did not come from `auth()`: it was hand-assembled by
|
|
405
|
+
// a caller resolving this predicate through the documented
|
|
406
|
+
// `Orm.instance.getAccess()` path. Letting that through would fall
|
|
407
|
+
// straight to the per-record filter below, which is a DENY becoming an
|
|
408
|
+
// ALLOW. This is the same rule the old guard on `request.path` enforced,
|
|
409
|
+
// moved to the argument this predicate now actually reads.
|
|
410
|
+
if (recordId === undefined) return false;
|
|
411
|
+
|
|
412
|
+
// THE `/archived` DENY, EXPRESSED AGAINST THE DECODED ID. It used to be
|
|
413
|
+
// `request.path.toLowerCase()` compared against `'/archived'`, and that
|
|
414
|
+
// was wrong in both directions at once.
|
|
383
415
|
//
|
|
384
|
-
//
|
|
385
|
-
//
|
|
416
|
+
// TOO PERMISSIVE: express sets `request.path` from the RAW pathname while
|
|
417
|
+
// the router DECODES `:id`, so `GET /owners/%61rchived` reached the
|
|
418
|
+
// comparison as `/%61rchived`, walked past the deny and was dispatched as
|
|
419
|
+
// the record `archived` — 200 with the record in full, and DELETE
|
|
420
|
+
// answered 204 with the record DESTROYED, unauthenticated. 255
|
|
421
|
+
// non-canonical spellings of that 8-character id decode to the same key,
|
|
422
|
+
// so no deny-list of spellings was ever going to close it.
|
|
386
423
|
//
|
|
387
|
-
//
|
|
388
|
-
//
|
|
389
|
-
//
|
|
390
|
-
// `
|
|
391
|
-
//
|
|
392
|
-
// `String(request.path ?? '')` is then `''`, which matches no sub-path
|
|
393
|
-
// rule and falls straight through to the per-record filter below. That is
|
|
394
|
-
// a DENY becoming an ALLOW. An input this function cannot identify DENIES,
|
|
395
|
-
// whichever ARGUMENT it arrived on — which is also why the `?? ''` this
|
|
396
|
-
// file's header condemns does not appear below.
|
|
397
|
-
if (typeof request?.path !== 'string' || request.path === '') return false;
|
|
398
|
-
|
|
399
|
-
// Lower-cased because the router matched case-insensitively, so a
|
|
400
|
-
// case-sensitive rule here would be stricter than the router and could be
|
|
401
|
-
// stepped around.
|
|
424
|
+
// TOO STRICT: a record id is a VALUE, not a literal route segment, and
|
|
425
|
+
// express's `case sensitive routing` governs literal segments only. With
|
|
426
|
+
// a distinct owner seeded at `ARCHIVED`, the `.toLowerCase()` 403'd
|
|
427
|
+
// `GET /owners/ARCHIVED` — the wrong record — while still admitting
|
|
428
|
+
// `GET /owners/%41RCHIVED`, the same record encoded.
|
|
402
429
|
//
|
|
403
|
-
//
|
|
404
|
-
//
|
|
405
|
-
//
|
|
406
|
-
//
|
|
407
|
-
//
|
|
408
|
-
//
|
|
409
|
-
//
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
430
|
+
// SO DO NOT NORMALISE `recordId`. It is already decoded, exactly ONCE,
|
|
431
|
+
// which is what a route parameter means: `/owners/%2561rchived` is the
|
|
432
|
+
// legitimate id `%61rchived`, and decoding until stable would deny it. Do
|
|
433
|
+
// not case-fold it. Do not rebuild it from `request.path` — decoding the
|
|
434
|
+
// whole path decodes THEN splits while the router splits THEN decodes,
|
|
435
|
+
// which over-denies the distinct record at `/owners/archived%2fx`.
|
|
436
|
+
//
|
|
437
|
+
// THE DENY IS NOW EXPRESSIBLE FROM THE CONTEXT ALONE, which is exactly
|
|
438
|
+
// what `recordId` bought — and it still must not be dropped. Deleting it
|
|
439
|
+
// does not remove a rule loudly, it turns a deny into an ALLOW, silently.
|
|
440
|
+
if (recordId === 'archived') return false;
|
|
413
441
|
|
|
414
442
|
// Returning a function plugs it in as a per-record filter, and it is
|
|
415
443
|
// enforced on every surface addressed to one of these records:
|
|
@@ -492,6 +520,16 @@ access class shipped with this repo has such a rule: its `/archived` deny
|
|
|
492
520
|
**cannot be expressed from the context alone**, and a predicate migrated to
|
|
493
521
|
context-only would silently drop it — a deny becoming an allow.
|
|
494
522
|
|
|
523
|
+
**Superseded 2026-09-01 by [#236](https://github.com/abofs/stonyx-orm/issues/236) /
|
|
524
|
+
[#237](https://github.com/abofs/stonyx-orm/issues/237).** The context also carries
|
|
525
|
+
`recordId` — the record this route was addressed to, already decoded — so the
|
|
526
|
+
`/archived` deny **is** expressible from the context alone, and the shipped sample
|
|
527
|
+
no longer reads `request.path`. The full contract is `AccessContext.recordId` in
|
|
528
|
+
`src/types/orm-types.ts`, which ships. This section — the signature, the key table
|
|
529
|
+
and this paragraph — is corrected by
|
|
530
|
+
[#238](https://github.com/abofs/stonyx-orm/issues/238); the pointer is here because
|
|
531
|
+
what it currently says is an instruction, and the instruction is wrong.
|
|
532
|
+
|
|
495
533
|
Note also that the related-resource and `?include=` surfaces serve *another
|
|
496
534
|
model's* records under `model: 'owner'`, and the context gives a predicate no
|
|
497
535
|
signal that it is authorizing a related-resource route. That is
|
|
@@ -787,6 +825,17 @@ these values should be matched on:
|
|
|
787
825
|
| `GET http://anything.example/owners/angela` | `http://anything.example/angela` | `http://anything.example/owners/angela` | `/owners` | `/angela` |
|
|
788
826
|
| `GET /api/animals/22` (`ORM_REST_ROUTE=/api`) | `/22` | `/api/animals/22` | `/api/animals` | `/22` |
|
|
789
827
|
|
|
828
|
+
**Superseded 2026-09-01 by [#236](https://github.com/abofs/stonyx-orm/issues/236) /
|
|
829
|
+
[#237](https://github.com/abofs/stonyx-orm/issues/237) — "Variant 3 survives" above,
|
|
830
|
+
and the two paragraphs below, are no longer true.** The access context now carries
|
|
831
|
+
`recordId`, the **decoded** route-parameter id, and the sample compares against it:
|
|
832
|
+
the one string comparison variant 3 lived in is gone, `#228` is **closed**, and no
|
|
833
|
+
read of argument one survives in the sample. The wording is left standing rather
|
|
834
|
+
than deleted because it appears at four sites (this file twice,
|
|
835
|
+
`src/orm-request.ts`, and the test fixture) and retiring one of four leaves the
|
|
836
|
+
shipped copies contradicting each other; retiring all four **with the measurement
|
|
837
|
+
that retires them** is [#238](https://github.com/abofs/stonyx-orm/issues/238).
|
|
838
|
+
|
|
790
839
|
**One read of argument one survives, and it must: `request.path`.** It is
|
|
791
840
|
mount-relative and query-free, and it is for rules that distinguish **sub-paths**
|
|
792
841
|
beneath the mount — as the `/archived` deny in the sample above does. The context
|
|
@@ -806,6 +855,20 @@ comparison as `/%61rchived`, walks past the deny, and is dispatched as the recor
|
|
|
806
855
|
`.toLowerCase()` there as a complete normalisation recipe.** Record ids are
|
|
807
856
|
case-sensitive and must be compared at their real case.
|
|
808
857
|
|
|
858
|
+
**Do not follow the two paragraphs above — superseded 2026-09-01 by
|
|
859
|
+
[#236](https://github.com/abofs/stonyx-orm/issues/236) /
|
|
860
|
+
[#237](https://github.com/abofs/stonyx-orm/issues/237).** They are *instructions*,
|
|
861
|
+
not merely stale observations, which is why this note is louder than a date. The
|
|
862
|
+
sample no longer reads `request.path` and no longer calls `.toLowerCase()` on
|
|
863
|
+
anything it compares: `.toLowerCase()` was measured wrong in **both directions at
|
|
864
|
+
once** — with a distinct owner seeded at `ARCHIVED`, `GET /owners/ARCHIVED` was a
|
|
865
|
+
false **deny** on the wrong record and `GET /owners/%41RCHIVED` a false **allow**
|
|
866
|
+
on that same record. Compare `recordId` **as it arrives**: do not case-fold it, do
|
|
867
|
+
not decode it, do not derive it from `request.path`. The contract is
|
|
868
|
+
`AccessContext.recordId` in `src/types/orm-types.ts`, which ships and says "Do NOT
|
|
869
|
+
case-fold it". Retirement of this wording, with its measurement:
|
|
870
|
+
[#238](https://github.com/abofs/stonyx-orm/issues/238).
|
|
871
|
+
|
|
809
872
|
**Fail closed on anything you cannot identify — on *either* argument.**
|
|
810
873
|
`String(request.originalUrl ?? '')` was once added here to stop a `TypeError`,
|
|
811
874
|
and it traded fail-closed for fail-**open**: an empty string matched no
|
|
@@ -1061,12 +1124,42 @@ Not this:
|
|
|
1061
1124
|
const linkage = (type, r) => Orm.instance.getAccess(type)?.(request)?.(r) ?? true;
|
|
1062
1125
|
```
|
|
1063
1126
|
|
|
1064
|
-
**`
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1127
|
+
**`createLinkageFilter` requires a live request, and there is no safe call
|
|
1128
|
+
without one.** `request` is the only authorization input the filter has — it is
|
|
1129
|
+
handed straight to your `access()` predicates, and a predicate that does not
|
|
1130
|
+
*read* it cannot fail closed when it is missing. Passing `undefined`, `null` or
|
|
1131
|
+
any non-object therefore denies **all** linkage and logs, once, at construction.
|
|
1132
|
+
Measured before that guard existed, `createLinkageFilter(undefined)` granted
|
|
1133
|
+
four of the five models in this repository's own fixture, silently.
|
|
1134
|
+
|
|
1135
|
+
**This is the catch for the request-less contexts named above.** In a queue
|
|
1136
|
+
consumer or a websocket handler there is no live request, so there is nothing to
|
|
1137
|
+
authorize against and nothing this package can resolve for you. Either carry the
|
|
1138
|
+
originating request through to the point of serialization, or publish no linkage
|
|
1139
|
+
at all — `record.toJSON({ linkage: () => false })` emits the document with every
|
|
1140
|
+
relationship empty. A stand-in is **not** a substitute: `{}` is an object
|
|
1141
|
+
and passes the guard, and any predicate that ignores its request will grant.
|
|
1142
|
+
|
|
1143
|
+
**`linkage` itself is validated, and an unusable value DENIES.** `undefined`
|
|
1144
|
+
means "no verdict supplied" and emits today's document. Anything else must be a
|
|
1145
|
+
**synchronous function that answers with a boolean**. Each of the following
|
|
1146
|
+
drops **all** linkage on that document and logs once:
|
|
1147
|
+
|
|
1148
|
+
- **A non-function** — `null`, `0`, `false`, `''`, `true`, a string, an object.
|
|
1149
|
+
`null` is the natural return of a resolver that could not resolve a session:
|
|
1150
|
+
it used to be read as "absent" and emit the full document silently.
|
|
1151
|
+
- **An `async` function, a generator function, or any predicate that returns a
|
|
1152
|
+
promise or thenable.** `toJSON` is the `JSON.stringify` hook and cannot await
|
|
1153
|
+
a verdict, and **an `async` resolver returns a promise, a promise is
|
|
1154
|
+
truthy**, so every related id was published, silently, exactly as if this fix
|
|
1155
|
+
were not here. If your
|
|
1156
|
+
authorization lookup is asynchronous, `await` it *before* you serialize and
|
|
1157
|
+
close over the result.
|
|
1158
|
+
- **Any answer that is not a boolean** — `{}`, `'no'`, `1`, `undefined`. A
|
|
1159
|
+
non-boolean is a resolver that did not answer, and a truthy one granted.
|
|
1160
|
+
- **A predicate that throws**, including a `class` passed by mistake. It is
|
|
1161
|
+
caught and denied; it used to escape the enclosing `JSON.stringify` and take
|
|
1162
|
+
the rest of that serialization down with it.
|
|
1070
1163
|
|
|
1071
1164
|
#### A predicate that ignores `context.model` makes cross-model resolution GRANT
|
|
1072
1165
|
|
|
@@ -1357,6 +1450,16 @@ Each hook receives a context object with comprehensive information:
|
|
|
1357
1450
|
- It contains a deep copy of the record's state **before** the operation executes (captured before the `before` hook fires)
|
|
1358
1451
|
- The deep copy is created via JSON serialization (`JSON.parse(JSON.stringify())`) to ensure complete isolation
|
|
1359
1452
|
- For `delete` operations, `recordId` is provided in after hooks since the record may no longer exist in the store
|
|
1453
|
+
- **`context.recordId` here is NOT `AccessContext.recordId`.** Same name, same-shaped
|
|
1454
|
+
object, different coverage: `_withHooks` sets this key **only** under
|
|
1455
|
+
`operation === 'delete'`, so on `get` / `list` / `create` / `update` the key is
|
|
1456
|
+
**absent** — `beforeHook('update', 'owner', ctx => ctx.recordId === 'archived' ? 403 : undefined)`
|
|
1457
|
+
never fires (measured: `PATCH /owners/{id}` → 200 with `ctx.recordId === undefined`
|
|
1458
|
+
and the id sitting in `ctx.params`). The access context, by contrast, carries
|
|
1459
|
+
`recordId` on every route it classifies and spells absence as `null`, never
|
|
1460
|
+
`undefined`. Tracked as
|
|
1461
|
+
[#242](https://github.com/abofs/stonyx-orm/issues/242); see
|
|
1462
|
+
`AccessContext.recordId` in `src/types/orm-types.ts` for the other side.
|
|
1360
1463
|
- `oldState` is captured as a deep copy of the record's data before the operation, providing access to the previous field values
|
|
1361
1464
|
|
|
1362
1465
|
### Usage Examples
|
package/dist/access-verdict.d.ts
CHANGED
|
@@ -55,5 +55,31 @@ export declare function interpretAccess(access: AccessMethod, operation: AccessO
|
|
|
55
55
|
* SCOPE IS ONE REQUEST. The filter closes over the request and must not outlive
|
|
56
56
|
* it -- a verdict cached across requests would answer a second caller with the
|
|
57
57
|
* first caller's authorization.
|
|
58
|
+
*
|
|
59
|
+
* A REQUEST IS REQUIRED, AND ITS ABSENCE IS CHECKED HERE RATHER THAN DELEGATED.
|
|
60
|
+
* This function is EXPORTED (src/index.ts), and the README's Consumer Contracts
|
|
61
|
+
* section points consumers at exactly the contexts that have no live request --
|
|
62
|
+
* a queue payload, a websocket frame, a custom route. Without one there is no
|
|
63
|
+
* caller to authorise against, and this file's header already says so: the
|
|
64
|
+
* shipped sample reads `request.path` and fail-closes when it is absent, so
|
|
65
|
+
* `getAccess('owner')(undefined, ...)` is `false`, while
|
|
66
|
+
* `getAccess('animal')(undefined, ...)` returns a per-record predicate and
|
|
67
|
+
* GRANTS. Measured on this repo's own fixture before this guard existed:
|
|
68
|
+
*
|
|
69
|
+
* createLinkageFilter(undefined | null | {} | 'x' | 0)
|
|
70
|
+
* -> owner=false animal=TRUE trait=TRUE category=TRUE phone-number=TRUE
|
|
71
|
+
*
|
|
72
|
+
* Four of five claimed models granted, with no log, because whether an absent
|
|
73
|
+
* request fails closed was left ENTIRELY to consumer predicates -- and a
|
|
74
|
+
* predicate that ignores its request cannot fail closed on one that is missing.
|
|
75
|
+
* A nullish or primitive `request` therefore denies every model outright and
|
|
76
|
+
* says so once, at construction, so the signal exists even for a caller that
|
|
77
|
+
* goes on to serialize nothing.
|
|
78
|
+
*
|
|
79
|
+
* WHAT THIS CANNOT CHECK: `{}` is an object and passes. There is no request
|
|
80
|
+
* contract this module owns -- `auth()` reads `.method`, the shipped sample
|
|
81
|
+
* reads `.path`, a consumer's reads whatever it likes -- so anything past
|
|
82
|
+
* "is it an object" would be this module inventing a shape for someone else's
|
|
83
|
+
* framework. The residual is documented in the README under Consumer Contracts.
|
|
58
84
|
*/
|
|
59
85
|
export declare function createLinkageFilter(request: unknown): LinkageFilter;
|
package/dist/access-verdict.js
CHANGED
|
@@ -144,7 +144,37 @@ function resolveVerdict(request, type) {
|
|
|
144
144
|
return DENIED;
|
|
145
145
|
let access;
|
|
146
146
|
try {
|
|
147
|
-
|
|
147
|
+
// `recordId: null`, AND NOT `request.params.id`. THE TEMPTING WRONG ANSWER
|
|
148
|
+
// IS RIGHT THERE, so this is pinned by assertion as well as by comment --
|
|
149
|
+
// test/unit/linkage-verdict-test.ts, `#234 + #241 -- recordId is null`.
|
|
150
|
+
//
|
|
151
|
+
// `AccessContext.recordId` (src/types/orm-types.ts, abofs/stonyx-orm#236 /
|
|
152
|
+
// #241) means "the record THIS ROUTE WAS ADDRESSED TO, as the store key of
|
|
153
|
+
// the model being authorised", and `null` means "addressed to no record".
|
|
154
|
+
// The id sitting on the request in hand names the PRIMARY record, which
|
|
155
|
+
// belongs to a DIFFERENT model -- `GET /owners/gina` carries
|
|
156
|
+
// `params.id === 'gina'`, and the ask being made HERE is about `animal` or
|
|
157
|
+
// `trait`. Filling this in from the request would hand the related model's
|
|
158
|
+
// predicate an id belonging to another model, which is byte-for-byte the
|
|
159
|
+
// cross-model confusion abofs/stonyx-orm#202 introduced this context to
|
|
160
|
+
// eliminate: the predicate would compare an owner's id against its own
|
|
161
|
+
// records and answer a question nobody asked. There is no record of THIS
|
|
162
|
+
// model addressed by this request, so `null` is the honest value -- the
|
|
163
|
+
// same spelling `auth()` uses for a collection route.
|
|
164
|
+
//
|
|
165
|
+
// NOR ANY RECORD'S OWN ID, WHICH IS THE SECOND-MOST TEMPTING ANSWER. This
|
|
166
|
+
// verdict is resolved ONCE PER TYPE and cached in `byType` below, before
|
|
167
|
+
// any record has been looked at; there is no per-record `AccessContext`
|
|
168
|
+
// built anywhere on this path. Seeding it from the first record of a type
|
|
169
|
+
// would let that record's identity answer for every later record of the
|
|
170
|
+
// same type -- the same "one record's verdict answers for another" defect
|
|
171
|
+
// the `decisions` raw-key argument below exists to prevent, just one level
|
|
172
|
+
// coarser. And it is unnecessary: `AccessContext` deliberately carries no
|
|
173
|
+
// `record` because auth-time and record-time are separate decision points,
|
|
174
|
+
// and the per-record point already receives the WHOLE record, id included,
|
|
175
|
+
// through `verdict.filter(record)`. A predicate that wants a record's id
|
|
176
|
+
// has the contract's own channel for it.
|
|
177
|
+
access = predicate(request, { model: type, operation: 'read', recordId: null });
|
|
148
178
|
}
|
|
149
179
|
catch (error) {
|
|
150
180
|
log.error?.(`[@stonyx/orm] access() threw while resolving linkage for model "${type}" -- denying. ${error instanceof Error ? error.message : String(error)}`);
|
|
@@ -186,8 +216,40 @@ function resolveVerdict(request, type) {
|
|
|
186
216
|
* SCOPE IS ONE REQUEST. The filter closes over the request and must not outlive
|
|
187
217
|
* it -- a verdict cached across requests would answer a second caller with the
|
|
188
218
|
* first caller's authorization.
|
|
219
|
+
*
|
|
220
|
+
* A REQUEST IS REQUIRED, AND ITS ABSENCE IS CHECKED HERE RATHER THAN DELEGATED.
|
|
221
|
+
* This function is EXPORTED (src/index.ts), and the README's Consumer Contracts
|
|
222
|
+
* section points consumers at exactly the contexts that have no live request --
|
|
223
|
+
* a queue payload, a websocket frame, a custom route. Without one there is no
|
|
224
|
+
* caller to authorise against, and this file's header already says so: the
|
|
225
|
+
* shipped sample reads `request.path` and fail-closes when it is absent, so
|
|
226
|
+
* `getAccess('owner')(undefined, ...)` is `false`, while
|
|
227
|
+
* `getAccess('animal')(undefined, ...)` returns a per-record predicate and
|
|
228
|
+
* GRANTS. Measured on this repo's own fixture before this guard existed:
|
|
229
|
+
*
|
|
230
|
+
* createLinkageFilter(undefined | null | {} | 'x' | 0)
|
|
231
|
+
* -> owner=false animal=TRUE trait=TRUE category=TRUE phone-number=TRUE
|
|
232
|
+
*
|
|
233
|
+
* Four of five claimed models granted, with no log, because whether an absent
|
|
234
|
+
* request fails closed was left ENTIRELY to consumer predicates -- and a
|
|
235
|
+
* predicate that ignores its request cannot fail closed on one that is missing.
|
|
236
|
+
* A nullish or primitive `request` therefore denies every model outright and
|
|
237
|
+
* says so once, at construction, so the signal exists even for a caller that
|
|
238
|
+
* goes on to serialize nothing.
|
|
239
|
+
*
|
|
240
|
+
* WHAT THIS CANNOT CHECK: `{}` is an object and passes. There is no request
|
|
241
|
+
* contract this module owns -- `auth()` reads `.method`, the shipped sample
|
|
242
|
+
* reads `.path`, a consumer's reads whatever it likes -- so anything past
|
|
243
|
+
* "is it an object" would be this module inventing a shape for someone else's
|
|
244
|
+
* framework. The residual is documented in the README under Consumer Contracts.
|
|
189
245
|
*/
|
|
190
246
|
export function createLinkageFilter(request) {
|
|
247
|
+
if (typeof request !== 'object' || request === null) {
|
|
248
|
+
log.error?.(`[@stonyx/orm] createLinkageFilter() was called with no request (received ${request === null ? 'null' : typeof request}) -- there is no caller to authorise against, so ALL relationship linkage it is asked about is denied.`);
|
|
249
|
+
return function isLinkable(_type, _record) {
|
|
250
|
+
return false;
|
|
251
|
+
};
|
|
252
|
+
}
|
|
191
253
|
const byType = new Map();
|
|
192
254
|
return function isLinkable(type, record) {
|
|
193
255
|
let entry = byType.get(type);
|
package/dist/hooks.d.ts
CHANGED
|
@@ -20,7 +20,21 @@ export interface HookContext {
|
|
|
20
20
|
state?: Record<string, unknown>;
|
|
21
21
|
/** Previous record state (available in update hooks). */
|
|
22
22
|
oldState?: unknown;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Target record ID for single-record operations.
|
|
25
|
+
*
|
|
26
|
+
* SET ONLY UNDER `delete`. `_withHooks` assigns this key in the two
|
|
27
|
+
* `operation === 'delete'` branches and nowhere else, so on `get`, `list`,
|
|
28
|
+
* `create` and `update` the key is ABSENT -- not `undefined`-valued, absent.
|
|
29
|
+
* A hook rule written as `ctx.recordId === '<id>'` never fires on an update;
|
|
30
|
+
* the addressed id is in `ctx.params`. Tracked as abofs/stonyx-orm#242.
|
|
31
|
+
*
|
|
32
|
+
* @see AccessContext.recordId in ./types/orm-types.ts -- an identically-named
|
|
33
|
+
* key on an identically-shaped context object, and NOT interchangeable with
|
|
34
|
+
* this one: it is present on every route `auth()` classifies, and spells
|
|
35
|
+
* absence as `null` rather than `undefined`. They differ in coverage on four
|
|
36
|
+
* of five operations, not only in the absence spelling.
|
|
37
|
+
*/
|
|
24
38
|
recordId?: string | number;
|
|
25
39
|
/** Response data (available in after hooks). */
|
|
26
40
|
response?: unknown;
|
package/dist/orm-request.d.ts
CHANGED
|
@@ -69,6 +69,15 @@
|
|
|
69
69
|
* records under `model: 'owner'`, and the context gives no signal of that
|
|
70
70
|
* (abofs/stonyx-orm#196).
|
|
71
71
|
*
|
|
72
|
+
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237, AND KEPT FOR THE
|
|
73
|
+
* CONSTRAINT IT STATES RATHER THAN AS A DESCRIPTION OF THE CODE. The context
|
|
74
|
+
* now also carries `recordId` -- the DECODED route-parameter id, see
|
|
75
|
+
* `AccessContext.recordId` in ./types/orm-types.ts -- so the fixture's
|
|
76
|
+
* `/archived` deny IS expressible from the context alone, and the shipped
|
|
77
|
+
* sample no longer reads `request.path` at all. Retiring this wording WITH the
|
|
78
|
+
* measurement that retires it, rather than by deletion, is
|
|
79
|
+
* abofs/stonyx-orm#238.
|
|
80
|
+
*
|
|
72
81
|
* `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
|
|
73
82
|
* matching but BEFORE any handler executes (`@stonyx/rest-server`
|
|
74
83
|
* `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
|
|
@@ -158,6 +167,11 @@
|
|
|
158
167
|
* `/archived` SUB-PATH rule is still a string match, and abofs/stonyx-orm#228 is
|
|
159
168
|
* a sixth spelling that gets past it.
|
|
160
169
|
*
|
|
170
|
+
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237: the `/archived` rule is
|
|
171
|
+
* no longer a string match against the request target -- it compares the
|
|
172
|
+
* decoded `recordId` the framework supplies -- and abofs/stonyx-orm#228 is
|
|
173
|
+
* CLOSED. Retirement of this wording: abofs/stonyx-orm#238.
|
|
174
|
+
*
|
|
161
175
|
* An intermediate revision of the sample read `request.baseUrl` -- the mount
|
|
162
176
|
* Express ACTUALLY MATCHED. That closed all five variants (no query string,
|
|
163
177
|
* not mount-relative, unaffected by absolute-form, already carrying the
|
|
@@ -173,12 +187,26 @@
|
|
|
173
187
|
* does not decode, so `GET /owners/%61rchived` steps past it. See the
|
|
174
188
|
* normalisation paragraph below and abofs/stonyx-orm#228.
|
|
175
189
|
*
|
|
190
|
+
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237. Variant 3 lived in that
|
|
191
|
+
* one string comparison, and the comparison is gone: the sample compares the
|
|
192
|
+
* decoded `recordId`. Left standing rather than edited because the same
|
|
193
|
+
* "variant 3 survives" wording sits at four sites -- this header, README.md
|
|
194
|
+
* twice, and test/sample/access/global-access.ts -- three of which SHIP, so
|
|
195
|
+
* retiring one of four leaves the shipped copies contradicting each other.
|
|
196
|
+
* Retiring all four WITH their measurement is abofs/stonyx-orm#238.
|
|
197
|
+
*
|
|
176
198
|
* ONE READ OF ARGUMENT ONE SURVIVES, AND IT MUST: `request.path`. It is
|
|
177
199
|
* mount-relative and query-free, and it is for rules that distinguish SUB-PATHS
|
|
178
200
|
* beneath the mount. The context names which model and which verb, NOT which
|
|
179
201
|
* route, so the sample's `/archived` deny cannot be expressed from the context
|
|
180
202
|
* alone and a context-ONLY rewrite would silently turn that deny into an allow.
|
|
181
203
|
*
|
|
204
|
+
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237: NO read of argument one
|
|
205
|
+
* survives in the shipped sample. `recordId` names WHICH RECORD the route was
|
|
206
|
+
* addressed to, so the `/archived` deny is expressible from the context alone
|
|
207
|
+
* -- and it still must not be dropped; expressible is not optional. Retirement
|
|
208
|
+
* of this wording: abofs/stonyx-orm#238.
|
|
209
|
+
*
|
|
182
210
|
* NORMALISE THE WAY THE ROUTER DOES, AND CASE-FOLDING ALONE IS NOT THAT. The
|
|
183
211
|
* sample lower-cases before comparing, because a matcher stricter than the
|
|
184
212
|
* case-insensitive router can be stepped around. That closes the case gap only.
|
|
@@ -188,6 +216,20 @@
|
|
|
188
216
|
* sample and is tracked as abofs/stonyx-orm#228; the `.toLowerCase()` is not a
|
|
189
217
|
* complete normalisation recipe. Compare record ids at their real case.
|
|
190
218
|
*
|
|
219
|
+
* DO NOT FOLLOW THE PARAGRAPH ABOVE. SUPERSEDED 2026-09-01 BY
|
|
220
|
+
* abofs/stonyx-orm#236/#237, and flagged here rather than merely dated because
|
|
221
|
+
* it is an INSTRUCTION, not a stale observation. `.toLowerCase()` on the access
|
|
222
|
+
* path was measured WRONG IN BOTH DIRECTIONS AT ONCE: with a distinct owner
|
|
223
|
+
* seeded at `ARCHIVED`, `GET /owners/ARCHIVED` was a false DENY on the wrong
|
|
224
|
+
* record and `GET /owners/%41RCHIVED` a false ALLOW on that same record. A
|
|
225
|
+
* record id is a VALUE, not a literal route segment, and express's
|
|
226
|
+
* `case sensitive routing` governs literal segments only. Compare
|
|
227
|
+
* `context.recordId` AS IT ARRIVES: do not case-fold it, do not decode it, do
|
|
228
|
+
* not derive it from `request.path`. `AccessContext.recordId` in
|
|
229
|
+
* ./types/orm-types.ts is the contract and says "Do NOT case-fold it"; the same
|
|
230
|
+
* published tarball ships both files, and THIS paragraph is the one that is
|
|
231
|
+
* wrong. Retiring it WITH its measurement is abofs/stonyx-orm#238.
|
|
232
|
+
*
|
|
191
233
|
* `?? ''` is not a defence. It converts an absent request target into an empty
|
|
192
234
|
* string, which matches no collection, which falls through to the permission
|
|
193
235
|
* array -- a total grant. An input you cannot identify must DENY, and that
|
|
@@ -196,6 +238,12 @@
|
|
|
196
238
|
* argument one. The sample returns `false` for an absent `model` AND for an
|
|
197
239
|
* absent or non-string `request.path`, rather than falling through either way.
|
|
198
240
|
*
|
|
241
|
+
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237 as to WHAT is guarded --
|
|
242
|
+
* the principle is unchanged. The sample no longer reads `request.path`, so it
|
|
243
|
+
* returns `false` for an absent `model` AND for an absent `recordId`
|
|
244
|
+
* (`undefined`, the one spelling `auth()` never produces). Retirement of this
|
|
245
|
+
* wording: abofs/stonyx-orm#238.
|
|
246
|
+
*
|
|
199
247
|
* THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model,
|
|
200
248
|
* the operation and the record. Prefer the array shape (`['read']`) or `false`
|
|
201
249
|
* until #202 lands; the function shape is what requires any matching at all.
|