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

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 +395 -10
  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 +60 -0
  18. package/dist/orm-request.js +634 -47
  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 +644 -45
  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,9 +1,70 @@
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';
4
64
  import { getPluralName } from './plural-registry.js';
5
65
  import { getBeforeHooks, getAfterHooks } from './hooks.js';
6
66
  import config from 'stonyx/config';
67
+ import log from 'stonyx/log';
7
68
  import { isOrmRecord } from './utils.js';
8
69
  const methodAccessMap = {
9
70
  GET: 'read',
@@ -48,13 +109,107 @@ function getBaseUrl(request) {
48
109
  const host = request.get('host');
49
110
  return `${protocol}://${host}`;
50
111
  }
112
+ /**
113
+ * The ONE coercion from a caller-supplied id to the key the store holds it
114
+ * under. Every id-bearing surface in this file goes through it, and none has a
115
+ * copy: `getId()` (URL params), `normalizeBodyId()` (JSON body), and the
116
+ * post-create `context.record` lookup in `_withHooks`.
117
+ *
118
+ * WHY IT IS SHARED RATHER THAN DUPLICATED. `getId()` and `normalizeBodyId()`
119
+ * each had their own arithmetic, and they disagreed: `parseInt(id)` versus
120
+ * `parseInt(id, 10)`. On a hex-shaped id that is a two-record difference --
121
+ *
122
+ * GET /animals/0x2391 -> record 9105 (getId -> parseInt('0x2391') = 9105)
123
+ * POST /animals {"id":"0x2391"} -> lookup under 0 (normalize -> parseInt('0x2391',10) = 0)
124
+ * -> a MISS, so the duplicate check was skipped and
125
+ * createRecord OVERWROTE 9105 in place, answering 200
126
+ *
127
+ * -- a narrower form of the raw-versus-normalised divergence that the body-id
128
+ * normalisation was added to close, reintroduced by the fix for it. Two
129
+ * coercions that must agree cannot be kept in agreement by review; they have to
130
+ * be one function. Pinned by assertion 43.
131
+ *
132
+ * The third copy was found later and in a quieter place: `_withHooks` populated
133
+ * `context.record` for `create` with `isNaN(id) ? id : parseInt(id)` -- this
134
+ * function's body, inlined verbatim, feeding `store.get`. It was equivalent on
135
+ * every input reachable there, which is exactly what the two that DID diverge
136
+ * looked like until someone tried a hex id.
137
+ *
138
+ * `parseInt` and not `Number`, deliberately, and the anchor is NOT `getId`.
139
+ * It is `src/transforms.ts:7` -- `number: (value) => parseInt(value as string)`,
140
+ * also radix-less -- because that transform is what actually produces the store
141
+ * KEY a record is filed under. `getId` merely agrees with it. They differ from
142
+ * `Number` on `'1e3'` (1 vs 1000) and `'9105.5'` (9105 vs 9105.5), so switching
143
+ * this function to `Number` would make the lookup key disagree with the landing
144
+ * key on those shapes.
145
+ *
146
+ * THE CHANGE THAT WOULD BREAK THIS: adding a radix to `transforms.number`
147
+ * (`parseInt(value, 10)`). That is a one-word edit in a file with no connection
148
+ * to authorization, it would silently reopen the hex divergence in the other
149
+ * direction, and this comment would still read as correct. Assertion 45 pins
150
+ * the transform's radix-less shape directly, so that edit turns a test red
151
+ * rather than shipping.
152
+ *
153
+ * The reason `parseInt` is safe here is the `isNaN` gate in front of it:
154
+ * `parseInt('9105h')` is `9105`, and truncating a partially-valid id into a
155
+ * DIFFERENT VALID key is exactly how a collision lookup gets skipped. The gate
156
+ * rejects it as a string instead, so nothing is ever truncated. That gate, not
157
+ * the parser, is the load-bearing half -- assertion 43 pins it.
158
+ */
159
+ function coerceId(id) {
160
+ if (isNaN(id))
161
+ return id;
162
+ return parseInt(id);
163
+ }
51
164
  function getId(params) {
52
165
  const id = params.id;
53
166
  if (!id)
54
167
  return '';
55
- if (isNaN(id))
168
+ return coerceId(id);
169
+ }
170
+ /**
171
+ * Normalise a caller-supplied BODY id to the key the store will hold it under.
172
+ *
173
+ * `getId()` above is params-shaped: it takes `{ id?: string }` off the URL,
174
+ * where the value is always a string and a falsy one means "no id". A JSON body
175
+ * id is neither -- it can arrive as a number, and `0` is a legitimate id that
176
+ * `getId()` would flatten to `''`.
177
+ *
178
+ * WHY THIS EXISTS AT ALL. createHandler used to look the collision up with the
179
+ * RAW body value while every other surface normalised through `getId()`. The
180
+ * store is a Map keyed by the coerced value, so `store.find(model, '21')` misses
181
+ * the entry held under `21` and the duplicate check is skipped by typing the id
182
+ * as a string. On `dev` that silently overwrote the colliding record and
183
+ * answered 200; combined with the denied-create rollback added for #190 it
184
+ * became an unauthenticated DELETE of any id. Normalising here is half of that
185
+ * fix -- see the rollback in createHandler for the other half.
186
+ *
187
+ * It shares `coerceId` with `getId` so the two surfaces cannot drift apart
188
+ * again, and differs from `getId` in exactly ONE place, below.
189
+ */
190
+ function normalizeBodyId(id) {
191
+ // Non-strings pass through untouched: a JSON body id can arrive as a number,
192
+ // and `getId`'s falsy-flatten must NOT apply to it -- `0` is a legitimate id
193
+ // and `getId` would turn it into `''`. (assertion 30 sweeps id `0`.)
194
+ if (typeof id !== 'string')
56
195
  return id;
57
- return parseInt(id);
196
+ // THE ONE DIVERGENCE FROM `getId`, and it mirrors rather than contradicts it:
197
+ // `getId` maps a falsy param to `''`, and `''` is the only string a body can
198
+ // carry that means "no id" -- `createRecord` treats it as absent and assigns a
199
+ // server id. Coercing it instead would make it address a real slot, because
200
+ // `parseInt('')` is `NaN` and a record CAN be held under `NaN` (a truthy but
201
+ // non-numeric id such as `' '` survives `assignRecordId`'s falsy guard and
202
+ // then NaNs in the id transform). So `POST {"id":""}` would answer 409 against
203
+ // an unrelated record it never named. Pinned by assertion 44.
204
+ //
205
+ // Note what is deliberately NOT special-cased here any more: whitespace.
206
+ // `id.trim() === ''` used to short-circuit `' '` as well, which made the
207
+ // body surface DISAGREE with the URL surface -- `getId({id:' '})` is `NaN`,
208
+ // so `' '` addresses the NaN slot on every other route while the collision
209
+ // lookup missed it. Same class of bug as the hex divergence above.
210
+ if (id === '')
211
+ return id;
212
+ return coerceId(id);
58
213
  }
59
214
  function buildResponse(data, includeParam, recordOrRecords, options = {}) {
60
215
  const { links, baseUrl } = options;
@@ -187,6 +342,39 @@ function createFilterPredicate(filters) {
187
342
  return String(current) === value;
188
343
  });
189
344
  }
