@stonyx/orm 0.3.2-alpha.7 → 0.3.2-alpha.71

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 (63) hide show
  1. package/README.md +968 -11
  2. package/config/environment.js +8 -0
  3. package/dist/access-verdict.d.ts +59 -0
  4. package/dist/access-verdict.js +222 -0
  5. package/dist/commands.js +34 -0
  6. package/dist/dynamodb/connection.d.ts +31 -0
  7. package/dist/dynamodb/connection.js +28 -0
  8. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  9. package/dist/dynamodb/dynamodb-db.js +596 -0
  10. package/dist/dynamodb/operation-builder.d.ts +76 -0
  11. package/dist/dynamodb/operation-builder.js +116 -0
  12. package/dist/dynamodb/type-map.d.ts +31 -0
  13. package/dist/dynamodb/type-map.js +48 -0
  14. package/dist/index.d.ts +3 -0
  15. package/dist/index.js +8 -0
  16. package/dist/main.d.ts +116 -0
  17. package/dist/main.js +129 -0
  18. package/dist/manage-record.js +268 -12
  19. package/dist/mysql/connection.d.ts +1 -0
  20. package/dist/mysql/mysql-db.d.ts +8 -0
  21. package/dist/mysql/mysql-db.js +44 -10
  22. package/dist/orm-request.d.ts +216 -3
  23. package/dist/orm-request.js +924 -55
  24. package/dist/postgres/connection.d.ts +1 -0
  25. package/dist/postgres/connection.js +8 -6
  26. package/dist/postgres/postgres-db.d.ts +8 -0
  27. package/dist/postgres/postgres-db.js +44 -10
  28. package/dist/record.d.ts +16 -0
  29. package/dist/record.js +62 -6
  30. package/dist/relationships.js +1 -1
  31. package/dist/serializer.js +38 -2
  32. package/dist/setup-rest-server.js +51 -5
  33. package/dist/standalone-db.js +17 -5
  34. package/dist/store.d.ts +13 -1
  35. package/dist/store.js +65 -6
  36. package/dist/types/orm-types.d.ts +139 -0
  37. package/dist/utils.d.ts +44 -0
  38. package/dist/utils.js +47 -0
  39. package/package.json +16 -7
  40. package/src/access-verdict.ts +248 -0
  41. package/src/commands.ts +43 -0
  42. package/src/dynamodb/connection.ts +50 -0
  43. package/src/dynamodb/dynamodb-db.ts +811 -0
  44. package/src/dynamodb/operation-builder.ts +202 -0
  45. package/src/dynamodb/type-map.ts +54 -0
  46. package/src/index.ts +10 -0
  47. package/src/main.ts +133 -0
  48. package/src/manage-record.ts +294 -18
  49. package/src/mysql/connection.ts +1 -0
  50. package/src/mysql/mysql-db.ts +44 -12
  51. package/src/orm-request.ts +944 -56
  52. package/src/postgres/connection.ts +10 -6
  53. package/src/postgres/postgres-db.ts +44 -12
  54. package/src/record.ts +82 -6
  55. package/src/relationships.ts +1 -1
  56. package/src/serializer.ts +39 -2
  57. package/src/setup-rest-server.ts +59 -6
  58. package/src/standalone-db.ts +17 -6
  59. package/src/store.ts +68 -6
  60. package/src/types/orm-types.ts +146 -1
  61. package/src/types/stonyx-rest-server.d.ts +14 -1
  62. package/src/types/stonyx.d.ts +7 -1
  63. package/src/utils.ts +50 -0
