@stonyx/orm 0.3.2-alpha.5 → 0.3.2-alpha.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +296 -4
- package/config/environment.js +99 -12
- 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/main.js +10 -0
- package/dist/manage-record.js +34 -3
- 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 +37 -0
- package/dist/orm-request.js +493 -43
- 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.js +7 -5
- package/dist/relationships.js +1 -1
- package/dist/serializer.js +38 -2
- package/dist/store.d.ts +13 -1
- package/dist/store.js +65 -6
- package/dist/types/orm-types.d.ts +11 -0
- package/package.json +16 -7
- 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/main.ts +10 -0
- package/src/manage-record.ts +41 -9
- package/src/mysql/connection.ts +1 -0
- package/src/mysql/mysql-db.ts +44 -12
- package/src/orm-request.ts +500 -41
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +44 -12
- package/src/record.ts +8 -5
- package/src/relationships.ts +1 -1
- package/src/serializer.ts +39 -2
- package/src/store.ts +68 -6
- package/src/types/orm-types.ts +12 -0
- package/src/types/stonyx.d.ts +7 -1
- package/config/environment.ts +0 -91
package/dist/orm-request.js
CHANGED
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* REST request handling and access enforcement for @stonyx/orm.
|
|
3
|
+
*
|
|
4
|
+
* ---------------------------------------------------------------------------
|
|
5
|
+
* THE DOCUMENTED `access()` PATTERN IS A STOPGAP. READ THIS BEFORE RELYING ON IT.
|
|
6
|
+
* ---------------------------------------------------------------------------
|
|
7
|
+
* `auth()` below hands your `access(request)` a raw transport artifact and asks
|
|
8
|
+
* you to re-derive from a URL string what this module already holds
|
|
9
|
+
* structurally: which model, which operation, which record. The three-line
|
|
10
|
+
* URL-matching example in README has failed **open** in four distinct ways
|
|
11
|
+
* during the review of a single change, each found only after the previous was
|
|
12
|
+
* fixed, by four different people:
|
|
13
|
+
*
|
|
14
|
+
* 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
|
|
15
|
+
* prefix match against it is ALWAYS false.
|
|
16
|
+
* 2. `request.originalUrl` carries the query string, so an anchored equality
|
|
17
|
+
* check misses `/owners?filter[age]=30`.
|
|
18
|
+
* 3. The router is a bare `express()` (`caseSensitive: false`) while a
|
|
19
|
+
* hand-written matcher is case-SENSITIVE, so `GET /OwNeRs/angela` walks
|
|
20
|
+
* past it. Router-side: abofs/stonyx-rest-server#47.
|
|
21
|
+
* 4. Under a configured `ORM_REST_ROUTE` a hard-coded `/owners` matches
|
|
22
|
+
* nothing -- environment-specifically, which is worse.
|
|
23
|
+
*
|
|
24
|
+
* The README sample closes all four. That is NOT the same as being safe; it is
|
|
25
|
+
* safe against the four variants we happen to have found, and there is no
|
|
26
|
+
* reason to believe the list is complete.
|
|
27
|
+
*
|
|
28
|
+
* THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model, the
|
|
29
|
+
* operation and the record, so there is no URL to parse and no variant to miss.
|
|
30
|
+
* Prefer the array shape (`['read']`) or `false` until #202 lands; the
|
|
31
|
+
* function shape is what requires the URL matching.
|
|
32
|
+
*
|
|
33
|
+
* The enforcement gates in this file (GATE 0/1/2, the create rollback, the
|
|
34
|
+
* per-handler `isDenied` re-checks) are correct independently of that -- they
|
|
35
|
+
* enforce whatever predicate you return. The stopgap is the part where YOU have
|
|
36
|
+
* to work out which predicate to return.
|
|
37
|
+
*/
|
|
1
38
|
import { Request } from '@stonyx/rest-server';
|
|
2
39
|
import Orm, { store, createRecord, updateRecord } from '@stonyx/orm';
|
|
3
40
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
@@ -48,13 +85,88 @@ function getBaseUrl(request) {
|
|
|
48
85
|
const host = request.get('host');
|
|
49
86
|
return `${protocol}://${host}`;
|
|
50
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* The ONE coercion from a caller-supplied id string to the key the store holds
|
|
90
|
+
* it under. Both id-bearing surfaces go through this, and neither has a copy.
|
|
91
|
+
*
|
|
92
|
+
* WHY IT IS SHARED RATHER THAN DUPLICATED. `getId()` (URL) and
|
|
93
|
+
* `normalizeBodyId()` (JSON body) each had their own arithmetic, and they
|
|
94
|
+
* disagreed: `parseInt(id)` versus `parseInt(id, 10)`. On a hex-shaped id that
|
|
95
|
+
* is a two-record difference --
|
|
96
|
+
*
|
|
97
|
+
* GET /animals/0x2391 -> record 9105 (getId -> parseInt('0x2391') = 9105)
|
|
98
|
+
* POST /animals {"id":"0x2391"} -> lookup under 0 (normalize -> parseInt('0x2391',10) = 0)
|
|
99
|
+
* -> a MISS, so the duplicate check was skipped and
|
|
100
|
+
* createRecord OVERWROTE 9105 in place, answering 200
|
|
101
|
+
*
|
|
102
|
+
* -- which is the raw-versus-normalised divergence that produced the round-3
|
|
103
|
+
* blocker, in a narrower form, reintroduced by the fix for it. Two coercions
|
|
104
|
+
* that must agree cannot be kept in agreement by review; they have to be one
|
|
105
|
+
* function. Pinned by assertion 43.
|
|
106
|
+
*
|
|
107
|
+
* `parseInt` and not `Number`, deliberately. They differ on `'1e3'` (1 vs 1000)
|
|
108
|
+
* and `'9105.5'` (9105 vs 9105.5), and `getId` -- which decides which record an
|
|
109
|
+
* id ADDRESSES -- is the reference, so `Number` would trade one divergence for
|
|
110
|
+
* two. The reason `parseInt` is safe here is the `isNaN` gate in front of it:
|
|
111
|
+
* `parseInt('9105h')` is `9105`, and truncating a partially-valid id into a
|
|
112
|
+
* DIFFERENT VALID key is exactly how a collision lookup gets skipped. The gate
|
|
113
|
+
* rejects it as a string instead, so nothing is ever truncated. That gate, not
|
|
114
|
+
* the parser, is the load-bearing half -- assertion 43 pins it.
|
|
115
|
+
*/
|
|
116
|
+
function coerceId(id) {
|
|
117
|
+
if (isNaN(id))
|
|
118
|
+
return id;
|
|
119
|
+
return parseInt(id);
|
|
120
|
+
}
|
|
51
121
|
function getId(params) {
|
|
52
122
|
const id = params.id;
|
|
53
123
|
if (!id)
|
|
54
124
|
return '';
|
|
55
|
-
|
|
125
|
+
return coerceId(id);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Normalise a caller-supplied BODY id to the key the store will hold it under.
|
|
129
|
+
*
|
|
130
|
+
* `getId()` above is params-shaped: it takes `{ id?: string }` off the URL,
|
|
131
|
+
* where the value is always a string and a falsy one means "no id". A JSON body
|
|
132
|
+
* id is neither -- it can arrive as a number, and `0` is a legitimate id that
|
|
133
|
+
* `getId()` would flatten to `''`.
|
|
134
|
+
*
|
|
135
|
+
* WHY THIS EXISTS AT ALL. createHandler used to look the collision up with the
|
|
136
|
+
* RAW body value while every other surface normalised through `getId()`. The
|
|
137
|
+
* store is a Map keyed by the coerced value, so `store.find(model, '21')` misses
|
|
138
|
+
* the entry held under `21` and the duplicate check is skipped by typing the id
|
|
139
|
+
* as a string. On `dev` that silently overwrote the colliding record and
|
|
140
|
+
* answered 200; combined with the denied-create rollback added for #190 it
|
|
141
|
+
* became an unauthenticated DELETE of any id. Normalising here is half of that
|
|
142
|
+
* fix -- see the rollback in createHandler for the other half.
|
|
143
|
+
*
|
|
144
|
+
* It shares `coerceId` with `getId` so the two surfaces cannot drift apart
|
|
145
|
+
* again, and differs from `getId` in exactly ONE place, below.
|
|
146
|
+
*/
|
|
147
|
+
function normalizeBodyId(id) {
|
|
148
|
+
// Non-strings pass through untouched: a JSON body id can arrive as a number,
|
|
149
|
+
// and `getId`'s falsy-flatten must NOT apply to it -- `0` is a legitimate id
|
|
150
|
+
// and `getId` would turn it into `''`. (assertion 30 sweeps id `0`.)
|
|
151
|
+
if (typeof id !== 'string')
|
|
56
152
|
return id;
|
|
57
|
-
|
|
153
|
+
// THE ONE DIVERGENCE FROM `getId`, and it mirrors rather than contradicts it:
|
|
154
|
+
// `getId` maps a falsy param to `''`, and `''` is the only string a body can
|
|
155
|
+
// carry that means "no id" -- `createRecord` treats it as absent and assigns a
|
|
156
|
+
// server id. Coercing it instead would make it address a real slot, because
|
|
157
|
+
// `parseInt('')` is `NaN` and a record CAN be held under `NaN` (a truthy but
|
|
158
|
+
// non-numeric id such as `' '` survives `assignRecordId`'s falsy guard and
|
|
159
|
+
// then NaNs in the id transform). So `POST {"id":""}` would answer 409 against
|
|
160
|
+
// an unrelated record it never named. Pinned by assertion 44.
|
|
161
|
+
//
|
|
162
|
+
// Note what is deliberately NOT special-cased here any more: whitespace.
|
|
163
|
+
// `id.trim() === ''` used to short-circuit `' '` as well, which made the
|
|
164
|
+
// body surface DISAGREE with the URL surface -- `getId({id:' '})` is `NaN`,
|
|
165
|
+
// so `' '` addresses the NaN slot on every other route while the collision
|
|
166
|
+
// lookup missed it. Same class of bug as the hex divergence above.
|
|
167
|
+
if (id === '')
|
|
168
|
+
return id;
|
|
169
|
+
return coerceId(id);
|
|
58
170
|
}
|
|
59
171
|
function buildResponse(data, includeParam, recordOrRecords, options = {}) {
|
|
60
172
|
const { links, baseUrl } = options;
|
|
@@ -187,6 +299,33 @@ function createFilterPredicate(filters) {
|
|
|
187
299
|
return String(current) === value;
|
|
188
300
|
});
|
|
189
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* A function-style `access` return is a per-record predicate, and it is only
|
|
304
|
+
* meaningful if every surface that can hand a record to a caller consults it.
|
|
305
|
+
* Before #190 exactly one of seven did.
|
|
306
|
+
*
|
|
307
|
+
* Enforcement is deliberately post-fetch. `access` returns an opaque JS
|
|
308
|
+
* predicate and `store.findAll(model, conditions)` accepts only an equality
|
|
309
|
+
* conditions object that the SQL drivers translate to a WHERE clause, so
|
|
310
|
+
* query-layer enforcement would require a breaking change to the published
|
|
311
|
+
* `access` contract. That belongs in #197, not in a security patch. Six of the
|
|
312
|
+
* seven surfaces fetch by primary key anyway, so this costs exactly one row.
|
|
313
|
+
*/
|
|
314
|
+
function isDenied(filter, record) {
|
|
315
|
+
if (typeof filter !== 'function')
|
|
316
|
+
return false;
|
|
317
|
+
// A predicate that throws is treated as a denial. Unguarded, a throw escapes
|
|
318
|
+
// to express's default handler, which answers 500 (with a stack trace outside
|
|
319
|
+
// NODE_ENV=production) while a missing id still answers 404 -- so a
|
|
320
|
+
// record-dependent throw re-separates "hidden" from "does not exist" and
|
|
321
|
+
// hands back the oracle this whole change exists to close.
|
|
322
|
+
try {
|
|
323
|
+
return !filter(record);
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
190
329
|
export default class OrmRequest extends Request {
|
|
191
330
|
model;
|
|
192
331
|
access;
|
|
@@ -216,10 +355,15 @@ export default class OrmRequest extends Request {
|
|
|
216
355
|
baseUrl
|
|
217
356
|
});
|
|
218
357
|
};
|
|
219
|
-
const getSingleHandler = async (request) => {
|
|
358
|
+
const getSingleHandler = async (request, { filter }) => {
|
|
220
359
|
const record = await store.find(model, getId(request.params));
|
|
221
360
|
if (!record)
|
|
222
361
|
return 404;
|
|
362
|
+
// 404, never 403: the status for "exists but filtered out" must be
|
|
363
|
+
// identical to "does not exist", or the fix trades an authorization
|
|
364
|
+
// bypass for a narrower existence oracle.
|
|
365
|
+
if (isDenied(filter, record))
|
|
366
|
+
return 404;
|
|
223
367
|
const fieldsMap = parseFields(request.query);
|
|
224
368
|
const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
|
|
225
369
|
const baseUrl = getBaseUrl(request);
|
|
@@ -228,19 +372,100 @@ export default class OrmRequest extends Request {
|
|
|
228
372
|
baseUrl
|
|
229
373
|
});
|
|
230
374
|
};
|
|
231
|
-
const createHandler = async ({ body, query }) => {
|
|
375
|
+
const createHandler = async ({ body, query }, { filter }) => {
|
|
232
376
|
const { type, id, attributes, relationships: rels } = (body?.data || {});
|
|
233
377
|
if (!type)
|
|
234
378
|
return 400; // Bad request
|
|
235
379
|
const fieldsMap = parseFields(query);
|
|
236
380
|
const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
|
|
237
|
-
//
|
|
238
|
-
|
|
239
|
-
|
|
381
|
+
// GATE 0 -- the POST existence oracle.
|
|
382
|
+
//
|
|
383
|
+
// The duplicate check runs before the filter and `store.find` sees hidden
|
|
384
|
+
// records, so POST leaks existence through its STATUS. A previous revision
|
|
385
|
+
// filtered the collision status (403 when the colliding record is denied,
|
|
386
|
+
// 409 when it is visible) and that is NOT sufficient, because the status
|
|
387
|
+
// of a create is a third outcome. With a payload the caller is permitted
|
|
388
|
+
// to create -- the normative case for a per-tenant filter, and the case an
|
|
389
|
+
// attacker picks -- all three are distinguishable in ONE request per id:
|
|
390
|
+
//
|
|
391
|
+
// POST /animals {id:22, owner:'gina'} -> 403 a HIDDEN record has this id
|
|
392
|
+
// POST /animals {id:9500, owner:'gina'} -> 200 this id is FREE
|
|
393
|
+
// POST /animals {id:8, owner:'gina'} -> 409 a VISIBLE record has this id
|
|
394
|
+
//
|
|
395
|
+
// Filtering only the collision status narrows that to callers who cannot
|
|
396
|
+
// create a record they are allowed to see. It does not close it.
|
|
397
|
+
//
|
|
398
|
+
// It cannot be closed while a caller both chooses the id and learns
|
|
399
|
+
// whether the create succeeded: a successful create must answer
|
|
400
|
+
// differently from a refused one. So when a per-record filter is in force
|
|
401
|
+
// the caller does not get to choose the id at all. The refusal is
|
|
402
|
+
// UNCONDITIONAL and happens BEFORE any store lookup, so no status, and no
|
|
403
|
+
// lookup cost, can depend on whether that id exists. 403 -- the same
|
|
404
|
+
// status as a denied create -- so the two cannot be separated either.
|
|
405
|
+
//
|
|
406
|
+
// BOTH HALVES OF THAT ARE PINNED, because both were once asserted here and
|
|
407
|
+
// pinned by nothing:
|
|
408
|
+
//
|
|
409
|
+
// status -- assertion 22 sweeps payload x id x id-type, plus `null`.
|
|
410
|
+
// latency -- assertion 41 asserts NO `store.find` is issued on this
|
|
411
|
+
// path. Moving the refusal to after a lookup and returning
|
|
412
|
+
// the same 403 left the suite green while re-opening a
|
|
413
|
+
// hit-versus-miss timing difference on every id-bearing POST,
|
|
414
|
+
// which is what would turn #197 from a ~0.06ms post-fetch
|
|
415
|
+
// residual into a live timing oracle on create.
|
|
416
|
+
//
|
|
417
|
+
// AND THE GUARANTEE IS ONLY AS WIDE AS THE CHANNELS IT COVERS. It reads on
|
|
418
|
+
// the `id` member of the resource object, so it holds only while that is
|
|
419
|
+
// the ONLY way a caller id can reach `createRecord`. It was not: the
|
|
420
|
+
// relationships loop below re-admitted one under `key === "id"` and the
|
|
421
|
+
// gate never fired. Both strips -- `attributes.id` and `relationships.id`
|
|
422
|
+
// -- are therefore part of THIS gate, not tidiness, and assertion 39 pins
|
|
423
|
+
// them. Adding a third channel without a strip re-opens the oracle.
|
|
424
|
+
//
|
|
425
|
+
// Scoped to function-style `access` because that is exactly the population
|
|
426
|
+
// the oracle exists for: with no per-record filter there are no hidden
|
|
427
|
+
// records, and 409 discloses nothing GET /:id does not already.
|
|
428
|
+
//
|
|
429
|
+
// RESIDUAL, stated rather than implied: a caller can still learn that a
|
|
430
|
+
// collection HAS a per-record filter (403 rather than 409/200 for an
|
|
431
|
+
// id-bearing POST). That discloses a configuration fact, not a record.
|
|
432
|
+
// See README `### Known limitations`.
|
|
433
|
+
if (id !== undefined) {
|
|
434
|
+
if (typeof filter === 'function')
|
|
435
|
+
return 403; // Forbidden
|
|
436
|
+
// `normalizeBodyId`, not the raw value: a string-typed id misses the
|
|
437
|
+
// store's numeric key, which skipped this check entirely.
|
|
438
|
+
const existing = await store.find(model, normalizeBodyId(id));
|
|
439
|
+
if (existing)
|
|
440
|
+
return 409; // Conflict
|
|
441
|
+
}
|
|
240
442
|
const { id: _ignoredId, ...sanitizedAttributes } = attributes || {};
|
|
241
|
-
// Extract relationship IDs from JSON:API relationships object
|
|
443
|
+
// Extract relationship IDs from JSON:API relationships object.
|
|
444
|
+
//
|
|
445
|
+
// `key` comes VERBATIM from the request body, so `id` is stripped here for
|
|
446
|
+
// exactly the same reason it is stripped from `attributes` on the line
|
|
447
|
+
// above -- and it must be, or GATE 0 is walked around by moving one field:
|
|
448
|
+
//
|
|
449
|
+
// POST /animals {"id":21, ...} -> 403 GATE 0 fires
|
|
450
|
+
// POST /animals {"relationships":{"id":{"data":{"id":21}}},
|
|
451
|
+
// "attributes":{"owner":"gina"}} -> 200 BYPASS
|
|
452
|
+
//
|
|
453
|
+
// Top-level `id` stayed `undefined`, so GATE 0 never fired and the
|
|
454
|
+
// collision lookup never ran; `createRecord` took its last-entry-wins
|
|
455
|
+
// branch, overwrote hidden record 21 in place and reset its `owner` to a
|
|
456
|
+
// value the caller chose -- de-hiding it permanently. That is #190 itself,
|
|
457
|
+
// on the create surface. Pinned by assertion 39.
|
|
458
|
+
//
|
|
459
|
+
// The `id` member of the resource object is now the ONLY channel a caller
|
|
460
|
+
// id can arrive on, which is what makes GATE 0's guarantee checkable
|
|
461
|
+
// rather than merely asserted. INHERITED from `dev`, which carries this
|
|
462
|
+
// loop verbatim; the general form -- the loop accepts any key, not just
|
|
463
|
+
// `id`, so a body key that is not a declared relationship is still
|
|
464
|
+
// mass-assigned -- is abofs/stonyx-orm#204.
|
|
242
465
|
if (rels) {
|
|
243
466
|
for (const [key, value] of Object.entries(rels)) {
|
|
467
|
+
if (key === 'id')
|
|
468
|
+
continue;
|
|
244
469
|
const relData = value?.data;
|
|
245
470
|
if (relData && relData.id !== undefined) {
|
|
246
471
|
sanitizedAttributes[key] = relData.id;
|
|
@@ -248,16 +473,89 @@ export default class OrmRequest extends Request {
|
|
|
248
473
|
}
|
|
249
474
|
}
|
|
250
475
|
const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
|
|
476
|
+
// Slot count BEFORE the write. `createRecord` writes to the store before
|
|
477
|
+
// the predicate can run, and the rollback below must be able to prove the
|
|
478
|
+
// slot it removes is one THIS REQUEST created. Identity alone cannot
|
|
479
|
+
// prove it: when `assignRecordId` lands on an occupied id, `createRecord`
|
|
480
|
+
// mutates the existing OrmRecord IN PLACE, so `store.get(...) === record`
|
|
481
|
+
// is true for a record the request did not create. The map's size is the
|
|
482
|
+
// only O(1) signal that distinguishes an insert from an overwrite.
|
|
483
|
+
const slotsBefore = store.get(model)?.size ?? 0;
|
|
251
484
|
const created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
|
|
252
485
|
const record = isOrmRecord(created) ? created : null;
|
|
253
486
|
if (!record)
|
|
254
487
|
return 500;
|
|
488
|
+
const createdNewSlot = (store.get(model)?.size ?? 0) > slotsBefore;
|
|
489
|
+
// 403 here, NOT 404. The oracle argument does not apply to create: there
|
|
490
|
+
// is no pre-existing record whose existence could leak, the caller
|
|
491
|
+
// supplied the attributes, and 404 on a mounted collection route is
|
|
492
|
+
// indistinguishable from "model not mounted" -- a genuinely different
|
|
493
|
+
// failure a developer needs to diagnose.
|
|
494
|
+
//
|
|
495
|
+
// The rollback is not optional. createRecord writes to the store BEFORE
|
|
496
|
+
// the predicate can run, so returning 403 alone would leave the record
|
|
497
|
+
// behind: a worse bug than the bypass being fixed.
|
|
498
|
+
if (isDenied(filter, record)) {
|
|
499
|
+
// ROLL BACK BY IDENTITY, NEVER BY ID. `store.remove(model, record.id)`
|
|
500
|
+
// on its own is a write primitive keyed by a value the caller may have
|
|
501
|
+
// supplied: with the raw-id collision bypass above, a denied
|
|
502
|
+
// `POST {"id":"21"}` answered 403 and DELETED hidden record 21 -- an
|
|
503
|
+
// unauthenticated deletion primitive across the whole id space, created
|
|
504
|
+
// by adding a rollback to a lookup that could be skipped.
|
|
505
|
+
//
|
|
506
|
+
// Both conditions are required and neither implies the other:
|
|
507
|
+
// createdNewSlot -- the store grew, so this request inserted rather
|
|
508
|
+
// than overwrote. Guards `assignRecordId` picking an
|
|
509
|
+
// id that is already taken (it returns
|
|
510
|
+
// last-INSERTED + 1, not max + 1, so a store whose
|
|
511
|
+
// insertion order is not ascending collides) -- see
|
|
512
|
+
// abofs/stonyx-orm#203.
|
|
513
|
+
// identity -- the slot still holds the object we just created,
|
|
514
|
+
// so nothing between createRecord and here replaced
|
|
515
|
+
// it. Deleting this half SURVIVES the suite, and it
|
|
516
|
+
// is kept anyway. WHY IT IS REDUNDANT: there is no
|
|
517
|
+
// `await` anywhere between `slotsBefore` and
|
|
518
|
+
// `store.remove` -- the whole window is synchronous,
|
|
519
|
+
// so it is atomic under Node's event loop; before-
|
|
520
|
+
// `create` hooks run BEFORE the handler
|
|
521
|
+
// (`_withHooks` runs its hook loop ahead of
|
|
522
|
+
// `await handler(...)`), and a consumer predicate
|
|
523
|
+
// inside `isDenied` runs AFTER `createdNewSlot` is
|
|
524
|
+
// computed and cannot flip it. That is a property of
|
|
525
|
+
// THIS function, not of GATE 0 -- an earlier note
|
|
526
|
+
// credited GATE 0, which was both wrong (a caller id
|
|
527
|
+
// reached createRecord through the relationships
|
|
528
|
+
// loop, #204) and the wrong kind of reason: a guard
|
|
529
|
+
// justified on code sixty lines upstream gets
|
|
530
|
+
// silently re-armed when that code moves.
|
|
531
|
+
// SO IT BECOMES REACHABLE IF AN `await` IS
|
|
532
|
+
// INTRODUCED HERE, which is the change a future
|
|
533
|
+
// editor would actually make. See the
|
|
534
|
+
// guards-redundant-by-construction table in
|
|
535
|
+
// docs/project-structure.md.
|
|
536
|
+
if (createdNewSlot && store.get(model, record.id) === record) {
|
|
537
|
+
store.remove(model, record.id, { _skipAutoPersist: true });
|
|
538
|
+
}
|
|
539
|
+
return 403;
|
|
540
|
+
}
|
|
255
541
|
return { data: record.toJSON?.({ fields: modelFields }) };
|
|
256
542
|
};
|
|
257
|
-
const updateHandler = async ({ body, params }) => {
|
|
543
|
+
const updateHandler = async ({ body, params }, { filter }) => {
|
|
258
544
|
const found = await store.find(model, getId(params));
|
|
259
545
|
if (!found || !isOrmRecord(found))
|
|
260
546
|
return 404;
|
|
547
|
+
// Checked BEFORE any attribute is applied. 404 rather than 403 for the
|
|
548
|
+
// same reason as GET /:id -- 403 would disclose both that the record
|
|
549
|
+
// exists and that this caller specifically is excluded.
|
|
550
|
+
//
|
|
551
|
+
// NOT redundant behind GATE 1, and not defence in depth either: GATE 1's
|
|
552
|
+
// verdict is computed BEFORE the before-hook loop runs, and a before-hook
|
|
553
|
+
// is a published extension point that can change the answer -- by
|
|
554
|
+
// mutating the record, or against a predicate that closes over
|
|
555
|
+
// per-request state. This is the only re-evaluation after that window.
|
|
556
|
+
// Pinned by assertion 32; deleting it turns a 404 into an applied update.
|
|
557
|
+
if (isDenied(filter, found))
|
|
558
|
+
return 404;
|
|
261
559
|
const record = found;
|
|
262
560
|
const { attributes, relationships: rels } = (body?.data || {});
|
|
263
561
|
if (!attributes && !rels)
|
|
@@ -277,6 +575,19 @@ export default class OrmRequest extends Request {
|
|
|
277
575
|
if (rels) {
|
|
278
576
|
const relUpdates = {};
|
|
279
577
|
for (const [key, value] of Object.entries(rels)) {
|
|
578
|
+
// The same missing key filter as createHandler's, and as the
|
|
579
|
+
// attribute loop directly above -- which already had it, while this
|
|
580
|
+
// loop did not. A PATCH carrying
|
|
581
|
+
// `relationships:{"id":{"data":{"id":9101}}}` reached `updateRecord`
|
|
582
|
+
// and RE-KEYED the record: the object held under store key 9102 then
|
|
583
|
+
// reported id 9101, so a visible record claimed a hidden record's
|
|
584
|
+
// identity on every surface that reads `record.id` rather than the map
|
|
585
|
+
// key. Gated by GATE 1 on the addressed record, so it is store
|
|
586
|
+
// corruption rather than a filter bypass -- but it is the same one-line
|
|
587
|
+
// omission two handlers apart. Pinned by assertion 40; INHERITED from
|
|
588
|
+
// `dev`; abofs/stonyx-orm#204.
|
|
589
|
+
if (key === 'id')
|
|
590
|
+
continue;
|
|
280
591
|
const relData = value?.data;
|
|
281
592
|
if (relData && relData.id !== undefined) {
|
|
282
593
|
relUpdates[key] = relData.id;
|
|
@@ -288,8 +599,23 @@ export default class OrmRequest extends Request {
|
|
|
288
599
|
}
|
|
289
600
|
return { data: record.toJSON?.() };
|
|
290
601
|
};
|
|
291
|
-
const deleteHandler = ({ params }) => {
|
|
292
|
-
store.
|
|
602
|
+
const deleteHandler = async ({ params }, { filter }) => {
|
|
603
|
+
const record = await store.find(model, getId(params));
|
|
604
|
+
// BEHAVIOUR CHANGE (#190): a DELETE of a record that never existed
|
|
605
|
+
// returned 204 before this change. It now returns 404, matching the
|
|
606
|
+
// denied case below. This is deliberate and load-bearing -- if a denied
|
|
607
|
+
// delete returned 404 while a missing one returned 204, the pair would be
|
|
608
|
+
// a perfect existence oracle and the whole fix would be worthless.
|
|
609
|
+
// Returning 204 for a denied delete was rejected instead: it falsely
|
|
610
|
+
// reports success for a request that changed nothing.
|
|
611
|
+
if (!record)
|
|
612
|
+
return 404;
|
|
613
|
+
// Re-evaluated after the before-hook loop, exactly as in updateHandler --
|
|
614
|
+
// GATE 1 decided before the hooks ran. Pinned by assertion 33; deleting it
|
|
615
|
+
// turns a 404 into a destroyed record.
|
|
616
|
+
if (isDenied(filter, record))
|
|
617
|
+
return 404;
|
|
618
|
+
store.remove(model, getId(params), { _skipAutoPersist: true });
|
|
293
619
|
return 204;
|
|
294
620
|
};
|
|
295
621
|
// Wrap handlers with hooks
|
|
@@ -314,9 +640,36 @@ export default class OrmRequest extends Request {
|
|
|
314
640
|
};
|
|
315
641
|
}
|
|
316
642
|
}
|
|
317
|
-
// Wraps a handler with before/after hook execution
|
|
643
|
+
// Wraps a handler with before/after hook execution.
|
|
644
|
+
//
|
|
645
|
+
// ===========================================================================
|
|
646
|
+
// TWO AUTHORIZATION GATES, AND EVERY EXECUTOR SITS BEHIND ONE OF THEM (#190)
|
|
647
|
+
//
|
|
648
|
+
// The defect this function was fixed for is NOT "a delete persists past a
|
|
649
|
+
// 404". It is that _withHooks has SEVERAL executors downstream of the
|
|
650
|
+
// handler, and originally the handler's response gated none of them. Three
|
|
651
|
+
// exist today:
|
|
652
|
+
//
|
|
653
|
+
// 1. sqlDb.persist -- issues real SQL against the backing store
|
|
654
|
+
// 2. the after-hook pipeline -- the PUBLISHED consumer extension point;
|
|
655
|
+
// a cascade delete, a webhook, a search-index
|
|
656
|
+
// purge. `context.recordId` and
|
|
657
|
+
// `context.oldState` are populated for it.
|
|
658
|
+
// 3. Orm.db.save() -- a full serialize-and-write of the store
|
|
659
|
+
//
|
|
660
|
+
// Gating them one at a time is how this keeps regressing, so the rule is:
|
|
661
|
+
// compute denial ONCE at each point where it becomes knowable, and keep every
|
|
662
|
+
// executor downstream of a gate. If you add a fourth executor to this
|
|
663
|
+
// function, it goes below GATE 2 or it is a security bug.
|
|
664
|
+
//
|
|
665
|
+
// GATE 1 (pre-handler) is required because before-hooks and `context.oldState`
|
|
666
|
+
// run/are built BEFORE the handler can consult the filter. Without it a denied
|
|
667
|
+
// DELETE still handed the hidden record's full contents to consumer code.
|
|
668
|
+
// GATE 2 (post-handler) covers everything the handler's status can reach.
|
|
669
|
+
// ===========================================================================
|
|
318
670
|
_withHooks(operation, handler) {
|
|
319
671
|
return async (request, state) => {
|
|
672
|
+
const { filter } = (state || {});
|
|
320
673
|
// Build context object for hooks
|
|
321
674
|
const context = {
|
|
322
675
|
model: this.model,
|
|
@@ -330,6 +683,26 @@ export default class OrmRequest extends Request {
|
|
|
330
683
|
// Capture old state for operations that modify data
|
|
331
684
|
if (operation === 'update' || operation === 'delete') {
|
|
332
685
|
const existingRecord = await store.find(this.model, getId(request.params));
|
|
686
|
+
// GATE 1 -- pre-handler. This record fetch already happened for
|
|
687
|
+
// oldState, so the check is free.
|
|
688
|
+
//
|
|
689
|
+
// Returning here rather than letting updateHandler/deleteHandler
|
|
690
|
+
// produce the same 404 is the point: everything between here and there
|
|
691
|
+
// is an executor the caller is not authorized to reach.
|
|
692
|
+
// - context.oldState is a deep copy of the HIDDEN RECORD'S CONTENTS.
|
|
693
|
+
// Building it and handing it to a before-hook discloses exactly what
|
|
694
|
+
// the filter exists to hide.
|
|
695
|
+
// - context.recordId is populated for delete BEFORE the handler runs,
|
|
696
|
+
// which is the same shape as the sqlDb landmine one layer up:
|
|
697
|
+
// `afterHook('delete', ctx => cascadeDelete(ctx.recordId))` destroys
|
|
698
|
+
// children behind a correct 404.
|
|
699
|
+
// - a before-hook may return a value and short-circuit, which would
|
|
700
|
+
// otherwise return a response without the filter ever executing.
|
|
701
|
+
//
|
|
702
|
+
// 404, not 403, for the same reason as getSingleHandler: the status for
|
|
703
|
+
// "exists but filtered out" must equal "does not exist".
|
|
704
|
+
if (existingRecord && isDenied(filter, existingRecord))
|
|
705
|
+
return 404;
|
|
333
706
|
if (existingRecord) {
|
|
334
707
|
// Deep copy the record's data to preserve old state
|
|
335
708
|
context.oldState = JSON.parse(JSON.stringify(existingRecord.__data || existingRecord));
|
|
@@ -348,9 +721,31 @@ export default class OrmRequest extends Request {
|
|
|
348
721
|
}
|
|
349
722
|
// Execute main handler
|
|
350
723
|
const response = await handler(request, state);
|
|
351
|
-
//
|
|
724
|
+
// Set context.record for update BEFORE persist so SQL drivers can read it
|
|
725
|
+
if (operation === 'update' && response?.data) {
|
|
726
|
+
context.record = store.get(this.model, getId(request.params));
|
|
727
|
+
}
|
|
728
|
+
// GATE 2 -- post-handler. A denied or failed handler returns a bare status
|
|
729
|
+
// integer, and no executor below may run for one.
|
|
730
|
+
//
|
|
731
|
+
// `>= 400` deliberately covers every failure status, not just the
|
|
732
|
+
// authorization ones: a 400 (POST with no `type`) and a 409 (duplicate id)
|
|
733
|
+
// are equally requests in which nothing happened, and a persist or a
|
|
734
|
+
// cascade hook for one of them is just as wrong.
|
|
735
|
+
const denied = Number.isInteger(response) && response >= 400;
|
|
736
|
+
// EXECUTOR 1 -- SQL persistence, for all write operations.
|
|
737
|
+
//
|
|
738
|
+
// `response` is passed to sqlDb.persist below, but it is dropped at the
|
|
739
|
+
// driver boundary: _persistDelete(modelName, context) never receives it
|
|
740
|
+
// and guards only on context.recordId -- which _withHooks set above,
|
|
741
|
+
// BEFORE the handler ran. Without this gate a correct 404 still issues
|
|
742
|
+
// DELETE FROM ... WHERE id = ? on every SQL backend.
|
|
743
|
+
//
|
|
744
|
+
// No file-backed test can observe that, because Orm.instance.sqlDb is
|
|
745
|
+
// null in file/directory mode. See the stubbed-sqlDb assertions in
|
|
746
|
+
// test/unit/access-filter-enforcement-test.ts.
|
|
352
747
|
const sqlDb = Orm.instance.sqlDb;
|
|
353
|
-
if (sqlDb && (operation
|
|
748
|
+
if (sqlDb && WRITE_OPERATIONS.has(operation) && !denied) {
|
|
354
749
|
await sqlDb.persist(operation, this.model, context, response);
|
|
355
750
|
}
|
|
356
751
|
// Add response and relevant records to context
|
|
@@ -367,19 +762,33 @@ export default class OrmRequest extends Request {
|
|
|
367
762
|
const recordId = isNaN(responseData.id) ? responseData.id : parseInt(responseData.id);
|
|
368
763
|
context.record = store.get(this.model, recordId);
|
|
369
764
|
}
|
|
370
|
-
else if (operation === 'update' && response?.data) {
|
|
371
|
-
context.record = store.get(this.model, getId(request.params));
|
|
372
|
-
}
|
|
373
765
|
else if (operation === 'delete') {
|
|
374
766
|
// For delete, the record may no longer exist, but we have oldState
|
|
375
767
|
context.recordId = getId(request.params);
|
|
376
768
|
}
|
|
377
|
-
//
|
|
378
|
-
|
|
379
|
-
|
|
769
|
+
// EXECUTOR 2 -- the after-hook pipeline. This is the published consumer
|
|
770
|
+
// extension point (`afterHook` is exported from @stonyx/orm and from
|
|
771
|
+
// ./hooks), so it is the executor with the widest possible blast radius:
|
|
772
|
+
// a cascade delete, a webhook, a token revocation, a search-index purge.
|
|
773
|
+
//
|
|
774
|
+
// BEHAVIOUR CHANGE (#190): after-hooks no longer fire for a request that
|
|
775
|
+
// failed. Previously `afterHook('delete', ...)` ran with a populated
|
|
776
|
+
// context.recordId on a 404, so a consumer cascade destroyed children for
|
|
777
|
+
// a request that deleted nothing. Firing a hook named "after<operation>"
|
|
778
|
+
// for an operation that did not occur is a booby trap, and the denied case
|
|
779
|
+
// is unreachable-before-#190 while the missing case is inherited debt --
|
|
780
|
+
// both are closed by the same gate. `context.response` therefore only ever
|
|
781
|
+
// carries a success status into a hook.
|
|
782
|
+
if (!denied) {
|
|
783
|
+
for (const hook of getAfterHooks(operation, this.model)) {
|
|
784
|
+
await hook(context);
|
|
785
|
+
}
|
|
380
786
|
}
|
|
381
|
-
//
|
|
382
|
-
|
|
787
|
+
// EXECUTOR 3 -- file/directory autosave. Ungated this let an
|
|
788
|
+
// unauthenticated caller force a full serialize-and-write of the entire
|
|
789
|
+
// store on every DELETE of any id, with no record touched: amplification
|
|
790
|
+
// rather than corruption, but the same root cause and the same fix.
|
|
791
|
+
if (config.orm.db.autosave === 'onUpdate' && WRITE_OPERATIONS.has(operation) && !denied) {
|
|
383
792
|
await Orm.db.save();
|
|
384
793
|
}
|
|
385
794
|
return response;
|
|
@@ -391,10 +800,21 @@ export default class OrmRequest extends Request {
|
|
|
391
800
|
// Dasherize the relationship name for URL paths (e.g., accessLinks -> access-links)
|
|
392
801
|
const dasherizedName = camelCaseToKebabCase(relationshipName);
|
|
393
802
|
// Related resource route: GET /:id/{relationship}
|
|
394
|
-
|
|
803
|
+
//
|
|
804
|
+
// These generated routes are not wrapped by _withHooks, which is why they
|
|
805
|
+
// were the least obvious two of the seven unguarded surfaces in #190.
|
|
806
|
+
// They are still dispatched by @stonyx/rest-server as
|
|
807
|
+
// `handler(req, getState(req))`, so `state` -- and therefore the filter
|
|
808
|
+
// planted by auth() -- has always been available here; it was simply
|
|
809
|
+
// never declared or read.
|
|
810
|
+
routes[`/:id/${dasherizedName}`] = async (request, { filter } = {}) => {
|
|
395
811
|
const record = await store.find(model, getId(request.params));
|
|
396
812
|
if (!record)
|
|
397
813
|
return 404;
|
|
814
|
+
// Filtering the PARENT: a caller who may not see the record may not see
|
|
815
|
+
// what it is related to either.
|
|
816
|
+
if (isDenied(filter, record))
|
|
817
|
+
return 404;
|
|
398
818
|
const relatedData = record.__relationships[relationshipName];
|
|
399
819
|
const baseUrl = getBaseUrl(request);
|
|
400
820
|
let data;
|
|
@@ -413,10 +833,12 @@ export default class OrmRequest extends Request {
|
|
|
413
833
|
};
|
|
414
834
|
};
|
|
415
835
|
// Relationship linkage route: GET /:id/relationships/{relationship}
|
|
416
|
-
routes[`/:id/relationships/${dasherizedName}`] = async (request) => {
|
|
836
|
+
routes[`/:id/relationships/${dasherizedName}`] = async (request, { filter } = {}) => {
|
|
417
837
|
const record = await store.find(model, getId(request.params));
|
|
418
838
|
if (!record)
|
|
419
839
|
return 404;
|
|
840
|
+
if (isDenied(filter, record))
|
|
841
|
+
return 404;
|
|
420
842
|
const relatedData = record.__relationships[relationshipName];
|
|
421
843
|
const baseUrl = getBaseUrl(request);
|
|
422
844
|
let data;
|
|
@@ -445,31 +867,59 @@ export default class OrmRequest extends Request {
|
|
|
445
867
|
};
|
|
446
868
|
};
|
|
447
869
|
}
|
|
448
|
-
// Catch-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
//
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
870
|
+
// Catch-alls for invalid relationship names. Every valid relationship was
|
|
871
|
+
// registered above, so reaching either of these means the relationship does
|
|
872
|
+
// not exist and the answer is 404 regardless of the record.
|
|
873
|
+
//
|
|
874
|
+
// These deliberately carry NO access check and no store lookup. An earlier
|
|
875
|
+
// revision of #190 added `if (isDenied(filter, record)) return 404` here for
|
|
876
|
+
// symmetry with the seven real surfaces, but both branches returned 404, so
|
|
877
|
+
// the guard was unobservable by construction -- a mutation deleting it
|
|
878
|
+
// survived the entire suite because no test that could distinguish it can
|
|
879
|
+
// exist. Unkillable code in an authorization diff reads as coverage and is
|
|
880
|
+
// not, so it is gone; skipping the lookup also removes the timing difference
|
|
881
|
+
// between an existing and a missing parent.
|
|
882
|
+
//
|
|
883
|
+
// IF THIS ROUTE EVER RETURNS ANYTHING OTHER THAN A CONSTANT 404, it becomes
|
|
884
|
+
// the eighth surface and must filter the parent first, exactly like
|
|
885
|
+
// `/:id/{relationship}` above.
|
|
886
|
+
routes[`/:id/:relationship`] = async () => 404;
|
|
887
|
+
routes[`/:id/relationships/:relationship`] = async () => 404;
|
|
463
888
|
return routes;
|
|
464
889
|
}
|
|
465
890
|
auth(request, state) {
|
|
466
|
-
|
|
891
|
+
// A consumer `access()` that throws is a DENIAL, matching `isDenied` one
|
|
892
|
+
// layer down. Unguarded it propagates to express's default handler, which
|
|
893
|
+
// answers 500 -- and the documented sample itself can throw
|
|
894
|
+
// (`request.originalUrl.split(...)` when originalUrl is absent), so the
|
|
895
|
+
// failure mode is reachable by following the docs.
|
|
896
|
+
let access;
|
|
897
|
+
try {
|
|
898
|
+
access = this.access(request);
|
|
899
|
+
}
|
|
900
|
+
catch {
|
|
901
|
+
return 403; // Forbidden
|
|
902
|
+
}
|
|
467
903
|
if (!access)
|
|
468
904
|
return 403;
|
|
469
|
-
if (
|
|
470
|
-
return 403;
|
|
471
|
-
if (typeof access === 'function')
|
|
905
|
+
if (typeof access === 'function') {
|
|
472
906
|
state.filter = access;
|
|
907
|
+
return undefined;
|
|
908
|
+
}
|
|
909
|
+
if (access === true)
|
|
910
|
+
return undefined;
|
|
911
|
+
// `AccessMethod` declares `string` legal and it fell through every branch
|
|
912
|
+
// above, returning undefined -- i.e. FULL CRUD, no filter. `return 'read'`
|
|
913
|
+
// is the natural reading of a type that lists `string` first, and it
|
|
914
|
+
// granted DELETE. A bare string is one permission, not a grant of all four.
|
|
915
|
+
const permitted = typeof access === 'string' ? [access] : access;
|
|
916
|
+
// Anything that is not a permission array by this point -- an object, a
|
|
917
|
+
// number, a Symbol -- is a consumer mistake, and the only safe reading of a
|
|
918
|
+
// shape the contract does not define is a denial. Fail CLOSED.
|
|
919
|
+
if (!Array.isArray(permitted))
|
|
920
|
+
return 403;
|
|
921
|
+
if (!permitted.includes(methodAccessMap[request.method]))
|
|
922
|
+
return 403;
|
|
473
923
|
return undefined;
|
|
474
924
|
}
|
|
475
925
|
}
|