@stonyx/orm 0.3.2-alpha.72 → 0.3.2-alpha.74
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 +176 -81
- package/dist/access-verdict.js +31 -1
- package/dist/hooks.d.ts +15 -1
- package/dist/orm-request.d.ts +48 -0
- package/dist/orm-request.js +208 -30
- package/dist/types/orm-types.d.ts +95 -0
- package/package.json +1 -1
- package/src/access-verdict.ts +31 -1
- package/src/hooks.ts +15 -1
- package/src/orm-request.ts +210 -32
- 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
|
|
@@ -867,20 +930,36 @@ per-record filter. An input you cannot identify must **deny**.
|
|
|
867
930
|
`include=`, related-resource routes and relationship-linkage routes. This is
|
|
868
931
|
**membership** — whether the related resource is served at all — and it is a
|
|
869
932
|
different question from which ids a document may *name*, immediately below.
|
|
870
|
-
- **Relationship linkage is filtered on
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
933
|
+
- **Relationship linkage is filtered on every request-bound surface that
|
|
934
|
+
serializes a record — the reads, the two writes, and `included`.** A
|
|
935
|
+
document's `relationships.*.data` used to publish the id of every related
|
|
936
|
+
record unconditionally, so a record hidden on every one of its own surfaces
|
|
937
|
+
was still named inside another model's document — with no `include=`, no
|
|
938
|
+
relationship route and no query string
|
|
875
939
|
([#234](https://github.com/abofs/stonyx-orm/issues/234)). The ORM now resolves
|
|
876
|
-
the **related** model's own access class on `GET /:models`, `GET /:models/:id
|
|
877
|
-
|
|
878
|
-
`
|
|
940
|
+
the **related** model's own access class on `GET /:models`, `GET /:models/:id`,
|
|
941
|
+
both `GET /:models/:id/{relationship}` shapes, the `POST /:models` and
|
|
942
|
+
`PATCH /:models/:id` **response documents**, and every record inside an
|
|
943
|
+
`?include=` **`included`** array
|
|
944
|
+
([#235](https://github.com/abofs/stonyx-orm/issues/235)), and asks it
|
|
945
|
+
`{ model: <related>, operation: 'read' }`. **`operation` is `'read'` even on a
|
|
946
|
+
write route, and that is correct rather than an oversight** — the question
|
|
947
|
+
asked of the *related* model is "may this caller **read** this id", not "may
|
|
948
|
+
they update it". An access class that grants `['create']` but not `['read']`
|
|
949
|
+
on the related model therefore denies that linkage on its own `POST`
|
|
950
|
+
response; that is the fail-closed direction. Do **not** wire these handlers to
|
|
951
|
+
`methodAccessMap[request.method]`: it would ask a different question on a
|
|
952
|
+
write route than on a read route, which is the two-vocabularies failure
|
|
953
|
+
`createLinkageFilter` exists to prevent. An unresolvable class
|
|
879
954
|
(`getAccess()` → `undefined`) and a predicate that throws both **deny**. A
|
|
880
955
|
filtered-out relationship is **indistinguishable from a genuinely empty one** —
|
|
881
956
|
an emptied `hasMany` is `data: []` and an emptied `belongsTo` is `data: null`,
|
|
882
957
|
both **keeping their `links`**, which are built from the serialized record's
|
|
883
|
-
own id and never from the related one.
|
|
958
|
+
own id and never from the related one. On the two **write** surfaces there are
|
|
959
|
+
no `links` to keep: neither handler passes a `baseUrl`, so a filtered and a
|
|
960
|
+
genuinely-empty relationship are both a bare `{ "data": … }` there. That is
|
|
961
|
+
pre-existing and deliberate — adding `baseUrl` to the write handlers would be
|
|
962
|
+
an unrelated change to their response shape. Nothing errors and no status changes,
|
|
884
963
|
because throwing here would be an existence oracle *and* would throw out of
|
|
885
964
|
the enclosing `JSON.stringify`.
|
|
886
965
|
|
|
@@ -907,28 +986,32 @@ per-record filter. An input you cannot identify must **deny**.
|
|
|
907
986
|
**Not yet covered, and each one still publishes ids the surfaces above
|
|
908
987
|
withhold:**
|
|
909
988
|
|
|
910
|
-
- **`included`** — [#235](https://github.com/abofs/stonyx-orm/issues/235). A
|
|
911
|
-
permitted record sideloaded by `?include=` emits its **own**
|
|
912
|
-
`relationships.*.data` unfiltered, so `GET /animals/1?include=owner,owner.pets`
|
|
913
|
-
returns `owner.data: null` on the primary document and then names angela in
|
|
914
|
-
`included`, along with eight permitted animals that each name
|
|
915
|
-
`{"type":"owner","id":"angela"}`. Separately,
|
|
916
|
-
[#233](https://github.com/abofs/stonyx-orm/issues/233) owns whether a
|
|
917
|
-
resource appears in `included` **at all** — that is membership, a different
|
|
918
|
-
question, and following it will not lead you to this residual.
|
|
919
|
-
- **The `POST`/`PATCH` response documents** —
|
|
920
|
-
[#235](https://github.com/abofs/stonyx-orm/issues/235). `createHandler` and
|
|
921
|
-
`updateHandler` destructure the request rather than binding it, so wiring
|
|
922
|
-
them needs a signature change rather than an argument. Until then **one HTTP
|
|
923
|
-
verb defeats the filter on the same record**: measured, `GET /animals/1`
|
|
924
|
-
returns `owner.data: null` and `PATCH /animals/1` returns **200 naming
|
|
925
|
-
angela**, seconds apart, with no query string and no relationship route. Any
|
|
926
|
-
caller who can read a record can also write it and be handed the id the read
|
|
927
|
-
withheld.
|
|
928
989
|
- **`GET /:models/:id/relationships/{relationship}`**, whose *primary data* is
|
|
929
990
|
linkage, so filtering it is a **membership** decision —
|
|
930
991
|
[#232](https://github.com/abofs/stonyx-orm/issues/232), the filed child of
|
|
931
|
-
[#196](https://github.com/abofs/stonyx-orm/issues/196).
|
|
992
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196). This route builds
|
|
993
|
+
its `{type, id}` objects by hand and never calls `toJSON`, so it does not
|
|
994
|
+
see a `linkage` option no matter who supplies one. Measured:
|
|
995
|
+
`GET /animals/1/relationships/owner` answers
|
|
996
|
+
`{"type":"owner","id":"angela"}` while `GET /owners/angela` is `404`.
|
|
997
|
+
- **Whether a related resource appears in `included` at all** —
|
|
998
|
+
[#233](https://github.com/abofs/stonyx-orm/issues/233). #235 filters what a
|
|
999
|
+
record *already in* `included` may **name**; a hidden record is still a
|
|
1000
|
+
**member** of that array. The two are different questions and neither closes
|
|
1001
|
+
the other: after #235, `GET /animals/1?include=owner,owner.pets` returns
|
|
1002
|
+
`owner.data: null` on every permitted animal it sideloads **and still
|
|
1003
|
+
includes the hidden owner as a resource**.
|
|
1004
|
+
- **A computed attribute that interpolates a related record's id.** This is a
|
|
1005
|
+
**consumer-side** residual and the ORM cannot close it. `relationships.*.data`
|
|
1006
|
+
is a structure this module builds, so it can be filtered; a computed
|
|
1007
|
+
property is arbitrary consumer code returning an arbitrary value, and
|
|
1008
|
+
deciding which substrings of it are identifiers is not something the
|
|
1009
|
+
framework can do. Measured on this repo's own fixture, where the `animal`
|
|
1010
|
+
model has a `get tag()` that interpolates `owner.id`: **every** animal
|
|
1011
|
+
document on **every** surface — including the ones above — carries
|
|
1012
|
+
`attributes.tag: "angela's small dog"` for an owner that answers `404`. If
|
|
1013
|
+
your access rules hide a record, audit your computed properties for its
|
|
1014
|
+
identifiers.
|
|
932
1015
|
- **A bare `toJSON()` still emits unfiltered linkage, and that is deliberate.**
|
|
933
1016
|
`Record.toJSON()` **applies** a verdict; it never **resolves** one. It has no
|
|
934
1017
|
request, and the documented `access()` contract permits a predicate to read
|
|
@@ -1326,13 +1409,15 @@ GET /animals/1
|
|
|
1326
1409
|
#### Limitations
|
|
1327
1410
|
|
|
1328
1411
|
- Only available on GET endpoints (not POST/PATCH)
|
|
1329
|
-
- **`included`
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
[
|
|
1412
|
+
- **`included` is access-filtered on one of the two questions, not both.** What
|
|
1413
|
+
a record already in `included` may **name** in its own
|
|
1414
|
+
`relationships.*.data` is filtered
|
|
1415
|
+
([#235](https://github.com/abofs/stonyx-orm/issues/235)) — `?include=` no
|
|
1416
|
+
longer republishes ids the primary document withholds. Whether a resource
|
|
1417
|
+
appears in `included` **at all** is *membership* and is still unfiltered
|
|
1418
|
+
([#233](https://github.com/abofs/stonyx-orm/issues/233)): a record that is
|
|
1419
|
+
404 on its own routes is still served as an `included` resource, attributes
|
|
1420
|
+
and all. See [Consumer Contracts](#consumer-contracts).
|
|
1336
1421
|
|
|
1337
1422
|
## Lifecycle Hooks
|
|
1338
1423
|
|
|
@@ -1387,6 +1472,16 @@ Each hook receives a context object with comprehensive information:
|
|
|
1387
1472
|
- It contains a deep copy of the record's state **before** the operation executes (captured before the `before` hook fires)
|
|
1388
1473
|
- The deep copy is created via JSON serialization (`JSON.parse(JSON.stringify())`) to ensure complete isolation
|
|
1389
1474
|
- For `delete` operations, `recordId` is provided in after hooks since the record may no longer exist in the store
|
|
1475
|
+
- **`context.recordId` here is NOT `AccessContext.recordId`.** Same name, same-shaped
|
|
1476
|
+
object, different coverage: `_withHooks` sets this key **only** under
|
|
1477
|
+
`operation === 'delete'`, so on `get` / `list` / `create` / `update` the key is
|
|
1478
|
+
**absent** — `beforeHook('update', 'owner', ctx => ctx.recordId === 'archived' ? 403 : undefined)`
|
|
1479
|
+
never fires (measured: `PATCH /owners/{id}` → 200 with `ctx.recordId === undefined`
|
|
1480
|
+
and the id sitting in `ctx.params`). The access context, by contrast, carries
|
|
1481
|
+
`recordId` on every route it classifies and spells absence as `null`, never
|
|
1482
|
+
`undefined`. Tracked as
|
|
1483
|
+
[#242](https://github.com/abofs/stonyx-orm/issues/242); see
|
|
1484
|
+
`AccessContext.recordId` in `src/types/orm-types.ts` for the other side.
|
|
1390
1485
|
- `oldState` is captured as a deep copy of the record's data before the operation, providing access to the previous field values
|
|
1391
1486
|
|
|
1392
1487
|
### Usage Examples
|
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)}`);
|
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.
|