@@ -1,10 +1,225 @@
1
+ /**
2
+ * REST request handling and access enforcement for @stonyx/orm.
3
+ *
4
+ * ---------------------------------------------------------------------------
5
+ * THE `access()` CONTRACT: `access(request, { model, operation })`
6
+ * ---------------------------------------------------------------------------
7
+ * `auth()` calls your predicate with TWO arguments. The second is the access
8
+ * CONTEXT -- the structural facts about the request, which the framework
9
+ * already holds and which you should read INSTEAD of parsing anything:
10
+ *
11
+ * context.model The model this route was mounted for, as a model name:
12
+ * kebab-case, exactly as declared under
13
+ * `config.orm.paths.model` and keyed in the store --
14
+ * `'owner'`, `'animal'`, `'phone-number'`. NOT the
15
+ * pluralised, dasherized, mount-prefixed ROUTE name. It is
16
+ * read from the OrmRequest instance, fixed at mount time,
17
+ * and no request can influence it.
18
+ *
19
+ * context.operation The operation being authorised. Exactly one of the four
20
+ * verbs `'read'`, `'create'`, `'update'`, `'delete'` --
21
+ * no second vocabulary ON THIS PATH, and never an HTTP
22
+ * method name like `'GET'`. These are the same four
23
+ * strings the permission-array return shape is written in
24
+ * (`['read', 'create']`), because both come from the one
25
+ * `methodAccessMap` below.
26
+ *
27
+ * NOT the hook vocabulary. `HookContext.operation`
28
+ * (`src/hooks.ts`, documented under "Hook Context Object"
29
+ * in the README) carries `'list' | 'get' | 'create' |
30
+ * 'update' | 'delete'` on an identically-named key of an
31
+ * identically-shaped context object, and the access
32
+ * vocabulary collapses `list` and `get` into `'read'`. For
33
+ * one `GET /animals/1` a hook sees `'get'` and `access()`
34
+ * sees `'read'`, so a predicate cannot tell a collection
35
+ * read from a record read. `AccessOperation` makes
36
+ * `operation === 'get'` a compile error for a TypeScript
37
+ * consumer, because a predicate that stops matching falls
38
+ * through to the permission array -- the misreading is
39
+ * fail-open shaped.
40
+ *
41
+ * `undefined` when the dispatched method has no entry in
42
+ * that map. Express delivers `HEAD` to the `GET` handler,
43
+ * so this is reachable. It is left undefined rather than
44
+ * defaulted on purpose -- a fabricated `'read'` would turn
45
+ * an unclassified request into an authorised one. Treat
46
+ * `undefined` as "not classified" and deny.
47
+ *
48
+ * So a consumer writes `if (model === 'owner' && operation === 'read')`. There
49
+ * is no string to parse, no variant to miss, and no way to fail open through a
50
+ * URL shape nobody anticipated.
51
+ *
52
+ * WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
53
+ * the verb, not the route. Measured over the live router, six surfaces produce
54
+ * one identical context:
55
+ *
56
+ * GET /owners { model: 'owner', operation: 'read' }
57
+ * GET /owners/gina { model: 'owner', operation: 'read' }
58
+ * GET /owners/gina/pets { model: 'owner', operation: 'read' }
59
+ * GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
60
+ * GET /owners/archived { model: 'owner', operation: 'read' }
61
+ * GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
62
+ *
63
+ * So a rule that depends on the SUB-PATH still needs `request.path` -- which is
64
+ * mount-relative and query-free, and is the one read of argument one the
65
+ * warning below sanctions. This repo's own fixture has such a rule: its
66
+ * `/archived` deny cannot be expressed from the context alone, and a predicate
67
+ * migrated to context-only would silently drop it, turning a deny into an
68
+ * allow. The related-resource and `?include=` surfaces serve ANOTHER model's
69
+ * records under `model: 'owner'`, and the context gives no signal of that
70
+ * (abofs/stonyx-orm#196).
71
+ *
72
+ * `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
73
+ * matching but BEFORE any handler executes (`@stonyx/rest-server`
74
+ * `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
75
+ * record would force a pre-fetch on every request, a second store hit and an
76
+ * ordering change in the middle of an authorization path. It is also
77
+ * unnecessary: the FUNCTION return shape already is the per-record hook. Return
78
+ * `(record) => boolean` and the handlers apply it to every record the request
79
+ * touches. Auth-time and record-time are separate decision points.
80
+ *
81
+ * THE SECOND ARGUMENT IS ADDITIVE. JavaScript ignores extra arguments, so an
82
+ * existing `access(request)` predicate keeps working exactly as before. The
83
+ * warning immediately below is therefore still live: `request` is still
84
+ * argument ONE, and reading it is still how predicates fail open.
85
+ *
86
+ * To reach ANOTHER model's predicate -- e.g. to check an animal while servicing
87
+ * an owners route -- use the boot-time registry:
88
+ *
89
+ * const predicate = Orm.instance.getAccess('animal');
90
+ * if (!predicate) return deny;
91
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
92
+ *
93
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
94
+ * includes the case where the model has an access class that failed to load,
95
+ * because `setup-rest-server.ts` catches a load failure, warns, and publishes
96
+ * whatever partial map it had. It does NOT mean the model is unrestricted.
97
+ * Treat it as DENY, the same way `operation === undefined` is treated above.
98
+ *
99
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
100
+ * the answer model-correct on its own -- the resolved predicate has to READ it.
101
+ * Measured against an ARITY-1 predicate, on a request express dispatched to
102
+ * `GET /owners/angela`, asked about ANIMALS:
103
+ *
104
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
105
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
106
+ *
107
+ * That is the OWNERS filter, and it returns `true` for animal 21 -- the record
108
+ * hidden on every animal surface. Under a mount that predicate recognises
109
+ * neither way it is worse: it falls through to
110
+ * `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
111
+ * context was supplied and the answer is not the animal answer, and it is wrong
112
+ * in the GRANTING direction, because that predicate is arity-1 and identifies
113
+ * its collection from the request. (Asserted on a live dispatch by AC9 in
114
+ * test/integration/orm-test.ts, against a deliberately arity-1 predicate.)
115
+ *
116
+ * This repo's own sample access class has since been MIGRATED to read the
117
+ * context (abofs/stonyx-orm#222), so `getAccess('animal')` here now answers
118
+ * with the animal filter. That is not true of a consumer tree: an arity-1
119
+ * predicate keeps working -- the second argument is additive -- and the caller
120
+ * has no supported way to tell which kind it got. The boot-time arity warning
121
+ * that surfaces one is abofs/stonyx-orm#221.
122
+ * So: pass the context, and do not treat a resolved predicate's answer as
123
+ * model-specific until that predicate has been migrated to read the context.
124
+ *
125
+ * ---------------------------------------------------------------------------
126
+ * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
127
+ * ---------------------------------------------------------------------------
128
+ * You do not have to. `auth()` below hands your predicate the ACCESS CONTEXT as
129
+ * argument two, and `context.model` already names the collection -- see the
130
+ * contract section above. Argument ONE is still the raw transport artifact, and
131
+ * everything from here to the end of this banner is the record of what happened
132
+ * when predicates worked the collection out from it. IT IS HISTORY, NOT
133
+ * GUIDANCE: do not write any of it into a new predicate. Every attempt to
134
+ * identify the collection by parsing the request target has failed OPEN. Five
135
+ * distinct variants of the same three-line example have now been found, each
136
+ * after the previous was fixed, by five different people:
137
+ *
138
+ * 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
139
+ * prefix match against it is ALWAYS false.
140
+ * 2. `request.originalUrl` carries the query string, so an anchored equality
141
+ * check misses `/owners?filter[age]=30`.
142
+ * 3. The router is a bare `express()` (`caseSensitive: false`) while a
143
+ * hand-written matcher is case-SENSITIVE, so `GET /OwNeRs/angela` walks
144
+ * past it. Router-side: abofs/stonyx-rest-server#47.
145
+ * 4. Under a configured `ORM_REST_ROUTE` a hard-coded `/owners` matches
146
+ * nothing -- environment-specifically, which is worse.
147
+ * 5. HTTP/1.1 permits an ABSOLUTE-FORM request-target. Express routes on
148
+ * `parseurl(req).pathname`, but `originalUrl` is the raw target, so
149
+ * `GET http://anything.example/owners/angela` reaches the handler with
150
+ * `originalUrl === 'http://anything.example/owners/angela'`. A `/owners`
151
+ * prefix match is false, `access()` falls through to whatever it returns
152
+ * last, and the record comes back in full. It walks past a hard
153
+ * `return false` deny the same way.
154
+ *
155
+ * The fix is not a sixth rule, and it is not a better string to match. It is to
156
+ * stop identifying the collection at all: read `context.model`. That is a claim
157
+ * about IDENTIFYING THE COLLECTION, not about the sample as a whole -- the
158
+ * `/archived` SUB-PATH rule is still a string match, and abofs/stonyx-orm#228 is
159
+ * a sixth spelling that gets past it.
160
+ *
161
+ * An intermediate revision of the sample read `request.baseUrl` -- the mount
162
+ * Express ACTUALLY MATCHED. That closed all five variants (no query string,
163
+ * not mount-relative, unaffected by absolute-form, already carrying the
164
+ * configured `ORM_REST_ROUTE` prefix), but it was a transport artifact
165
+ * standing in for a structural fact and the sample no longer does it.
166
+ * `context.model` IS the structural fact, so variants 1, 2, 4 and 5 are
167
+ * unconstructible against a migrated predicate rather than handled.
168
+ *
169
+ * VARIANT 3 SURVIVES, and is deliberately not in that list. It is the general
170
+ * shape "a hand-written matcher normalises differently from the router", and a
171
+ * migrated predicate still runs one string comparison for any SUB-PATH rule --
172
+ * in the shipped sample, the `/archived` deny. That comparison folds case but
173
+ * does not decode, so `GET /owners/%61rchived` steps past it. See the
174
+ * normalisation paragraph below and abofs/stonyx-orm#228.
175
+ *
176
+ * ONE READ OF ARGUMENT ONE SURVIVES, AND IT MUST: `request.path`. It is
177
+ * mount-relative and query-free, and it is for rules that distinguish SUB-PATHS
178
+ * beneath the mount. The context names which model and which verb, NOT which
179
+ * route, so the sample's `/archived` deny cannot be expressed from the context
180
+ * alone and a context-ONLY rewrite would silently turn that deny into an allow.
181
+ *
182
+ * NORMALISE THE WAY THE ROUTER DOES, AND CASE-FOLDING ALONE IS NOT THAT. The
183
+ * sample lower-cases before comparing, because a matcher stricter than the
184
+ * case-insensitive router can be stepped around. That closes the case gap only.
185
+ * Express sets `request.path` from the RAW, UNDECODED pathname while the router
186
+ * DECODES `:id`, so `GET /owners/%61rchived` reaches a `path === '/archived'`
187
+ * comparison as `/%61rchived` and walks past the deny. That gap is live in the
188
+ * sample and is tracked as abofs/stonyx-orm#228; the `.toLowerCase()` is not a
189
+ * complete normalisation recipe. Compare record ids at their real case.
190
+ *
191
+ * `?? ''` is not a defence. It converts an absent request target into an empty
192
+ * string, which matches no collection, which falls through to the permission
193
+ * array -- a total grant. An input you cannot identify must DENY, and that
194
+ * applies to BOTH arguments: since #202 the guard and the read can sit on
195
+ * different objects, and a guard on argument two does not protect a read of
196
+ * argument one. The sample returns `false` for an absent `model` AND for an
197
+ * absent or non-string `request.path`, rather than falling through either way.
198
+ *
199
+ * THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model,
200
+ * the operation and the record. Prefer the array shape (`['read']`) or `false`
201
+ * until #202 lands; the function shape is what requires any matching at all.
202
+ *
203
+ * The enforcement gates in this file (GATE 0/1/2, the create rollback, the
204
+ * per-handler `isDenied` re-checks) are correct independently of that -- they
205
+ * enforce whatever predicate you return. The stopgap is the part where YOU have
206
+ * to work out which predicate to return.
207
+ *
208
+ * AND A PREDICATE IS NOT A GUARANTEE THAT A HIDDEN RECORD CANNOT BE MODIFIED.
209
+ * It is evaluated against the record the route is ADDRESSED TO, on that model
210
+ * only. A write to a DIFFERENT collection can still re-parent a hidden record
211
+ * and de-hide it -- abofs/stonyx-orm#207, which is blocked on #202 and #196.
212
+ * See `### Known limitations` in README.
213
+ */
1
214
  import { Request } from '@stonyx/rest-server';
2
215
  import Orm, { store, createRecord, updateRecord } from '@stonyx/orm';
3
216
  import { camelCaseToKebabCase } from '@stonyx/utils/string';
4
217
  import { getPluralName } from './plural-registry.js';
5
218
  import { getBeforeHooks, getAfterHooks } from './hooks.js';
6
219
  import config from 'stonyx/config';