345
+ /**
346
+ * A function-style `access` return is a per-record predicate, and it is only
347
+ * meaningful if every surface that can hand a record to a caller consults it.
348
+ * Before #190 exactly one of seven did.
349
+ *
350
+ * Enforcement is deliberately post-fetch. `access` returns an opaque JS
351
+ * predicate and `store.findAll(model, conditions)` accepts only an equality
352
+ * conditions object that the SQL drivers translate to a WHERE clause, so
353
+ * query-layer enforcement would require a breaking change to the published
354
+ * `access` contract. That belongs in #197, not in a security patch. Six of the
355
+ * seven surfaces fetch by primary key anyway, so this costs exactly one row.
356
+ */
357
+ function isDenied(filter, record) {
358
+ if (typeof filter !== 'function')
359
+ return false;
360
+ // A predicate that throws is treated as a denial. Unguarded, a throw escapes
361
+ // to express's default handler, which answers 500 (with a stack trace outside
362
+ // NODE_ENV=production) while a missing id still answers 404 -- so a
363
+ // record-dependent throw re-separates "hidden" from "does not exist" and
364
+ // hands back the oracle this whole change exists to close.
365
+ try {
366
+ return !filter(record);
367
+ }
368
+ catch (error) {
369
+ // Denied, but not silently. A consumer predicate that throws on every
370
+ // record turns the whole collection into a 404 wall, and with no
371
+ // diagnostic that is indistinguishable from an empty database. `stonyx/log`
372
+ // is the module convention (see setup-rest-server.ts); optional-call
373
+ // because a consumer may not have configured the log types.
374
+ log.error?.(`[@stonyx/orm] access filter threw for model -- denying. ${error instanceof Error ? error.message : String(error)}`);
375
+ return true;
376
+ }
377
+ }
190
378
  export default class OrmRequest extends Request {
191
379
  model;
192
380
  access;
@@ -216,10 +404,15 @@ export default class OrmRequest extends Request {
216
404
  baseUrl
217
405
  });
218
406
  };
219
- const getSingleHandler = async (request) => {
407
+ const getSingleHandler = async (request, { filter }) => {
220
408
  const record = await store.find(model, getId(request.params));
221
409
  if (!record)
222
410
  return 404;
411
+ // 404, never 403: the status for "exists but filtered out" must be
412
+ // identical to "does not exist", or the fix trades an authorization
413
+ // bypass for a narrower existence oracle.
414
+ if (isDenied(filter, record))
415
+ return 404;
223
416
  const fieldsMap = parseFields(request.query);
224
417
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
225
418
  const baseUrl = getBaseUrl(request);
@@ -228,19 +421,118 @@ export default class OrmRequest extends Request {
228
421
  baseUrl
229
422
  });
230
423
  };
