@stonyx/orm 0.3.2-beta.145 → 0.3.2-beta.147

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.
@@ -1,3 +1,63 @@
1
+ /**
2
+ * REST request handling and access enforcement for @stonyx/orm.
3
+ *
4
+ * ---------------------------------------------------------------------------
5
+ * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
6
+ * ---------------------------------------------------------------------------
7
+ * `auth()` below hands your `access(request)` a raw transport artifact and asks
8
+ * you to work out which collection it addresses. Every attempt to do that by
9
+ * parsing the request target has failed OPEN. Five distinct variants of the
10
+ * same three-line example have now been found, each after the previous was
11
+ * fixed, by five different people:
12
+ *
13
+ * 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
14
+ * prefix match against it is ALWAYS false.
15
+ * 2. `request.originalUrl` carries the query string, so an anchored equality
16
+ * check misses `/owners?filter[age]=30`.
17
+ * 3. The router is a bare `express()` (`caseSensitive: false`) while a
18
+ * hand-written matcher is case-SENSITIVE, so `GET /OwNeRs/angela` walks
19
+ * past it. Router-side: abofs/stonyx-rest-server#47.
20
+ * 4. Under a configured `ORM_REST_ROUTE` a hard-coded `/owners` matches
21
+ * nothing -- environment-specifically, which is worse.
22
+ * 5. HTTP/1.1 permits an ABSOLUTE-FORM request-target. Express routes on
23
+ * `parseurl(req).pathname`, but `originalUrl` is the raw target, so
24
+ * `GET http://anything.example/owners/angela` reaches the handler with
25
+ * `originalUrl === 'http://anything.example/owners/angela'`. A `/owners`
26
+ * prefix match is false, `access()` falls through to whatever it returns
27
+ * last, and the record comes back in full. It walks past a hard
28
+ * `return false` deny the same way.
29
+ *
30
+ * The fix is not a sixth rule. It is to stop parsing:
31
+ *
32
+ * `request.baseUrl` is the mount Express ACTUALLY MATCHED when it dispatched
33
+ * the request. It carries no query string, it is not mount-relative, it is
34
+ * unaffected by absolute-form, and it already includes the configured
35
+ * `ORM_REST_ROUTE` prefix -- so there is nothing to derive and nothing to
36
+ * join. Compare it lower-cased (the router matched case-insensitively) and
37
+ * fail CLOSED when it is absent. Use `request.path` -- mount-relative and
38
+ * query-free -- if you need to distinguish sub-paths.
39
+ *
40
+ * `?? ''` is not a defence. It converts an absent request target into an empty
41
+ * string, which matches no collection, which falls through to the permission
42
+ * array -- a total grant. An input you cannot identify must DENY.
43
+ *
44
+ * THAT IS STILL A STOPGAP. `baseUrl` closes all five variants, but it is a
45
+ * transport artifact being asked to stand in for a structural fact.
46
+ * THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model,
47
+ * the operation and the record. Prefer the array shape (`['read']`) or `false`
48
+ * until #202 lands; the function shape is what requires any matching at all.
49
+ *
50
+ * The enforcement gates in this file (GATE 0/1/2, the create rollback, the
51
+ * per-handler `isDenied` re-checks) are correct independently of that -- they
52
+ * enforce whatever predicate you return. The stopgap is the part where YOU have
53
+ * to work out which predicate to return.
54
+ *
55
+ * AND A PREDICATE IS NOT A GUARANTEE THAT A HIDDEN RECORD CANNOT BE MODIFIED.
56
+ * It is evaluated against the record the route is ADDRESSED TO, on that model
57
+ * only. A write to a DIFFERENT collection can still re-parent a hidden record
58
+ * and de-hide it -- abofs/stonyx-orm#207, which is blocked on #202 and #196.
59
+ * See `### Known limitations` in README.
60
+ */
1
61
  import { Request } from '@stonyx/rest-server';
2
62
  import Orm, { store, createRecord, updateRecord } from '@stonyx/orm';
3
63
  import { camelCaseToKebabCase } from '@stonyx/utils/string';
@@ -5,6 +65,7 @@ import { getPluralName } from './plural-registry.js';
5
65
  import { getBeforeHooks, getAfterHooks } from './hooks.js';
6
66
  import type { HookContext } from './hooks.js';
7
67
  import config from 'stonyx/config';
68
+ import log from 'stonyx/log';
8
69
  import type { OrmRecord } from './types/orm-types.js';
9
70
  import { isOrmRecord } from './utils.js';
10
71
 
@@ -84,12 +145,119 @@ function getBaseUrl(request: OrmRequest$): string {
84
145
  return `${protocol}://${host}`;
85
146
  }
86
147
 
