@stonyx/orm 0.3.2-alpha.5 → 0.3.2-alpha.50

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.
Files changed (48) hide show
  1. package/README.md +296 -4
  2. package/config/environment.js +99 -12
  3. package/dist/commands.js +34 -0
  4. package/dist/dynamodb/connection.d.ts +31 -0
  5. package/dist/dynamodb/connection.js +28 -0
  6. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  7. package/dist/dynamodb/dynamodb-db.js +596 -0
  8. package/dist/dynamodb/operation-builder.d.ts +76 -0
  9. package/dist/dynamodb/operation-builder.js +116 -0
  10. package/dist/dynamodb/type-map.d.ts +31 -0
  11. package/dist/dynamodb/type-map.js +48 -0
  12. package/dist/main.js +10 -0
  13. package/dist/manage-record.js +34 -3
  14. package/dist/mysql/connection.d.ts +1 -0
  15. package/dist/mysql/mysql-db.d.ts +8 -0
  16. package/dist/mysql/mysql-db.js +44 -10
  17. package/dist/orm-request.d.ts +37 -0
  18. package/dist/orm-request.js +493 -43
  19. package/dist/postgres/connection.d.ts +1 -0
  20. package/dist/postgres/connection.js +8 -6
  21. package/dist/postgres/postgres-db.d.ts +8 -0
  22. package/dist/postgres/postgres-db.js +44 -10
  23. package/dist/record.js +7 -5
  24. package/dist/relationships.js +1 -1
  25. package/dist/serializer.js +38 -2
  26. package/dist/store.d.ts +13 -1
  27. package/dist/store.js +65 -6
  28. package/dist/types/orm-types.d.ts +11 -0
  29. package/package.json +16 -7
  30. package/src/commands.ts +43 -0
  31. package/src/dynamodb/connection.ts +50 -0
  32. package/src/dynamodb/dynamodb-db.ts +811 -0
  33. package/src/dynamodb/operation-builder.ts +202 -0
  34. package/src/dynamodb/type-map.ts +54 -0
  35. package/src/main.ts +10 -0
  36. package/src/manage-record.ts +41 -9
  37. package/src/mysql/connection.ts +1 -0
  38. package/src/mysql/mysql-db.ts +44 -12
  39. package/src/orm-request.ts +500 -41
  40. package/src/postgres/connection.ts +10 -6
  41. package/src/postgres/postgres-db.ts +44 -12
  42. package/src/record.ts +8 -5
  43. package/src/relationships.ts +1 -1
  44. package/src/serializer.ts +39 -2
  45. package/src/store.ts +68 -6
  46. package/src/types/orm-types.ts +12 -0
  47. package/src/types/stonyx.d.ts +7 -1
  48. package/config/environment.ts +0 -91
@@ -1,3 +1,40 @@
1
+ /**
2
+ * REST request handling and access enforcement for @stonyx/orm.
3
+ *
4
+ * ---------------------------------------------------------------------------
5
+ * THE DOCUMENTED `access()` PATTERN IS A STOPGAP. READ THIS BEFORE RELYING ON IT.
6
+ * ---------------------------------------------------------------------------
7
+ * `auth()` below hands your `access(request)` a raw transport artifact and asks
8
+ * you to re-derive from a URL string what this module already holds
9
+ * structurally: which model, which operation, which record. The three-line
10
+ * URL-matching example in README has failed **open** in four distinct ways
11
+ * during the review of a single change, each found only after the previous was
12
+ * fixed, by four different people:
13
+ *
14
+ * 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
15
+ * prefix match against it is ALWAYS false.
16
+ * 2. `request.originalUrl` carries the query string, so an anchored equality
17
+ * check misses `/owners?filter[age]=30`.
18
+ * 3. The router is a bare `express()` (`caseSensitive: false`) while a
19
+ * hand-written matcher is case-SENSITIVE, so `GET /OwNeRs/angela` walks
20
+ * past it. Router-side: abofs/stonyx-rest-server#47.
21
+ * 4. Under a configured `ORM_REST_ROUTE` a hard-coded `/owners` matches
22
+ * nothing -- environment-specifically, which is worse.
23
+ *
24
+ * The README sample closes all four. That is NOT the same as being safe; it is
25
+ * safe against the four variants we happen to have found, and there is no
26
+ * reason to believe the list is complete.
27
+ *
28
+ * THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model, the
29
+ * operation and the record, so there is no URL to parse and no variant to miss.
30
+ * Prefer the array shape (`['read']`) or `false` until #202 lands; the
31
+ * function shape is what requires the URL matching.
32
+ *
33
+ * The enforcement gates in this file (GATE 0/1/2, the create rollback, the
34
+ * per-handler `isDenied` re-checks) are correct independently of that -- they
35
+ * enforce whatever predicate you return. The stopgap is the part where YOU have
36
+ * to work out which predicate to return.
37
+ */
1
38
  import { Request } from '@stonyx/rest-server';
2
39
  import Orm, { store, createRecord, updateRecord } from '@stonyx/orm';
3
40
  import { camelCaseToKebabCase } from '@stonyx/utils/string';