7
- import { isOrmRecord } from './utils.js';
220
+ import log from 'stonyx/log';
221
+ import { isOrmRecord, NO_FREE_ID_ERROR } from './utils.js';
222
+ import { interpretAccess, createLinkageFilter } from './access-verdict.js';
8
223
  const methodAccessMap = {
9
224
  GET: 'read',
10
225
  POST: 'create',
@@ -48,13 +263,117 @@ function getBaseUrl(request) {
48
263
  const host = request.get('host');
49
264
  return `${protocol}://${host}`;
50
265
  }
266
+ /**
267
+ * The ONE coercion from a caller-supplied id to the key the store holds it
268
+ * under. Every id-bearing surface in this file goes through it, and none has a
269
+ * copy: `getId()` (URL params), `normalizeBodyId()` (JSON body), and the
270
+ * post-create `context.record` lookup in `_withHooks`.
271
+ *
272
+ * WHY IT IS SHARED RATHER THAN DUPLICATED. `getId()` and `normalizeBodyId()`
273
+ * each had their own arithmetic, and they disagreed: `parseInt(id)` versus
274
+ * `parseInt(id, 10)`. On a hex-shaped id that is a two-record difference --
275
+ *
276
+ * GET /animals/0x2391 -> record 9105 (getId -> parseInt('0x2391') = 9105)
277
+ * POST /animals {"id":"0x2391"} -> lookup under 0 (normalize -> parseInt('0x2391',10) = 0)
278
+ * -> a MISS, so the duplicate check was skipped and
279
+ * createRecord OVERWROTE 9105 in place, answering 200
280
+ *
281
+ * -- a narrower form of the raw-versus-normalised divergence that the body-id
282
+ * normalisation was added to close, reintroduced by the fix for it. Two
283
+ * coercions that must agree cannot be kept in agreement by review; they have to
284
+ * be one function. Pinned by assertion 43.
285
+ *
286
+ * The third copy was found later and in a quieter place: `_withHooks` populated
287
+ * `context.record` for `create` with `isNaN(id) ? id : parseInt(id)` -- this
288
+ * function's body, inlined verbatim, feeding `store.get`. It was equivalent on
289
+ * every input reachable there, which is exactly what the two that DID diverge
290
+ * looked like until someone tried a hex id.
291
+ *
292
+ * SHARING IT IS NOT THE SAME AS IT BEING RIGHT EVERYWHERE. On a model declaring
293
+ * `id = attr('string')` a numeric-looking id is filed under the STRING key, so
294
+ * this coercion resolves `'9107'` to `9107` and the post-create lookup misses:
295
+ * `context.record` is `undefined` for an after-`create` hook. Inherited -- the
296
+ * inlined copy computed the same thing -- and NOT fixed here, because picking
297
+ * the right coercion needs the model's declared id type, which is the same
298
+ * structural information abofs/stonyx-orm#202 is about. Filed as
299
+ * abofs/stonyx-orm#209 and pinned by assertion 50, so closing it turns a test
300
+ * red rather than passing silently.
301
+ *
302
+ * `parseInt` and not `Number`, deliberately, and the anchor is NOT `getId`.
303
+ * It is `src/transforms.ts:7` -- `number: (value) => parseInt(value as string)`,
304
+ * also radix-less -- because that transform is what actually produces the store
305
+ * KEY a record is filed under. `getId` merely agrees with it. They differ from
306
+ * `Number` on `'1e3'` (1 vs 1000) and `'9105.5'` (9105 vs 9105.5), so switching
307
+ * this function to `Number` would make the lookup key disagree with the landing
308
+ * key on those shapes.
309
+ *
310
+ * THE CHANGE THAT WOULD BREAK THIS: adding a radix to `transforms.number`
311
+ * (`parseInt(value, 10)`). That is a one-word edit in a file with no connection
312
+ * to authorization, it would silently reopen the hex divergence in the other
313
+ * direction, and this comment would still read as correct. Assertion 45 pins
314
+ * the transform's radix-less shape directly, so that edit turns a test red
315
+ * rather than shipping.
316
+ *
317
+ * The reason `parseInt` is safe here is the `isNaN` gate in front of it:
318
+ * `parseInt('9105h')` is `9105`, and truncating a partially-valid id into a
319
+ * DIFFERENT VALID key is exactly how a collision lookup gets skipped. The gate
320
+ * rejects it as a string instead, so nothing is ever truncated. That gate, not
321
+ * the parser, is the load-bearing half -- assertion 43 pins it.
322
+ */
323
+ function coerceId(id) {
324
+ if (isNaN(id))
325
+ return id;
326
+ return parseInt(id);
327
+ }
51
328
  function getId(params) {
52
329
  const id = params.id;
53
330
  if (!id)
54
331
  return '';
55
- if (isNaN(id))
332
+ return coerceId(id);
333
+ }
334
+ /**
335
+ * Normalise a caller-supplied BODY id to the key the store will hold it under.
336
+ *
337
+ * `getId()` above is params-shaped: it takes `{ id?: string }` off the URL,
338
+ * where the value is always a string and a falsy one means "no id". A JSON body
339
+ * id is neither -- it can arrive as a number, and `0` is a legitimate id that
340
+ * `getId()` would flatten to `''`.
341
+ *
342
+ * WHY THIS EXISTS AT ALL. createHandler used to look the collision up with the
343
+ * RAW body value while every other surface normalised through `getId()`. The
344
+ * store is a Map keyed by the coerced value, so `store.find(model, '21')` misses
345
+ * the entry held under `21` and the duplicate check is skipped by typing the id
346
+ * as a string. On `dev` that silently overwrote the colliding record and
347
+ * answered 200; combined with the denied-create rollback added for #190 it
348
+ * became an unauthenticated DELETE of any id. Normalising here is half of that
349
+ * fix -- see the rollback in createHandler for the other half.
350
+ *
351
+ * It shares `coerceId` with `getId` so the two surfaces cannot drift apart
352
+ * again, and differs from `getId` in exactly ONE place, below.
353
+ */
354
+ function normalizeBodyId(id) {
355
+ // Non-strings pass through untouched: a JSON body id can arrive as a number,
356
+ // and `getId`'s falsy-flatten must NOT apply to it -- `0` is a legitimate id
357
+ // and `getId` would turn it into `''`. (assertion 30 sweeps id `0`.)
358
+ if (typeof id !== 'string')
56
359
  return id;
57
- return parseInt(id);
360
+ // THE ONE DIVERGENCE FROM `getId`, and it mirrors rather than contradicts it:
361
+ // `getId` maps a falsy param to `''`, and `''` is the only string a body can
362
+ // carry that means "no id" -- `createRecord` treats it as absent and assigns a
363
+ // server id. Coercing it instead would make it address a real slot, because
364
+ // `parseInt('')` is `NaN` and a record CAN be held under `NaN` (a truthy but
365
+ // non-numeric id such as `' '` survives `assignRecordId`'s falsy guard and
366
+ // then NaNs in the id transform). So `POST {"id":""}` would answer 409 against
367
+ // an unrelated record it never named. Pinned by assertion 44.
368
+ //
369
+ // Note what is deliberately NOT special-cased here any more: whitespace.
370
+ // `id.trim() === ''` used to short-circuit `' '` as well, which made the
371
+ // body surface DISAGREE with the URL surface -- `getId({id:' '})` is `NaN`,
372
+ // so `' '` addresses the NaN slot on every other route while the collision
373
+ // lookup missed it. Same class of bug as the hex divergence above.
374
+ if (id === '')
375
+ return id;
376
+ return coerceId(id);
58
377
  }
59
378
  function buildResponse(data, includeParam, recordOrRecords, options = {}) {
60
379
  const { links, baseUrl } = options;
@@ -70,6 +389,13 @@ function buildResponse(data, includeParam, recordOrRecords, options = {}) {
70
389
  return response;
71
390
  const includedRecords = collectIncludedRecords(recordOrRecords, includes);
72
391
  if (includedRecords.length > 0) {
392
+ // NO `linkage` ARGUMENT, deliberately, and abofs/stonyx-orm#235 owns adding
393
+ // one. Until it does, a PERMITTED record here emits the full pre-#234
394
+ // document: `GET /animals/1?include=owner` filters the primary document's
395
+ // `owner.data` to `null` and then names `owner:angela` in `included`.
396
+ // Whether a resource reaches this array at all is a different question
397
+ // (membership, abofs/stonyx-orm#233) and closing that one does not close
398
+ // this one.
73
399
  response.included = includedRecords.map(record => record.toJSON?.({ baseUrl }));
74
400
  }
75
401
  return response;
@@ -187,6 +513,39 @@ function createFilterPredicate(filters) {
187
513
  return String(current) === value;
188
514
  });
189
515
  }
516
+ /**
517
+ * A function-style `access` return is a per-record predicate, and it is only
518
+ * meaningful if every surface that can hand a record to a caller consults it.
519
+ * Before #190 exactly one of seven did.
520
+ *
521
+ * Enforcement is deliberately post-fetch. `access` returns an opaque JS
522
+ * predicate and `store.findAll(model, conditions)` accepts only an equality
523
+ * conditions object that the SQL drivers translate to a WHERE clause, so
524
+ * query-layer enforcement would require a breaking change to the published
525
+ * `access` contract. That belongs in #197, not in a security patch. Six of the
526
+ * seven surfaces fetch by primary key anyway, so this costs exactly one row.
527
+ */
528
+ function isDenied(filter, record) {
529
+ if (typeof filter !== 'function')
530
+ return false;
531
+ // A predicate that throws is treated as a denial. Unguarded, a throw escapes
532
+ // to express's default handler, which answers 500 (with a stack trace outside
533
+ // NODE_ENV=production) while a missing id still answers 404 -- so a
534
+ // record-dependent throw re-separates "hidden" from "does not exist" and
535
+ // hands back the oracle this whole change exists to close.
536
+ try {
537
+ return !filter(record);
538
+ }
539
+ catch (error) {
540
+ // Denied, but not silently. A consumer predicate that throws on every
541
+ // record turns the whole collection into a 404 wall, and with no
542
+ // diagnostic that is indistinguishable from an empty database. `stonyx/log`
543
+ // is the module convention (see setup-rest-server.ts); optional-call
544
+ // because a consumer may not have configured the log types.
545
+ log.error?.(`[@stonyx/orm] access filter threw for model -- denying. ${error instanceof Error ? error.message : String(error)}`);
546
+ return true;
547
+ }
548
+ }
190
549
  export default class OrmRequest extends Request {
191
550
  model;
192
551
  access;
@@ -210,37 +569,164 @@ export default class OrmRequest extends Request {
210
569
  if (queryFilterPredicate)
211
570
  recordsToReturn = recordsToReturn.filter(queryFilterPredicate);
212
571
  const baseUrl = getBaseUrl(request);
213
- const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl }));
572
+ // ONE filter per REQUEST, not one per record: it carries the per-type
573
+ // verdict cache and the per-(type, id) decision cache, and both are
574
+ // worthless if it is rebuilt inside the map. Measured on this exact
575
+ // surface with no `include=`: 48 linkage entries collapse to 7 distinct
576
+ // (type, id) pairs.
577
+ const linkage = createLinkageFilter(request);
578
+ const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl, linkage }));
214
579
  return buildResponse(data, request.query?.include, recordsToReturn, {
215
580
  links: { self: `${baseUrl}/${pluralizedModel}` },
216
581
  baseUrl
217
582
  });