148
+ /**
149
+ * The ONE coercion from a caller-supplied id to the key the store holds it
150
+ * under. Every id-bearing surface in this file goes through it, and none has a
151
+ * copy: `getId()` (URL params), `normalizeBodyId()` (JSON body), and the
152
+ * post-create `context.record` lookup in `_withHooks`.
153
+ *
154
+ * WHY IT IS SHARED RATHER THAN DUPLICATED. `getId()` and `normalizeBodyId()`
155
+ * each had their own arithmetic, and they disagreed: `parseInt(id)` versus
156
+ * `parseInt(id, 10)`. On a hex-shaped id that is a two-record difference --
157
+ *
158
+ * GET /animals/0x2391 -> record 9105 (getId -> parseInt('0x2391') = 9105)
159
+ * POST /animals {"id":"0x2391"} -> lookup under 0 (normalize -> parseInt('0x2391',10) = 0)
160
+ * -> a MISS, so the duplicate check was skipped and
161
+ * createRecord OVERWROTE 9105 in place, answering 200
162
+ *
163
+ * -- a narrower form of the raw-versus-normalised divergence that the body-id
164
+ * normalisation was added to close, reintroduced by the fix for it. Two
165
+ * coercions that must agree cannot be kept in agreement by review; they have to
166
+ * be one function. Pinned by assertion 43.
167
+ *
168
+ * The third copy was found later and in a quieter place: `_withHooks` populated
169
+ * `context.record` for `create` with `isNaN(id) ? id : parseInt(id)` -- this
170
+ * function's body, inlined verbatim, feeding `store.get`. It was equivalent on
171
+ * every input reachable there, which is exactly what the two that DID diverge
172
+ * looked like until someone tried a hex id.
173
+ *
174
+ * SHARING IT IS NOT THE SAME AS IT BEING RIGHT EVERYWHERE. On a model declaring
175
+ * `id = attr('string')` a numeric-looking id is filed under the STRING key, so
176
+ * this coercion resolves `'9107'` to `9107` and the post-create lookup misses:
177
+ * `context.record` is `undefined` for an after-`create` hook. Inherited -- the
178
+ * inlined copy computed the same thing -- and NOT fixed here, because picking
179
+ * the right coercion needs the model's declared id type, which is the same
180
+ * structural information abofs/stonyx-orm#202 is about. Filed as
181
+ * abofs/stonyx-orm#209 and pinned by assertion 50, so closing it turns a test
182
+ * red rather than passing silently.
183
+ *
184
+ * `parseInt` and not `Number`, deliberately, and the anchor is NOT `getId`.
185
+ * It is `src/transforms.ts:7` -- `number: (value) => parseInt(value as string)`,
186
+ * also radix-less -- because that transform is what actually produces the store
187
+ * KEY a record is filed under. `getId` merely agrees with it. They differ from
188
+ * `Number` on `'1e3'` (1 vs 1000) and `'9105.5'` (9105 vs 9105.5), so switching
189
+ * this function to `Number` would make the lookup key disagree with the landing
190
+ * key on those shapes.
191
+ *
192
+ * THE CHANGE THAT WOULD BREAK THIS: adding a radix to `transforms.number`
193
+ * (`parseInt(value, 10)`). That is a one-word edit in a file with no connection
194
+ * to authorization, it would silently reopen the hex divergence in the other
195
+ * direction, and this comment would still read as correct. Assertion 45 pins
196
+ * the transform's radix-less shape directly, so that edit turns a test red
197
+ * rather than shipping.
198
+ *
199
+ * The reason `parseInt` is safe here is the `isNaN` gate in front of it:
200
+ * `parseInt('9105h')` is `9105`, and truncating a partially-valid id into a
201
+ * DIFFERENT VALID key is exactly how a collision lookup gets skipped. The gate
202
+ * rejects it as a string instead, so nothing is ever truncated. That gate, not
203
+ * the parser, is the load-bearing half -- assertion 43 pins it.
204
+ */
205
+ function coerceId(id: string): string | number {
206
+ if (isNaN(id as unknown as number)) return id;
207
+
208
+ return parseInt(id);
209
+ }
210
+
87
211
  function getId(params: { id?: string; [key: string]: unknown }): string | number {
88
212
  const id = params.id;
89
213
  if (!id) return '';
90
- if (isNaN(id as unknown as number)) return id;
91
214
 
92
- return parseInt(id);
215
+ return coerceId(id);
216
+ }
217
+
218
+ /**
219
+ * Normalise a caller-supplied BODY id to the key the store will hold it under.
220
+ *
221
+ * `getId()` above is params-shaped: it takes `{ id?: string }` off the URL,
222
+ * where the value is always a string and a falsy one means "no id". A JSON body
223
+ * id is neither -- it can arrive as a number, and `0` is a legitimate id that
224
+ * `getId()` would flatten to `''`.
225
+ *
226
+ * WHY THIS EXISTS AT ALL. createHandler used to look the collision up with the
227
+ * RAW body value while every other surface normalised through `getId()`. The
228
+ * store is a Map keyed by the coerced value, so `store.find(model, '21')` misses
229
+ * the entry held under `21` and the duplicate check is skipped by typing the id
230
+ * as a string. On `dev` that silently overwrote the colliding record and
231
+ * answered 200; combined with the denied-create rollback added for #190 it
232
+ * became an unauthenticated DELETE of any id. Normalising here is half of that
233
+ * fix -- see the rollback in createHandler for the other half.
234
+ *
235
+ * It shares `coerceId` with `getId` so the two surfaces cannot drift apart
236
+ * again, and differs from `getId` in exactly ONE place, below.
237
+ */
238
+ function normalizeBodyId(id: string | number): string | number {
239
+ // Non-strings pass through untouched: a JSON body id can arrive as a number,
240
+ // and `getId`'s falsy-flatten must NOT apply to it -- `0` is a legitimate id
241
+ // and `getId` would turn it into `''`. (assertion 30 sweeps id `0`.)
242
+ if (typeof id !== 'string') return id;
243
+
244
+ // THE ONE DIVERGENCE FROM `getId`, and it mirrors rather than contradicts it:
245
+ // `getId` maps a falsy param to `''`, and `''` is the only string a body can
246
+ // carry that means "no id" -- `createRecord` treats it as absent and assigns a
247
+ // server id. Coercing it instead would make it address a real slot, because
248
+ // `parseInt('')` is `NaN` and a record CAN be held under `NaN` (a truthy but
249
+ // non-numeric id such as `' '` survives `assignRecordId`'s falsy guard and
250
+ // then NaNs in the id transform). So `POST {"id":""}` would answer 409 against
251
+ // an unrelated record it never named. Pinned by assertion 44.
252
+ //
253
+ // Note what is deliberately NOT special-cased here any more: whitespace.
254
+ // `id.trim() === ''` used to short-circuit `' '` as well, which made the
255
+ // body surface DISAGREE with the URL surface -- `getId({id:' '})` is `NaN`,
256
+ // so `' '` addresses the NaN slot on every other route while the collision
257
+ // lookup missed it. Same class of bug as the hex divergence above.
258
+ if (id === '') return id;
259
+
260
+ return coerceId(id);
93
261
  }
94
262
 