@@ -84,12 +121,90 @@ function getBaseUrl(request: OrmRequest$): string {
84
121
  return `${protocol}://${host}`;
85
122
  }
86
123
 
124
+ /**
125
+ * The ONE coercion from a caller-supplied id string to the key the store holds
126
+ * it under. Both id-bearing surfaces go through this, and neither has a copy.
127
+ *
128
+ * WHY IT IS SHARED RATHER THAN DUPLICATED. `getId()` (URL) and
129
+ * `normalizeBodyId()` (JSON body) each had their own arithmetic, and they
130
+ * disagreed: `parseInt(id)` versus `parseInt(id, 10)`. On a hex-shaped id that
131
+ * is a two-record difference --
132
+ *
133
+ * GET /animals/0x2391 -> record 9105 (getId -> parseInt('0x2391') = 9105)
134
+ * POST /animals {"id":"0x2391"} -> lookup under 0 (normalize -> parseInt('0x2391',10) = 0)
135
+ * -> a MISS, so the duplicate check was skipped and
136
+ * createRecord OVERWROTE 9105 in place, answering 200
137
+ *
138
+ * -- which is the raw-versus-normalised divergence that produced the round-3
139
+ * blocker, in a narrower form, reintroduced by the fix for it. Two coercions
140
+ * that must agree cannot be kept in agreement by review; they have to be one
141
+ * function. Pinned by assertion 43.
142
+ *
143
+ * `parseInt` and not `Number`, deliberately. They differ on `'1e3'` (1 vs 1000)
144
+ * and `'9105.5'` (9105 vs 9105.5), and `getId` -- which decides which record an
145
+ * id ADDRESSES -- is the reference, so `Number` would trade one divergence for
146
+ * two. The reason `parseInt` is safe here is the `isNaN` gate in front of it:
147
+ * `parseInt('9105h')` is `9105`, and truncating a partially-valid id into a
148
+ * DIFFERENT VALID key is exactly how a collision lookup gets skipped. The gate
149
+ * rejects it as a string instead, so nothing is ever truncated. That gate, not
150
+ * the parser, is the load-bearing half -- assertion 43 pins it.
151
+ */
152
+ function coerceId(id: string): string | number {
153
+ if (isNaN(id as unknown as number)) return id;
154
+
155
+ return parseInt(id);
156
+ }
157
+
87
158
  function getId(params: { id?: string; [key: string]: unknown }): string | number {
88
159
  const id = params.id;
89
160
  if (!id) return '';
90
- if (isNaN(id as unknown as number)) return id;
91
161
 
92
- return parseInt(id);
162
+ return coerceId(id);
163
+ }
164
+
165
+ /**
166
+ * Normalise a caller-supplied BODY id to the key the store will hold it under.
167
+ *
168
+ * `getId()` above is params-shaped: it takes `{ id?: string }` off the URL,
169
+ * where the value is always a string and a falsy one means "no id". A JSON body
170
+ * id is neither -- it can arrive as a number, and `0` is a legitimate id that
171
+ * `getId()` would flatten to `''`.
172
+ *
173
+ * WHY THIS EXISTS AT ALL. createHandler used to look the collision up with the
174
+ * RAW body value while every other surface normalised through `getId()`. The
175
+ * store is a Map keyed by the coerced value, so `store.find(model, '21')` misses
176
+ * the entry held under `21` and the duplicate check is skipped by typing the id
177
+ * as a string. On `dev` that silently overwrote the colliding record and
178
+ * answered 200; combined with the denied-create rollback added for #190 it
179
+ * became an unauthenticated DELETE of any id. Normalising here is half of that
180
+ * fix -- see the rollback in createHandler for the other half.
181
+ *
182
+ * It shares `coerceId` with `getId` so the two surfaces cannot drift apart
183
+ * again, and differs from `getId` in exactly ONE place, below.
184
+ */
185
+ function normalizeBodyId(id: string | number): string | number {
186
+ // Non-strings pass through untouched: a JSON body id can arrive as a number,
187
+ // and `getId`'s falsy-flatten must NOT apply to it -- `0` is a legitimate id
188
+ // and `getId` would turn it into `''`. (assertion 30 sweeps id `0`.)
189
+ if (typeof id !== 'string') return id;
190
+
191
+ // THE ONE DIVERGENCE FROM `getId`, and it mirrors rather than contradicts it:
192
+ // `getId` maps a falsy param to `''`, and `''` is the only string a body can
193
+ // carry that means "no id" -- `createRecord` treats it as absent and assigns a
194
+ // server id. Coercing it instead would make it address a real slot, because
195
+ // `parseInt('')` is `NaN` and a record CAN be held under `NaN` (a truthy but
196
+ // non-numeric id such as `' '` survives `assignRecordId`'s falsy guard and
197
+ // then NaNs in the id transform). So `POST {"id":""}` would answer 409 against
198
+ // an unrelated record it never named. Pinned by assertion 44.
199
+ //
200
+ // Note what is deliberately NOT special-cased here any more: whitespace.
201
+ // `id.trim() === ''` used to short-circuit `' '` as well, which made the
202
+ // body surface DISAGREE with the URL surface -- `getId({id:' '})` is `NaN`,
203
+ // so `' '` addresses the NaN slot on every other route while the collision
204
+ // lookup missed it. Same class of bug as the hex divergence above.
205
+ if (id === '') return id;
206
+
207
+ return coerceId(id);
93
208
  }