218
583
  };
219
- const getSingleHandler = async (request) => {
584
+ const getSingleHandler = async (request, { filter }) => {
220
585
  const record = await store.find(model, getId(request.params));
221
586
  if (!record)
222
587
  return 404;
588
+ // 404, never 403: the status for "exists but filtered out" must be
589
+ // identical to "does not exist", or the fix trades an authorization
590
+ // bypass for a narrower existence oracle.
591
+ if (isDenied(filter, record))
592
+ return 404;
223
593
  const fieldsMap = parseFields(request.query);
224
594
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
225
595
  const baseUrl = getBaseUrl(request);
226
- return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl }), request.query?.include, record, {
596
+ const linkage = createLinkageFilter(request);
597
+ // `buildResponse` is deliberately NOT given the linkage filter, and the
598
+ // residual that leaves is NOT the one #233 owns. Two different questions:
599
+ //
600
+ // - WHETHER A RESOURCE APPEARS in `included` at all is MEMBERSHIP ->
601
+ // abofs/stonyx-orm#233.
602
+ // - What a record already IN `included` may NAME is LINKAGE -- the same
603
+ // question #234 answers for the primary document -- and it is
604
+ // abofs/stonyx-orm#235, which also owns createHandler/updateHandler.
605
+ //
606
+ // The residual, stated so the next reader does not have to derive it:
607
+ // `buildResponse` calls `record.toJSON?.({ baseUrl })` with no `linkage`
608
+ // argument, so a PERMITTED record in `included` emits the full pre-#234
609
+ // document. Measured: `GET /animals/1?include=owner` returns
610
+ // `owner.data: null` on the primary document and `owner:angela` in
611
+ // `included`. One query parameter deep. Only the PRIMARY document's
612
+ // linkage is filtered here.
613
+ return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl, linkage }), request.query?.include, record, {
227
614
  links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}` },
228
615
  baseUrl
229
616
  });
230
617
  };
231
- const createHandler = async ({ body, query }) => {
618
+ const createHandler = async ({ body, query }, { filter }) => {
232
619
  const { type, id, attributes, relationships: rels } = (body?.data || {});
233
620
  if (!type)
234
621
  return 400; // Bad request
235
622
  const fieldsMap = parseFields(query);
236
623
  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
624
+ // GATE 0 -- the POST existence oracle.
625
+ //
626
+ // The duplicate check runs before the filter and `store.find` sees hidden
627
+ // records, so POST leaks existence through its STATUS. A previous revision
628
+ // filtered the collision status (403 when the colliding record is denied,
629
+ // 409 when it is visible) and that is NOT sufficient, because the status
630
+ // of a create is a third outcome. With a payload the caller is permitted
631
+ // to create -- the normative case for a per-tenant filter, and the case an
632
+ // attacker picks -- all three are distinguishable in ONE request per id:
633
+ //
634
+ // POST /animals {id:22, owner:'gina'} -> 403 a HIDDEN record has this id
635
+ // POST /animals {id:9500, owner:'gina'} -> 200 this id is FREE
636
+ // POST /animals {id:8, owner:'gina'} -> 409 a VISIBLE record has this id
637
+ //
638
+ // Filtering only the collision status narrows that to callers who cannot
639
+ // create a record they are allowed to see. It does not close it.
640
+ //
641
+ // It cannot be closed while a caller both chooses the id and learns
642
+ // whether the create succeeded: a successful create must answer
643
+ // differently from a refused one. So when a per-record filter is in force
644
+ // the caller does not get to choose the id at all. The refusal is
645
+ // UNCONDITIONAL and happens BEFORE any store lookup, so no status, and no
646
+ // lookup cost, can depend on whether that id exists. 403 -- the same
647
+ // status as a denied create -- so the two cannot be separated either.
648
+ //
649
+ // BOTH HALVES OF THAT ARE PINNED, because both were once asserted here and
650
+ // pinned by nothing:
651
+ //
652
+ // status -- assertion 22 sweeps payload x id x id-type, plus `null`.
653
+ // latency -- assertion 41 asserts NO `store.find` is issued on this
654
+ // path. Moving the refusal to after a lookup and returning
655
+ // the same 403 left the suite green while re-opening a
656
+ // hit-versus-miss timing difference on every id-bearing POST,
657
+ // which is what would turn #197 from a ~0.06ms post-fetch
658
+ // residual into a live timing oracle on create.
659
+ //
660
+ // AND THE GUARANTEE IS ONLY AS WIDE AS THE CHANNELS IT COVERS. It reads on
661
+ // the `id` member of the resource object, so it holds only while that is
662
+ // the ONLY way a caller id can reach `createRecord`. It was not: the
663
+ // relationships loop below re-admitted one under `key === "id"` and the
664
+ // gate never fired. Both strips -- `attributes.id` and `relationships.id`
665
+ // -- are therefore part of THIS gate, not tidiness, and assertion 39 pins
666
+ // them. Adding a third channel without a strip re-opens the oracle.
667
+ //
668
+ // Scoped to function-style `access` because that is exactly the population
669
+ // the oracle exists for: with no per-record filter there are no hidden
670
+ // records, and 409 discloses nothing GET /:id does not already.
671
+ //
672
+ // RESIDUALS, stated rather than implied.
673
+ //
674
+ // - a caller can still learn that a collection HAS a per-record filter
675
+ // (403 rather than 409/200 for an id-bearing POST). That discloses a
676
+ // configuration fact, not a record.
677
+ // - this gate is about ids arriving on THIS model's create route. It
678
+ // says nothing about a write to ANOTHER collection: a `POST /owners`
679
+ // carrying `relationships: {pets: {data: {id: 21}}}` -- or
680
+ // `attributes: {pets: [21, 22]}`, which never enters the
681
+ // relationships loop at all -- re-parents hidden animal 21 onto an
682
+ // owner the caller may write, which changes the very field the
683
+ // animals predicate reads and DE-HIDES it. Blocking that needs animal
684
+ // 21 checked against the ANIMAL model's predicate while servicing an
685
+ // OWNERS route, i.e. cross-model access resolution: abofs/stonyx-orm
686
+ // #207, blocked on #202 (`access` receives the model structurally)
687
+ // and #196 (setup-rest-server discards the model->predicate map at
688
+ // boot). NOT closed here, and no comment in this file may say it is.
689
+ //
690
+ // See README `### Known limitations`.
691
+ if (id !== undefined) {
692
+ if (typeof filter === 'function')
693
+ return 403; // Forbidden
694
+ // `normalizeBodyId`, not the raw value: a string-typed id misses the
695
+ // store's numeric key, which skipped this check entirely.
696
+ const existing = await store.find(model, normalizeBodyId(id));
697
+ if (existing)
698
+ return 409; // Conflict
699
+ }
240
700
  const { id: _ignoredId, ...sanitizedAttributes } = attributes || {};