95
263
  function buildResponse(
@@ -251,6 +419,40 @@ function createFilterPredicate(filters: Filter[]): ((record: { [key: string]: un
251
419
  });
252
420
  }
253
421
 
422
+ /**
423
+ * A function-style `access` return is a per-record predicate, and it is only
424
+ * meaningful if every surface that can hand a record to a caller consults it.
425
+ * Before #190 exactly one of seven did.
426
+ *
427
+ * Enforcement is deliberately post-fetch. `access` returns an opaque JS
428
+ * predicate and `store.findAll(model, conditions)` accepts only an equality
429
+ * conditions object that the SQL drivers translate to a WHERE clause, so
430
+ * query-layer enforcement would require a breaking change to the published
431
+ * `access` contract. That belongs in #197, not in a security patch. Six of the
432
+ * seven surfaces fetch by primary key anyway, so this costs exactly one row.
433
+ */
434
+ function isDenied(filter: unknown, record: unknown): boolean {
435
+ if (typeof filter !== 'function') return false;
436
+
437
+ // A predicate that throws is treated as a denial. Unguarded, a throw escapes
438
+ // to express's default handler, which answers 500 (with a stack trace outside
439
+ // NODE_ENV=production) while a missing id still answers 404 -- so a
440
+ // record-dependent throw re-separates "hidden" from "does not exist" and
441
+ // hands back the oracle this whole change exists to close.
442
+ try {
443
+ return !(filter as (record: unknown) => boolean)(record);
444
+ } catch (error) {
445
+ // Denied, but not silently. A consumer predicate that throws on every
446
+ // record turns the whole collection into a 404 wall, and with no
447
+ // diagnostic that is indistinguishable from an empty database. `stonyx/log`
448
+ // is the module convention (see setup-rest-server.ts); optional-call
449
+ // because a consumer may not have configured the log types.
450
+ log.error?.(`[@stonyx/orm] access filter threw for model -- denying. ${error instanceof Error ? error.message : String(error)}`);
451
+
452
+ return true;
453
+ }
454
+ }
455
+
254
456
  export default class OrmRequest extends Request {
255
457
  model: string;
256
458
  access: (request: unknown) => AccessMethod;
@@ -287,9 +489,13 @@ export default class OrmRequest extends Request {
287
489
  });
288
490
  };
289
491
 
290
- const getSingleHandler: HandlerFn = async (request) => {
492
+ const getSingleHandler: HandlerFn = async (request, { filter }) => {
291
493
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
292
494
  if (!record) return 404;
495
+ // 404, never 403: the status for "exists but filtered out" must be
496
+ // identical to "does not exist", or the fix trades an authorization
497
+ // bypass for a narrower existence oracle.
498
+ if (isDenied(filter, record)) return 404;
293
499
 
294
500
  const fieldsMap = parseFields(request.query);
295
501
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
@@ -301,7 +507,7 @@ export default class OrmRequest extends Request {
301
507
  });
302
508
  };
303
509
 