94
209
 
95
210
  function buildResponse(
@@ -251,6 +366,33 @@ function createFilterPredicate(filters: Filter[]): ((record: { [key: string]: un
251
366
  });
252
367
  }
253
368
 
369
+ /**
370
+ * A function-style `access` return is a per-record predicate, and it is only
371
+ * meaningful if every surface that can hand a record to a caller consults it.
372
+ * Before #190 exactly one of seven did.
373
+ *
374
+ * Enforcement is deliberately post-fetch. `access` returns an opaque JS
375
+ * predicate and `store.findAll(model, conditions)` accepts only an equality
376
+ * conditions object that the SQL drivers translate to a WHERE clause, so
377
+ * query-layer enforcement would require a breaking change to the published
378
+ * `access` contract. That belongs in #197, not in a security patch. Six of the
379
+ * seven surfaces fetch by primary key anyway, so this costs exactly one row.
380
+ */
381
+ function isDenied(filter: unknown, record: unknown): boolean {
382
+ if (typeof filter !== 'function') return false;
383
+
384
+ // A predicate that throws is treated as a denial. Unguarded, a throw escapes
385
+ // to express's default handler, which answers 500 (with a stack trace outside
386
+ // NODE_ENV=production) while a missing id still answers 404 -- so a
387
+ // record-dependent throw re-separates "hidden" from "does not exist" and
388
+ // hands back the oracle this whole change exists to close.
389
+ try {
390
+ return !(filter as (record: unknown) => boolean)(record);
391
+ } catch {
392
+ return true;
393
+ }
394
+ }
395
+
254
396
  export default class OrmRequest extends Request {
255
397
  model: string;
256
398
  access: (request: unknown) => AccessMethod;
@@ -287,9 +429,13 @@ export default class OrmRequest extends Request {
287
429
  });
288
430
  };
289
431
 
290
- const getSingleHandler: HandlerFn = async (request) => {
432
+ const getSingleHandler: HandlerFn = async (request, { filter }) => {
291
433
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
292
434
  if (!record) return 404;
435
+ // 404, never 403: the status for "exists but filtered out" must be
436
+ // identical to "does not exist", or the fix trades an authorization
437
+ // bypass for a narrower existence oracle.
438
+ if (isDenied(filter, record)) return 404;
293
439
 
294
440
  const fieldsMap = parseFields(request.query);
295
441
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
@@ -301,7 +447,7 @@ export default class OrmRequest extends Request {
301
447
  });
302
448
  };
303
449
 