241
- // Extract relationship IDs from JSON:API relationships object
701
+ // Extract relationship IDs from JSON:API relationships object.
702
+ //
703
+ // `key` comes VERBATIM from the request body, so `id` is stripped here for
704
+ // exactly the same reason it is stripped from `attributes` on the line
705
+ // above -- and it must be, or GATE 0 is walked around by moving one field:
706
+ //
707
+ // POST /animals {"id":21, ...} -> 403 GATE 0 fires
708
+ // POST /animals {"relationships":{"id":{"data":{"id":21}}},
709
+ // "attributes":{"owner":"gina"}} -> 200 BYPASS
710
+ //
711
+ // Top-level `id` stayed `undefined`, so GATE 0 never fired and the
712
+ // collision lookup never ran; `createRecord` took its last-entry-wins
713
+ // branch, overwrote hidden record 21 in place and reset its `owner` to a
714
+ // value the caller chose -- de-hiding it permanently. That is #190 itself,
715
+ // on the create surface. Pinned by assertion 39.
716
+ //
717
+ // The `id` member of the resource object is now the ONLY channel a caller
718
+ // id can arrive on FOR THIS MODEL'S OWN CREATE ROUTE, which is what makes
719
+ // GATE 0's guarantee checkable rather than merely asserted. It is not a
720
+ // statement about the record's reachability in general -- a relationship
721
+ // write on another collection reaches it without ever touching this
722
+ // handler (abofs/stonyx-orm#207). INHERITED from `dev`, which carries this
723
+ // loop verbatim; the general form -- the loop accepts any key, not just
724
+ // `id`, so a body key that is not a declared relationship is still
725
+ // mass-assigned -- is abofs/stonyx-orm#204.
242
726
  if (rels) {
243
727
  for (const [key, value] of Object.entries(rels)) {
728
+ if (key === 'id')
729
+ continue;
244
730
  const relData = value?.data;
245
731
  if (relData && relData.id !== undefined) {
246
732
  sanitizedAttributes[key] = relData.id;
@@ -248,16 +734,144 @@ export default class OrmRequest extends Request {
248
734
  }
249
735
  }
250
736
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
251
- const created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
737
+ // Slot count BEFORE the write. `createRecord` writes to the store before
738
+ // the predicate can run, and the rollback below must be able to prove the
739
+ // slot it removes is one THIS REQUEST created. Identity alone cannot
740
+ // prove it: when `assignRecordId` lands on an occupied id, `createRecord`
741
+ // mutates the existing OrmRecord IN PLACE, so `store.get(...) === record`
742
+ // is true for a record the request did not create. The map's size is the
743
+ // only O(1) signal that distinguishes an insert from an overwrite.
744
+ const slotsBefore = store.get(model)?.size ?? 0;
745
+ // THE ONE `createRecord` FAILURE THIS ROUTE ANSWERS RATHER THAN
746
+ // PROPAGATES, and it is narrow on purpose.
747
+ //
748
+ // `assignRecordId` throws when it cannot derive a free store key for a
749
+ // server-assigned id. Unguarded that rejection is auto-forwarded -- there
750
+ // is no catch here, none in @stonyx/rest-server's dispatcher
751
+ // (dist/request.js:41-70), and express 5 hands it to its default error
752
+ // handler, which serialises the STACK, with absolute install paths and the
753
+ // internal module graph, to an unauthenticated caller outside
754
+ // NODE_ENV=production. That is the hazard :553-558 already names in this
755
+ // file, and every sibling refusal in this handler returns an integer
756
+ // status instead. So this one returns 409, matching the client-duplicate
757
+ // refusal at :713: the caller asked for a record and the collection has no
758
+ // id to give it.
759
+ //
760
+ // MATCHED ON THE SHARED PREFIX, not on a literal, and NOT by catching
761
+ // everything: `createRecord` also throws for "ORM is not ready", a
762
+ // read-only view and an unregistered model store, and turning any of those
763
+ // into a 409 would report a configuration fault as a conflict. Anything
764
+ // else is re-thrown unchanged.
765
+ let created;
766
+ try {
767
+ created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
768
+ }
769
+ catch (error) {
770
+ if (!(error instanceof Error) || !error.message.startsWith(NO_FREE_ID_ERROR))
771
+ throw error;
772
+ // Not silently. A collection that can no longer assign an id is a
773
+ // configuration fault (a non-injective id transform), and a bare 409
774
+ // with no diagnostic is indistinguishable from an ordinary duplicate.
775
+ log.error?.(`[@stonyx/orm] ${error.message}`);
776
+ return 409; // Conflict
777
+ }
252
778
  const record = isOrmRecord(created) ? created : null;
253
779
  if (!record)
254
780
  return 500;
781
+ const createdNewSlot = (store.get(model)?.size ?? 0) > slotsBefore;
782
+ // 403 here, NOT 404. The oracle argument does not apply to create: there
783
+ // is no pre-existing record whose existence could leak, the caller
784
+ // supplied the attributes, and 404 on a mounted collection route is
785
+ // indistinguishable from "model not mounted" -- a genuinely different
786
+ // failure a developer needs to diagnose.
787
+ //
788
+ // The rollback is not optional. createRecord writes to the store BEFORE
789
+ // the predicate can run, so returning 403 alone would leave the record
790
+ // behind: a worse bug than the bypass being fixed.
791
+ if (isDenied(filter, record)) {
792
+ // ROLL BACK BY IDENTITY, NEVER BY ID. `store.remove(model, record.id)`
793
+ // on its own is a write primitive keyed by a value the caller may have
794
+ // supplied: with the raw-id collision bypass above, a denied
795
+ // `POST {"id":"21"}` answered 403 and DELETED hidden record 21 -- an
796
+ // unauthenticated deletion primitive across the whole id space, created
797
+ // by adding a rollback to a lookup that could be skipped.
798
+ //
799
+ // Both conditions are required and neither implies the other:
800
+ // createdNewSlot -- the store grew, so this request inserted rather
801
+ // than overwrote. SURVIVOR AS OF #203, AND THAT IS
802
+ // WHAT THIS NOTE IS FOR. It used to be killable:
803
+ // `assignRecordId` returned last-INSERTED + 1, so a
804
+ // server-assigned id could land on an occupied slot,
805
+ // `createRecord` updated in place, and removing this
806
+ // half turned access-filter-enforcement-test.ts
807
+ // assertion 31 red. #203 closed that: the
808
+ // server-assigned path now walks past occupied keys,
809
+ // so no create reaching here can overwrite. Measured
810
+ // -- delete `createdNewSlot &&` below: `dev` gives
811
+ // 55 pass / 1 fail with assertion 31 RED, this tree
812
+ // gives 56 pass / 0 fail, GREEN.
813
+ // KEPT ANYWAY, AND NOT FOR THE OLD REASON. Without
814
+ // it a denied create becomes `store.remove` on a key
815
+ // the caller may have influenced, which :815-820
816
+ // records as having been an unauthenticated deletion
817
+ // primitive across the whole id space. BECOMES
818
+ // KILLABLE AGAIN the moment any caller-supplied id
819
+ // can reach `createRecord` from this handler --
820
+ // which is exactly what has-many.ts:65 and
821
+ // belongs-to.ts:45 already do for ANOTHER model's
822
+ // store (abofs/stonyx-orm#207), and what a third
823
+ // un-stripped id channel would do for this one
824
+ // (#204). Do not delete it on the strength of #203
825
+ // being closed; that is the reasoning :862-867 warns
826
+ // about, one level up.
827
+ // identity -- the slot still holds the object we just created,
828
+ // so nothing between createRecord and here replaced
829
+ // it. Deleting this half SURVIVES the suite, and it
830
+ // is kept anyway. WHY IT IS REDUNDANT: there is no
831
+ // `await` anywhere between `slotsBefore` and
832
+ // `store.remove` -- the whole window is synchronous,
833
+ // so it is atomic under Node's event loop; before-
834
+ // `create` hooks run BEFORE the handler
835
+ // (`_withHooks` runs its hook loop ahead of
836
+ // `await handler(...)`), and a consumer predicate
837
+ // inside `isDenied` runs AFTER `createdNewSlot` is
838
+ // computed and cannot flip it. That is a property of
839
+ // THIS function, not of GATE 0 -- an earlier note
840
+ // credited GATE 0, which was both wrong (a caller id
841
+ // reached createRecord through the relationships
842
+ // loop, #204) and the wrong kind of reason: a guard
843
+ // justified on code sixty lines upstream gets
844
+ // silently re-armed when that code moves.
845
+ // SO IT BECOMES REACHABLE IF AN `await` IS
846
+ // INTRODUCED HERE, which is the change a future
847
+ // editor would actually make. Stated here rather
848
+ // than by reference: `docs/` is not in `files`, so
849
+ // a pointer into it resolves to nothing for anyone
850
+ // who installed this package. README carries the
851
+ // consumer-facing half.
852
+ if (createdNewSlot && store.get(model, record.id) === record) {
853
+ store.remove(model, record.id, { _skipAutoPersist: true });
854
+ }
855
+ return 403;
856
+ }
255
857
  return { data: record.toJSON?.({ fields: modelFields }) };
256
858
  };
257
- const updateHandler = async ({ body, params }) => {
859
+ const updateHandler = async ({ body, params }, { filter }) => {
258
860
  const found = await store.find(model, getId(params));
259
861
  if (!found || !isOrmRecord(found))
260
862
  return 404;
863
+ // Checked BEFORE any attribute is applied. 404 rather than 403 for the
864
+ // same reason as GET /:id -- 403 would disclose both that the record
865
+ // exists and that this caller specifically is excluded.
866
+ //
867
+ // NOT redundant behind GATE 1, and not defence in depth either: GATE 1's
868
+ // verdict is computed BEFORE the before-hook loop runs, and a before-hook
869
+ // is a published extension point that can change the answer -- by
870
+ // mutating the record, or against a predicate that closes over
871
+ // per-request state. This is the only re-evaluation after that window.
872
+ // Pinned by assertion 32; deleting it turns a 404 into an applied update.
873
+ if (isDenied(filter, found))
874
+ return 404;
261
875
  const record = found;
262
876
  const { attributes, relationships: rels } = (body?.data || {});
263
877
  if (!attributes && !rels)
@@ -277,6 +891,19 @@ export default class OrmRequest extends Request {
277
891
  if (rels) {
278
892
  const relUpdates = {};
279
893
  for (const [key, value] of Object.entries(rels)) {
894
+ // The same missing key filter as createHandler's, and as the
895
+ // attribute loop directly above -- which already had it, while this
896
+ // loop did not. A PATCH carrying
897
+ // `relationships:{"id":{"data":{"id":9101}}}` reached `updateRecord`
898
+ // and RE-KEYED the record: the object held under store key 9102 then
899
+ // reported id 9101, so a visible record claimed a hidden record's
900
+ // identity on every surface that reads `record.id` rather than the map
901
+ // key. Gated by GATE 1 on the addressed record, so it is store
902
+ // corruption rather than a filter bypass -- but it is the same one-line
903
+ // omission two handlers apart. Pinned by assertion 40; INHERITED from
904
+ // `dev`; abofs/stonyx-orm#204.
905
+ if (key === 'id')
906
+ continue;
280
907
  const relData = value?.data;
281
908
  if (relData && relData.id !== undefined) {
282
909
  relUpdates[key] = relData.id;
@@ -288,8 +915,31 @@ export default class OrmRequest extends Request {
288
915
  }
289
916
  return { data: record.toJSON?.() };
290
917
  };
291
- const deleteHandler = ({ params }) => {
292
- store.remove(model, getId(params));
918
+ const deleteHandler = async ({ params }, { filter }) => {
919
+ // Coerced ONCE. `getId(params)` was evaluated twice here -- once to find
920
+ // the record and once to remove it -- and a coercion evaluated repeatedly
921
+ // is a coercion that can be edited in one place and not the other, which
922
+ // is the defect `coerceId` exists to prevent.
923
+ const recordId = getId(params);
924
+ const record = await store.find(model, recordId);
925
+ // BEHAVIOUR CHANGE (#190): a DELETE of a record that never existed
926
+ // returned 204 before this change. It now returns 404, matching the
927
+ // denied case below. This is deliberate and load-bearing -- if a denied
928
+ // delete returned 404 while a missing one returned 204, the pair would be
929
+ // a perfect existence oracle and the whole fix would be worthless.
930
+ // Returning 204 for a denied delete was rejected instead: it falsely
931
+ // reports success for a request that changed nothing.
932
+ if (!record)
933
+ return 404;
934
+ // Re-evaluated after the before-hook loop, exactly as in updateHandler --
935
+ // GATE 1 decided before the hooks ran. Pinned by assertion 33; deleting it
936
+ // turns a 404 into a destroyed record.
937
+ if (isDenied(filter, record))
938
+ return 404;
939
+ // Removed by the id of the record actually fetched, not by re-deriving it
940
+ // from the params a second time: the record the filter tested and the
941
+ // record removed are then provably the same one.
942
+ store.remove(model, record.id, { _skipAutoPersist: true });
293
943
  return 204;
294
944
  };
295
945
  // Wrap handlers with hooks
@@ -314,9 +964,63 @@ export default class OrmRequest extends Request {
314
964
  };
315
965
  }
316
966
  }
317
- // Wraps a handler with before/after hook execution
967
+ // Wraps a handler with before/after hook execution.
968
+ //
969
+ // ===========================================================================
970
+ // TWO AUTHORIZATION GATES, AND EVERY EXECUTOR SITS BEHIND ONE OF THEM (#190)
971
+ //
972
+ // The defect this function was fixed for is NOT "a delete persists past a
973
+ // 404". It is that _withHooks has SEVERAL executors downstream of the
974
+ // handler, and originally the handler's response gated none of them. Three
975
+ // exist today:
976
+ //
977
+ // 1. sqlDb.persist -- issues real SQL against the backing store
978
+ // 2. the after-hook pipeline -- the PUBLISHED consumer extension point;
979
+ // a cascade delete, a webhook, a search-index
980
+ // purge. `context.recordId` and
981
+ // `context.oldState` are populated for it.
982
+ // 3. Orm.db.save() -- a full serialize-and-write of the store
983
+ //
984
+ // Gating them one at a time is how this keeps regressing, so the rule is:
985
+ // compute denial ONCE at each point where it becomes knowable, and keep every
986
+ // executor downstream of a gate. If you add a fourth executor to this
987
+ // function, it goes below GATE 2 or it is a security bug.
988
+ //
989
+ // GATE 1 (pre-handler) is required because before-hooks and `context.oldState`
990
+ // run/are built BEFORE the handler can consult the filter. Without it a denied
991
+ // DELETE still handed the hidden record's full contents to consumer code.
992
+ // GATE 2 (post-handler) covers everything the handler's status can reach.
993
+ // ===========================================================================
318
994
  _withHooks(operation, handler) {
319
995
  return async (request, state) => {
996
+ // `|| {}` so this function behaves like the relationship routes below,
997
+ // which declare `state` with a `= {}` default. It is unkillable through
998
+ // the rest-server dispatcher, which always passes `getState(req)`; it is
999
+ // listed as such in the guards-redundant-by-construction table rather
1000
+ // than left silently unkillable, and it defends the WHOLE function (the
1001
+ // context, the snapshot and the handler call all read `callState`) rather
1002
+ // than one destructure that the next line would throw past anyway.
1003
+ const callState = (state || {});
1004
+ // ---------------------------------------------------------------------
1005
+ // THE AUTHORIZATION SNAPSHOT. Read ONCE, here, before any consumer code
1006
+ // can run.
1007
+ //
1008
+ // `callState` is the object `auth()` planted the filter in, and it is
1009
+ // also handed to every before-hook as `context.state` -- a published,
1010
+ // WRITABLE extension point. So `state.filter` is an INPUT to the
1011
+ // authorization decision, not only an output channel, and re-reading it
1012
+ // after the hook loop lets a consumer hook disarm the filter:
1013
+ //
1014
+ // beforeHook('get', 'animal', ctx => { delete ctx.state.filter })
1015
+ // -> GET /animals/21 turned 404 into 200
1016
+ // -> GET /animals turned 20 records into 22
1017
+ //
1018
+ // GATE 1 already used this snapshot, so writes held; the READ handlers
1019
+ // re-destructured `filter` from the live bag and did not. Everything
1020
+ // downstream now reads `filter` from here, and the handler is handed
1021
+ // `handlerState` below -- never `callState`.
1022
+ // ---------------------------------------------------------------------
1023
+ const { filter } = callState;
320
1024
  // Build context object for hooks
321
1025
  const context = {
322
1026
  model: this.model,
@@ -325,11 +1029,35 @@ export default class OrmRequest extends Request {
325
1029
  params: request.params,
326
1030
  body: request.body,
327
1031
  query: request.query,
328
- state,
1032
+ // Deliberately the LIVE object: `redirect` and `pipe` are read back off
1033
+ // it by @stonyx/rest-server after the handler returns, so hooks must be
1034
+ // able to write to it. What must not happen is the authorization
1035
+ // decision reading it back, which is what the snapshot above prevents.
1036
+ state: callState,
329
1037
  };
330
1038
  // Capture old state for operations that modify data
331
1039
  if (operation === 'update' || operation === 'delete') {
332
1040
  const existingRecord = await store.find(this.model, getId(request.params));
1041
+ // GATE 1 -- pre-handler. This record fetch already happened for
1042
+ // oldState, so the check is free.
1043
+ //
1044
+ // Returning here rather than letting updateHandler/deleteHandler
1045
+ // produce the same 404 is the point: everything between here and there
1046
+ // is an executor the caller is not authorized to reach.
1047
+ // - context.oldState is a deep copy of the HIDDEN RECORD'S CONTENTS.
1048
+ // Building it and handing it to a before-hook discloses exactly what
1049
+ // the filter exists to hide.
1050
+ // - context.recordId is populated for delete BEFORE the handler runs,
1051
+ // which is the same shape as the sqlDb landmine one layer up:
1052
+ // `afterHook('delete', ctx => cascadeDelete(ctx.recordId))` destroys
1053
+ // children behind a correct 404.
1054
+ // - a before-hook may return a value and short-circuit, which would
1055
+ // otherwise return a response without the filter ever executing.
1056
+ //
1057
+ // 404, not 403, for the same reason as getSingleHandler: the status for
1058
+ // "exists but filtered out" must equal "does not exist".
1059
+ if (existingRecord && isDenied(filter, existingRecord))
1060
+ return 404;
333
1061
  if (existingRecord) {
334
1062
  // Deep copy the record's data to preserve old state
335
1063
  context.oldState = JSON.parse(JSON.stringify(existingRecord.__data || existingRecord));
@@ -347,10 +1075,49 @@ export default class OrmRequest extends Request {
347
1075
  }
348
1076
  }
349
1077
  // 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)
1078
+ // The handler receives the SNAPSHOT, never the live bag. `filter` is
1079
+ // assigned LAST so it wins over anything a before-hook wrote to
1080
+ // `callState.filter` -- including a `delete`, which the spread would
1081
+ // otherwise carry through as an absent key. Every other key a hook adds
1082
+ // is still visible to the handler; only the authorization input is
1083
+ // pinned.
1084
+ const handlerState = { ...callState, filter };
1085
+ const response = await handler(request, handlerState);
1086
+ // Set context.record for update BEFORE persist so SQL drivers can read it
1087
+ if (operation === 'update' && response?.data) {
1088
+ context.record = store.get(this.model, getId(request.params));
1089
+ }
1090
+ // GATE 2 -- post-handler. A denied or failed handler returns a bare status
1091
+ // integer, and no executor below may run for one.
1092
+ //
1093
+ // `>= 400` deliberately covers every failure status, not just the
1094
+ // authorization ones: a 400 (POST with no `type`) and a 409 (duplicate id)
1095
+ // are equally requests in which nothing happened, and a persist or a
1096
+ // cascade hook for one of them is just as wrong.
1097
+ // `Number.isInteger` is a TYPE guard, not a behaviour guard, and it is
1098
+ // unkillable TODAY: the only non-integer a handler in this file can
1099
+ // return is a `{ data, links }` object, and `{} >= 400` is `false` by JS
1100
+ // coercion, so dropping it changes no reachable outcome. It is kept
1101
+ // because `>=` coerces rather than rejects, and the shapes it coerces
1102
+ // are not obvious -- `[500] >= 400` is TRUE, so a handler that one day
1103
+ // returned an array would have every response read as a denial. Listed
1104
+ // as an equivalent mutant rather than left to read as coverage; it
1105
+ // becomes killable the moment a handler returns anything array-like or
1106
+ // numeric-string-like.
1107
+ const denied = Number.isInteger(response) && response >= 400;
1108
+ // EXECUTOR 1 -- SQL persistence, for all write operations.
1109
+ //
1110
+ // `response` is passed to sqlDb.persist below, but it is dropped at the
1111
+ // driver boundary: _persistDelete(modelName, context) never receives it
1112
+ // and guards only on context.recordId -- which _withHooks set above,
1113
+ // BEFORE the handler ran. Without this gate a correct 404 still issues
1114
+ // DELETE FROM ... WHERE id = ? on every SQL backend.
1115
+ //
1116
+ // No file-backed test can observe that, because Orm.instance.sqlDb is
1117
+ // null in file/directory mode. See the stubbed-sqlDb assertions in
1118
+ // test/unit/access-filter-enforcement-test.ts.
352
1119
  const sqlDb = Orm.instance.sqlDb;
353
- if (sqlDb && (operation === 'create' || operation === 'update')) {
1120
+ if (sqlDb && WRITE_OPERATIONS.has(operation) && !denied) {
354
1121
  await sqlDb.persist(operation, this.model, context, response);
355
1122
  }
356
1123
  // Add response and relevant records to context
@@ -364,22 +1131,44 @@ export default class OrmRequest extends Request {
364
1131
  else if (operation === 'create' && response?.data && (response.data.id)) {
365
1132
  // For create, get the record from store using the ID from the response
366
1133
  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));
1134
+ // `normalizeBodyId`, not a copy of its body. This line WAS
1135
+ // `isNaN(id) ? id : parseInt(id)` -- `coerceId` inlined verbatim, a
1136
+ // third coercion feeding a store lookup, sitting under a docblock that
1137
+ // said neither surface had a copy. Equivalent on every input that can
1138
+ // reach this truthy-guarded branch (`21`->`21`, `'21'`->`21`,
1139
+ // `'angela'`->`'angela'`; `''` and `0` cannot reach it), so this is a
1140
+ // de-duplication rather than a behaviour change -- and that is the
1141
+ // point: the two that disagreed were equivalent on every input anyone
1142
+ // checked, too.
1143
+ context.record = store.get(this.model, normalizeBodyId(responseData.id));
372
1144
  }
373
1145
  else if (operation === 'delete') {
374
1146
  // For delete, the record may no longer exist, but we have oldState
375
1147
  context.recordId = getId(request.params);
376
1148
  }
377
- // Run after hooks sequentially
378
- for (const hook of getAfterHooks(operation, this.model)) {
379
- await hook(context);
1149
+ // EXECUTOR 2 -- the after-hook pipeline. This is the published consumer
1150
+ // extension point (`afterHook` is exported from @stonyx/orm and from
1151
+ // ./hooks), so it is the executor with the widest possible blast radius:
1152
+ // a cascade delete, a webhook, a token revocation, a search-index purge.
1153
+ //
1154
+ // BEHAVIOUR CHANGE (#190): after-hooks no longer fire for a request that
1155
+ // failed. Previously `afterHook('delete', ...)` ran with a populated
1156
+ // context.recordId on a 404, so a consumer cascade destroyed children for
1157
+ // a request that deleted nothing. Firing a hook named "after<operation>"
1158
+ // for an operation that did not occur is a booby trap, and the denied case
1159
+ // is unreachable-before-#190 while the missing case is inherited debt --
1160
+ // both are closed by the same gate. `context.response` therefore only ever
1161
+ // carries a success status into a hook.
1162
+ if (!denied) {
1163
+ for (const hook of getAfterHooks(operation, this.model)) {
1164
+ await hook(context);
1165
+ }
380
1166
  }
381
- // Auto-save DB after write operations when configured
382
- if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation)) {
1167
+ // EXECUTOR 3 -- file/directory autosave. Ungated this let an
1168
+ // unauthenticated caller force a full serialize-and-write of the entire
1169
+ // store on every DELETE of any id, with no record touched: amplification
1170
+ // rather than corruption, but the same root cause and the same fix.
1171
+ if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation) && !denied) {
383
1172
  await Orm.db.save();
384
1173
  }
385
1174
  return response;
@@ -391,21 +1180,38 @@ export default class OrmRequest extends Request {
391
1180
  // Dasherize the relationship name for URL paths (e.g., accessLinks -> access-links)
392
1181
  const dasherizedName = camelCaseToKebabCase(relationshipName);
393
1182
  // Related resource route: GET /:id/{relationship}
394
- routes[`/:id/${dasherizedName}`] = async (request) => {
1183
+ //
1184
+ // These generated routes are not wrapped by _withHooks, which is why they
1185
+ // were the least obvious two of the seven unguarded surfaces in #190.
1186
+ // They are still dispatched by @stonyx/rest-server as
1187
+ // `handler(req, getState(req))`, so `state` -- and therefore the filter
1188
+ // planted by auth() -- has always been available here; it was simply
1189
+ // never declared or read.
1190
+ routes[`/:id/${dasherizedName}`] = async (request, { filter } = {}) => {
395
1191
  const record = await store.find(model, getId(request.params));
396
1192
  if (!record)
397
1193
  return 404;
1194
+ // Filtering the PARENT: a caller who may not see the record may not see
1195
+ // what it is related to either.
1196
+ if (isDenied(filter, record))
1197
+ return 404;
398
1198
  const relatedData = record.__relationships[relationshipName];
399
1199
  const baseUrl = getBaseUrl(request);
1200
+ // LINKAGE ONLY. This filter decides which ids the emitted documents may
1201
+ // NAME in their own `relationships.*.data`; it does NOT decide whether
1202
+ // the related records themselves are served -- that is the parent-only
1203
+ // filtering this route has done since #190, and widening it to the
1204
+ // related record is abofs/stonyx-orm#196.
1205
+ const linkage = createLinkageFilter(request);
400
1206
  let data;
401
1207
  if (info.isArray) {
402
1208
  // hasMany - return array
403
1209
  const related = Array.isArray(relatedData) ? relatedData.filter(isOrmRecord) : [];
404
- data = related.map(r => r.toJSON?.({ baseUrl }));
1210
+ data = related.map(r => r.toJSON?.({ baseUrl, linkage }));
405
1211
  }
406
1212
  else {
407
1213
  // belongsTo - return single or null
408
- data = isOrmRecord(relatedData) ? relatedData.toJSON?.({ baseUrl }) : null;
1214
+ data = isOrmRecord(relatedData) ? relatedData.toJSON?.({ baseUrl, linkage }) : null;
409
1215
  }
410
1216
  return {
411
1217
  links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}/${dasherizedName}` },
@@ -413,10 +1219,12 @@ export default class OrmRequest extends Request {
413
1219
  };
414
1220
  };
415
1221
  // Relationship linkage route: GET /:id/relationships/{relationship}
416
- routes[`/:id/relationships/${dasherizedName}`] = async (request) => {
1222
+ routes[`/:id/relationships/${dasherizedName}`] = async (request, { filter } = {}) => {
417
1223
  const record = await store.find(model, getId(request.params));
418
1224
  if (!record)
419
1225
  return 404;
1226
+ if (isDenied(filter, record))
1227
+ return 404;
420
1228
  const relatedData = record.__relationships[relationshipName];
421
1229
  const baseUrl = getBaseUrl(request);
422
1230
  let data;
@@ -445,31 +1253,92 @@ export default class OrmRequest extends Request {
445
1253
  };
446
1254
  };
447
1255
  }
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
- };
1256
+ // Catch-alls for invalid relationship names. Every valid relationship was
1257
+ // registered above, so reaching either of these means the relationship does
1258
+ // not exist and the answer is 404 regardless of the record.
1259
+ //
1260
+ // These deliberately carry NO access check and no store lookup. An earlier
1261
+ // revision of #190 added `if (isDenied(filter, record)) return 404` here for
1262
+ // symmetry with the seven real surfaces, but both branches returned 404, so
1263
+ // the guard was unobservable by construction -- a mutation deleting it
1264
+ // survived the entire suite because no test that could distinguish it can
1265
+ // exist. Unkillable code in an authorization diff reads as coverage and is
1266
+ // not, so it is gone; skipping the lookup also removes the timing difference
1267
+ // between an existing and a missing parent.
1268
+ //
1269
+ // IF THIS ROUTE EVER RETURNS ANYTHING OTHER THAN A CONSTANT 404, it becomes
1270
+ // the eighth surface and must filter the parent first, exactly like
1271
+ // `/:id/{relationship}` above.
1272
+ routes[`/:id/:relationship`] = async () => 404;
1273
+ routes[`/:id/relationships/:relationship`] = async () => 404;
463
1274
  return routes;
464
1275
  }
465
1276
  auth(request, state) {
466
- const access = this.access(request);
467
- if (!access)
468
- return 403;
469
- if (Array.isArray(access) && !access.includes(methodAccessMap[request.method]))
1277
+ // A consumer `access()` that throws is a DENIAL, matching `isDenied` one
1278
+ // layer down. Unguarded it propagates to express's default handler, which
1279
+ // answers 500 -- and the documented sample itself can throw
1280
+ // (`request.originalUrl.split(...)` when originalUrl is absent), so the
1281
+ // failure mode is reachable by following the docs.
1282
+ // -------------------------------------------------------------------------
1283
+ // #202 -- hand the consumer the STRUCTURAL facts, not just the transport.
1284
+ //
1285
+ // Both members are already in hand here. `model` is `this.model`, the name
1286
+ // setup-rest-server mounted this route for; `operation` is the SAME
1287
+ // `methodAccessMap` lookup the permission-array branch at the bottom of
1288
+ // this method performs, so the predicate form and the array form cannot
1289
+ // answer differently about the same request.
1290
+ //
1291
+ // NEITHER IS DERIVED FROM THE REQUEST TARGET, and that is the whole point.
1292
+ // Deriving `model` here from `request.baseUrl` (or from the mounted route
1293
+ // name, or from `getPluralName(this.model)`) would move all five fail-open
1294
+ // variants listed in this file's header OUT of the consumer and INTO the
1295
+ // framework, where every consumer inherits them at once. `this.model` is
1296
+ // assigned once at mount time and no request can influence it.
1297
+ //
1298
+ // `operation` is left UNDEFINED for a method with no entry in
1299
+ // `methodAccessMap`, rather than defaulted. Express delivers HEAD to the
1300
+ // GET handler, so an unmapped method really does reach this line; a
1301
+ // `?? 'read'` here would hand the consumer a fabricated authorisation fact
1302
+ // and turn an unclassified request into an authorised one. Undefined is
1303
+ // the honest answer.
1304
+ //
1305
+ // `record` is deliberately absent -- see `AccessContext` in
1306
+ // src/types/orm-types.ts. Nothing is fetched at this point and adding a
1307
+ // lookup here would put a store read in the middle of an authorization
1308
+ // path. The function return shape below IS the per-record hook.
1309
+ // -------------------------------------------------------------------------
1310
+ const context = {
1311
+ model: this.model,
1312
+ operation: methodAccessMap[request.method],
1313
+ };
1314
+ let access;
1315
+ try {
1316
+ access = this.access(request, context);
1317
+ }
1318
+ catch (error) {
1319
+ // Same reasoning as `isDenied`: fail closed, but say so. An `access()`
1320
+ // that throws denies EVERY request to the collection, and a silent 403
1321
+ // wall is the hardest possible thing to diagnose from the outside.
1322
+ log.error?.(`[@stonyx/orm] access() threw for model "${this.model}" -- denying. ${error instanceof Error ? error.message : String(error)}`);
1323
+ return 403; // Forbidden
1324
+ }
1325
+ // THE READING OF THE RETURN SHAPE LIVES IN ONE PLACE (#234).
1326
+ //
1327
+ // It used to be inline here, and it was the only copy, which was fine while
1328
+ // `auth()` was the only thing that had to ask. It is not any more: the
1329
+ // linkage path has to ask model X's predicate about model X's records while
1330
+ // servicing a request routed to model Y, and a second inline copy of these
1331
+ // six branches would be a second authorization vocabulary -- one that can
1332
+ // drift, and that reviewers would have to notice had drifted. The branch
1333
+ // order in `interpretAccess` is this block, moved, not rewritten.
1334
+ const verdict = interpretAccess(access, methodAccessMap[request.method]);
1335
+ if (!verdict.granted)
470
1336
  return 403;
471
- if (typeof access === 'function')
472
- state.filter = access;
1337
+ // The function return shape is the per-record hook, and `state` is the
1338
+ // whole transport for it: @stonyx/rest-server memoises one state object per
1339
+ // request and hands the same one to `auth()` and to the handler.
1340
+ if (verdict.filter)
1341
+ state.filter = verdict.filter;
473
1342
  return undefined;
474
1343
  }
475
1344
  }