231
- const createHandler = async ({ body, query }) => {
424
+ const createHandler = async ({ body, query }, { filter }) => {
232
425
  const { type, id, attributes, relationships: rels } = (body?.data || {});
233
426
  if (!type)
234
427
  return 400; // Bad request
235
428
  const fieldsMap = parseFields(query);
236
429
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
237
- // Check for duplicate ID
238
- if (id !== undefined && await store.find(model, id))
239
- return 409; // Conflict
430
+ // GATE 0 -- the POST existence oracle.
431
+ //
432
+ // The duplicate check runs before the filter and `store.find` sees hidden
433
+ // records, so POST leaks existence through its STATUS. A previous revision
434
+ // filtered the collision status (403 when the colliding record is denied,
435
+ // 409 when it is visible) and that is NOT sufficient, because the status
436
+ // of a create is a third outcome. With a payload the caller is permitted
437
+ // to create -- the normative case for a per-tenant filter, and the case an
438
+ // attacker picks -- all three are distinguishable in ONE request per id:
439
+ //
440
+ // POST /animals {id:22, owner:'gina'} -> 403 a HIDDEN record has this id
441
+ // POST /animals {id:9500, owner:'gina'} -> 200 this id is FREE
442
+ // POST /animals {id:8, owner:'gina'} -> 409 a VISIBLE record has this id
443
+ //
444
+ // Filtering only the collision status narrows that to callers who cannot
445
+ // create a record they are allowed to see. It does not close it.
446
+ //
447
+ // It cannot be closed while a caller both chooses the id and learns
448
+ // whether the create succeeded: a successful create must answer
449
+ // differently from a refused one. So when a per-record filter is in force
450
+ // the caller does not get to choose the id at all. The refusal is
451
+ // UNCONDITIONAL and happens BEFORE any store lookup, so no status, and no
452
+ // lookup cost, can depend on whether that id exists. 403 -- the same
453
+ // status as a denied create -- so the two cannot be separated either.
454
+ //
455
+ // BOTH HALVES OF THAT ARE PINNED, because both were once asserted here and
456
+ // pinned by nothing:
457
+ //
458
+ // status -- assertion 22 sweeps payload x id x id-type, plus `null`.
459
+ // latency -- assertion 41 asserts NO `store.find` is issued on this
460
+ // path. Moving the refusal to after a lookup and returning
461
+ // the same 403 left the suite green while re-opening a
462
+ // hit-versus-miss timing difference on every id-bearing POST,
463
+ // which is what would turn #197 from a ~0.06ms post-fetch
464
+ // residual into a live timing oracle on create.
465
+ //
466
+ // AND THE GUARANTEE IS ONLY AS WIDE AS THE CHANNELS IT COVERS. It reads on
467
+ // the `id` member of the resource object, so it holds only while that is
468
+ // the ONLY way a caller id can reach `createRecord`. It was not: the
469
+ // relationships loop below re-admitted one under `key === "id"` and the
470
+ // gate never fired. Both strips -- `attributes.id` and `relationships.id`
471
+ // -- are therefore part of THIS gate, not tidiness, and assertion 39 pins
472
+ // them. Adding a third channel without a strip re-opens the oracle.
473
+ //
474
+ // Scoped to function-style `access` because that is exactly the population
475
+ // the oracle exists for: with no per-record filter there are no hidden
476
+ // records, and 409 discloses nothing GET /:id does not already.
477
+ //
478
+ // RESIDUALS, stated rather than implied.
479
+ //
480
+ // - a caller can still learn that a collection HAS a per-record filter
481
+ // (403 rather than 409/200 for an id-bearing POST). That discloses a
482
+ // configuration fact, not a record.
483
+ // - this gate is about ids arriving on THIS model's create route. It
484
+ // says nothing about a write to ANOTHER collection: a `POST /owners`
485
+ // carrying `relationships: {pets: {data: {id: 21}}}` -- or
486
+ // `attributes: {pets: [21, 22]}`, which never enters the
487
+ // relationships loop at all -- re-parents hidden animal 21 onto an
488
+ // owner the caller may write, which changes the very field the
489
+ // animals predicate reads and DE-HIDES it. Blocking that needs animal
490
+ // 21 checked against the ANIMAL model's predicate while servicing an
491
+ // OWNERS route, i.e. cross-model access resolution: abofs/stonyx-orm
492
+ // #207, blocked on #202 (`access` receives the model structurally)
493
+ // and #196 (setup-rest-server discards the model->predicate map at
494
+ // boot). NOT closed here, and no comment in this file may say it is.
495
+ //
496
+ // See README `### Known limitations`.
497
+ if (id !== undefined) {
498
+ if (typeof filter === 'function')
499
+ return 403; // Forbidden
500
+ // `normalizeBodyId`, not the raw value: a string-typed id misses the
501
+ // store's numeric key, which skipped this check entirely.
502
+ const existing = await store.find(model, normalizeBodyId(id));
503
+ if (existing)
504
+ return 409; // Conflict
505
+ }
240
506
  const { id: _ignoredId, ...sanitizedAttributes } = attributes || {};
241
- // Extract relationship IDs from JSON:API relationships object
507
+ // Extract relationship IDs from JSON:API relationships object.
508
+ //
509
+ // `key` comes VERBATIM from the request body, so `id` is stripped here for
510
+ // exactly the same reason it is stripped from `attributes` on the line
511
+ // above -- and it must be, or GATE 0 is walked around by moving one field:
512
+ //
513
+ // POST /animals {"id":21, ...} -> 403 GATE 0 fires
514
+ // POST /animals {"relationships":{"id":{"data":{"id":21}}},
515
+ // "attributes":{"owner":"gina"}} -> 200 BYPASS
516
+ //
517
+ // Top-level `id` stayed `undefined`, so GATE 0 never fired and the
518
+ // collision lookup never ran; `createRecord` took its last-entry-wins
519
+ // branch, overwrote hidden record 21 in place and reset its `owner` to a
520
+ // value the caller chose -- de-hiding it permanently. That is #190 itself,
521
+ // on the create surface. Pinned by assertion 39.
522
+ //
523
+ // The `id` member of the resource object is now the ONLY channel a caller
524
+ // id can arrive on FOR THIS MODEL'S OWN CREATE ROUTE, which is what makes
525
+ // GATE 0's guarantee checkable rather than merely asserted. It is not a
526
+ // statement about the record's reachability in general -- a relationship
527
+ // write on another collection reaches it without ever touching this
528
+ // handler (abofs/stonyx-orm#207). INHERITED from `dev`, which carries this
529
+ // loop verbatim; the general form -- the loop accepts any key, not just
530
+ // `id`, so a body key that is not a declared relationship is still
531
+ // mass-assigned -- is abofs/stonyx-orm#204.
242
532
  if (rels) {
243
533
  for (const [key, value] of Object.entries(rels)) {
534
+ if (key === 'id')
535
+ continue;
244
536
  const relData = value?.data;
245
537
  if (relData && relData.id !== undefined) {
246
538
  sanitizedAttributes[key] = relData.id;
@@ -248,16 +540,91 @@ export default class OrmRequest extends Request {
248
540
  }
249
541
  }
250
542
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
543
+ // Slot count BEFORE the write. `createRecord` writes to the store before
544
+ // the predicate can run, and the rollback below must be able to prove the
545
+ // slot it removes is one THIS REQUEST created. Identity alone cannot
546
+ // prove it: when `assignRecordId` lands on an occupied id, `createRecord`
547
+ // mutates the existing OrmRecord IN PLACE, so `store.get(...) === record`
548
+ // is true for a record the request did not create. The map's size is the
549
+ // only O(1) signal that distinguishes an insert from an overwrite.
550
+ const slotsBefore = store.get(model)?.size ?? 0;
251
551
  const created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
252
552
  const record = isOrmRecord(created) ? created : null;
253
553
  if (!record)
254
554
  return 500;
555
+ const createdNewSlot = (store.get(model)?.size ?? 0) > slotsBefore;
556
+ // 403 here, NOT 404. The oracle argument does not apply to create: there
557
+ // is no pre-existing record whose existence could leak, the caller
558
+ // supplied the attributes, and 404 on a mounted collection route is
559
+ // indistinguishable from "model not mounted" -- a genuinely different
560
+ // failure a developer needs to diagnose.
561
+ //
562
+ // The rollback is not optional. createRecord writes to the store BEFORE
563
+ // the predicate can run, so returning 403 alone would leave the record
564
+ // behind: a worse bug than the bypass being fixed.
565
+ if (isDenied(filter, record)) {
566
+ // ROLL BACK BY IDENTITY, NEVER BY ID. `store.remove(model, record.id)`
567
+ // on its own is a write primitive keyed by a value the caller may have
568
+ // supplied: with the raw-id collision bypass above, a denied
569
+ // `POST {"id":"21"}` answered 403 and DELETED hidden record 21 -- an
570
+ // unauthenticated deletion primitive across the whole id space, created
571
+ // by adding a rollback to a lookup that could be skipped.
572
+ //
573
+ // Both conditions are required and neither implies the other:
574
+ // createdNewSlot -- the store grew, so this request inserted rather
575
+ // than overwrote. Guards `assignRecordId` picking an
576
+ // id that is already taken (it returns
577
+ // last-INSERTED + 1, not max + 1, so a store whose
578
+ // insertion order is not ascending collides) -- see
579
+ // abofs/stonyx-orm#203.
580
+ // identity -- the slot still holds the object we just created,
581
+ // so nothing between createRecord and here replaced
582
+ // it. Deleting this half SURVIVES the suite, and it
583
+ // is kept anyway. WHY IT IS REDUNDANT: there is no
584
+ // `await` anywhere between `slotsBefore` and
585
+ // `store.remove` -- the whole window is synchronous,
586
+ // so it is atomic under Node's event loop; before-
587
+ // `create` hooks run BEFORE the handler
588
+ // (`_withHooks` runs its hook loop ahead of
589
+ // `await handler(...)`), and a consumer predicate
590
+ // inside `isDenied` runs AFTER `createdNewSlot` is
591
+ // computed and cannot flip it. That is a property of
592
+ // THIS function, not of GATE 0 -- an earlier note
593
+ // credited GATE 0, which was both wrong (a caller id
594
+ // reached createRecord through the relationships
595
+ // loop, #204) and the wrong kind of reason: a guard
596
+ // justified on code sixty lines upstream gets
597
+ // silently re-armed when that code moves.
598
+ // SO IT BECOMES REACHABLE IF AN `await` IS
599
+ // INTRODUCED HERE, which is the change a future
600
+ // editor would actually make. Stated here rather
601
+ // than by reference: `docs/` is not in `files`, so
602
+ // a pointer into it resolves to nothing for anyone
603
+ // who installed this package. README carries the
604
+ // consumer-facing half.
605
+ if (createdNewSlot && store.get(model, record.id) === record) {
606
+ store.remove(model, record.id, { _skipAutoPersist: true });
607
+ }
608
+ return 403;
609
+ }
255
610
  return { data: record.toJSON?.({ fields: modelFields }) };
256
611
  };
257
- const updateHandler = async ({ body, params }) => {
612
+ const updateHandler = async ({ body, params }, { filter }) => {
258
613
  const found = await store.find(model, getId(params));
259
614
  if (!found || !isOrmRecord(found))
260
615
  return 404;
616
+ // Checked BEFORE any attribute is applied. 404 rather than 403 for the
617
+ // same reason as GET /:id -- 403 would disclose both that the record
618
+ // exists and that this caller specifically is excluded.
619
+ //
620
+ // NOT redundant behind GATE 1, and not defence in depth either: GATE 1's
621
+ // verdict is computed BEFORE the before-hook loop runs, and a before-hook
622
+ // is a published extension point that can change the answer -- by
623
+ // mutating the record, or against a predicate that closes over
624
+ // per-request state. This is the only re-evaluation after that window.
625
+ // Pinned by assertion 32; deleting it turns a 404 into an applied update.
626
+ if (isDenied(filter, found))
627
+ return 404;
261
628
  const record = found;
262
629
  const { attributes, relationships: rels } = (body?.data || {});
263
630
  if (!attributes && !rels)
@@ -277,6 +644,19 @@ export default class OrmRequest extends Request {
277
644
  if (rels) {
278
645
  const relUpdates = {};
279
646
  for (const [key, value] of Object.entries(rels)) {
647
+ // The same missing key filter as createHandler's, and as the
648
+ // attribute loop directly above -- which already had it, while this
649
+ // loop did not. A PATCH carrying
650
+ // `relationships:{"id":{"data":{"id":9101}}}` reached `updateRecord`
651
+ // and RE-KEYED the record: the object held under store key 9102 then
652
+ // reported id 9101, so a visible record claimed a hidden record's
653
+ // identity on every surface that reads `record.id` rather than the map
654
+ // key. Gated by GATE 1 on the addressed record, so it is store
655
+ // corruption rather than a filter bypass -- but it is the same one-line
656
+ // omission two handlers apart. Pinned by assertion 40; INHERITED from
657
+ // `dev`; abofs/stonyx-orm#204.
658
+ if (key === 'id')
659
+ continue;
280
660
  const relData = value?.data;
281
661
  if (relData && relData.id !== undefined) {
282
662
  relUpdates[key] = relData.id;
@@ -288,8 +668,31 @@ export default class OrmRequest extends Request {
288
668
  }
289
669
  return { data: record.toJSON?.() };
290
670
  };
291
- const deleteHandler = ({ params }) => {
292
- store.remove(model, getId(params));
671
+ const deleteHandler = async ({ params }, { filter }) => {
672
+ // Coerced ONCE. `getId(params)` was evaluated twice here -- once to find
673
+ // the record and once to remove it -- and a coercion evaluated repeatedly
674
+ // is a coercion that can be edited in one place and not the other, which
675
+ // is the defect `coerceId` exists to prevent.
676
+ const recordId = getId(params);
677
+ const record = await store.find(model, recordId);
678
+ // BEHAVIOUR CHANGE (#190): a DELETE of a record that never existed
679
+ // returned 204 before this change. It now returns 404, matching the
680
+ // denied case below. This is deliberate and load-bearing -- if a denied
681
+ // delete returned 404 while a missing one returned 204, the pair would be
682
+ // a perfect existence oracle and the whole fix would be worthless.
683
+ // Returning 204 for a denied delete was rejected instead: it falsely
684
+ // reports success for a request that changed nothing.
685
+ if (!record)
686
+ return 404;
687
+ // Re-evaluated after the before-hook loop, exactly as in updateHandler --
688
+ // GATE 1 decided before the hooks ran. Pinned by assertion 33; deleting it
689
+ // turns a 404 into a destroyed record.
690
+ if (isDenied(filter, record))
691
+ return 404;
692
+ // Removed by the id of the record actually fetched, not by re-deriving it
693
+ // from the params a second time: the record the filter tested and the
694
+ // record removed are then provably the same one.
695
+ store.remove(model, record.id, { _skipAutoPersist: true });
293
696
  return 204;
294
697
  };
295
698
  // Wrap handlers with hooks
@@ -314,9 +717,63 @@ export default class OrmRequest extends Request {
314
717
  };
315
718
  }
316
719
  }
317
- // Wraps a handler with before/after hook execution
720
+ // Wraps a handler with before/after hook execution.
721
+ //
722
+ // ===========================================================================
723
+ // TWO AUTHORIZATION GATES, AND EVERY EXECUTOR SITS BEHIND ONE OF THEM (#190)
724
+ //
725
+ // The defect this function was fixed for is NOT "a delete persists past a
726
+ // 404". It is that _withHooks has SEVERAL executors downstream of the
727
+ // handler, and originally the handler's response gated none of them. Three
728
+ // exist today:
729
+ //
730
+ // 1. sqlDb.persist -- issues real SQL against the backing store
731
+ // 2. the after-hook pipeline -- the PUBLISHED consumer extension point;
732
+ // a cascade delete, a webhook, a search-index
733
+ // purge. `context.recordId` and
734
+ // `context.oldState` are populated for it.
735
+ // 3. Orm.db.save() -- a full serialize-and-write of the store
736
+ //
737
+ // Gating them one at a time is how this keeps regressing, so the rule is:
738
+ // compute denial ONCE at each point where it becomes knowable, and keep every
739
+ // executor downstream of a gate. If you add a fourth executor to this
740
+ // function, it goes below GATE 2 or it is a security bug.
741
+ //
742
+ // GATE 1 (pre-handler) is required because before-hooks and `context.oldState`
743
+ // run/are built BEFORE the handler can consult the filter. Without it a denied
744
+ // DELETE still handed the hidden record's full contents to consumer code.
745
+ // GATE 2 (post-handler) covers everything the handler's status can reach.
746
+ // ===========================================================================
318
747
  _withHooks(operation, handler) {
319
748
  return async (request, state) => {
749
+ // `|| {}` so this function behaves like the relationship routes below,
750
+ // which declare `state` with a `= {}` default. It is unkillable through
751
+ // the rest-server dispatcher, which always passes `getState(req)`; it is
752
+ // listed as such in the guards-redundant-by-construction table rather
753
+ // than left silently unkillable, and it defends the WHOLE function (the
754
+ // context, the snapshot and the handler call all read `callState`) rather
755
+ // than one destructure that the next line would throw past anyway.
756
+ const callState = (state || {});
757
+ // ---------------------------------------------------------------------
758
+ // THE AUTHORIZATION SNAPSHOT. Read ONCE, here, before any consumer code
759
+ // can run.
760
+ //
761
+ // `callState` is the object `auth()` planted the filter in, and it is
762
+ // also handed to every before-hook as `context.state` -- a published,
763
+ // WRITABLE extension point. So `state.filter` is an INPUT to the
764
+ // authorization decision, not only an output channel, and re-reading it
765
+ // after the hook loop lets a consumer hook disarm the filter:
766
+ //
767
+ // beforeHook('get', 'animal', ctx => { delete ctx.state.filter })
768
+ // -> GET /animals/21 turned 404 into 200
769
+ // -> GET /animals turned 20 records into 22
770
+ //
771
+ // GATE 1 already used this snapshot, so writes held; the READ handlers
772
+ // re-destructured `filter` from the live bag and did not. Everything
773
+ // downstream now reads `filter` from here, and the handler is handed
774
+ // `handlerState` below -- never `callState`.
775
+ // ---------------------------------------------------------------------
776
+ const { filter } = callState;
320
777
  // Build context object for hooks
321
778
  const context = {
322
779
  model: this.model,
@@ -325,11 +782,35 @@ export default class OrmRequest extends Request {
325
782
  params: request.params,
326
783
  body: request.body,
327
784
  query: request.query,
328
- state,
785
+ // Deliberately the LIVE object: `redirect` and `pipe` are read back off
786
+ // it by @stonyx/rest-server after the handler returns, so hooks must be
787
+ // able to write to it. What must not happen is the authorization
788
+ // decision reading it back, which is what the snapshot above prevents.
789
+ state: callState,
329
790
  };
330
791
  // Capture old state for operations that modify data
331
792
  if (operation === 'update' || operation === 'delete') {
332
793
  const existingRecord = await store.find(this.model, getId(request.params));
794
+ // GATE 1 -- pre-handler. This record fetch already happened for
795
+ // oldState, so the check is free.
796
+ //
797
+ // Returning here rather than letting updateHandler/deleteHandler
798
+ // produce the same 404 is the point: everything between here and there
799
+ // is an executor the caller is not authorized to reach.
800
+ // - context.oldState is a deep copy of the HIDDEN RECORD'S CONTENTS.
801
+ // Building it and handing it to a before-hook discloses exactly what
802
+ // the filter exists to hide.
803
+ // - context.recordId is populated for delete BEFORE the handler runs,
804
+ // which is the same shape as the sqlDb landmine one layer up:
805
+ // `afterHook('delete', ctx => cascadeDelete(ctx.recordId))` destroys
806
+ // children behind a correct 404.
807
+ // - a before-hook may return a value and short-circuit, which would
808
+ // otherwise return a response without the filter ever executing.
809
+ //
810
+ // 404, not 403, for the same reason as getSingleHandler: the status for
811
+ // "exists but filtered out" must equal "does not exist".
812
+ if (existingRecord && isDenied(filter, existingRecord))
813
+ return 404;
333
814
  if (existingRecord) {
334
815
  // Deep copy the record's data to preserve old state
335
816
  context.oldState = JSON.parse(JSON.stringify(existingRecord.__data || existingRecord));
@@ -347,10 +828,49 @@ export default class OrmRequest extends Request {
347
828
  }
348
829
  }
349
830
  // Execute main handler
350
- const response = await handler(request, state);
351
- // Persist to SQL database for create/update (delete is handled by store.remove auto-persist)
831
+ // The handler receives the SNAPSHOT, never the live bag. `filter` is
832
+ // assigned LAST so it wins over anything a before-hook wrote to
833
+ // `callState.filter` -- including a `delete`, which the spread would
834
+ // otherwise carry through as an absent key. Every other key a hook adds
835
+ // is still visible to the handler; only the authorization input is
836
+ // pinned.
837
+ const handlerState = { ...callState, filter };
838
+ const response = await handler(request, handlerState);
839
+ // Set context.record for update BEFORE persist so SQL drivers can read it
840
+ if (operation === 'update' && response?.data) {
841
+ context.record = store.get(this.model, getId(request.params));
842
+ }
843
+ // GATE 2 -- post-handler. A denied or failed handler returns a bare status
844
+ // integer, and no executor below may run for one.
845
+ //
846
+ // `>= 400` deliberately covers every failure status, not just the
847
+ // authorization ones: a 400 (POST with no `type`) and a 409 (duplicate id)
848
+ // are equally requests in which nothing happened, and a persist or a
849
+ // cascade hook for one of them is just as wrong.
850
+ // `Number.isInteger` is a TYPE guard, not a behaviour guard, and it is
851
+ // unkillable TODAY: the only non-integer a handler in this file can
852
+ // return is a `{ data, links }` object, and `{} >= 400` is `false` by JS
853
+ // coercion, so dropping it changes no reachable outcome. It is kept
854
+ // because `>=` coerces rather than rejects, and the shapes it coerces
855
+ // are not obvious -- `[500] >= 400` is TRUE, so a handler that one day
856
+ // returned an array would have every response read as a denial. Listed
857
+ // as an equivalent mutant rather than left to read as coverage; it
858
+ // becomes killable the moment a handler returns anything array-like or
859
+ // numeric-string-like.
860
+ const denied = Number.isInteger(response) && response >= 400;
861
+ // EXECUTOR 1 -- SQL persistence, for all write operations.
862
+ //
863
+ // `response` is passed to sqlDb.persist below, but it is dropped at the
864
+ // driver boundary: _persistDelete(modelName, context) never receives it
865
+ // and guards only on context.recordId -- which _withHooks set above,
866
+ // BEFORE the handler ran. Without this gate a correct 404 still issues
867
+ // DELETE FROM ... WHERE id = ? on every SQL backend.
868
+ //
869
+ // No file-backed test can observe that, because Orm.instance.sqlDb is
870
+ // null in file/directory mode. See the stubbed-sqlDb assertions in
871
+ // test/unit/access-filter-enforcement-test.ts.
352
872
  const sqlDb = Orm.instance.sqlDb;
353
- if (sqlDb && (operation === 'create' || operation === 'update')) {
873
+ if (sqlDb && WRITE_OPERATIONS.has(operation) && !denied) {
354
874
  await sqlDb.persist(operation, this.model, context, response);
355
875
  }
356
876
  // Add response and relevant records to context
@@ -364,22 +884,44 @@ export default class OrmRequest extends Request {
364
884
  else if (operation === 'create' && response?.data && (response.data.id)) {
365
885
  // For create, get the record from store using the ID from the response
366
886
  const responseData = response.data;
367
- const recordId = isNaN(responseData.id) ? responseData.id : parseInt(responseData.id);
368
- context.record = store.get(this.model, recordId);
369
- }
370
- else if (operation === 'update' && response?.data) {
371
- context.record = store.get(this.model, getId(request.params));
887
+ // `normalizeBodyId`, not a copy of its body. This line WAS
888
+ // `isNaN(id) ? id : parseInt(id)` -- `coerceId` inlined verbatim, a
889
+ // third coercion feeding a store lookup, sitting under a docblock that
890
+ // said neither surface had a copy. Equivalent on every input that can
891
+ // reach this truthy-guarded branch (`21`->`21`, `'21'`->`21`,
892
+ // `'angela'`->`'angela'`; `''` and `0` cannot reach it), so this is a
893
+ // de-duplication rather than a behaviour change -- and that is the
894
+ // point: the two that disagreed were equivalent on every input anyone
895
+ // checked, too.
896
+ context.record = store.get(this.model, normalizeBodyId(responseData.id));
372
897
  }
373
898
  else if (operation === 'delete') {
374
899
  // For delete, the record may no longer exist, but we have oldState
375
900
  context.recordId = getId(request.params);
376
901
  }
377
- // Run after hooks sequentially
378
- for (const hook of getAfterHooks(operation, this.model)) {
379
- await hook(context);
902
+ // EXECUTOR 2 -- the after-hook pipeline. This is the published consumer
903
+ // extension point (`afterHook` is exported from @stonyx/orm and from
904
+ // ./hooks), so it is the executor with the widest possible blast radius:
905
+ // a cascade delete, a webhook, a token revocation, a search-index purge.
906
+ //
907
+ // BEHAVIOUR CHANGE (#190): after-hooks no longer fire for a request that
908
+ // failed. Previously `afterHook('delete', ...)` ran with a populated
909
+ // context.recordId on a 404, so a consumer cascade destroyed children for
910
+ // a request that deleted nothing. Firing a hook named "after<operation>"
911
+ // for an operation that did not occur is a booby trap, and the denied case
912
+ // is unreachable-before-#190 while the missing case is inherited debt --
913
+ // both are closed by the same gate. `context.response` therefore only ever
914
+ // carries a success status into a hook.
915
+ if (!denied) {
916
+ for (const hook of getAfterHooks(operation, this.model)) {
917
+ await hook(context);
918
+ }
380
919
  }
381
- // Auto-save DB after write operations when configured
382
- if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation)) {
920
+ // EXECUTOR 3 -- file/directory autosave. Ungated this let an
921
+ // unauthenticated caller force a full serialize-and-write of the entire
922
+ // store on every DELETE of any id, with no record touched: amplification
923
+ // rather than corruption, but the same root cause and the same fix.
924
+ if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation) && !denied) {
383
925
  await Orm.db.save();
384
926
  }
385
927
  return response;
@@ -391,10 +933,21 @@ export default class OrmRequest extends Request {
391
933
  // Dasherize the relationship name for URL paths (e.g., accessLinks -> access-links)
392
934
  const dasherizedName = camelCaseToKebabCase(relationshipName);
393
935
  // Related resource route: GET /:id/{relationship}
394
- routes[`/:id/${dasherizedName}`] = async (request) => {
936
+ //
937
+ // These generated routes are not wrapped by _withHooks, which is why they
938
+ // were the least obvious two of the seven unguarded surfaces in #190.
939
+ // They are still dispatched by @stonyx/rest-server as
940
+ // `handler(req, getState(req))`, so `state` -- and therefore the filter
941
+ // planted by auth() -- has always been available here; it was simply
942
+ // never declared or read.
943
+ routes[`/:id/${dasherizedName}`] = async (request, { filter } = {}) => {
395
944
  const record = await store.find(model, getId(request.params));
396
945
  if (!record)
397
946
  return 404;
947
+ // Filtering the PARENT: a caller who may not see the record may not see
948
+ // what it is related to either.
949
+ if (isDenied(filter, record))
950
+ return 404;
398
951
  const relatedData = record.__relationships[relationshipName];
399
952
  const baseUrl = getBaseUrl(request);
400
953
  let data;
@@ -413,10 +966,12 @@ export default class OrmRequest extends Request {
413
966
  };
414
967
  };
415
968
  // Relationship linkage route: GET /:id/relationships/{relationship}
416
- routes[`/:id/relationships/${dasherizedName}`] = async (request) => {
969
+ routes[`/:id/relationships/${dasherizedName}`] = async (request, { filter } = {}) => {
417
970
  const record = await store.find(model, getId(request.params));
418
971
  if (!record)
419
972
  return 404;
973
+ if (isDenied(filter, record))
974
+ return 404;
420
975
  const relatedData = record.__relationships[relationshipName];
421
976
  const baseUrl = getBaseUrl(request);
422
977
  let data;
@@ -445,31 +1000,63 @@ export default class OrmRequest extends Request {
445
1000
  };
446
1001
  };
447
1002
  }
448
- // Catch-all for invalid relationship names on related resource route
449
- routes[`/:id/:relationship`] = async (request) => {
450
- const record = await store.find(model, getId(request.params));
451
- if (!record)
452
- return 404;
453
- // If we reach here, relationship doesn't exist (valid ones were registered above)
454
- return 404;
455
- };
456
- // Catch-all for invalid relationship names on relationship linkage route
457
- routes[`/:id/relationships/:relationship`] = async (request) => {
458
- const record = await store.find(model, getId(request.params));
459
- if (!record)
460
- return 404;
461
- return 404;
462
- };
1003
+ // Catch-alls for invalid relationship names. Every valid relationship was
1004
+ // registered above, so reaching either of these means the relationship does
1005
+ // not exist and the answer is 404 regardless of the record.
1006
+ //
1007
+ // These deliberately carry NO access check and no store lookup. An earlier
1008
+ // revision of #190 added `if (isDenied(filter, record)) return 404` here for
1009
+ // symmetry with the seven real surfaces, but both branches returned 404, so
1010
+ // the guard was unobservable by construction -- a mutation deleting it
1011
+ // survived the entire suite because no test that could distinguish it can
1012
+ // exist. Unkillable code in an authorization diff reads as coverage and is
1013
+ // not, so it is gone; skipping the lookup also removes the timing difference
1014
+ // between an existing and a missing parent.
1015
+ //
1016
+ // IF THIS ROUTE EVER RETURNS ANYTHING OTHER THAN A CONSTANT 404, it becomes
1017
+ // the eighth surface and must filter the parent first, exactly like
1018
+ // `/:id/{relationship}` above.
1019
+ routes[`/:id/:relationship`] = async () => 404;
1020
+ routes[`/:id/relationships/:relationship`] = async () => 404;
463
1021
  return routes;
464
1022
  }
465
1023
  auth(request, state) {
466
- const access = this.access(request);
1024
+ // A consumer `access()` that throws is a DENIAL, matching `isDenied` one
1025
+ // layer down. Unguarded it propagates to express's default handler, which
1026
+ // answers 500 -- and the documented sample itself can throw
1027
+ // (`request.originalUrl.split(...)` when originalUrl is absent), so the
1028
+ // failure mode is reachable by following the docs.
1029
+ let access;
1030
+ try {
1031
+ access = this.access(request);
1032
+ }
1033
+ catch (error) {
1034
+ // Same reasoning as `isDenied`: fail closed, but say so. An `access()`
1035
+ // that throws denies EVERY request to the collection, and a silent 403
1036
+ // wall is the hardest possible thing to diagnose from the outside.
1037
+ log.error?.(`[@stonyx/orm] access() threw for model "${this.model}" -- denying. ${error instanceof Error ? error.message : String(error)}`);
1038
+ return 403; // Forbidden
1039
+ }
467
1040
  if (!access)
468
1041
  return 403;
469
- if (Array.isArray(access) && !access.includes(methodAccessMap[request.method]))
470
- return 403;
471
- if (typeof access === 'function')
1042
+ if (typeof access === 'function') {
472
1043
  state.filter = access;
1044
+ return undefined;
1045
+ }
1046
+ if (access === true)
1047
+ return undefined;
1048
+ // `AccessMethod` declares `string` legal and it fell through every branch
1049
+ // above, returning undefined -- i.e. FULL CRUD, no filter. `return 'read'`
1050
+ // is the natural reading of a type that lists `string` first, and it
1051
+ // granted DELETE. A bare string is one permission, not a grant of all four.
1052
+ const permitted = typeof access === 'string' ? [access] : access;
1053
+ // Anything that is not a permission array by this point -- an object, a
1054
+ // number, a Symbol -- is a consumer mistake, and the only safe reading of a
1055
+ // shape the contract does not define is a denial. Fail CLOSED.
1056
+ if (!Array.isArray(permitted))
1057
+ return 403;
1058
+ if (!permitted.includes(methodAccessMap[request.method]))
1059
+ return 403;
473
1060
  return undefined;
474
1061
  }
475
1062
  }