304
- const createHandler: HandlerFn = async ({ body, query }) => {
450
+ const createHandler: HandlerFn = async ({ body, query }, { filter }) => {
305
451
  const { type, id, attributes, relationships: rels } = (body?.data || {}) as {
306
452
  type?: string;
307
453
  id?: string | number;
@@ -314,14 +460,95 @@ export default class OrmRequest extends Request {
314
460
  const fieldsMap = parseFields(query);
315
461
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
316
462
 
317
- // Check for duplicate ID
318
- if (id !== undefined && await store.find(model, id)) return 409; // Conflict
463
+ // GATE 0 -- the POST existence oracle.
464
+ //
465
+ // The duplicate check runs before the filter and `store.find` sees hidden
466
+ // records, so POST leaks existence through its STATUS. A previous revision
467
+ // filtered the collision status (403 when the colliding record is denied,
468
+ // 409 when it is visible) and that is NOT sufficient, because the status
469
+ // of a create is a third outcome. With a payload the caller is permitted
470
+ // to create -- the normative case for a per-tenant filter, and the case an
471
+ // attacker picks -- all three are distinguishable in ONE request per id:
472
+ //
473
+ // POST /animals {id:22, owner:'gina'} -> 403 a HIDDEN record has this id
474
+ // POST /animals {id:9500, owner:'gina'} -> 200 this id is FREE
475
+ // POST /animals {id:8, owner:'gina'} -> 409 a VISIBLE record has this id
476
+ //
477
+ // Filtering only the collision status narrows that to callers who cannot
478
+ // create a record they are allowed to see. It does not close it.
479
+ //
480
+ // It cannot be closed while a caller both chooses the id and learns
481
+ // whether the create succeeded: a successful create must answer
482
+ // differently from a refused one. So when a per-record filter is in force
483
+ // the caller does not get to choose the id at all. The refusal is
484
+ // UNCONDITIONAL and happens BEFORE any store lookup, so no status, and no
485
+ // lookup cost, can depend on whether that id exists. 403 -- the same
486
+ // status as a denied create -- so the two cannot be separated either.
487
+ //
488
+ // BOTH HALVES OF THAT ARE PINNED, because both were once asserted here and
489
+ // pinned by nothing:
490
+ //
491
+ // status -- assertion 22 sweeps payload x id x id-type, plus `null`.
492
+ // latency -- assertion 41 asserts NO `store.find` is issued on this
493
+ // path. Moving the refusal to after a lookup and returning
494
+ // the same 403 left the suite green while re-opening a
495
+ // hit-versus-miss timing difference on every id-bearing POST,
496
+ // which is what would turn #197 from a ~0.06ms post-fetch
497
+ // residual into a live timing oracle on create.
498
+ //
499
+ // AND THE GUARANTEE IS ONLY AS WIDE AS THE CHANNELS IT COVERS. It reads on
500
+ // the `id` member of the resource object, so it holds only while that is
501
+ // the ONLY way a caller id can reach `createRecord`. It was not: the
502
+ // relationships loop below re-admitted one under `key === "id"` and the
503
+ // gate never fired. Both strips -- `attributes.id` and `relationships.id`
504
+ // -- are therefore part of THIS gate, not tidiness, and assertion 39 pins
505
+ // them. Adding a third channel without a strip re-opens the oracle.
506
+ //
507
+ // Scoped to function-style `access` because that is exactly the population
508
+ // the oracle exists for: with no per-record filter there are no hidden
509
+ // records, and 409 discloses nothing GET /:id does not already.
510
+ //
511
+ // RESIDUAL, stated rather than implied: a caller can still learn that a
512
+ // collection HAS a per-record filter (403 rather than 409/200 for an
513
+ // id-bearing POST). That discloses a configuration fact, not a record.
514
+ // See README `### Known limitations`.
515
+ if (id !== undefined) {
516
+ if (typeof filter === 'function') return 403; // Forbidden
517
+
518
+ // `normalizeBodyId`, not the raw value: a string-typed id misses the
519
+ // store's numeric key, which skipped this check entirely.
520
+ const existing = await store.find(model, normalizeBodyId(id));
521
+ if (existing) return 409; // Conflict
522
+ }
319
523
 
320
524
  const { id: _ignoredId, ...sanitizedAttributes } = attributes || {};
321
525
 
322
- // Extract relationship IDs from JSON:API relationships object
526
+ // Extract relationship IDs from JSON:API relationships object.
527
+ //
528
+ // `key` comes VERBATIM from the request body, so `id` is stripped here for
529
+ // exactly the same reason it is stripped from `attributes` on the line
530
+ // above -- and it must be, or GATE 0 is walked around by moving one field:
531
+ //
532
+ // POST /animals {"id":21, ...} -> 403 GATE 0 fires
533
+ // POST /animals {"relationships":{"id":{"data":{"id":21}}},
534
+ // "attributes":{"owner":"gina"}} -> 200 BYPASS
535
+ //
536
+ // Top-level `id` stayed `undefined`, so GATE 0 never fired and the
537
+ // collision lookup never ran; `createRecord` took its last-entry-wins
538
+ // branch, overwrote hidden record 21 in place and reset its `owner` to a
539
+ // value the caller chose -- de-hiding it permanently. That is #190 itself,
540
+ // on the create surface. Pinned by assertion 39.
541
+ //
542
+ // The `id` member of the resource object is now the ONLY channel a caller
543
+ // id can arrive on, which is what makes GATE 0's guarantee checkable
544
+ // rather than merely asserted. INHERITED from `dev`, which carries this
545
+ // loop verbatim; the general form -- the loop accepts any key, not just
546
+ // `id`, so a body key that is not a declared relationship is still
547
+ // mass-assigned -- is abofs/stonyx-orm#204.
323
548
  if (rels) {
324
549
  for (const [key, value] of Object.entries(rels)) {
550
+ if (key === 'id') continue;
551
+
325
552
  const relData = value?.data;
326
553
  if (relData && relData.id !== undefined) {
327
554
  (sanitizedAttributes as { [key: string]: unknown })[key] = relData.id;
@@ -330,16 +557,93 @@ export default class OrmRequest extends Request {
330
557
  }
331
558
 
332
559
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
560
+
561
+ // Slot count BEFORE the write. `createRecord` writes to the store before
562
+ // the predicate can run, and the rollback below must be able to prove the
563
+ // slot it removes is one THIS REQUEST created. Identity alone cannot
564
+ // prove it: when `assignRecordId` lands on an occupied id, `createRecord`
565
+ // mutates the existing OrmRecord IN PLACE, so `store.get(...) === record`
566
+ // is true for a record the request did not create. The map's size is the
567
+ // only O(1) signal that distinguishes an insert from an overwrite.
568
+ const slotsBefore = (store.get(model) as Map<string | number, unknown> | undefined)?.size ?? 0;
569
+
333
570
  const created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false, _skipAutoPersist: true });
334
571
  const record = isOrmRecord(created) ? created : null;
335
572
  if (!record) return 500;
336
573
 
574
+ const createdNewSlot = ((store.get(model) as Map<string | number, unknown> | undefined)?.size ?? 0) > slotsBefore;
575
+
576
+ // 403 here, NOT 404. The oracle argument does not apply to create: there
577
+ // is no pre-existing record whose existence could leak, the caller
578
+ // supplied the attributes, and 404 on a mounted collection route is
579
+ // indistinguishable from "model not mounted" -- a genuinely different
580
+ // failure a developer needs to diagnose.
581
+ //
582
+ // The rollback is not optional. createRecord writes to the store BEFORE
583
+ // the predicate can run, so returning 403 alone would leave the record
584
+ // behind: a worse bug than the bypass being fixed.
585
+ if (isDenied(filter, record)) {
586
+ // ROLL BACK BY IDENTITY, NEVER BY ID. `store.remove(model, record.id)`
587
+ // on its own is a write primitive keyed by a value the caller may have
588
+ // supplied: with the raw-id collision bypass above, a denied
589
+ // `POST {"id":"21"}` answered 403 and DELETED hidden record 21 -- an
590
+ // unauthenticated deletion primitive across the whole id space, created
591
+ // by adding a rollback to a lookup that could be skipped.
592
+ //
593
+ // Both conditions are required and neither implies the other:
594
+ // createdNewSlot -- the store grew, so this request inserted rather
595
+ // than overwrote. Guards `assignRecordId` picking an
596
+ // id that is already taken (it returns
597
+ // last-INSERTED + 1, not max + 1, so a store whose
598
+ // insertion order is not ascending collides) -- see
599
+ // abofs/stonyx-orm#203.
600
+ // identity -- the slot still holds the object we just created,
601
+ // so nothing between createRecord and here replaced
602
+ // it. Deleting this half SURVIVES the suite, and it
603
+ // is kept anyway. WHY IT IS REDUNDANT: there is no
604
+ // `await` anywhere between `slotsBefore` and
605
+ // `store.remove` -- the whole window is synchronous,
606
+ // so it is atomic under Node's event loop; before-
607
+ // `create` hooks run BEFORE the handler
608
+ // (`_withHooks` runs its hook loop ahead of
609
+ // `await handler(...)`), and a consumer predicate
610
+ // inside `isDenied` runs AFTER `createdNewSlot` is
611
+ // computed and cannot flip it. That is a property of
612
+ // THIS function, not of GATE 0 -- an earlier note
613
+ // credited GATE 0, which was both wrong (a caller id
614
+ // reached createRecord through the relationships
615
+ // loop, #204) and the wrong kind of reason: a guard
616
+ // justified on code sixty lines upstream gets
617
+ // silently re-armed when that code moves.
618
+ // SO IT BECOMES REACHABLE IF AN `await` IS
619
+ // INTRODUCED HERE, which is the change a future
620
+ // editor would actually make. See the
621
+ // guards-redundant-by-construction table in
622
+ // docs/project-structure.md.
623
+ if (createdNewSlot && store.get(model, record.id as string | number) === record) {
624
+ store.remove(model, record.id as string | number, { _skipAutoPersist: true });
625
+ }
626
+
627
+ return 403;
628
+ }
629
+
337
630
  return { data: record.toJSON?.({ fields: modelFields }) };
338
631
  };
339
632
 
340
- const updateHandler: HandlerFn = async ({ body, params }) => {
633
+ const updateHandler: HandlerFn = async ({ body, params }, { filter }) => {
341
634
  const found = await store.find(model, getId(params));
342
635
  if (!found || !isOrmRecord(found)) return 404;
636
+ // Checked BEFORE any attribute is applied. 404 rather than 403 for the
637
+ // same reason as GET /:id -- 403 would disclose both that the record
638
+ // exists and that this caller specifically is excluded.
639
+ //
640
+ // NOT redundant behind GATE 1, and not defence in depth either: GATE 1's
641
+ // verdict is computed BEFORE the before-hook loop runs, and a before-hook
642
+ // is a published extension point that can change the answer -- by
643
+ // mutating the record, or against a predicate that closes over
644
+ // per-request state. This is the only re-evaluation after that window.
645
+ // Pinned by assertion 32; deleting it turns a 404 into an applied update.
646
+ if (isDenied(filter, found)) return 404;
343
647
  const record = found;
344
648
  const { attributes, relationships: rels } = (body?.data || {}) as {
345
649
  attributes?: { [key: string]: unknown };
@@ -362,6 +666,19 @@ export default class OrmRequest extends Request {
362
666
  if (rels) {
363
667
  const relUpdates: { [key: string]: unknown } = {};
364
668
  for (const [key, value] of Object.entries(rels)) {
669
+ // The same missing key filter as createHandler's, and as the
670
+ // attribute loop directly above -- which already had it, while this
671
+ // loop did not. A PATCH carrying
672
+ // `relationships:{"id":{"data":{"id":9101}}}` reached `updateRecord`
673
+ // and RE-KEYED the record: the object held under store key 9102 then
674
+ // reported id 9101, so a visible record claimed a hidden record's
675
+ // identity on every surface that reads `record.id` rather than the map
676
+ // key. Gated by GATE 1 on the addressed record, so it is store
677
+ // corruption rather than a filter bypass -- but it is the same one-line
678
+ // omission two handlers apart. Pinned by assertion 40; INHERITED from
679
+ // `dev`; abofs/stonyx-orm#204.
680
+ if (key === 'id') continue;
681
+
365
682
  const relData = value?.data;
366
683
  if (relData && relData.id !== undefined) {
367
684
  relUpdates[key] = relData.id;
@@ -375,8 +692,23 @@ export default class OrmRequest extends Request {
375
692
  return { data: record.toJSON?.() };
376
693
  };
377
694
 
378
- const deleteHandler: HandlerFn = ({ params }) => {
379
- store.remove(model, getId(params));
695
+ const deleteHandler: HandlerFn = async ({ params }, { filter }) => {
696
+ const record = await store.find(model, getId(params));
697
+
698
+ // BEHAVIOUR CHANGE (#190): a DELETE of a record that never existed
699
+ // returned 204 before this change. It now returns 404, matching the
700
+ // denied case below. This is deliberate and load-bearing -- if a denied
701
+ // delete returned 404 while a missing one returned 204, the pair would be
702
+ // a perfect existence oracle and the whole fix would be worthless.
703
+ // Returning 204 for a denied delete was rejected instead: it falsely
704
+ // reports success for a request that changed nothing.
705
+ if (!record) return 404;
706
+ // Re-evaluated after the before-hook loop, exactly as in updateHandler --
707
+ // GATE 1 decided before the hooks ran. Pinned by assertion 33; deleting it
708
+ // turns a 404 into a destroyed record.
709
+ if (isDenied(filter, record)) return 404;
710
+
711
+ store.remove(model, getId(params), { _skipAutoPersist: true });
380
712
  return 204;
381
713
  };
382
714
 
@@ -405,9 +737,37 @@ export default class OrmRequest extends Request {
405
737
  }
406
738
  }
407
739
 
408
- // Wraps a handler with before/after hook execution
740
+ // Wraps a handler with before/after hook execution.
741
+ //
742
+ // ===========================================================================
743
+ // TWO AUTHORIZATION GATES, AND EVERY EXECUTOR SITS BEHIND ONE OF THEM (#190)
744
+ //
745
+ // The defect this function was fixed for is NOT "a delete persists past a
746
+ // 404". It is that _withHooks has SEVERAL executors downstream of the
747
+ // handler, and originally the handler's response gated none of them. Three
748
+ // exist today:
749
+ //
750
+ // 1. sqlDb.persist -- issues real SQL against the backing store
751
+ // 2. the after-hook pipeline -- the PUBLISHED consumer extension point;
752
+ // a cascade delete, a webhook, a search-index
753
+ // purge. `context.recordId` and
754
+ // `context.oldState` are populated for it.
755
+ // 3. Orm.db.save() -- a full serialize-and-write of the store
756
+ //
757
+ // Gating them one at a time is how this keeps regressing, so the rule is:
758
+ // compute denial ONCE at each point where it becomes knowable, and keep every
759
+ // executor downstream of a gate. If you add a fourth executor to this
760
+ // function, it goes below GATE 2 or it is a security bug.
761
+ //
762
+ // GATE 1 (pre-handler) is required because before-hooks and `context.oldState`
763
+ // run/are built BEFORE the handler can consult the filter. Without it a denied
764
+ // DELETE still handed the hidden record's full contents to consumer code.
765
+ // GATE 2 (post-handler) covers everything the handler's status can reach.
766
+ // ===========================================================================
409
767
  private _withHooks(operation: string, handler: HandlerFn): HandlerFn {
410
768
  return async (request: OrmRequest$, state: { [key: string]: unknown }) => {
769
+ const { filter } = (state || {}) as { filter?: unknown };
770
+
411
771
  // Build context object for hooks
412
772
  const context: HookContext = {
413
773
  model: this.model,
@@ -422,6 +782,27 @@ export default class OrmRequest extends Request {
422
782
  // Capture old state for operations that modify data
423
783
  if (operation === 'update' || operation === 'delete') {
424
784
  const existingRecord = await store.find(this.model, getId(request.params)) as OrmRecord | undefined;
785
+
786
+ // GATE 1 -- pre-handler. This record fetch already happened for
787
+ // oldState, so the check is free.
788
+ //
789
+ // Returning here rather than letting updateHandler/deleteHandler
790
+ // produce the same 404 is the point: everything between here and there
791
+ // is an executor the caller is not authorized to reach.
792
+ // - context.oldState is a deep copy of the HIDDEN RECORD'S CONTENTS.
793
+ // Building it and handing it to a before-hook discloses exactly what
794
+ // the filter exists to hide.
795
+ // - context.recordId is populated for delete BEFORE the handler runs,
796
+ // which is the same shape as the sqlDb landmine one layer up:
797
+ // `afterHook('delete', ctx => cascadeDelete(ctx.recordId))` destroys
798
+ // children behind a correct 404.
799
+ // - a before-hook may return a value and short-circuit, which would
800
+ // otherwise return a response without the filter ever executing.
801
+ //
802
+ // 404, not 403, for the same reason as getSingleHandler: the status for
803
+ // "exists but filtered out" must equal "does not exist".
804
+ if (existingRecord && isDenied(filter, existingRecord)) return 404;
805
+
425
806
  if (existingRecord) {
426
807
  // Deep copy the record's data to preserve old state
427
808
  context.oldState = JSON.parse(JSON.stringify(existingRecord.__data || existingRecord));
@@ -443,9 +824,33 @@ export default class OrmRequest extends Request {
443
824
  // Execute main handler
444
825
  const response = await handler(request, state);
445
826
 
446
- // Persist to SQL database for create/update (delete is handled by store.remove auto-persist)
827
+ // Set context.record for update BEFORE persist so SQL drivers can read it
828
+ if (operation === 'update' && (response as JsonApiResponse)?.data) {
829
+ context.record = store.get(this.model, getId(request.params));
830
+ }
831
+
832
+ // GATE 2 -- post-handler. A denied or failed handler returns a bare status
833
+ // integer, and no executor below may run for one.
834
+ //
835
+ // `>= 400` deliberately covers every failure status, not just the
836
+ // authorization ones: a 400 (POST with no `type`) and a 409 (duplicate id)
837
+ // are equally requests in which nothing happened, and a persist or a
838
+ // cascade hook for one of them is just as wrong.
839
+ const denied = Number.isInteger(response) && (response as number) >= 400;
840
+
841
+ // EXECUTOR 1 -- SQL persistence, for all write operations.
842
+ //
843
+ // `response` is passed to sqlDb.persist below, but it is dropped at the
844
+ // driver boundary: _persistDelete(modelName, context) never receives it
845
+ // and guards only on context.recordId -- which _withHooks set above,
846
+ // BEFORE the handler ran. Without this gate a correct 404 still issues
847
+ // DELETE FROM ... WHERE id = ? on every SQL backend.
848
+ //
849
+ // No file-backed test can observe that, because Orm.instance.sqlDb is
850
+ // null in file/directory mode. See the stubbed-sqlDb assertions in
851
+ // test/unit/access-filter-enforcement-test.ts.
447
852
  const sqlDb = Orm.instance.sqlDb;
448
- if (sqlDb && (operation === 'create' || operation === 'update')) {
853
+ if (sqlDb && WRITE_OPERATIONS.has(operation) && !denied) {
449
854
  await sqlDb.persist(operation, this.model, context, response);
450
855
  }
451
856
 
@@ -461,20 +866,35 @@ export default class OrmRequest extends Request {
461
866
  const responseData = (response as { data: { id: string | number } }).data;
462
867
  const recordId = isNaN(responseData.id as unknown as number) ? responseData.id : parseInt(responseData.id as string);
463
868
  context.record = store.get(this.model, recordId);
464
- } else if (operation === 'update' && (response as JsonApiResponse)?.data) {
465
- context.record = store.get(this.model, getId(request.params));
466
869
  } else if (operation === 'delete') {
467
870
  // For delete, the record may no longer exist, but we have oldState
468
871
  context.recordId = getId(request.params);
469
872
  }
470
873
 
471
- // Run after hooks sequentially
472
- for (const hook of getAfterHooks(operation, this.model)) {
473
- await hook(context);
874
+ // EXECUTOR 2 -- the after-hook pipeline. This is the published consumer
875
+ // extension point (`afterHook` is exported from @stonyx/orm and from
876
+ // ./hooks), so it is the executor with the widest possible blast radius:
877
+ // a cascade delete, a webhook, a token revocation, a search-index purge.
878
+ //
879
+ // BEHAVIOUR CHANGE (#190): after-hooks no longer fire for a request that
880
+ // failed. Previously `afterHook('delete', ...)` ran with a populated
881
+ // context.recordId on a 404, so a consumer cascade destroyed children for
882
+ // a request that deleted nothing. Firing a hook named "after<operation>"
883
+ // for an operation that did not occur is a booby trap, and the denied case
884
+ // is unreachable-before-#190 while the missing case is inherited debt --
885
+ // both are closed by the same gate. `context.response` therefore only ever
886
+ // carries a success status into a hook.
887
+ if (!denied) {
888
+ for (const hook of getAfterHooks(operation, this.model)) {
889
+ await hook(context);
890
+ }
474
891
  }
475
892
 
476
- // Auto-save DB after write operations when configured
477
- if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation)) {
893
+ // EXECUTOR 3 -- file/directory autosave. Ungated this let an
894
+ // unauthenticated caller force a full serialize-and-write of the entire
895
+ // store on every DELETE of any id, with no record touched: amplification
896
+ // rather than corruption, but the same root cause and the same fix.
897
+ if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation) && !denied) {
478
898
  await (Orm.db as { save(): Promise<void> }).save();
479
899
  }
480
900
 
@@ -494,9 +914,19 @@ export default class OrmRequest extends Request {
494
914
  const dasherizedName = camelCaseToKebabCase(relationshipName);
495
915
 
496
916
  // Related resource route: GET /:id/{relationship}
497
- routes[`/:id/${dasherizedName}`] = async (request: OrmRequest$) => {
917
+ //
918
+ // These generated routes are not wrapped by _withHooks, which is why they
919
+ // were the least obvious two of the seven unguarded surfaces in #190.
920
+ // They are still dispatched by @stonyx/rest-server as
921
+ // `handler(req, getState(req))`, so `state` -- and therefore the filter
922
+ // planted by auth() -- has always been available here; it was simply
923
+ // never declared or read.
924
+ routes[`/:id/${dasherizedName}`] = async (request: OrmRequest$, { filter }: { [key: string]: unknown } = {}) => {
498
925
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
499
926
  if (!record) return 404;
927
+ // Filtering the PARENT: a caller who may not see the record may not see
928
+ // what it is related to either.
929
+ if (isDenied(filter, record)) return 404;
500
930
 
501
931
  const relatedData = record.__relationships[relationshipName];
502
932
  const baseUrl = getBaseUrl(request);
@@ -518,9 +948,10 @@ export default class OrmRequest extends Request {
518
948
  };
519
949
 
520
950
  // Relationship linkage route: GET /:id/relationships/{relationship}
521
- routes[`/:id/relationships/${dasherizedName}`] = async (request: OrmRequest$) => {
951
+ routes[`/:id/relationships/${dasherizedName}`] = async (request: OrmRequest$, { filter }: { [key: string]: unknown } = {}) => {
522
952
  const record = await store.find(model, getId(request.params)) as OrmRecord | undefined;
523
953
  if (!record) return 404;
954
+ if (isDenied(filter, record)) return 404;
524
955
 
525
956
  const relatedData = record.__relationships[relationshipName];
526
957
  const baseUrl = getBaseUrl(request);
@@ -551,32 +982,60 @@ export default class OrmRequest extends Request {
551
982
  };
552
983
  }
553
984
 
554
- // Catch-all for invalid relationship names on related resource route
555
- routes[`/:id/:relationship`] = async (request: OrmRequest$) => {
556
- const record = await store.find(model, getId(request.params));
557
- if (!record) return 404;
558
-
559
- // If we reach here, relationship doesn't exist (valid ones were registered above)
560
- return 404;
561
- };
562
-
563
- // Catch-all for invalid relationship names on relationship linkage route
564
- routes[`/:id/relationships/:relationship`] = async (request: OrmRequest$) => {
565
- const record = await store.find(model, getId(request.params));
566
- if (!record) return 404;
567
-
568
- return 404;
569
- };
985
+ // Catch-alls for invalid relationship names. Every valid relationship was
986
+ // registered above, so reaching either of these means the relationship does
987
+ // not exist and the answer is 404 regardless of the record.
988
+ //
989
+ // These deliberately carry NO access check and no store lookup. An earlier
990
+ // revision of #190 added `if (isDenied(filter, record)) return 404` here for
991
+ // symmetry with the seven real surfaces, but both branches returned 404, so
992
+ // the guard was unobservable by construction -- a mutation deleting it
993
+ // survived the entire suite because no test that could distinguish it can
994
+ // exist. Unkillable code in an authorization diff reads as coverage and is
995
+ // not, so it is gone; skipping the lookup also removes the timing difference
996
+ // between an existing and a missing parent.
997
+ //
998
+ // IF THIS ROUTE EVER RETURNS ANYTHING OTHER THAN A CONSTANT 404, it becomes
999
+ // the eighth surface and must filter the parent first, exactly like
1000
+ // `/:id/{relationship}` above.
1001
+ routes[`/:id/:relationship`] = async () => 404;
1002
+ routes[`/:id/relationships/:relationship`] = async () => 404;
570
1003
 
571
1004
  return routes;
572
1005
  }
573
1006
 
574
1007
  auth(request: OrmRequest$, state: { [key: string]: unknown }): number | undefined {
575
- const access = this.access(request);
1008
+ // A consumer `access()` that throws is a DENIAL, matching `isDenied` one
1009
+ // layer down. Unguarded it propagates to express's default handler, which
1010
+ // answers 500 -- and the documented sample itself can throw
1011
+ // (`request.originalUrl.split(...)` when originalUrl is absent), so the
1012
+ // failure mode is reachable by following the docs.
1013
+ let access: AccessMethod;
1014
+ try {
1015
+ access = this.access(request);
1016
+ } catch {
1017
+ return 403; // Forbidden
1018
+ }
576
1019
 
577
1020
  if (!access) return 403;
578
- if (Array.isArray(access) && !access.includes(methodAccessMap[request.method])) return 403;
579
- if (typeof access === 'function') state.filter = access;
1021
+ if (typeof access === 'function') {
1022
+ state.filter = access;
1023
+ return undefined;
1024
+ }
1025
+ if (access === true) return undefined;
1026
+
1027
+ // `AccessMethod` declares `string` legal and it fell through every branch
1028
+ // above, returning undefined -- i.e. FULL CRUD, no filter. `return 'read'`
1029
+ // is the natural reading of a type that lists `string` first, and it
1030
+ // granted DELETE. A bare string is one permission, not a grant of all four.
1031
+ const permitted = typeof access === 'string' ? [access] : access;
1032
+
1033
+ // Anything that is not a permission array by this point -- an object, a
1034
+ // number, a Symbol -- is a consumer mistake, and the only safe reading of a
1035
+ // shape the contract does not define is a denial. Fail CLOSED.
1036
+ if (!Array.isArray(permitted)) return 403;
1037
+ if (!permitted.includes(methodAccessMap[request.method])) return 403;
1038
+
580
1039
  return undefined;
581
1040
  }
582
1041
  }