304
- const createHandler: HandlerFn = async ({ body, query }) => {
510
+ const createHandler: HandlerFn = async ({ body, query }, { filter }) => {
305
511
  const { type, id, attributes, relationships: rels } = (body?.data || {}) as {
306
512
  type?: string;
307
513
  id?: string | number;
@@ -314,14 +520,113 @@ export default class OrmRequest extends Request {
314
520
  const fieldsMap = parseFields(query);
315
521
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
316
522
 
317
- // Check for duplicate ID
318
- if (id !== undefined && await store.find(model, id)) return 409; // Conflict
523
+ // GATE 0 -- the POST existence oracle.
524
+ //
525
+ // The duplicate check runs before the filter and `store.find` sees hidden
526
+ // records, so POST leaks existence through its STATUS. A previous revision
527
+ // filtered the collision status (403 when the colliding record is denied,
528
+ // 409 when it is visible) and that is NOT sufficient, because the status
529
+ // of a create is a third outcome. With a payload the caller is permitted
530
+ // to create -- the normative case for a per-tenant filter, and the case an
531
+ // attacker picks -- all three are distinguishable in ONE request per id:
532
+ //
533
+ // POST /animals {id:22, owner:'gina'} -> 403 a HIDDEN record has this id
534
+ // POST /animals {id:9500, owner:'gina'} -> 200 this id is FREE
535
+ // POST /animals {id:8, owner:'gina'} -> 409 a VISIBLE record has this id
536
+ //
537
+ // Filtering only the collision status narrows that to callers who cannot
538
+ // create a record they are allowed to see. It does not close it.
539
+ //
540
+ // It cannot be closed while a caller both chooses the id and learns
541
+ // whether the create succeeded: a successful create must answer
542
+ // differently from a refused one. So when a per-record filter is in force
543
+ // the caller does not get to choose the id at all. The refusal is
544
+ // UNCONDITIONAL and happens BEFORE any store lookup, so no status, and no
545
+ // lookup cost, can depend on whether that id exists. 403 -- the same
546
+ // status as a denied create -- so the two cannot be separated either.
547
+ //
548
+ // BOTH HALVES OF THAT ARE PINNED, because both were once asserted here and
549
+ // pinned by nothing:
550
+ //
551
+ // status -- assertion 22 sweeps payload x id x id-type, plus `null`.
552
+ // latency -- assertion 41 asserts NO `store.find` is issued on this
553
+ // path. Moving the refusal to after a lookup and returning
554
+ // the same 403 left the suite green while re-opening a
555
+ // hit-versus-miss timing difference on every id-bearing POST,
556
+ // which is what would turn #197 from a ~0.06ms post-fetch
557
+ // residual into a live timing oracle on create.
558
+ //
559
+ // AND THE GUARANTEE IS ONLY AS WIDE AS THE CHANNELS IT COVERS. It reads on
560
+ // the `id` member of the resource object, so it holds only while that is
561
+ // the ONLY way a caller id can reach `createRecord`. It was not: the
562
+ // relationships loop below re-admitted one under `key === "id"` and the
563
+ // gate never fired. Both strips -- `attributes.id` and `relationships.id`
564
+ // -- are therefore part of THIS gate, not tidiness, and assertion 39 pins
565
+ // them. Adding a third channel without a strip re-opens the oracle.
566
+ //
567
+ // Scoped to function-style `access` because that is exactly the population
568
+ // the oracle exists for: with no per-record filter there are no hidden
569
+ // records, and 409 discloses nothing GET /:id does not already.
570
+ //
571
+ // RESIDUALS, stated rather than implied.
572
+ //
573
+ // - a caller can still learn that a collection HAS a per-record filter
574
+ // (403 rather than 409/200 for an id-bearing POST). That discloses a
575
+ // configuration fact, not a record.
576
+ // - this gate is about ids arriving on THIS model's create route. It
577
+ // says nothing about a write to ANOTHER collection: a `POST /owners`
578
+ // carrying `relationships: {pets: {data: {id: 21}}}` -- or
579
+ // `attributes: {pets: [21, 22]}`, which never enters the
580
+ // relationships loop at all -- re-parents hidden animal 21 onto an
581
+ // owner the caller may write, which changes the very field the
582
+ // animals predicate reads and DE-HIDES it. Blocking that needs animal
583
+ // 21 checked against the ANIMAL model's predicate while servicing an
584
+ // OWNERS route, i.e. cross-model access resolution: abofs/stonyx-orm
585
+ // #207, blocked on #202 (`access` receives the model structurally)
586
+ // and #196 (setup-rest-server discards the model->predicate map at
587
+ // boot). NOT closed here, and no comment in this file may say it is.
588
+ //
589
+ // See README `### Known limitations`.
590
+ if (id !== undefined) {
591
+ if (typeof filter === 'function') return 403; // Forbidden
592
+
593
+ // `normalizeBodyId`, not the raw value: a string-typed id misses the
594
+ // store's numeric key, which skipped this check entirely.
595
+ const existing = await store.find(model, normalizeBodyId(id));
596
+ if (existing) return 409; // Conflict
597
+ }
319
598
 
320
599
  const { id: _ignoredId, ...sanitizedAttributes } = attributes || {};
321
600
 
322
- // Extract relationship IDs from JSON:API relationships object
601
+ // Extract relationship IDs from JSON:API relationships object.
602
+ //
603
+ // `key` comes VERBATIM from the request body, so `id` is stripped here for
604
+ // exactly the same reason it is stripped from `attributes` on the line
605
+ // above -- and it must be, or GATE 0 is walked around by moving one field:
606
+ //
607
+ // POST /animals {"id":21, ...} -> 403 GATE 0 fires
608
+ // POST /animals {"relationships":{"id":{"data":{"id":21}}},
609
+ // "attributes":{"owner":"gina"}} -> 200 BYPASS
610
+ //
611
+ // Top-level `id` stayed `undefined`, so GATE 0 never fired and the
612
+ // collision lookup never ran; `createRecord` took its last-entry-wins
613
+ // branch, overwrote hidden record 21 in place and reset its `owner` to a
614
+ // value the caller chose -- de-hiding it permanently. That is #190 itself,
615
+ // on the create surface. Pinned by assertion 39.
616
+ //
617
+ // The `id` member of the resource object is now the ONLY channel a caller
618
+ // id can arrive on FOR THIS MODEL'S OWN CREATE ROUTE, which is what makes
619
+ // GATE 0's guarantee checkable rather than merely asserted. It is not a
620
+ // statement about the record's reachability in general -- a relationship
621
+ // write on another collection reaches it without ever touching this
622
+ // handler (abofs/stonyx-orm#207). INHERITED from `dev`, which carries this
623
+ // loop verbatim; the general form -- the loop accepts any key, not just
624
+ // `id`, so a body key that is not a declared relationship is still
625
+ // mass-assigned -- is abofs/stonyx-orm#204.
323
626
  if (rels) {
324
627
  for (const [key, value] of Object.entries(rels)) {
628
+ if (key === 'id') continue;
629
+
325
630
  const relData = value?.data;
326
631
  if (relData && relData.id !== undefined) {
327
632
  (sanitizedAttributes as { [key: string]: unknown })[key] = relData.id;
@@ -330,16 +635,95 @@ export default class OrmRequest extends Request {
330
635
  }
331
636
 
332
637
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
638
+
639
+ // Slot count BEFORE the write. `createRecord` writes to the store before
640
+ // the predicate can run, and the rollback below must be able to prove the
641
+ // slot it removes is one THIS REQUEST created. Identity alone cannot
642
+ // prove it: when `assignRecordId` lands on an occupied id, `createRecord`
643
+ // mutates the existing OrmRecord IN PLACE, so `store.get(...) === record`
644
+ // is true for a record the request did not create. The map's size is the
645
+ // only O(1) signal that distinguishes an insert from an overwrite.
646
+ const slotsBefore = store.get(model)?.size ?? 0;
647
+
333
648
  const created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false, _skipAutoPersist: true });
334
649
  const record = isOrmRecord(created) ? created : null;
335
650
  if (!record) return 500;
336
651
 
652
+ const createdNewSlot = (store.get(model)?.size ?? 0) > slotsBefore;
653
+
654
+ // 403 here, NOT 404. The oracle argument does not apply to create: there
655
+ // is no pre-existing record whose existence could leak, the caller
656
+ // supplied the attributes, and 404 on a mounted collection route is
657
+ // indistinguishable from "model not mounted" -- a genuinely different
658
+ // failure a developer needs to diagnose.
659
+ //
660
+ // The rollback is not optional. createRecord writes to the store BEFORE
661
+ // the predicate can run, so returning 403 alone would leave the record
662
+ // behind: a worse bug than the bypass being fixed.
663
+ if (isDenied(filter, record)) {
664
+ // ROLL BACK BY IDENTITY, NEVER BY ID. `store.remove(model, record.id)`
665
+ // on its own is a write primitive keyed by a value the caller may have
666
+ // supplied: with the raw-id collision bypass above, a denied
667
+ // `POST {"id":"21"}` answered 403 and DELETED hidden record 21 -- an
668
+ // unauthenticated deletion primitive across the whole id space, created
669
+ // by adding a rollback to a lookup that could be skipped.
670
+ //
671
+ // Both conditions are required and neither implies the other:
672
+ // createdNewSlot -- the store grew, so this request inserted rather
673
+ // than overwrote. Guards `assignRecordId` picking an
674
+ // id that is already taken (it returns
675
+ // last-INSERTED + 1, not max + 1, so a store whose
676
+ // insertion order is not ascending collides) -- see
677
+ // abofs/stonyx-orm#203.
678
+ // identity -- the slot still holds the object we just created,
679
+ // so nothing between createRecord and here replaced
680
+ // it. Deleting this half SURVIVES the suite, and it
681
+ // is kept anyway. WHY IT IS REDUNDANT: there is no
682
+ // `await` anywhere between `slotsBefore` and
683
+ // `store.remove` -- the whole window is synchronous,
684
+ // so it is atomic under Node's event loop; before-
685
+ // `create` hooks run BEFORE the handler
686
+ // (`_withHooks` runs its hook loop ahead of
687
+ // `await handler(...)`), and a consumer predicate
688
+ // inside `isDenied` runs AFTER `createdNewSlot` is
689
+ // computed and cannot flip it. That is a property of
690
+ // THIS function, not of GATE 0 -- an earlier note
691
+ // credited GATE 0, which was both wrong (a caller id
692
+ // reached createRecord through the relationships
693
+ // loop, #204) and the wrong kind of reason: a guard
694
+ // justified on code sixty lines upstream gets
695
+ // silently re-armed when that code moves.
696
+ // SO IT BECOMES REACHABLE IF AN `await` IS
697
+ // INTRODUCED HERE, which is the change a future
698
+ // editor would actually make. Stated here rather
699
+ // than by reference: `docs/` is not in `files`, so
700
+ // a pointer into it resolves to nothing for anyone
701
+ // who installed this package. README carries the
702
+ // consumer-facing half.
703
+ if (createdNewSlot && store.get(model, record.id as string | number) === record) {
704
+ store.remove(model, record.id as string | number, { _skipAutoPersist: true });
705
+ }
706
+
707
+ return 403;
708
+ }
709
+
337
710
  return { data: record.toJSON?.({ fields: modelFields }) };
338
711
  };
339
712
 
340
- const updateHandler: HandlerFn = async ({ body, params }) => {
713
+ const updateHandler: HandlerFn = async ({ body, params }, { filter }) => {
341
714
  const found = await store.find(model, getId(params));
342
715
  if (!found || !isOrmRecord(found)) return 404;
716
+ // Checked BEFORE any attribute is applied. 404 rather than 403 for the
717
+ // same reason as GET /:id -- 403 would disclose both that the record
718
+ // exists and that this caller specifically is excluded.
719
+ //
720
+ // NOT redundant behind GATE 1, and not defence in depth either: GATE 1's
721
+ // verdict is computed BEFORE the before-hook loop runs, and a before-hook
722
+ // is a published extension point that can change the answer -- by
723
+ // mutating the record, or against a predicate that closes over
724
+ // per-request state. This is the only re-evaluation after that window.
725
+ // Pinned by assertion 32; deleting it turns a 404 into an applied update.
726
+ if (isDenied(filter, found)) return 404;
343
727
  const record = found;
344
728
  const { attributes, relationships: rels } = (body?.data || {}) as {
345
729
  attributes?: { [key: string]: unknown };
@@ -362,6 +746,19 @@ export default class OrmRequest extends Request {
362
746
  if (rels) {
363
747
  const relUpdates: { [key: string]: unknown } = {};
364
748
  for (const [key, value] of Object.entries(rels)) {
749
+ // The same missing key filter as createHandler's, and as the
750
+ // attribute loop directly above -- which already had it, while this
751
+ // loop did not. A PATCH carrying
752
+ // `relationships:{"id":{"data":{"id":9101}}}` reached `updateRecord`
753
+ // and RE-KEYED the record: the object held under store key 9102 then
754
+ // reported id 9101, so a visible record claimed a hidden record's
755
+ // identity on every surface that reads `record.id` rather than the map
756
+ // key. Gated by GATE 1 on the addressed record, so it is store
757
+ // corruption rather than a filter bypass -- but it is the same one-line
758
+ // omission two handlers apart. Pinned by assertion 40; INHERITED from
759
+ // `dev`; abofs/stonyx-orm#204.
760
+ if (key === 'id') continue;
761
+
365
762
  const relData = value?.data;
366
763
  if (relData && relData.id !== undefined) {
367
764
  relUpdates[key] = relData.id;
@@ -375,8 +772,31 @@ export default class OrmRequest extends Request {
375
772
  return { data: record.toJSON?.() };
376
773
  };
377
774
 
378
- const deleteHandler: HandlerFn = ({ params }) => {
379
- store.remove(model, getId(params), { _skipAutoPersist: true });
775
+ const deleteHandler: HandlerFn = async ({ params }, { filter }) => {
776
+ // Coerced ONCE. `getId(params)` was evaluated twice here -- once to find
777
+ // the record and once to remove it -- and a coercion evaluated repeatedly
778
+ // is a coercion that can be edited in one place and not the other, which
779
+ // is the defect `coerceId` exists to prevent.
780
+ const recordId = getId(params);
781
+ const record = await store.find(model, recordId) as OrmRecord | undefined;
782
+
783
+ // BEHAVIOUR CHANGE (#190): a DELETE of a record that never existed
784
+ // returned 204 before this change. It now returns 404, matching the
785
+ // denied case below. This is deliberate and load-bearing -- if a denied
786
+ // delete returned 404 while a missing one returned 204, the pair would be
787
+ // a perfect existence oracle and the whole fix would be worthless.
788
+ // Returning 204 for a denied delete was rejected instead: it falsely
789
+ // reports success for a request that changed nothing.
790
+ if (!record) return 404;
791
+ // Re-evaluated after the before-hook loop, exactly as in updateHandler --
792
+ // GATE 1 decided before the hooks ran. Pinned by assertion 33; deleting it
793
+ // turns a 404 into a destroyed record.
794
+ if (isDenied(filter, record)) return 404;
795
+
796
+ // Removed by the id of the record actually fetched, not by re-deriving it
797
+ // from the params a second time: the record the filter tested and the
798
+ // record removed are then provably the same one.
799
+ store.remove(model, record.id as string | number, { _skipAutoPersist: true });
380
800
  return 204;
381
801
  };
382
802
 
@@ -405,9 +825,65 @@ export default class OrmRequest extends Request {
405
825
  }
406
826
  }
407
827
 
408
- // Wraps a handler with before/after hook execution
828
+ // Wraps a handler with before/after hook execution.
829
+ //
830
+ // ===========================================================================
831
+ // TWO AUTHORIZATION GATES, AND EVERY EXECUTOR SITS BEHIND ONE OF THEM (#190)
832
+ //
833
+ // The defect this function was fixed for is NOT "a delete persists past a
834
+ // 404". It is that _withHooks has SEVERAL executors downstream of the
835
+ // handler, and originally the handler's response gated none of them. Three
836
+ // exist today:
837
+ //
838
+ // 1. sqlDb.persist -- issues real SQL against the backing store
839
+ // 2. the after-hook pipeline -- the PUBLISHED consumer extension point;
840
+ // a cascade delete, a webhook, a search-index
841
+ // purge. `context.recordId` and
842
+ // `context.oldState` are populated for it.
843
+ // 3. Orm.db.save() -- a full serialize-and-write of the store
844
+ //
845
+ // Gating them one at a time is how this keeps regressing, so the rule is:
846
+ // compute denial ONCE at each point where it becomes knowable, and keep every
847
+ // executor downstream of a gate. If you add a fourth executor to this
848
+ // function, it goes below GATE 2 or it is a security bug.
849
+ //
850
+ // GATE 1 (pre-handler) is required because before-hooks and `context.oldState`
851
+ // run/are built BEFORE the handler can consult the filter. Without it a denied
852
+ // DELETE still handed the hidden record's full contents to consumer code.
853
+ // GATE 2 (post-handler) covers everything the handler's status can reach.
854
+ // ===========================================================================
409
855
  private _withHooks(operation: string, handler: HandlerFn): HandlerFn {
410
856
  return async (request: OrmRequest$, state: { [key: string]: unknown }) => {
857
+ // `|| {}` so this function behaves like the relationship routes below,
858
+ // which declare `state` with a `= {}` default. It is unkillable through
859
+ // the rest-server dispatcher, which always passes `getState(req)`; it is
860
+ // listed as such in the guards-redundant-by-construction table rather
861
+ // than left silently unkillable, and it defends the WHOLE function (the
862
+ // context, the snapshot and the handler call all read `callState`) rather
863
+ // than one destructure that the next line would throw past anyway.
864
+ const callState = (state || {}) as { [key: string]: unknown };
865
+
866
+ // ---------------------------------------------------------------------
867
+ // THE AUTHORIZATION SNAPSHOT. Read ONCE, here, before any consumer code
868
+ // can run.
869
+ //
870
+ // `callState` is the object `auth()` planted the filter in, and it is
871
+ // also handed to every before-hook as `context.state` -- a published,
872
+ // WRITABLE extension point. So `state.filter` is an INPUT to the
873
+ // authorization decision, not only an output channel, and re-reading it
874
+ // after the hook loop lets a consumer hook disarm the filter:
875
+ //
876
+ // beforeHook('get', 'animal', ctx => { delete ctx.state.filter })
877
+ // -> GET /animals/21 turned 404 into 200
878
+ // -> GET /animals turned 20 records into 22
879
+ //
880
+ // GATE 1 already used this snapshot, so writes held; the READ handlers
881
+ // re-destructured `filter` from the live bag and did not. Everything
882
+ // downstream now reads `filter` from here, and the handler is handed
883
+ // `handlerState` below -- never `callState`.
884
+ // ---------------------------------------------------------------------
885
+ const { filter } = callState as { filter?: unknown };
886
+
411
887
  // Build context object for hooks
412
888
  const context: HookContext = {
413
889
  model: this.model,
@@ -416,12 +892,37 @@ export default class OrmRequest extends Request {
416
892
  params: request.params,
417
893
  body: request.body,
418
894
  query: request.query,
419
- state,
895
+ // Deliberately the LIVE object: `redirect` and `pipe` are read back off
896
+ // it by @stonyx/rest-server after the handler returns, so hooks must be
897
+ // able to write to it. What must not happen is the authorization
898
+ // decision reading it back, which is what the snapshot above prevents.
899
+ state: callState,
420
900
  };
421
901
 
422
902
  // Capture old state for operations that modify data
423
903
  if (operation === 'update' || operation === 'delete') {
424
904
  const existingRecord = await store.find(this.model, getId(request.params)) as OrmRecord | undefined;
905
+
906
+ // GATE 1 -- pre-handler. This record fetch already happened for
907
+ // oldState, so the check is free.
908
+ //
909
+ // Returning here rather than letting updateHandler/deleteHandler
910
+ // produce the same 404 is the point: everything between here and there
911
+ // is an executor the caller is not authorized to reach.
912
+ // - context.oldState is a deep copy of the HIDDEN RECORD'S CONTENTS.
913
+ // Building it and handing it to a before-hook discloses exactly what
914
+ // the filter exists to hide.
915
+ // - context.recordId is populated for delete BEFORE the handler runs,
916
+ // which is the same shape as the sqlDb landmine one layer up:
917
+ // `afterHook('delete', ctx => cascadeDelete(ctx.recordId))` destroys
918
+ // children behind a correct 404.
919
+ // - a before-hook may return a value and short-circuit, which would
920
+ // otherwise return a response without the filter ever executing.
921
+ //
922
+ // 404, not 403, for the same reason as getSingleHandler: the status for
923
+ // "exists but filtered out" must equal "does not exist".
924
+ if (existingRecord && isDenied(filter, existingRecord)) return 404;
925
+
425
926
  if (existingRecord) {
426
927
  // Deep copy the record's data to preserve old state
427
928
  context.oldState = JSON.parse(JSON.stringify(existingRecord.__data || existingRecord));
@@ -441,16 +942,52 @@ export default class OrmRequest extends Request {
441
942
  }
442
943
 
443
944
  // Execute main handler
444
- const response = await handler(request, state);
945
+ // The handler receives the SNAPSHOT, never the live bag. `filter` is
946
+ // assigned LAST so it wins over anything a before-hook wrote to
947
+ // `callState.filter` -- including a `delete`, which the spread would
948
+ // otherwise carry through as an absent key. Every other key a hook adds
949
+ // is still visible to the handler; only the authorization input is
950
+ // pinned.
951
+ const handlerState = { ...callState, filter };
952
+ const response = await handler(request, handlerState);
445
953
 
446
954
  // Set context.record for update BEFORE persist so SQL drivers can read it
447
955
  if (operation === 'update' && (response as JsonApiResponse)?.data) {
448
956
  context.record = store.get(this.model, getId(request.params));
449
957
  }
450
958
 
451
- // Persist to SQL database for all write operations (create/update/delete)
959
+ // GATE 2 -- post-handler. A denied or failed handler returns a bare status
960
+ // integer, and no executor below may run for one.
961
+ //
962
+ // `>= 400` deliberately covers every failure status, not just the
963
+ // authorization ones: a 400 (POST with no `type`) and a 409 (duplicate id)
964
+ // are equally requests in which nothing happened, and a persist or a
965
+ // cascade hook for one of them is just as wrong.
966
+ // `Number.isInteger` is a TYPE guard, not a behaviour guard, and it is
967
+ // unkillable TODAY: the only non-integer a handler in this file can
968
+ // return is a `{ data, links }` object, and `{} >= 400` is `false` by JS
969
+ // coercion, so dropping it changes no reachable outcome. It is kept
970
+ // because `>=` coerces rather than rejects, and the shapes it coerces
971
+ // are not obvious -- `[500] >= 400` is TRUE, so a handler that one day
972
+ // returned an array would have every response read as a denial. Listed
973
+ // as an equivalent mutant rather than left to read as coverage; it
974
+ // becomes killable the moment a handler returns anything array-like or
975
+ // numeric-string-like.
976
+ const denied = Number.isInteger(response) && (response as number) >= 400;
977
+
978
+ // EXECUTOR 1 -- SQL persistence, for all write operations.
979
+ //
980
+ // `response` is passed to sqlDb.persist below, but it is dropped at the
981
+ // driver boundary: _persistDelete(modelName, context) never receives it
982
+ // and guards only on context.recordId -- which _withHooks set above,
983
+ // BEFORE the handler ran. Without this gate a correct 404 still issues
984
+ // DELETE FROM ... WHERE id = ? on every SQL backend.
985
+ //
986
+ // No file-backed test can observe that, because Orm.instance.sqlDb is
987
+ // null in file/directory mode. See the stubbed-sqlDb assertions in
988
+ // test/unit/access-filter-enforcement-test.ts.
452
989
  const sqlDb = Orm.instance.sqlDb;
453
- if (sqlDb && WRITE_OPERATIONS.has(operation)) {
990
+ if (sqlDb && WRITE_OPERATIONS.has(operation) && !denied) {
454
991
  await sqlDb.persist(operation, this.model, context, response);
455
992
  }
456
993
 
@@ -464,20 +1001,45 @@ export default class OrmRequest extends Request {
464
1001
  } else if (operation === 'create' && (response as JsonApiResponse)?.data && ((response as { data: { id?: unknown } }).data.id)) {
465
1002
  // For create, get the record from store using the ID from the response
466
1003
  const responseData = (response as { data: { id: string | number } }).data;
467
- const recordId = isNaN(responseData.id as unknown as number) ? responseData.id : parseInt(responseData.id as string);
468
- context.record = store.get(this.model, recordId);
1004
+ // `normalizeBodyId`, not a copy of its body. This line WAS
1005
+ // `isNaN(id) ? id : parseInt(id)` -- `coerceId` inlined verbatim, a
1006
+ // third coercion feeding a store lookup, sitting under a docblock that
1007
+ // said neither surface had a copy. Equivalent on every input that can
1008
+ // reach this truthy-guarded branch (`21`->`21`, `'21'`->`21`,
1009
+ // `'angela'`->`'angela'`; `''` and `0` cannot reach it), so this is a
1010
+ // de-duplication rather than a behaviour change -- and that is the
1011
+ // point: the two that disagreed were equivalent on every input anyone
1012
+ // checked, too.
1013
+ context.record = store.get(this.model, normalizeBodyId(responseData.id) as string | number);
469
1014
  } else if (operation === 'delete') {
470
1015
  // For delete, the record may no longer exist, but we have oldState
471
1016
  context.recordId = getId(request.params);
472
1017
  }
473
1018
 
474
- // Run after hooks sequentially
475
- for (const hook of getAfterHooks(operation, this.model)) {
476
- await hook(context);
1019
+ // EXECUTOR 2 -- the after-hook pipeline. This is the published consumer
1020
+ // extension point (`afterHook` is exported from @stonyx/orm and from
1021
+ // ./hooks), so it is the executor with the widest possible blast radius:
1022
+ // a cascade delete, a webhook, a token revocation, a search-index purge.
1023
+ //
1024
+ // BEHAVIOUR CHANGE (#190): after-hooks no longer fire for a request that
1025
+ // failed. Previously `afterHook('delete', ...)` ran with a populated
1026
+ // context.recordId on a 404, so a consumer cascade destroyed children for
1027
+ // a request that deleted nothing. Firing a hook named "after<operation>"
1028
+ // for an operation that did not occur is a booby trap, and the denied case
1029
+ // is unreachable-before-#190 while the missing case is inherited debt --
1030
+ // both are closed by the same gate. `context.response` therefore only ever
1031
+ // carries a success status into a hook.
1032
+ if (!denied) {
1033
+ for (const hook of getAfterHooks(operation, this.model)) {
1034
+ await hook(context);
1035
+ }
477
1036
  }
478
1037
 
479
- // Auto-save DB after write operations when configured
480
- if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation)) {
1038
+ // EXECUTOR 3 -- file/directory autosave. Ungated this let an
1039
+ // unauthenticated caller force a full serialize-and-write of the entire
1040
+ // store on every DELETE of any id, with no record touched: amplification
1041
+ // rather than corruption, but the same root cause and the same fix.
1042
+ if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation) && !denied) {
481
1043
  await (Orm.db as { save(): Promise<void> }).save();
482
1044
  }
483
1045
 
@@ -497,9 +1059,19 @@ export default class OrmRequest extends Request {
497
1059
  const dasherizedName = camelCaseToKebabCase(relationshipName);
498
1060
 
499
1061
  // Related resource route: GET /:id/{relationship}
500
- routes[`/:id/${dasherizedName}`] = async (request: OrmRequest$) => {
1062
+ //
1063
+ // These generated routes are not wrapped by _withHooks, which is why they
1064
+ // were the least obvious two of the seven unguarded surfaces in #190.
1065
+ // They are still dispatched by @stonyx/rest-server as
1066
+ // `handler(req, getState(req))`, so `state` -- and therefore the filter
1067
+ // planted by auth() -- has always been available here; it was simply
1068
+ // never declared or read.
1069
+ routes[`/:id/${dasherizedName}`] = async (request: OrmRequest$, { filter }: { [key: string]: unknown } = {}) => {
501
1070
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
502
1071
  if (!record) return 404;
1072
+ // Filtering the PARENT: a caller who may not see the record may not see
1073
+ // what it is related to either.
1074
+ if (isDenied(filter, record)) return 404;
503
1075
 
504
1076
  const relatedData = record.__relationships[relationshipName];
505
1077
  const baseUrl = getBaseUrl(request);
@@ -521,9 +1093,10 @@ export default class OrmRequest extends Request {
521
1093
  };
522
1094
 
523
1095
  // Relationship linkage route: GET /:id/relationships/{relationship}
524
- routes[`/:id/relationships/${dasherizedName}`] = async (request: OrmRequest$) => {
1096
+ routes[`/:id/relationships/${dasherizedName}`] = async (request: OrmRequest$, { filter }: { [key: string]: unknown } = {}) => {
525
1097
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
526
1098
  if (!record) return 404;
1099
+ if (isDenied(filter, record)) return 404;
527
1100
 
528
1101
  const relatedData = record.__relationships[relationshipName];
529
1102
  const baseUrl = getBaseUrl(request);
@@ -554,32 +1127,65 @@ export default class OrmRequest extends Request {
554
1127
  };
555
1128
  }
556
1129
 
557
- // Catch-all for invalid relationship names on related resource route
558
- routes[`/:id/:relationship`] = async (request: OrmRequest$) => {
559
- const record = await store.find(model, getId(request.params));
560
- if (!record) return 404;
561
-
562
- // If we reach here, relationship doesn't exist (valid ones were registered above)
563
- return 404;
564
- };
565
-
566
- // Catch-all for invalid relationship names on relationship linkage route
567
- routes[`/:id/relationships/:relationship`] = async (request: OrmRequest$) => {
568
- const record = await store.find(model, getId(request.params));
569
- if (!record) return 404;
570
-
571
- return 404;
572
- };
1130
+ // Catch-alls for invalid relationship names. Every valid relationship was
1131
+ // registered above, so reaching either of these means the relationship does
1132
+ // not exist and the answer is 404 regardless of the record.
1133
+ //
1134
+ // These deliberately carry NO access check and no store lookup. An earlier
1135
+ // revision of #190 added `if (isDenied(filter, record)) return 404` here for
1136
+ // symmetry with the seven real surfaces, but both branches returned 404, so
1137
+ // the guard was unobservable by construction -- a mutation deleting it
1138
+ // survived the entire suite because no test that could distinguish it can
1139
+ // exist. Unkillable code in an authorization diff reads as coverage and is
1140
+ // not, so it is gone; skipping the lookup also removes the timing difference
1141
+ // between an existing and a missing parent.
1142
+ //
1143
+ // IF THIS ROUTE EVER RETURNS ANYTHING OTHER THAN A CONSTANT 404, it becomes
1144
+ // the eighth surface and must filter the parent first, exactly like
1145
+ // `/:id/{relationship}` above.
1146
+ routes[`/:id/:relationship`] = async () => 404;
1147
+ routes[`/:id/relationships/:relationship`] = async () => 404;
573
1148
 
574
1149
  return routes;
575
1150
  }
576
1151
 
577
1152
  auth(request: OrmRequest$, state: { [key: string]: unknown }): number | undefined {
578
- const access = this.access(request);
1153
+ // A consumer `access()` that throws is a DENIAL, matching `isDenied` one
1154
+ // layer down. Unguarded it propagates to express's default handler, which
1155
+ // answers 500 -- and the documented sample itself can throw
1156
+ // (`request.originalUrl.split(...)` when originalUrl is absent), so the
1157
+ // failure mode is reachable by following the docs.
1158
+ let access: AccessMethod;
1159
+ try {
1160
+ access = this.access(request);
1161
+ } catch (error) {
1162
+ // Same reasoning as `isDenied`: fail closed, but say so. An `access()`
1163
+ // that throws denies EVERY request to the collection, and a silent 403
1164
+ // wall is the hardest possible thing to diagnose from the outside.
1165
+ log.error?.(`[@stonyx/orm] access() threw for model "${this.model}" -- denying. ${error instanceof Error ? error.message : String(error)}`);
1166
+
1167
+ return 403; // Forbidden
1168
+ }
579
1169
 
580
1170
  if (!access) return 403;
581
- if (Array.isArray(access) && !access.includes(methodAccessMap[request.method])) return 403;
582
- if (typeof access === 'function') state.filter = access;
1171
+ if (typeof access === 'function') {
1172
+ state.filter = access;
1173
+ return undefined;
1174
+ }
1175
+ if (access === true) return undefined;
1176
+
1177
+ // `AccessMethod` declares `string` legal and it fell through every branch
1178
+ // above, returning undefined -- i.e. FULL CRUD, no filter. `return 'read'`
1179
+ // is the natural reading of a type that lists `string` first, and it
1180
+ // granted DELETE. A bare string is one permission, not a grant of all four.
1181
+ const permitted = typeof access === 'string' ? [access] : access;
1182
+
1183
+ // Anything that is not a permission array by this point -- an object, a
1184
+ // number, a Symbol -- is a consumer mistake, and the only safe reading of a
1185
+ // shape the contract does not define is a denial. Fail CLOSED.
1186
+ if (!Array.isArray(permitted)) return 403;
1187
+ if (!permitted.includes(methodAccessMap[request.method])) return 403;
1188
+
583
1189
  return undefined;
584
1190
  }
585
1191
  }