@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.
- package/README.md +968 -11
- package/config/environment.js +8 -0
- package/dist/access-verdict.d.ts +59 -0
- package/dist/access-verdict.js +222 -0
- package/dist/commands.js +34 -0
- package/dist/dynamodb/connection.d.ts +31 -0
- package/dist/dynamodb/connection.js +28 -0
- package/dist/dynamodb/dynamodb-db.d.ts +142 -0
- package/dist/dynamodb/dynamodb-db.js +596 -0
- package/dist/dynamodb/operation-builder.d.ts +76 -0
- package/dist/dynamodb/operation-builder.js +116 -0
- package/dist/dynamodb/type-map.d.ts +31 -0
- package/dist/dynamodb/type-map.js +48 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/main.d.ts +116 -0
- package/dist/main.js +129 -0
- package/dist/manage-record.js +268 -12
- package/dist/mysql/connection.d.ts +1 -0
- package/dist/mysql/mysql-db.d.ts +8 -0
- package/dist/mysql/mysql-db.js +44 -10
- package/dist/orm-request.d.ts +216 -3
- package/dist/orm-request.js +924 -55
- package/dist/postgres/connection.d.ts +1 -0
- package/dist/postgres/connection.js +8 -6
- package/dist/postgres/postgres-db.d.ts +8 -0
- package/dist/postgres/postgres-db.js +44 -10
- package/dist/record.d.ts +16 -0
- package/dist/record.js +62 -6
- package/dist/relationships.js +1 -1
- package/dist/serializer.js +38 -2
- package/dist/setup-rest-server.js +51 -5
- package/dist/standalone-db.js +17 -5
- package/dist/store.d.ts +13 -1
- package/dist/store.js +65 -6
- package/dist/types/orm-types.d.ts +139 -0
- package/dist/utils.d.ts +44 -0
- package/dist/utils.js +47 -0
- package/package.json +16 -7
- package/src/access-verdict.ts +248 -0
- package/src/commands.ts +43 -0
- package/src/dynamodb/connection.ts +50 -0
- package/src/dynamodb/dynamodb-db.ts +811 -0
- package/src/dynamodb/operation-builder.ts +202 -0
- package/src/dynamodb/type-map.ts +54 -0
- package/src/index.ts +10 -0
- package/src/main.ts +133 -0
- package/src/manage-record.ts +294 -18
- package/src/mysql/connection.ts +1 -0
- package/src/mysql/mysql-db.ts +44 -12
- package/src/orm-request.ts +944 -56
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +44 -12
- package/src/record.ts +82 -6
- package/src/relationships.ts +1 -1
- package/src/serializer.ts +39 -2
- package/src/setup-rest-server.ts +59 -6
- package/src/standalone-db.ts +17 -6
- package/src/store.ts +68 -6
- package/src/types/orm-types.ts +146 -1
- package/src/types/stonyx-rest-server.d.ts +14 -1
- package/src/types/stonyx.d.ts +7 -1
- package/src/utils.ts +50 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A lightweight ORM for Stonyx projects, featuring model definitions, serializers,
|
|
|
13
13
|
- **Models**: Define attributes with type-safe proxies (`attr`) and relationships (`hasMany`, `belongsTo`).
|
|
14
14
|
- **Serializers**: Map raw data into model-friendly structures, including nested properties.
|
|
15
15
|
- **Transforms**: Apply custom transformations on data values automatically.
|
|
16
|
-
- **DB Integration**: Optional file-based persistence with auto-save support, or MySQL for production workloads.
|
|
16
|
+
- **DB Integration**: Optional file-based persistence with auto-save support, or MySQL/PostgreSQL/TimescaleDB/DynamoDB for production workloads.
|
|
17
17
|
- **REST Server Integration**: Automatic route setup with customizable access control.
|
|
18
18
|
- **Lifecycle Hooks**: Middleware-based before/after hooks for validation, authorization, side effects, and auditing.
|
|
19
19
|
|
|
@@ -65,13 +65,16 @@ const {
|
|
|
65
65
|
MYSQL_DATABASE,
|
|
66
66
|
MYSQL_CONNECTION_LIMIT,
|
|
67
67
|
MYSQL_MIGRATIONS_DIR,
|
|
68
|
+
DYNAMODB_REGION,
|
|
69
|
+
DYNAMODB_ENDPOINT,
|
|
70
|
+
DYNAMODB_TABLE_PREFIX,
|
|
68
71
|
} = process.env;
|
|
69
72
|
|
|
70
73
|
export default {
|
|
71
74
|
orm: {
|
|
72
75
|
logColor: 'white',
|
|
73
76
|
logMethod: 'db',
|
|
74
|
-
|
|
77
|
+
|
|
75
78
|
db: {
|
|
76
79
|
autosave: DB_AUTO_SAVE ?? 'false',
|
|
77
80
|
file: DB_FILE ?? 'db.json',
|
|
@@ -95,6 +98,12 @@ export default {
|
|
|
95
98
|
connectionLimit: parseInt(MYSQL_CONNECTION_LIMIT ?? '10'),
|
|
96
99
|
migrationsDir: MYSQL_MIGRATIONS_DIR ?? 'migrations',
|
|
97
100
|
migrationsTable: '__migrations',
|
|
101
|
+
autoMigrate: AUTO_MIGRATE === 'true' ? true : AUTO_MIGRATE === 'false' ? false : undefined,
|
|
102
|
+
} : undefined,
|
|
103
|
+
dynamodb: DYNAMODB_REGION ? {
|
|
104
|
+
region: DYNAMODB_REGION,
|
|
105
|
+
endpoint: DYNAMODB_ENDPOINT, // optional, for DynamoDB Local
|
|
106
|
+
tablePrefix: DYNAMODB_TABLE_PREFIX, // optional table name prefix
|
|
98
107
|
} : undefined,
|
|
99
108
|
restServer: {
|
|
100
109
|
enabled: ORM_USE_REST_SERVER ?? 'true',
|
|
@@ -243,6 +252,31 @@ Set the `MYSQL_HOST` environment variable to enable MySQL persistence. The ORM l
|
|
|
243
252
|
| `stonyx db:migrate` | Apply pending migrations |
|
|
244
253
|
| `stonyx db:migrate:rollback` | Rollback the most recent migration |
|
|
245
254
|
| `stonyx db:migrate:status` | Show migration status |
|
|
255
|
+
| `stonyx db:sync` | Sync DynamoDB table definitions to match current model schemas |
|
|
256
|
+
|
|
257
|
+
### DynamoDB Mode
|
|
258
|
+
|
|
259
|
+
Set the `DYNAMODB_REGION` environment variable to enable DynamoDB persistence. Tables are created with PAY_PER_REQUEST (on-demand) billing. Global Secondary Indexes (GSIs) are auto-provisioned at startup based on model `belongsTo` relationships — each FK column gets a GSI. `findAll()` with conditions routes to a GSI Query when the condition key matches a GSI partition key; non-indexed attribute conditions fall back to Scan + FilterExpression (expensive for large tables). ULID generation replaces auto-increment for numeric-ID models.
|
|
260
|
+
|
|
261
|
+
```javascript
|
|
262
|
+
dynamodb: {
|
|
263
|
+
region: 'us-east-1',
|
|
264
|
+
endpoint: 'http://localhost:8000', // optional, for DynamoDB Local
|
|
265
|
+
tablePrefix: 'myapp-', // optional table name prefix
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Environment variables:
|
|
270
|
+
|
|
271
|
+
* `DYNAMODB_REGION`: AWS region for DynamoDB (e.g., `'us-east-1'`).
|
|
272
|
+
* `DYNAMODB_ENDPOINT`: Optional custom endpoint URL, useful for DynamoDB Local during development.
|
|
273
|
+
* `DYNAMODB_TABLE_PREFIX`: Optional prefix prepended to all table names (e.g., `'myapp-'` yields `'myapp-animals'`).
|
|
274
|
+
|
|
275
|
+
**Peer dependencies:** `@aws-sdk/client-dynamodb` and `@aws-sdk/lib-dynamodb` must be installed when using the DynamoDB driver. The AWS SDK is dynamically imported and only loaded when the DynamoDB driver is selected.
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
npm install @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
|
|
279
|
+
```
|
|
246
280
|
|
|
247
281
|
### Running MySQL Tests
|
|
248
282
|
|
|
@@ -275,19 +309,901 @@ import setupRestServer from '@stonyx/orm/setup-rest-server';
|
|
|
275
309
|
await setupRestServer('/', './access');
|
|
276
310
|
```
|
|
277
311
|
|
|
278
|
-
Access classes define models and provide custom filtering/authorization logic
|
|
312
|
+
Access classes define models and provide custom filtering/authorization logic.
|
|
313
|
+
|
|
314
|
+
> **Do not reconstruct the request path inside `access()`. Read
|
|
315
|
+
> [Identifying the collection](#identifying-the-collection) before copying this.**
|
|
316
|
+
> Every attempt to identify the collection by parsing the request target has
|
|
317
|
+
> failed **open** — five distinct variants of this same example, each found only
|
|
318
|
+
> after the previous was fixed, by five different people. That section is now a
|
|
319
|
+
> record of what not to do, not a matching recipe: the sample below reads `model`
|
|
320
|
+
> from [the access context](#the-access-context-second-argument) and never looks
|
|
321
|
+
> at the mount at all, so variants 1, 2, 4 and 5 are **unconstructible** against
|
|
322
|
+
> it rather than merely handled. **Variant 3 survives.** It is the general shape
|
|
323
|
+
> "a hand-written matcher normalises differently from the router", and the
|
|
324
|
+
> migrated sample still runs one string comparison — the `/archived` sub-path
|
|
325
|
+
> deny — which folds case but does not decode, so `GET /owners/%61rchived` steps
|
|
326
|
+
> past it ([#228](https://github.com/abofs/stonyx-orm/issues/228)).
|
|
327
|
+
>
|
|
328
|
+
> That is still a stopgap. **The real fix is
|
|
329
|
+
> [#202](https://github.com/abofs/stonyx-orm/issues/202)** — `access()` should
|
|
330
|
+
> receive the model, the operation and the record, so there is nothing to
|
|
331
|
+
> identify. Until it lands, prefer the array shape (`['read']`) or `false` where
|
|
332
|
+
> you can: the **function** shape is the one that requires any matching at all.
|
|
333
|
+
>
|
|
334
|
+
> The one read of argument **one** that survives is `request.path`, for the
|
|
335
|
+
> `/archived` sub-path deny — and it has to. The context names which model and
|
|
336
|
+
> which verb, not which route, so that deny **cannot be expressed from the
|
|
337
|
+
> context alone** and a context-only rewrite would silently turn it into an
|
|
338
|
+
> allow.
|
|
339
|
+
>
|
|
340
|
+
> The same warning is repeated at the top of `src/orm-request.ts`, which ships;
|
|
341
|
+
> the longer write-up in `docs/usage-patterns.md` does **not** ship, so this
|
|
342
|
+
> README and that source header are the two copies a consumer sees.
|
|
343
|
+
>
|
|
344
|
+
> This sample is the same code as the shipped test fixture, and a test asserts
|
|
345
|
+
> the two `access()` bodies are identical line for line. For four rounds they
|
|
346
|
+
> were two independently written copies — and the fifth fail-open variant was
|
|
347
|
+
> found in the one nothing was mutating.
|
|
279
348
|
|
|
280
349
|
```js
|
|
281
350
|
export default class GlobalAccess {
|
|
282
351
|
models = ['owner', 'animal'];
|
|
283
352
|
|
|
284
|
-
access(request) {
|
|
285
|
-
|
|
353
|
+
access(request, { model, operation }) {
|
|
354
|
+
// `model` is the model this route was mounted for. It is assigned once, at
|
|
355
|
+
// mount time, and no request can influence it — not a mount prefix, not a
|
|
356
|
+
// query string, not a case-varied path, not an absolute-form request
|
|
357
|
+
// target. Nothing below parses anything, so variants 1, 2, 4 and 5 are not
|
|
358
|
+
// constructible against this predicate any more — they are history, not
|
|
359
|
+
// rules to follow. VARIANT 3 IS THE EXCEPTION AND THE CLAIM IS NARROWER
|
|
360
|
+
// THAN IT WAS: a matcher stricter than the router can still be stepped
|
|
361
|
+
// around, because the sub-path rule below is still a string comparison.
|
|
362
|
+
// Case is handled; percent-encoding is not — abofs/stonyx-orm#228.
|
|
363
|
+
//
|
|
364
|
+
// `operation` is destructured to name the whole contract at the point of
|
|
365
|
+
// use. This sample's rules are per-model and per-sub-path rather than
|
|
366
|
+
// per-verb, so it does not branch on it; the permission array at the bottom
|
|
367
|
+
// is where the verb is answered.
|
|
368
|
+
|
|
369
|
+
// FAIL CLOSED ON ARGUMENT TWO. `model` is absent for any caller that
|
|
370
|
+
// resolved this predicate without supplying the context, and a request this
|
|
371
|
+
// function cannot identify DENIES rather than falling through to the CRUD
|
|
372
|
+
// grant at the bottom. An unidentifiable input must never be the permissive
|
|
373
|
+
// path. Argument ONE is guarded at its own read, below — this guard does
|
|
374
|
+
// not cover it.
|
|
375
|
+
if (typeof model !== 'string' || model === '') return false;
|
|
376
|
+
|
|
377
|
+
if (model === 'owner') {
|
|
378
|
+
// The context names WHICH MODEL and WHICH VERB — not which route. Six
|
|
379
|
+
// distinct owner surfaces produce one identical context, so a rule that
|
|
380
|
+
// depends on the SUB-PATH still needs argument one. `request.path` is
|
|
381
|
+
// mount-relative and query-free, and it is the one read of the raw
|
|
382
|
+
// request the README sanctions. false → 403 for the whole request.
|
|
383
|
+
//
|
|
384
|
+
// THIS DENY CANNOT BE EXPRESSED FROM THE CONTEXT ALONE. Migrating it away
|
|
385
|
+
// does not remove a rule, it turns a deny into an ALLOW, silently.
|
|
386
|
+
//
|
|
387
|
+
// FAIL CLOSED ON ARGUMENT ONE TOO. The guard above covers the context;
|
|
388
|
+
// this one covers the request, and since #202 they are two different
|
|
389
|
+
// objects. A caller that resolves this predicate through the documented
|
|
390
|
+
// `Orm.instance.getAccess()` path and hand-assembles a request can supply
|
|
391
|
+
// a perfectly valid context with no usable `path` — and
|
|
392
|
+
// `String(request.path ?? '')` is then `''`, which matches no sub-path
|
|
393
|
+
// rule and falls straight through to the per-record filter below. That is
|
|
394
|
+
// a DENY becoming an ALLOW. An input this function cannot identify DENIES,
|
|
395
|
+
// whichever ARGUMENT it arrived on — which is also why the `?? ''` this
|
|
396
|
+
// file's header condemns does not appear below.
|
|
397
|
+
if (typeof request?.path !== 'string' || request.path === '') return false;
|
|
398
|
+
|
|
399
|
+
// Lower-cased because the router matched case-insensitively, so a
|
|
400
|
+
// case-sensitive rule here would be stricter than the router and could be
|
|
401
|
+
// stepped around.
|
|
402
|
+
//
|
|
403
|
+
// CASE-FOLDING ALONE IS NOT A SUFFICIENT NORMALISATION, and this line is
|
|
404
|
+
// not a recipe for one. Express sets `request.path` from the RAW pathname
|
|
405
|
+
// while the router DECODES `:id`, so `GET /owners/%61rchived` reaches this
|
|
406
|
+
// comparison as `/%61rchived`, walks past the deny, and is dispatched as
|
|
407
|
+
// the record `archived` — abofs/stonyx-orm#228. A matcher must normalise
|
|
408
|
+
// the way the router that dispatched the request does. Record ids are
|
|
409
|
+
// case-sensitive and must be compared at their real case.
|
|
410
|
+
const path = request.path.toLowerCase();
|
|
411
|
+
|
|
412
|
+
if (path === '/archived' || path.startsWith('/archived/')) return false;
|
|
413
|
+
|
|
414
|
+
// Returning a function plugs it in as a per-record filter, and it is
|
|
415
|
+
// enforced on every surface addressed to one of these records:
|
|
416
|
+
// /owners, /owners/:id, /owners/:id/pets, /owners/:id/relationships/pets
|
|
417
|
+
// A rejected record is 404 on record routes — the same status as a record
|
|
418
|
+
// that does not exist — so the filter is not an existence oracle.
|
|
419
|
+
return record => record.id !== 'angela' && record.id !== 'restricted';
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// `record.owner` resolves to an OrmRecord, not to the owner's id string —
|
|
423
|
+
// comparing it directly against a string is the bug that made this predicate
|
|
424
|
+
// inert. Deliberately NO `?? record.owner` fallback: accepting the raw
|
|
425
|
+
// shape as well as the resolved one would absorb a resolution regression
|
|
426
|
+
// silently, which is exactly what blinded this fixture before.
|
|
427
|
+
if (model === 'animal') return record => record.owner?.id !== 'restricted';
|
|
428
|
+
|
|
429
|
+
// Allows full access to all calls that don't match any of the above conditions
|
|
286
430
|
return ['read', 'create', 'update', 'delete'];
|
|
287
431
|
}
|
|
288
432
|
}
|
|
289
433
|
```
|
|
290
434
|
|
|
435
|
+
|
|
436
|
+
### The access context (second argument)
|
|
437
|
+
|
|
438
|
+
`access()` is called with **two** arguments:
|
|
439
|
+
|
|
440
|
+
```js
|
|
441
|
+
access(request, { model, operation })
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
The second is the **access context** — the structural facts about the request,
|
|
445
|
+
which the framework already holds at authorization time. Read these instead of
|
|
446
|
+
parsing anything.
|
|
447
|
+
|
|
448
|
+
| Key | Value |
|
|
449
|
+
|---|---|
|
|
450
|
+
| `model` | The model this route was mounted for, as a **model name**: kebab-case, exactly as declared under `config.orm.paths.model` and keyed in the store — `'owner'`, `'animal'`, `'phone-number'`. **Not** the pluralized, dasherized, mount-prefixed *route* name. |
|
|
451
|
+
| `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary *on this path*. Never an HTTP method name like `'GET'`, and **not** the hook vocabulary either (see [below](#operation-is-not-the-hook-operation)). `undefined` when the dispatched method has no entry in the framework's method map. |
|
|
452
|
+
|
|
453
|
+
So a predicate can be written without reference to any URL:
|
|
454
|
+
|
|
455
|
+
```js
|
|
456
|
+
export default class OwnerAccess {
|
|
457
|
+
models = ['owner'];
|
|
458
|
+
|
|
459
|
+
access(request, { model, operation }) {
|
|
460
|
+
if (model === 'owner' && operation === 'read') {
|
|
461
|
+
return record => record.id !== 'angela';
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return ['read'];
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
There is no string to parse, no variant to miss, and no way to fail open through
|
|
470
|
+
a URL shape nobody anticipated. `model` is fixed at mount time and no request
|
|
471
|
+
can influence it — not a mount prefix, not a query string, not a case-varied
|
|
472
|
+
path, not an absolute-form request target.
|
|
473
|
+
|
|
474
|
+
#### What the context does not tell you: which surface
|
|
475
|
+
|
|
476
|
+
It names **which model and which verb**, not **which route**. Measured over the
|
|
477
|
+
live router, six surfaces produce one identical context:
|
|
478
|
+
|
|
479
|
+
```
|
|
480
|
+
GET /owners { model: 'owner', operation: 'read' }
|
|
481
|
+
GET /owners/gina { model: 'owner', operation: 'read' }
|
|
482
|
+
GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
483
|
+
GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
484
|
+
GET /owners/archived { model: 'owner', operation: 'read' }
|
|
485
|
+
GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
So a rule that depends on the **sub-path** still needs `request.path` —
|
|
489
|
+
mount-relative and query-free, and the one read of argument one that
|
|
490
|
+
[Identifying the collection](#identifying-the-collection) sanctions. The sample
|
|
491
|
+
access class shipped with this repo has such a rule: its `/archived` deny
|
|
492
|
+
**cannot be expressed from the context alone**, and a predicate migrated to
|
|
493
|
+
context-only would silently drop it — a deny becoming an allow.
|
|
494
|
+
|
|
495
|
+
Note also that the related-resource and `?include=` surfaces serve *another
|
|
496
|
+
model's* records under `model: 'owner'`, and the context gives a predicate no
|
|
497
|
+
signal that it is authorizing a related-resource route. That is
|
|
498
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196).
|
|
499
|
+
|
|
500
|
+
#### `operation` is not the hook `operation`
|
|
501
|
+
|
|
502
|
+
This module exposes a **second** `operation` vocabulary, on an identically-named
|
|
503
|
+
key of an identically-shaped context object:
|
|
504
|
+
[hook contexts](#hook-context-object) carry `list` / `get` / `create` /
|
|
505
|
+
`update` / `delete`. The access vocabulary collapses `list` and `get` into
|
|
506
|
+
`'read'`, so for one `GET /animals/1` a hook sees `'get'` while `access()` sees
|
|
507
|
+
`'read'` — and a predicate cannot distinguish a collection read from a
|
|
508
|
+
record read.
|
|
509
|
+
|
|
510
|
+
"No second vocabulary" above is a statement about the **access path**, where
|
|
511
|
+
both the context and the permission array come from one method map. It is not a
|
|
512
|
+
statement about the module. Writing `operation === 'get'` in a predicate never
|
|
513
|
+
matches, and a predicate that stops matching falls through to the permission
|
|
514
|
+
array — so the misreading is fail-open shaped. In TypeScript the exported
|
|
515
|
+
`AccessOperation` union makes it a compile error.
|
|
516
|
+
|
|
517
|
+
The four `operation` values are the same four strings the permission-array
|
|
518
|
+
return shape is written in (`['read', 'create', 'update', 'delete']`), because
|
|
519
|
+
both come from one method map inside the framework. The two forms cannot
|
|
520
|
+
disagree about the same request.
|
|
521
|
+
|
|
522
|
+
**`operation` is `undefined`, never defaulted, for an unmapped method.** Express
|
|
523
|
+
delivers `HEAD` to the `GET` handler, so this is reachable. It is deliberately
|
|
524
|
+
not defaulted to `'read'`: a fabricated operation would turn an unclassified
|
|
525
|
+
request into an authorized one. Treat `undefined` as *not classified* and deny.
|
|
526
|
+
|
|
527
|
+
**The second argument is additive.** JavaScript ignores extra arguments, so an
|
|
528
|
+
existing `access(request)` predicate keeps working exactly as it did. Nothing
|
|
529
|
+
needs to be migrated to keep running — but note that argument **one** is still
|
|
530
|
+
the raw request, so the warning in
|
|
531
|
+
[Identifying the collection](#identifying-the-collection) still applies to any
|
|
532
|
+
predicate that reads it.
|
|
533
|
+
|
|
534
|
+
#### `record` is not in the context
|
|
535
|
+
|
|
536
|
+
Deliberately, and it is not an oversight. `auth()` runs after route matching but
|
|
537
|
+
**before any handler executes**, so nothing has been fetched yet. Supplying a
|
|
538
|
+
record would force a pre-fetch on every request — a second store hit, a new
|
|
539
|
+
failure mode, and an ordering change in the middle of an authorization path.
|
|
540
|
+
|
|
541
|
+
It is also unnecessary: the **function** return shape already *is* the
|
|
542
|
+
per-record hook. Return `(record) => boolean` and the handlers apply it to every
|
|
543
|
+
record the request touches. Auth-time and record-time are separate decision
|
|
544
|
+
points, and the contract keeps them separate.
|
|
545
|
+
|
|
546
|
+
#### Reaching another model's predicate
|
|
547
|
+
|
|
548
|
+
The model → predicate map is published on the ORM instance at boot, before any
|
|
549
|
+
route is mounted, so a predicate can be resolved by model name and asked about a
|
|
550
|
+
request routed to a *different* model:
|
|
551
|
+
|
|
552
|
+
```js
|
|
553
|
+
import Orm from '@stonyx/orm';
|
|
554
|
+
|
|
555
|
+
const predicate = Orm.instance.getAccess('animal');
|
|
556
|
+
if (!predicate) return deny;
|
|
557
|
+
|
|
558
|
+
const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
**`undefined` means no predicate could be resolved — not that the model is
|
|
562
|
+
unrestricted. Treat it as deny.** It covers a model with no access class *and* a
|
|
563
|
+
model whose access class failed to **load**: a load failure is caught and warned
|
|
564
|
+
about, and the partial map is published anyway, so a missing key is not evidence
|
|
565
|
+
of an unrestricted model. This is the same rule as `operation === undefined`
|
|
566
|
+
above, and for the same reason.
|
|
567
|
+
|
|
568
|
+
The raw map is `Orm.instance.accessFunctions`, keyed by model name; prefer
|
|
569
|
+
`getAccess()` — it is guarded against inherited `Object.prototype` members and a
|
|
570
|
+
direct index is not. Note that it maps a model name to the predicate of the
|
|
571
|
+
access *class* that claims it, which may claim many models: against this repo's
|
|
572
|
+
sample, `getAccess('owner') === getAccess('animal')`.
|
|
573
|
+
|
|
574
|
+
#### Passing the context makes a model-correct answer *possible*
|
|
575
|
+
|
|
576
|
+
It does not make the answer model-correct on its own. **The resolved predicate
|
|
577
|
+
has to read the context.** Against a predicate that ignores it the failure is
|
|
578
|
+
measurable. On a request Express dispatched to `GET /owners/angela`, asked about
|
|
579
|
+
**animals**, the sample as it shipped before
|
|
580
|
+
[#222](https://github.com/abofs/stonyx-orm/issues/222) answered:
|
|
581
|
+
|
|
582
|
+
```
|
|
583
|
+
getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
584
|
+
-> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
That is the **owners** filter, and it returns `true` for animal 21 — the record
|
|
588
|
+
hidden on every animal surface. Under a mount such a predicate recognizes
|
|
589
|
+
neither way it is worse still: it falls through to
|
|
590
|
+
`['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
|
|
591
|
+
context was supplied and the answer is not the animal answer, and it is wrong in
|
|
592
|
+
the direction that **grants** — because that predicate was single-argument and
|
|
593
|
+
identified its collection from the request, so it answered about the collection
|
|
594
|
+
the request was *addressed to* while being asked about another one.
|
|
595
|
+
|
|
596
|
+
The sample shipped with this repo has since been migrated to read the context,
|
|
597
|
+
and the same call now answers with the **animal** filter:
|
|
598
|
+
|
|
599
|
+
```
|
|
600
|
+
getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
601
|
+
-> record => record.owner?.id !== 'restricted'
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
A single-argument predicate remains the default in every consumer tree, and a
|
|
605
|
+
caller has no supported way to tell which kind it resolved. The boot-time arity
|
|
606
|
+
warning that surfaces one is
|
|
607
|
+
[#221](https://github.com/abofs/stonyx-orm/issues/221).
|
|
608
|
+
|
|
609
|
+
So: pass the context, and do not treat a resolved predicate's answer as
|
|
610
|
+
model-specific until that predicate has been migrated to read it.
|
|
611
|
+
|
|
612
|
+
### Return values
|
|
613
|
+
|
|
614
|
+
| `access()` returns | Effect |
|
|
615
|
+
|---|---|
|
|
616
|
+
| `false` (or any falsy value) | `403` for the whole request |
|
|
617
|
+
| `true` | full access, no filter |
|
|
618
|
+
| `['read', 'create', 'update', 'delete']` | the listed operations only; anything else is `403` |
|
|
619
|
+
| `'read'` (a bare string) | **one** permission, equivalent to `['read']` |
|
|
620
|
+
| a function | a per-record filter — see below |
|
|
621
|
+
| anything else | `403` — unknown shapes fail **closed** |
|
|
622
|
+
|
|
623
|
+
A `throw` inside `access()` is a **denial**, not a 500.
|
|
624
|
+
|
|
625
|
+
### Filter functions
|
|
626
|
+
|
|
627
|
+
A function return value is a **per-record predicate**, and it is enforced on
|
|
628
|
+
every endpoint that is addressed to a record — not only on the collection.
|
|
629
|
+
|
|
630
|
+
It is evaluated against the record the route is *addressed to*, **on that model
|
|
631
|
+
only**. It is not a guarantee that a hidden record cannot be reached or modified:
|
|
632
|
+
a write to a *different* collection can still re-parent one. See
|
|
633
|
+
[Known limitations](#known-limitations) and
|
|
634
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207).
|
|
635
|
+
|
|
636
|
+
| Endpoint | A record the predicate rejects |
|
|
637
|
+
|---|---|
|
|
638
|
+
| `GET /:models` | omitted from the collection |
|
|
639
|
+
| `GET /:models/:id` | `404` |
|
|
640
|
+
| `GET /:models/:id/{relationship}` | `404` — the **addressed** record is filtered, not the related one |
|
|
641
|
+
| `GET /:models/:id/relationships/{relationship}` | `404` — same |
|
|
642
|
+
| `PATCH /:models/:id` | `404`, no attribute is applied |
|
|
643
|
+
| `DELETE /:models/:id` | `404`, the record is not removed and no SQL `DELETE` is issued |
|
|
644
|
+
| `POST /:models` | `403`, and a record **this request inserted** is rolled back — see [Known limitations](#known-limitations) for why the rollback is conditional |
|
|
645
|
+
|
|
646
|
+
**Denied record-level requests return 404, not 403.** This is deliberate and it
|
|
647
|
+
is the property most easily "improved" away. 403 would confirm that the record
|
|
648
|
+
exists to a caller who is not allowed to know that, which turns the filter into
|
|
649
|
+
an existence oracle: `404` means "no such record", `403` means "there is one and
|
|
650
|
+
it is not yours". Every status on a record route must therefore be identical for
|
|
651
|
+
"filtered out" and "does not exist" — including `DELETE`, which is why deleting
|
|
652
|
+
a record that never existed also returns 404 rather than 204.
|
|
653
|
+
|
|
654
|
+
`POST` is the one exception and returns **403**, because 404 on a mounted
|
|
655
|
+
collection route is indistinguishable from "model not mounted" — a genuinely
|
|
656
|
+
different failure a developer needs to diagnose.
|
|
657
|
+
|
|
658
|
+
**A client-supplied `id` on `POST` is refused with `403` whenever a function
|
|
659
|
+
filter is in force.** This is the part that keeps `POST` from being an
|
|
660
|
+
enumeration oracle, and it is worth understanding rather than working around.
|
|
661
|
+
The duplicate-id check has to run before the filter, and it sees records the
|
|
662
|
+
filter hides, so the *status* of a `POST` otherwise leaks whether an id is
|
|
663
|
+
taken:
|
|
664
|
+
|
|
665
|
+
| `POST /animals` with a payload the caller may create | before | now |
|
|
666
|
+
|---|---|---|
|
|
667
|
+
| an id held by a record the filter **hides** | `403` | `403` |
|
|
668
|
+
| an id that is **free** | `200` | `403` |
|
|
669
|
+
| an id held by a record the caller **can see** | `409` | `403` |
|
|
670
|
+
|
|
671
|
+
Three outcomes, one request per id, the whole id space. Filtering only the
|
|
672
|
+
*collision* status narrows that to callers who cannot create a record they are
|
|
673
|
+
allowed to see; it does not close it. It cannot be closed while a caller both
|
|
674
|
+
chooses the id and learns whether the create succeeded — so under a filter the
|
|
675
|
+
caller does not choose the id. The refusal happens before any store lookup, so
|
|
676
|
+
neither the status nor the response time depends on whether the id exists.
|
|
677
|
+
|
|
678
|
+
Let the server assign the id and read it back from the response — and read it
|
|
679
|
+
back rather than predicting it, because the value it returns is documented but
|
|
680
|
+
not stable across model kinds: a numeric-id model gets `max + 1` (or, at the
|
|
681
|
+
numeric ceiling, the lowest free integer), and a string-id model gets
|
|
682
|
+
`<model>-<n>`. See breaking change 8.
|
|
683
|
+
|
|
684
|
+
**What a server-assigned id is not.** It is not a secret. On a string-id
|
|
685
|
+
collection it is dense and enumerable from `1`, where previously it inherited
|
|
686
|
+
whatever entropy the last-inserted id happened to carry — a UUID-seeded store
|
|
687
|
+
answered a UUID-derived key. If a collection has **no** `access` config its
|
|
688
|
+
record-level routes are ungated, so the id was the only thing standing between
|
|
689
|
+
an unauthenticated caller and `GET`/`PATCH`/`DELETE` on a record. That was never
|
|
690
|
+
a control and must not become one; configure `access`.
|
|
691
|
+
|
|
692
|
+
**And the id itself is an occupancy signal — on both model kinds.**
|
|
693
|
+
`assignRecordId` reads the whole store, not the caller's filtered view — it
|
|
694
|
+
never sees `state.filter` — so the id it returns is a function of records the
|
|
695
|
+
caller may not be permitted to read. **This applies to numeric-id collections
|
|
696
|
+
as well as string-id ones**, and the conditions differ, so read both:
|
|
697
|
+
|
|
698
|
+
- **String-id collections, always.** The assigned `n` is the smallest positive
|
|
699
|
+
integer whose landing key is free, which tells the caller that every key
|
|
700
|
+
below it is taken, hidden or not.
|
|
701
|
+
- **Numeric-id collections, once one record sits at the numeric ceiling.** The
|
|
702
|
+
normal answer is `max + 1`, which discloses only the maximum. But `max + 1`
|
|
703
|
+
is not representable at or above 2^53, so the walk restarts from `1` (see
|
|
704
|
+
breaking change 8) and the assigned id becomes the smallest free integer —
|
|
705
|
+
the same occupancy predicate, now over arbitrary low keys. Each subsequent
|
|
706
|
+
no-id `POST` names the next free one, so a caller can enumerate the holes in
|
|
707
|
+
a range it cannot read.
|
|
708
|
+
|
|
709
|
+
**A ceiling record reaches a filter-protected collection even though `POST`
|
|
710
|
+
refuses caller ids on one.** Breaking change 3 makes
|
|
711
|
+
`POST /animals {"id": 9007199254740992}` answer `403`, but the same id lands
|
|
712
|
+
through a *relationship write on another collection* —
|
|
713
|
+
`POST /owners` carrying `attributes: { pets: [{ "id": 9007199254740992 }] }`
|
|
714
|
+
creates the animal under that key
|
|
715
|
+
([#207](https://github.com/abofs/stonyx-orm/issues/207), the same channel the
|
|
716
|
+
**Known limitations** re-parenting note describes). So the precondition is
|
|
717
|
+
reachable by an unauthenticated caller on exactly the collections `access`
|
|
718
|
+
exists to protect. Measured on the sample fixture, with every animal hidden by
|
|
719
|
+
the `/animals` predicate and keys 4 and 7 deleted:
|
|
720
|
+
|
|
721
|
+
```
|
|
722
|
+
GET /animals -> 200 [] (nothing visible)
|
|
723
|
+
GET /animals/4 -> 404 (free — indistinguishable from hidden)
|
|
724
|
+
POST /animals {"id":4} -> 403 (breaking change 3)
|
|
725
|
+
POST /owners {... pets:[{"id":9007199254740992}]} -> 200 (#207 plants the ceiling record)
|
|
726
|
+
POST /animals (no id) -> 200 id=4 <- names a hole in the hidden range
|
|
727
|
+
POST /animals (no id) -> 200 id=7 <- and the other one
|
|
728
|
+
POST /animals (no id) -> 200 id=13
|
|
729
|
+
POST /animals (no id) -> 200 id=14
|
|
730
|
+
```
|
|
731
|
+
|
|
732
|
+
Closing this requires the assignment to be filter-aware, which is a change to
|
|
733
|
+
the `access` contract rather than a fix; it is stated here rather than left to
|
|
734
|
+
be discovered. Callers with no function-style filter are unaffected — there are
|
|
735
|
+
no hidden records to disclose.
|
|
736
|
+
|
|
737
|
+
### Identifying the collection
|
|
738
|
+
|
|
739
|
+
**Do not reconstruct the request path — and since
|
|
740
|
+
[#202](https://github.com/abofs/stonyx-orm/issues/202) you do not have to
|
|
741
|
+
identify the collection at all.** Read `model` from
|
|
742
|
+
[the access context](#the-access-context-second-argument): it is fixed at mount
|
|
743
|
+
time, no request can influence it, and there is nothing left to parse.
|
|
744
|
+
|
|
745
|
+
**Everything below is the record of what happened when this sample did parse
|
|
746
|
+
it.** It is kept as history, not as a recipe — none of these matching strategies
|
|
747
|
+
should be written into a new predicate. Every version of this sample that tried
|
|
748
|
+
to identify the collection from the request target failed **open**, and each
|
|
749
|
+
variant was found only after the previous one was fixed:
|
|
750
|
+
|
|
751
|
+
| # | Variant | Why it fails open |
|
|
752
|
+
|---|---|---|
|
|
753
|
+
| 1 | match `request.url` | `RestServer.mountRoute` mounts each model as an Express **sub-app**, so `url` is mount-relative — `GET /owners/angela` arrives as `/angela`. A `/owners` prefix match is **always false**, so the branch never fires and `access()` falls through to whatever it returns last. |
|
|
754
|
+
| 2 | anchored match on a raw `request.originalUrl` | `originalUrl` carries the query string, so `=== '/owners'` misses `/owners?filter[age]=30` and that collection comes back unfiltered. `endsWith('/owners')` is the older half of the same trap: it leaves every record route unguarded. |
|
|
755
|
+
| 3 | case-sensitive matcher | `RestServer` mounts with a bare `express()`, whose default is `caseSensitive: false`. A matcher stricter than the router that dispatched the request can simply be stepped around: `GET /owners/angela` → 404 but `GET /OwNeRs/angela` → 200 in full, and `DELETE /ANIMALS/22` destroyed a hidden record. Router-side fix: [stonyx-rest-server#47](https://github.com/abofs/stonyx-rest-server/issues/47). |
|
|
756
|
+
| 4 | hard-coded `/owners` | With `ORM_REST_ROUTE=/api` every url becomes `/api/owners/...` and the sample matches nothing — environment-specifically, which is harder to notice than failing everywhere. The remediation this document used to give was itself broken: `` `${config.orm.restServer.route}owners` `` evaluates to **`/apiowners`**, so a reader who followed the correction exactly still failed open and believed they had handled it. |
|
|
757
|
+
| 5 | any match on `originalUrl` at all | HTTP/1.1 permits an **absolute-form** request-target. Express routes on `parseurl(req).pathname`, so the request dispatches normally — but `originalUrl` is the raw target. `GET http://anything.example/owners/angela` yields `originalUrl === 'http://anything.example/owners/angela'`, which has no `/owners` prefix. Measured: the record came back in full, `DELETE` succeeded, and it walked past a hard `return false` deny the same way. |
|
|
758
|
+
|
|
759
|
+
**The fix is not a sixth rule, and it is not a better string to match.** It is
|
|
760
|
+
to stop identifying the collection at all. That is a statement about
|
|
761
|
+
**identifying the collection**, and it is not a statement about the sample as a
|
|
762
|
+
whole: the `/archived` sub-path rule *is* still a string match, and
|
|
763
|
+
[#228](https://github.com/abofs/stonyx-orm/issues/228) is a sixth spelling that
|
|
764
|
+
gets past it. Sub-path rules are the residue this fix does not cover, which is
|
|
765
|
+
why they must normalise the way the router does.
|
|
766
|
+
|
|
767
|
+
An intermediate revision read **`request.baseUrl`** — the mount Express
|
|
768
|
+
*actually matched*. That closed all five variants: it carries no query string
|
|
769
|
+
(variant 2), it is not mount-relative (variant 1), it already contains the
|
|
770
|
+
configured `ORM_REST_ROUTE` prefix (variant 4 — there is nothing left to derive,
|
|
771
|
+
so `/apiowners` is unconstructible), and it is unaffected by an absolute-form
|
|
772
|
+
target (variant 5). It was still a transport artifact standing in for a
|
|
773
|
+
structural fact, and it is **no longer what the sample does**: the sample reads
|
|
774
|
+
`model`, so variants 1, 2, 4 and 5 are unconstructible against it rather than
|
|
775
|
+
handled. **Variant 3 survives**, in the one string comparison the migration
|
|
776
|
+
leaves behind: the `/archived` sub-path deny folds case but does not decode
|
|
777
|
+
([#228](https://github.com/abofs/stonyx-orm/issues/228)). The table below is
|
|
778
|
+
retained as the measured evidence behind the five variants, not because any of
|
|
779
|
+
these values should be matched on:
|
|
780
|
+
|
|
781
|
+
| request | `request.url` | `request.originalUrl` | `request.baseUrl` | `request.path` |
|
|
782
|
+
|---|---|---|---|---|
|
|
783
|
+
| `GET /owners` | `/` | `/owners` | `/owners` | `/` |
|
|
784
|
+
| `GET /owners/angela` | `/angela` | `/owners/angela` | `/owners` | `/angela` |
|
|
785
|
+
| `GET /owners/angela?filter[age]=30` | `/angela?filter[age]=30` | `/owners/angela?filter[age]=30` | `/owners` | `/angela` |
|
|
786
|
+
| `GET /OwNeRs/angela` | `/angela` | `/OwNeRs/angela` | `/OwNeRs` | `/angela` |
|
|
787
|
+
| `GET http://anything.example/owners/angela` | `http://anything.example/angela` | `http://anything.example/owners/angela` | `/owners` | `/angela` |
|
|
788
|
+
| `GET /api/animals/22` (`ORM_REST_ROUTE=/api`) | `/22` | `/api/animals/22` | `/api/animals` | `/22` |
|
|
789
|
+
|
|
790
|
+
**One read of argument one survives, and it must: `request.path`.** It is
|
|
791
|
+
mount-relative and query-free, and it is for rules that distinguish **sub-paths**
|
|
792
|
+
beneath the mount — as the `/archived` deny in the sample above does. The context
|
|
793
|
+
names which model and which verb, **not which route**, so that deny *cannot be
|
|
794
|
+
expressed from the context alone*, and a context-only rewrite would silently turn
|
|
795
|
+
it into an allow.
|
|
796
|
+
|
|
797
|
+
**Normalise the way the router that dispatched the request does — and
|
|
798
|
+
case-folding alone does not.** A matcher stricter than the router can be stepped
|
|
799
|
+
around, so the sample lower-cases before comparing (the router matched
|
|
800
|
+
case-insensitively). That closes the case gap and **it is not the whole rule**:
|
|
801
|
+
Express sets `request.path` from the **raw, undecoded** pathname while the router
|
|
802
|
+
**decodes** `:id`, so `GET /owners/%61rchived` reaches a `path === '/archived'`
|
|
803
|
+
comparison as `/%61rchived`, walks past the deny, and is dispatched as the record
|
|
804
|
+
`archived`. That gap is live in the sample above and is tracked as
|
|
805
|
+
[#228](https://github.com/abofs/stonyx-orm/issues/228) — **do not read the
|
|
806
|
+
`.toLowerCase()` there as a complete normalisation recipe.** Record ids are
|
|
807
|
+
case-sensitive and must be compared at their real case.
|
|
808
|
+
|
|
809
|
+
**Fail closed on anything you cannot identify — on *either* argument.**
|
|
810
|
+
`String(request.originalUrl ?? '')` was once added here to stop a `TypeError`,
|
|
811
|
+
and it traded fail-closed for fail-**open**: an empty string matched no
|
|
812
|
+
collection, so `access()` fell through to the permission array and granted full
|
|
813
|
+
CRUD. The same rule applies to the context — the sample returns `false` for an
|
|
814
|
+
absent `model` rather than falling through. Since #202 the guard and the read can
|
|
815
|
+
sit on **different objects**, and a guard on argument two does not protect a read
|
|
816
|
+
of argument one: the sample therefore also returns `false` when `request.path` is
|
|
817
|
+
absent or is not a string, rather than letting `?? ''` fall through to the
|
|
818
|
+
per-record filter. An input you cannot identify must **deny**.
|
|
819
|
+
|
|
820
|
+
### Known limitations
|
|
821
|
+
|
|
822
|
+
- **A function-style filter is not a guarantee that a hidden record cannot be
|
|
823
|
+
modified.** A write to a *different* collection can re-parent one and de-hide
|
|
824
|
+
it: `POST /owners` (or `PATCH /owners/{id}`) carrying
|
|
825
|
+
`relationships: { pets: { data: { id: 21 } } }` — or
|
|
826
|
+
`attributes: { pets: [21, 22] } `, which never enters the relationships loop at
|
|
827
|
+
all — re-parents animal 21 onto an owner the caller is permitted to write. The
|
|
828
|
+
animal's `owner` is the field the `/animals` predicate reads, so the record
|
|
829
|
+
stops being rejected: it becomes readable through `GET /animals/21` and
|
|
830
|
+
deletable through `DELETE /animals/21`. **Reachable unauthenticated** wherever
|
|
831
|
+
one collection is writable and another is filtered on a field the first can
|
|
832
|
+
set. Blocking it requires checking animal 21 against the **animal** model's
|
|
833
|
+
predicate while servicing an **owners** route — cross-model access resolution,
|
|
834
|
+
which the contract could not express before
|
|
835
|
+
[#202](https://github.com/abofs/stonyx-orm/issues/202): `access()` never
|
|
836
|
+
received the model structurally and `setup-rest-server.ts` discarded the
|
|
837
|
+
model→predicate map at boot. **#202 has landed and both halves now exist** —
|
|
838
|
+
see [The access context](#the-access-context-second-argument):
|
|
839
|
+
`Orm.instance.getAccess(modelName)` makes another model's predicate
|
|
840
|
+
**reachable**, and `context.model` makes a **model-correct answer possible** —
|
|
841
|
+
possible, not guaranteed: the resolved predicate has to read the context. The
|
|
842
|
+
sample shipped with this repo now does
|
|
843
|
+
([#222](https://github.com/abofs/stonyx-orm/issues/222)), so
|
|
844
|
+
`getAccess('animal')` answers with the animal filter; a predicate that ignores
|
|
845
|
+
the second argument still answers about the collection the request is
|
|
846
|
+
addressed to, and the boot-time warning that surfaces one is
|
|
847
|
+
[#221](https://github.com/abofs/stonyx-orm/issues/221). **The mechanism
|
|
848
|
+
exists; the ORM does not yet use it on this path.** The re-parenting write above is still
|
|
849
|
+
**not refused** — that enforcement is
|
|
850
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196) and
|
|
851
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207), which were blocked on
|
|
852
|
+
#202 and are now free to proceed. Until they land, do not rely on a filter to
|
|
853
|
+
keep a record unmodifiable; keep the *writable* collections' predicates as tight as
|
|
854
|
+
the hidden ones.
|
|
855
|
+
- **Authorization by identifying the collection is a consumer-side
|
|
856
|
+
reconstruction of information the framework already holds.** `access()`
|
|
857
|
+
receives a transport artifact and is asked to work out which model, which
|
|
858
|
+
operation and which record the request addresses. The five variants above are
|
|
859
|
+
the five ways that has been observed to fail open so far. Tracked as
|
|
860
|
+
[#202](https://github.com/abofs/stonyx-orm/issues/202).
|
|
861
|
+
- **Related and included records are not filtered.** The predicate is evaluated
|
|
862
|
+
against the record the route is *addressed to*. `GET /animals/1/owner`,
|
|
863
|
+
`GET /animals/1/relationships/owner` and `?include=owner` all serialize the
|
|
864
|
+
related record without resolving that model's own access class, so a filter on
|
|
865
|
+
`/owners` does not hide an owner reached through `/animals`. Tracked as
|
|
866
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196), which covers
|
|
867
|
+
`include=`, related-resource routes and relationship-linkage routes. This is
|
|
868
|
+
**membership** — whether the related resource is served at all — and it is a
|
|
869
|
+
different question from which ids a document may *name*, immediately below.
|
|
870
|
+
- **Relationship linkage is filtered on the four request-bound read surfaces,
|
|
871
|
+
and only there.** A document's `relationships.*.data` used to publish the id
|
|
872
|
+
of every related record unconditionally, so a record hidden on every one of
|
|
873
|
+
its own surfaces was still named inside another model's document — with no
|
|
874
|
+
`include=`, no relationship route and no query string
|
|
875
|
+
([#234](https://github.com/abofs/stonyx-orm/issues/234)). The ORM now resolves
|
|
876
|
+
the **related** model's own access class on `GET /:models`, `GET /:models/:id`
|
|
877
|
+
and both `GET /:models/:id/{relationship}` shapes, and asks it
|
|
878
|
+
`{ model: <related>, operation: 'read' }`. An unresolvable class
|
|
879
|
+
(`getAccess()` → `undefined`) and a predicate that throws both **deny**. A
|
|
880
|
+
filtered-out relationship is **indistinguishable from a genuinely empty one** —
|
|
881
|
+
an emptied `hasMany` is `data: []` and an emptied `belongsTo` is `data: null`,
|
|
882
|
+
both **keeping their `links`**, which are built from the serialized record's
|
|
883
|
+
own id and never from the related one. Nothing errors and no status changes,
|
|
884
|
+
because throwing here would be an existence oracle *and* would throw out of
|
|
885
|
+
the enclosing `JSON.stringify`.
|
|
886
|
+
|
|
887
|
+
**That resolves the right class; it does not guarantee a model-correct
|
|
888
|
+
answer, and the failure direction is not the safe one.** Only a predicate that
|
|
889
|
+
*reads* `context.model` can answer about the model it was asked about — see
|
|
890
|
+
[Passing the context makes a model-correct answer *possible*](#passing-the-context-makes-a-model-correct-answer-possible)
|
|
891
|
+
above. A **single-argument predicate remains the default in every consumer
|
|
892
|
+
tree**, it identifies its collection from the request, and asked about
|
|
893
|
+
`owner` on a request dispatched to `/animals` it answers about **animals**.
|
|
894
|
+
Measured against this repo's own fixture with an arity-1 predicate registered
|
|
895
|
+
for `owner`: `GET /owners` correctly returns `["gina","michael","bob"]` while
|
|
896
|
+
`GET /animals/1` returns `owner.data {"type":"owner","id":"angela"}` — the
|
|
897
|
+
#234 defect, on the #234 surface, after the #234 fix. This is not a
|
|
898
|
+
regression (the id was published unconditionally before), it cannot be fixed
|
|
899
|
+
from this side, and the signal that surfaces such a predicate is
|
|
900
|
+
[#221](https://github.com/abofs/stonyx-orm/issues/221) /
|
|
901
|
+
[#213](https://github.com/abofs/stonyx-orm/issues/213). **Migrate your
|
|
902
|
+
predicates to read the context before relying on this filter.** A migrated,
|
|
903
|
+
context-reading predicate degrades the other way — it can over-deny a
|
|
904
|
+
*permitted* related record, which is recorded in the release notes as a
|
|
905
|
+
breaking change.
|
|
906
|
+
|
|
907
|
+
**Not yet covered, and each one still publishes ids the surfaces above
|
|
908
|
+
withhold:**
|
|
909
|
+
|
|
910
|
+
- **`included`** — [#235](https://github.com/abofs/stonyx-orm/issues/235). A
|
|
911
|
+
permitted record sideloaded by `?include=` emits its **own**
|
|
912
|
+
`relationships.*.data` unfiltered, so `GET /animals/1?include=owner,owner.pets`
|
|
913
|
+
returns `owner.data: null` on the primary document and then names angela in
|
|
914
|
+
`included`, along with eight permitted animals that each name
|
|
915
|
+
`{"type":"owner","id":"angela"}`. Separately,
|
|
916
|
+
[#233](https://github.com/abofs/stonyx-orm/issues/233) owns whether a
|
|
917
|
+
resource appears in `included` **at all** — that is membership, a different
|
|
918
|
+
question, and following it will not lead you to this residual.
|
|
919
|
+
- **The `POST`/`PATCH` response documents** —
|
|
920
|
+
[#235](https://github.com/abofs/stonyx-orm/issues/235). `createHandler` and
|
|
921
|
+
`updateHandler` destructure the request rather than binding it, so wiring
|
|
922
|
+
them needs a signature change rather than an argument. Until then **one HTTP
|
|
923
|
+
verb defeats the filter on the same record**: measured, `GET /animals/1`
|
|
924
|
+
returns `owner.data: null` and `PATCH /animals/1` returns **200 naming
|
|
925
|
+
angela**, seconds apart, with no query string and no relationship route. Any
|
|
926
|
+
caller who can read a record can also write it and be handed the id the read
|
|
927
|
+
withheld.
|
|
928
|
+
- **`GET /:models/:id/relationships/{relationship}`**, whose *primary data* is
|
|
929
|
+
linkage, so filtering it is a **membership** decision —
|
|
930
|
+
[#232](https://github.com/abofs/stonyx-orm/issues/232), the filed child of
|
|
931
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196).
|
|
932
|
+
- **A bare `toJSON()` still emits unfiltered linkage, and that is deliberate.**
|
|
933
|
+
`Record.toJSON()` **applies** a verdict; it never **resolves** one. It has no
|
|
934
|
+
request, and the documented `access()` contract permits a predicate to read
|
|
935
|
+
one — the sample in this README does, for its sub-path rule — so a filter
|
|
936
|
+
resolved inside `toJSON()` denies *permitted* records rather than hidden ones
|
|
937
|
+
(measured: 967 → 964, all three failures over-denials). `toJSON` is also the
|
|
938
|
+
`JSON.stringify` hook, so `JSON.stringify(record)`, `res.json(record)` and
|
|
939
|
+
`console.log(JSON.stringify(record))` reach it with a **string** in the
|
|
940
|
+
options slot and have no syntactic place to pass a verdict. The no-argument
|
|
941
|
+
call therefore returns the pre-#234 document unchanged. Fail-closed by default
|
|
942
|
+
is not available either: `Orm.instance.accessFunctions` is `{}` in any process
|
|
943
|
+
that never ran `setup-rest-server` — a CLI, an SQL-only process, a test — so
|
|
944
|
+
it would empty every relationship on every document in processes with no REST
|
|
945
|
+
surface to protect. Closing the residual means moving JSON:API serialization
|
|
946
|
+
**off** the `toJSON` name, tracked as
|
|
947
|
+
[#230](https://github.com/abofs/stonyx-orm/issues/230). If you hand a `Record`
|
|
948
|
+
to an untrusted consumer, serialize it through the REST layer, or resolve a
|
|
949
|
+
verdict with the **exported** `createLinkageFilter(request)` and pass it as
|
|
950
|
+
the `linkage` option — do not write your own reading of `access()`. This is a
|
|
951
|
+
consumer obligation with no signal when it lapses; it is stated once, in full,
|
|
952
|
+
under [Consumer Contracts](#consumer-contracts) below.
|
|
953
|
+
- **`format()` and `serialize()` are deliberately not filtered, and must stay
|
|
954
|
+
that way.** `format()` is the **persistence** path — its output is what
|
|
955
|
+
`Orm.db.save()` writes to disk — so applying an access filter there would
|
|
956
|
+
write a truncated database. That is **data loss**, not disclosure prevention.
|
|
957
|
+
Neither method appears anywhere in the REST response path.
|
|
958
|
+
- **A before-hook that returns a value short-circuits the request.** On write
|
|
959
|
+
operations addressed to a record the filter is consulted first, so a hook
|
|
960
|
+
cannot answer for a record the caller may not see. On reads it is not, so a
|
|
961
|
+
`beforeHook('get', ...)` read-through cache can answer past the filter.
|
|
962
|
+
- **`beforeHook('create', ...)` still runs for a `POST` that is denied.** For
|
|
963
|
+
`create` there is no record to test until the handler has built one, so the
|
|
964
|
+
denial is not knowable in time. Every *after*-hook is gated, and every
|
|
965
|
+
before-hook on `update` and `delete` is gated; before-`create` is the one
|
|
966
|
+
exception. A before-`create` hook must not assume the create will succeed.
|
|
967
|
+
- **A caller can still learn that a collection *has* a per-record filter**, by
|
|
968
|
+
observing `403` rather than `409`/`200` for an id-bearing `POST`. That
|
|
969
|
+
discloses a configuration fact, not the existence of any record.
|
|
970
|
+
- **Enforcement is post-fetch.** The record is loaded and then tested, which
|
|
971
|
+
leaves a small timing difference between a hidden record and one that never
|
|
972
|
+
existed. Tracked as [#197](https://github.com/abofs/stonyx-orm/issues/197).
|
|
973
|
+
- **A `relationships` key that is not a declared relationship is still applied
|
|
974
|
+
to the record.** The key comes verbatim from the request body and is checked
|
|
975
|
+
against nothing except `id`, which is stripped. On a `POST` that makes an
|
|
976
|
+
undeclared key a mass-assigned attribute; on a `PATCH` it is passed to
|
|
977
|
+
`updateRecord`. `id` is stripped in both handlers because it defeats breaking
|
|
978
|
+
change 3 above; the general form is tracked as
|
|
979
|
+
[#204](https://github.com/abofs/stonyx-orm/issues/204).
|
|
980
|
+
- **A `POST` body `id` that the duplicate check cannot resolve can still
|
|
981
|
+
overwrite a different record on an unfiltered collection.** The lookup is
|
|
982
|
+
correct and deliberately does not coerce: `"9105h"` is rejected as a string
|
|
983
|
+
rather than truncated, and `9105.5`, `[9105]` and `true` are passed through as
|
|
984
|
+
themselves. The model's id transform then coerces anyway — a bare `parseInt`
|
|
985
|
+
with no such guard — so the create lands on `9105` (or on `NaN`) and
|
|
986
|
+
overwrites whatever is there. **This is not string-only**: any body id whose
|
|
987
|
+
transform output differs from its lookup key is the same defect. Filtered
|
|
988
|
+
collections are unaffected — breaking change 3 refuses any client-supplied id
|
|
989
|
+
— so this reaches consumers with **no** function-style filter. Tracked as
|
|
990
|
+
[#205](https://github.com/abofs/stonyx-orm/issues/205). Its sibling
|
|
991
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — a **server-assigned**
|
|
992
|
+
id landing on an occupied slot — is **fixed**; see breaking change 8. #205 is
|
|
993
|
+
the client-supplied half and is still open.
|
|
994
|
+
- **`context.record` is `undefined` for an after-`create` hook when a string-id
|
|
995
|
+
model is given a numeric-looking id.** The post-create lookup uses the same id
|
|
996
|
+
coercion as every other surface, which resolves `'9107'` to the number `9107`,
|
|
997
|
+
while a model declaring `id = attr('string')` files the record under the string
|
|
998
|
+
key. The create itself succeeds and `context.response.data` is correct; only
|
|
999
|
+
the hook's view of the record is wrong, and it is wrong *silently*. Tracked as
|
|
1000
|
+
[#209](https://github.com/abofs/stonyx-orm/issues/209).
|
|
1001
|
+
- **A denied `POST` rolls back only a record it *inserted*.** The rollback
|
|
1002
|
+
requires the store to have grown, because removing by id alone is a write
|
|
1003
|
+
primitive keyed by a caller-supplied value. **The reachability condition this
|
|
1004
|
+
bullet used to state is gone**: it was
|
|
1005
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — `assignRecordId`
|
|
1006
|
+
returned last-*inserted* + 1, so a server-assigned id could land on an
|
|
1007
|
+
occupied slot and `createRecord` would update it in place — and #203 is fixed
|
|
1008
|
+
(breaking change 8). A server-assigned create can no longer overwrite, so on a
|
|
1009
|
+
collection whose only id channel is `createHandler` this guard has no
|
|
1010
|
+
observable effect today. It is kept because a caller-supplied id reaching
|
|
1011
|
+
`createRecord` from another route — a relationship write,
|
|
1012
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207) — puts the condition
|
|
1013
|
+
back, and without the guard a denied `403` would delete a record the request
|
|
1014
|
+
did not create.
|
|
1015
|
+
|
|
1016
|
+
### Consumer Contracts
|
|
1017
|
+
|
|
1018
|
+
Obligations this package **cannot enforce**, where nothing fails, warns or
|
|
1019
|
+
changes shape when a consumer omits them. One place, findable, per
|
|
1020
|
+
`quality.md` rule 2 — if you are relying on `@stonyx/orm` for access control,
|
|
1021
|
+
read all of these.
|
|
1022
|
+
|
|
1023
|
+
#### `Record.toJSON()` does not filter relationship linkage unless you pass a verdict
|
|
1024
|
+
|
|
1025
|
+
**The framework owns this on four surfaces. You own it everywhere else.**
|
|
1026
|
+
|
|
1027
|
+
`GET /:models`, `GET /:models/:id` and both `GET /:models/:id/{relationship}`
|
|
1028
|
+
shapes resolve a linkage verdict and pass it to `toJSON()` for you. Any other
|
|
1029
|
+
path to a document — `JSON.stringify(record)`, `res.json(record)`,
|
|
1030
|
+
`console.log(record)`, a custom route, a queue payload, a websocket frame —
|
|
1031
|
+
calls `toJSON()` with no verdict, and **the no-verdict document names every
|
|
1032
|
+
related id, including records hidden on every one of their own surfaces**
|
|
1033
|
+
([#234](https://github.com/abofs/stonyx-orm/issues/234)). That default is
|
|
1034
|
+
deliberate and cannot be inverted; the reasons are in
|
|
1035
|
+
[Known limitations](#known-limitations) above.
|
|
1036
|
+
|
|
1037
|
+
**There is no signal when you omit it.** `linkage` is optional, absent is the
|
|
1038
|
+
default, the default is the unfiltered document, and a filtered relationship is
|
|
1039
|
+
byte-identical to a genuinely empty one — so nothing on the wire distinguishes
|
|
1040
|
+
"filtered" from "forgotten".
|
|
1041
|
+
|
|
1042
|
+
Do this:
|
|
1043
|
+
|
|
1044
|
+
```js
|
|
1045
|
+
import { createLinkageFilter } from '@stonyx/orm';
|
|
1046
|
+
|
|
1047
|
+
// `request` is the live request the caller was authorised against. The verdict
|
|
1048
|
+
// is REQUEST-SCOPED: build one per request and never cache it across requests,
|
|
1049
|
+
// or a second caller is answered with the first caller's authorization.
|
|
1050
|
+
const linkage = createLinkageFilter(request);
|
|
1051
|
+
|
|
1052
|
+
res.json({ data: record.toJSON({ baseUrl, linkage }) });
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
Not this:
|
|
1056
|
+
|
|
1057
|
+
```js
|
|
1058
|
+
// A second, unreviewed reading of access(). It will drift from the one in
|
|
1059
|
+
// src/access-verdict.ts, and it will drift in consumer code where no reviewer
|
|
1060
|
+
// of this repository will ever see it.
|
|
1061
|
+
const linkage = (type, r) => Orm.instance.getAccess(type)?.(request)?.(r) ?? true;
|
|
1062
|
+
```
|
|
1063
|
+
|
|
1064
|
+
**`linkage` is validated, and an unusable value DENIES.** `undefined` means "no
|
|
1065
|
+
verdict supplied" and emits today's document. Any other non-function — `null`,
|
|
1066
|
+
`0`, `false`, `''`, `true`, a string, an object — drops **all** linkage on that
|
|
1067
|
+
document and logs. This matters because `null` is the natural return of a
|
|
1068
|
+
resolver that could not resolve a session: it used to be read as "absent" and
|
|
1069
|
+
emit the full document silently.
|
|
1070
|
+
|
|
1071
|
+
#### A predicate that ignores `context.model` makes cross-model resolution GRANT
|
|
1072
|
+
|
|
1073
|
+
The linkage filter above asks the **related** model's access class the
|
|
1074
|
+
model-correct question, but only a predicate that *reads*
|
|
1075
|
+
[`context.model`](#the-access-context-second-argument) can give a model-correct
|
|
1076
|
+
answer. A single-argument predicate identifies its collection from the request
|
|
1077
|
+
and therefore answers about the collection the request was *addressed to* —
|
|
1078
|
+
which is the direction that **grants**. Measured, and worked through in
|
|
1079
|
+
[Known limitations](#known-limitations). There is no boot-time warning yet
|
|
1080
|
+
([#221](https://github.com/abofs/stonyx-orm/issues/221)). **Migrate your
|
|
1081
|
+
predicates to the two-argument contract.**
|
|
1082
|
+
|
|
1083
|
+
#### `format()` and `serialize()` are never filtered, by design
|
|
1084
|
+
|
|
1085
|
+
They are the persistence path. Do not hand their output to an untrusted
|
|
1086
|
+
consumer, and do not add a filter to them — `Orm.db.save()` writes `format()`
|
|
1087
|
+
output to disk, so filtering there is data loss rather than disclosure
|
|
1088
|
+
prevention.
|
|
1089
|
+
|
|
1090
|
+
### Breaking changes
|
|
1091
|
+
|
|
1092
|
+
These land in the next published build. There is no changelog or release-notes channel yet
|
|
1093
|
+
([stonyx-workflows#17](https://github.com/abofs/stonyx-workflows/issues/17)), so
|
|
1094
|
+
they are recorded here.
|
|
1095
|
+
|
|
1096
|
+
1. **`DELETE /{collection}/{id}` on a record that does not exist returns `404`
|
|
1097
|
+
instead of `204`.** This affects **every** consumer issuing a DELETE against
|
|
1098
|
+
any mounted collection, whether or not an access filter is configured, and
|
|
1099
|
+
`models: '*'` mounts every model by default. It is not optional: if a denied
|
|
1100
|
+
delete returned 404 while a missing one returned 204, the pair would be a
|
|
1101
|
+
perfect existence oracle and the filter would be worthless.
|
|
1102
|
+
2. **After-hooks no longer fire for a request that failed** — denied, missing,
|
|
1103
|
+
`400` or `409`. The gate is on the handler's status, not on the operation, so
|
|
1104
|
+
it covers reads as well: a `GET /:id` that answers 404 runs no after-`get`
|
|
1105
|
+
hook either. Previously `afterHook('delete', ...)` ran with a populated
|
|
1106
|
+
`context.recordId` on a request that deleted nothing, so a consumer cascade
|
|
1107
|
+
destroyed children behind a 404.
|
|
1108
|
+
3. **`POST` with a client-supplied `id` returns `403` when a function-style
|
|
1109
|
+
`access` filter is in force**, whatever the payload and whether or not the id
|
|
1110
|
+
exists, and *before* any store lookup — so neither the status nor the lookup
|
|
1111
|
+
cost can depend on whether that id exists. Only affects function-style
|
|
1112
|
+
`access` users. See [Filter functions](#filter-functions) for why, and let the
|
|
1113
|
+
server assign the id instead. `409`-on-duplicate is unchanged for everyone
|
|
1114
|
+
else.
|
|
1115
|
+
|
|
1116
|
+
"Whatever the payload" is a statement about the **`id` member of the resource
|
|
1117
|
+
object**, and it holds only because that is the sole channel a caller id can
|
|
1118
|
+
arrive on. It was not always: a caller id moved into
|
|
1119
|
+
`relationships: {"id": {"data": {"id": 21}}}` reached `createRecord` past this
|
|
1120
|
+
refusal and overwrote a hidden record in place. Both strips — `attributes.id`
|
|
1121
|
+
and `relationships.id` — are part of this behaviour, not tidiness. A future
|
|
1122
|
+
change that adds a third channel without stripping it re-opens the oracle;
|
|
1123
|
+
see [#204](https://github.com/abofs/stonyx-orm/issues/204).
|
|
1124
|
+
4. **Function-style `access` is now enforced on all seven surfaces.** Records
|
|
1125
|
+
previously reachable by id despite being filtered from the collection now
|
|
1126
|
+
return 404. Only affects function-style `access` users, for whom the old
|
|
1127
|
+
behaviour was the bypass.
|
|
1128
|
+
|
|
1129
|
+
"Seven surfaces" means the seven endpoints of **the filtered model**. A write
|
|
1130
|
+
to another collection can still reach one of its records through a
|
|
1131
|
+
relationship — [#207](https://github.com/abofs/stonyx-orm/issues/207), which
|
|
1132
|
+
is **not** closed here.
|
|
1133
|
+
5. **A predicate that throws is treated as a denial** rather than propagating to
|
|
1134
|
+
Express's default 500 handler. So is an `access()` that throws.
|
|
1135
|
+
6. **`access()` returning a bare string is one permission, not full access.**
|
|
1136
|
+
`AccessMethod` declares `string` legal, and it previously fell through every
|
|
1137
|
+
branch and granted all four operations — `return 'read'` allowed `DELETE`.
|
|
1138
|
+
It is now equivalent to `['read']`. Any other unrecognised shape (an object,
|
|
1139
|
+
a number) now returns `403` rather than granting full access.
|
|
1140
|
+
7. **`POST` with a client-supplied `id` normalises the body id before the
|
|
1141
|
+
duplicate check**, so an id shape that previously *missed* the store's key
|
|
1142
|
+
now finds it. `POST {"id":"0x2391"}` and `POST {"id":" 21 "}` answer `409`
|
|
1143
|
+
where they answered `200`, and the `200` was not a success: the lookup missed,
|
|
1144
|
+
the duplicate check was skipped, and `createRecord` overwrote the colliding
|
|
1145
|
+
record in place. **This one reaches consumers with no filter at all** — the
|
|
1146
|
+
population breaking changes 3 and 4 explicitly exempt. If you were relying on
|
|
1147
|
+
a hex-shaped or whitespace-padded id creating a second record, it never did.
|
|
1148
|
+
|
|
1149
|
+
8. **Server-assigned ids change value on string-id models, numeric ids stop
|
|
1150
|
+
being monotonic at the numeric ceiling, and the create route gains a
|
|
1151
|
+
`409`.** Three consumer-visible changes from
|
|
1152
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203).
|
|
1153
|
+
|
|
1154
|
+
**The value.** A `POST` with no `id` against a model declaring
|
|
1155
|
+
`id = attr('string')` previously produced the *last-inserted* id with `1`
|
|
1156
|
+
concatenated onto it — an owner store holding `['gina', 'bob']` answered
|
|
1157
|
+
`'bob1'`. It now answers `'owner-1'`: the model name, a hyphen, and the
|
|
1158
|
+
lowest positive integer whose landing key is free. **No test in this repo
|
|
1159
|
+
pinned the old value**, so a consumer relying on it gets no failing test, no
|
|
1160
|
+
deprecation and no other signal — which is why it is recorded here. Numeric
|
|
1161
|
+
id models (`id = attr('number')`, the default) are unaffected in shape: they
|
|
1162
|
+
still get an integer, but it is now the **maximum** existing id plus one
|
|
1163
|
+
rather than the last-inserted id plus one, which is the defect #203 is about.
|
|
1164
|
+
They are **not** unaffected in *sequence* — see the monotonicity half below.
|
|
1165
|
+
|
|
1166
|
+
The value is deliberately **not** numeric-looking, and that is not cosmetic.
|
|
1167
|
+
Every id-bearing surface resolves a numeric-looking string id to a **number**
|
|
1168
|
+
(`GET /owners/1` looks up `1`), while a string-id model files its records
|
|
1169
|
+
under the **string** key `'1'`. A server-assigned `'1'` would therefore be a
|
|
1170
|
+
record that was created successfully and could not be fetched, updated or
|
|
1171
|
+
deleted by id, and whose after-`create` hook received
|
|
1172
|
+
`context.record === undefined`
|
|
1173
|
+
([#209](https://github.com/abofs/stonyx-orm/issues/209)).
|
|
1174
|
+
|
|
1175
|
+
**Numeric ids are no longer monotonic, and deleted ids can be re-issued.**
|
|
1176
|
+
The precondition is narrow but it is reachable, and there is no signal when
|
|
1177
|
+
it is met: **one record filed at or above 2^53** (`9007199254740992`). `max
|
|
1178
|
+
+ 1` is not representable there, so assignment restarts from `1` and walks
|
|
1179
|
+
up to the lowest free key — which means the id of a *deleted* record is
|
|
1180
|
+
handed to the next `POST`. Both `dev` and every prior release were strictly
|
|
1181
|
+
monotonic and never re-issued a numeric id, so a consumer that relied on
|
|
1182
|
+
that — audit rows, cursors, cached authorization decisions, external
|
|
1183
|
+
references keyed on the id — now has a stale reference that silently points
|
|
1184
|
+
at a **different record, created by a different caller**, rather than at a
|
|
1185
|
+
deleted one. Nothing fails; the reference simply resolves to the wrong
|
|
1186
|
+
record.
|
|
1187
|
+
|
|
1188
|
+
The restart is deliberate and is not itself optional: without it, one record
|
|
1189
|
+
at the ceiling made every subsequent server-assigned create on that
|
|
1190
|
+
collection fail permanently. Re-use is the cost of keeping the collection
|
|
1191
|
+
writable. **If you need monotonic ids, assign them yourself** rather than
|
|
1192
|
+
letting the server assign, and note that a ceiling record can be planted by
|
|
1193
|
+
an unauthenticated caller — see *And the id itself is an occupancy signal*
|
|
1194
|
+
under [Filter functions](#filter-functions) for the reachability path.
|
|
1195
|
+
String-id models are unaffected by this half: their keys are
|
|
1196
|
+
`<model>-<n>` and were never monotonic over an integer sequence.
|
|
1197
|
+
|
|
1198
|
+
**The status.** `POST /{collection}` can now answer `409` for a reason other
|
|
1199
|
+
than a duplicate id: the server could not derive a free id. That requires a
|
|
1200
|
+
**non-injective** id transform — one that maps distinct candidates onto the
|
|
1201
|
+
same store key, such as `boolean`, or anything you registered on
|
|
1202
|
+
`Orm.instance.transforms` and named as an id type. It is a configuration
|
|
1203
|
+
fault rather than a request fault; the message is logged through
|
|
1204
|
+
`stonyx/log`. Previously this case threw out of the handler and express
|
|
1205
|
+
answered `500` with a stack trace.
|
|
1206
|
+
|
|
291
1207
|
### Include Parameter (Sideloading Relationships)
|
|
292
1208
|
|
|
293
1209
|
The ORM supports JSON API-compliant relationship sideloading via the `include` query parameter. This reduces the need for multiple API requests by embedding related records in a single response.
|
|
@@ -380,6 +1296,13 @@ GET /animals/1
|
|
|
380
1296
|
#### Limitations
|
|
381
1297
|
|
|
382
1298
|
- Only available on GET endpoints (not POST/PATCH)
|
|
1299
|
+
- **`included` records are not access-filtered, on either question.** Whether a
|
|
1300
|
+
resource appears in `included` at all is
|
|
1301
|
+
[#233](https://github.com/abofs/stonyx-orm/issues/233); a record that *is*
|
|
1302
|
+
permitted still emits its **own** `relationships.*.data` unfiltered, so
|
|
1303
|
+
`?include=` republishes ids the primary document withholds
|
|
1304
|
+
([#235](https://github.com/abofs/stonyx-orm/issues/235)). See
|
|
1305
|
+
[Consumer Contracts](#consumer-contracts).
|
|
383
1306
|
|
|
384
1307
|
## Lifecycle Hooks
|
|
385
1308
|
|
|
@@ -561,7 +1484,19 @@ afterHook('delete', 'animal', async (context) => {
|
|
|
561
1484
|
// Additional access control - halt with 403 if unauthorized
|
|
562
1485
|
beforeHook('delete', 'animal', (context) => {
|
|
563
1486
|
const user = context.state.currentUser;
|
|
564
|
-
|
|
1487
|
+
|
|
1488
|
+
// `context.oldState` — NOT a store lookup. For `update` and `delete` the ORM
|
|
1489
|
+
// has already fetched the record (and already applied the access filter to
|
|
1490
|
+
// it) before this hook runs, so re-fetching it here is a fourth id coercion
|
|
1491
|
+
// that has to agree with three others.
|
|
1492
|
+
//
|
|
1493
|
+
// And it would not agree. `context.params.id` is the raw url segment, always
|
|
1494
|
+
// a string, while the store keys numeric-id models by NUMBER — so
|
|
1495
|
+
// `store.get('animal', '21')` misses the record held under `21`, and so does
|
|
1496
|
+
// `store.find('animal', '21')`: neither coerces. The miss reads as "no such
|
|
1497
|
+
// record", which in an authorization hook fails whichever way your code
|
|
1498
|
+
// happens to handle a null.
|
|
1499
|
+
const animal = context.oldState;
|
|
565
1500
|
|
|
566
1501
|
if (animal.owner !== user.id && !user.isAdmin) {
|
|
567
1502
|
return 403; // Forbidden
|
|
@@ -569,6 +1504,10 @@ beforeHook('delete', 'animal', (context) => {
|
|
|
569
1504
|
});
|
|
570
1505
|
```
|
|
571
1506
|
|
|
1507
|
+
> If you do need a lookup for some *other* model inside a hook, coerce the id
|
|
1508
|
+
> yourself to the type that model's `id` attribute declares — the store is a
|
|
1509
|
+
> `Map` and `'21'` and `21` are different keys.
|
|
1510
|
+
|
|
572
1511
|
#### Auditing
|
|
573
1512
|
|
|
574
1513
|
```javascript
|
|
@@ -702,11 +1641,29 @@ beforeHook('create', 'post', (context) => {
|
|
|
702
1641
|
|
|
703
1642
|
### Hook Execution Order
|
|
704
1643
|
|
|
705
|
-
1. **
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
1644
|
+
1. **Authorization is evaluated first for `update` and `delete`.** A record the
|
|
1645
|
+
access filter rejects returns `404` **before any before-hook runs**, so a
|
|
1646
|
+
hook never sees a record — or a `context.oldState` — that the caller is not
|
|
1647
|
+
allowed to read. `create` is the exception: there is no record to test until
|
|
1648
|
+
the handler has built one, so `beforeHook('create', ...)` **does** fire for a
|
|
1649
|
+
`POST` that goes on to answer `403`.
|
|
1650
|
+
2. **Before hooks** fire next (sequentially, in registration order).
|
|
1651
|
+
3. **Main operation** executes (if no before hook halted).
|
|
1652
|
+
4. **After hooks** fire last (sequentially, in registration order) — **only if
|
|
1653
|
+
the request succeeded.**
|
|
1654
|
+
|
|
1655
|
+
Before hooks can halt the operation by returning a value, and that value becomes
|
|
1656
|
+
the response.
|
|
1657
|
+
|
|
1658
|
+
**After hooks do not run for a failed request.** Any status `>= 400` — denied,
|
|
1659
|
+
missing, `400`, `409` — skips the entire after-hook pipeline, along with SQL
|
|
1660
|
+
persistence and `onUpdate` autosave. This is a behaviour change; see
|
|
1661
|
+
[Breaking changes](#breaking-changes). It applies to the samples above: the
|
|
1662
|
+
`afterHook('delete', ...)` auditing hook writes **no** audit row for a `DELETE`
|
|
1663
|
+
that answered `404`, and the `afterHook('update', ...)` change-tracking hook
|
|
1664
|
+
writes none for a `PATCH` that answered `404` or `400`. If you need a record of
|
|
1665
|
+
refused requests, log them from a before-hook or from your own middleware —
|
|
1666
|
+
`after<operation>` fires only for an operation that actually happened.
|
|
710
1667
|
|
|
711
1668
|
### Best Practices
|
|
712
1669
|
|