@terreno/api 57.6.0 → 57.6.1
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/dist/api.js +58 -39
- package/package.json +3 -3
- package/src/api.arrayOperations.test.ts +44 -0
- package/src/api.ts +64 -39
package/dist/api.js
CHANGED
|
@@ -659,6 +659,52 @@ const _buildModelRouter = (model, options, routePath) => {
|
|
|
659
659
|
});
|
|
660
660
|
return res.status(204).json({});
|
|
661
661
|
}));
|
|
662
|
+
/**
|
|
663
|
+
* Applies consumer update transforms and authorization hooks for array subroutes.
|
|
664
|
+
* Keeping this pipeline shared ensures invalid field requests cannot bypass consumer
|
|
665
|
+
* authorization before modelRouter returns its own validation error.
|
|
666
|
+
*/
|
|
667
|
+
const applyArrayOperationUpdateHooks = async (inputBody, req) => {
|
|
668
|
+
let body = inputBody;
|
|
669
|
+
try {
|
|
670
|
+
body = (0, transformers_1.transform)(options, body, "update", req.user);
|
|
671
|
+
}
|
|
672
|
+
catch (error) {
|
|
673
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
674
|
+
code: "transform-error",
|
|
675
|
+
status: 403,
|
|
676
|
+
title: "Transform error",
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
if (options.preUpdate) {
|
|
680
|
+
try {
|
|
681
|
+
body = await options.preUpdate(body, req);
|
|
682
|
+
}
|
|
683
|
+
catch (error) {
|
|
684
|
+
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
685
|
+
code: "pre-update-hook-error",
|
|
686
|
+
detail: `preUpdate hook error on ${req.params.id}: ${(0, errors_1.errorDetail)(error)}`,
|
|
687
|
+
status: 400,
|
|
688
|
+
title: "preUpdate hook error",
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
if (body === undefined) {
|
|
692
|
+
throw new errors_1.ForbiddenError({
|
|
693
|
+
code: "update-not-allowed",
|
|
694
|
+
detail: "A body must be returned from preUpdate",
|
|
695
|
+
title: "Update not allowed",
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
if (body === null) {
|
|
699
|
+
throw new errors_1.ForbiddenError({
|
|
700
|
+
code: "update-not-allowed",
|
|
701
|
+
detail: `preUpdate hook on ${req.params.id} returned null`,
|
|
702
|
+
title: "Update not allowed",
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return body;
|
|
707
|
+
};
|
|
662
708
|
const arrayOperation = async (req, res, operation) => {
|
|
663
709
|
// TODO Combine array operations and .patch(), as they are very similar.
|
|
664
710
|
if (!(await (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user))) {
|
|
@@ -701,7 +747,17 @@ const _buildModelRouter = (model, options, routePath) => {
|
|
|
701
747
|
title: "Malformed array operation body",
|
|
702
748
|
});
|
|
703
749
|
}
|
|
704
|
-
const
|
|
750
|
+
const currentFieldValue = doc[field];
|
|
751
|
+
if (!Array.isArray(currentFieldValue)) {
|
|
752
|
+
await applyArrayOperationUpdateHooks(req.body, req);
|
|
753
|
+
throw new errors_1.BadRequestError({
|
|
754
|
+
code: "array-operation-field-not-array",
|
|
755
|
+
detail: `Cannot apply an array operation to non-array field ${field}`,
|
|
756
|
+
meta: { field },
|
|
757
|
+
title: "Array operation field is not an array",
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
const array = [...currentFieldValue];
|
|
705
761
|
if (operation === "POST") {
|
|
706
762
|
array.push(req.body[field]);
|
|
707
763
|
}
|
|
@@ -743,44 +799,7 @@ const _buildModelRouter = (model, options, routePath) => {
|
|
|
743
799
|
title: "Invalid array operation",
|
|
744
800
|
});
|
|
745
801
|
}
|
|
746
|
-
let body = { [field]: array };
|
|
747
|
-
try {
|
|
748
|
-
body = (0, transformers_1.transform)(options, body, "update", req.user);
|
|
749
|
-
}
|
|
750
|
-
catch (error) {
|
|
751
|
-
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
752
|
-
code: "transform-error",
|
|
753
|
-
status: 403,
|
|
754
|
-
title: "Transform error",
|
|
755
|
-
});
|
|
756
|
-
}
|
|
757
|
-
if (options.preUpdate) {
|
|
758
|
-
try {
|
|
759
|
-
body = await options.preUpdate(body, req);
|
|
760
|
-
}
|
|
761
|
-
catch (error) {
|
|
762
|
-
throw (0, errors_1.passthroughOrWrap)(error, {
|
|
763
|
-
code: "pre-update-hook-error",
|
|
764
|
-
detail: `preUpdate hook error on ${req.params.id}: ${(0, errors_1.errorDetail)(error)}`,
|
|
765
|
-
status: 400,
|
|
766
|
-
title: "preUpdate hook error",
|
|
767
|
-
});
|
|
768
|
-
}
|
|
769
|
-
if (body === undefined) {
|
|
770
|
-
throw new errors_1.ForbiddenError({
|
|
771
|
-
code: "update-not-allowed",
|
|
772
|
-
detail: "A body must be returned from preUpdate",
|
|
773
|
-
title: "Update not allowed",
|
|
774
|
-
});
|
|
775
|
-
}
|
|
776
|
-
if (body === null) {
|
|
777
|
-
throw new errors_1.ForbiddenError({
|
|
778
|
-
code: "update-not-allowed",
|
|
779
|
-
detail: `preUpdate hook on ${req.params.id} returned null`,
|
|
780
|
-
title: "Update not allowed",
|
|
781
|
-
});
|
|
782
|
-
}
|
|
783
|
-
}
|
|
802
|
+
let body = await applyArrayOperationUpdateHooks({ [field]: array }, req);
|
|
784
803
|
body = (0, auth_1.omitUserRolesFromWriteBody)(model.modelName, options.accessControl, body, req.terrenoAllowUserAdminWrite === true);
|
|
785
804
|
if (options.access && options.accessControl && body && typeof body === "object") {
|
|
786
805
|
await (0, modelRouterAccess_1.validateAccessWriteBody)({
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"@modelcontextprotocol/node": "^2.0.0",
|
|
12
12
|
"@modelcontextprotocol/server": "^2.0.0",
|
|
13
13
|
"@sentry/bun": "^10.39.0",
|
|
14
|
-
"@terreno/test": "57.6.
|
|
14
|
+
"@terreno/test": "57.6.1",
|
|
15
15
|
"@thream/socketio-jwt": "^3.1.4",
|
|
16
16
|
"@types/qs": "^6.14.0",
|
|
17
17
|
"ajv": "8.20.0",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@biomejs/biome": "^2.3.6",
|
|
50
50
|
"@modelcontextprotocol/client": "^2.0.0",
|
|
51
|
-
"@terreno/syncdb": "57.6.
|
|
51
|
+
"@terreno/syncdb": "57.6.1",
|
|
52
52
|
"@types/bun": "^1.4.0",
|
|
53
53
|
"@types/cors": "^2.8.17",
|
|
54
54
|
"@types/express": "^5.0.6",
|
|
@@ -132,5 +132,5 @@
|
|
|
132
132
|
"updateSnapshot": "bun test --update-snapshots"
|
|
133
133
|
},
|
|
134
134
|
"types": "dist/index.d.ts",
|
|
135
|
-
"version": "57.6.
|
|
135
|
+
"version": "57.6.1"
|
|
136
136
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {beforeEach, describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
2
3
|
import type express from "express";
|
|
3
4
|
import supertest from "supertest";
|
|
4
5
|
import type TestAgent from "supertest/lib/agent";
|
|
@@ -422,6 +423,49 @@ describe("array operation errors", () => {
|
|
|
422
423
|
addAuthRoutes(app, UserModel as unknown as AuthUserModel);
|
|
423
424
|
});
|
|
424
425
|
|
|
426
|
+
it("rejects array operations on non-array fields with a structured error", async () => {
|
|
427
|
+
app.use(
|
|
428
|
+
"/food",
|
|
429
|
+
modelRouter(FoodModel, {
|
|
430
|
+
allowAnonymous: true,
|
|
431
|
+
permissions: {
|
|
432
|
+
create: [Permissions.IsAdmin],
|
|
433
|
+
delete: [Permissions.IsAdmin],
|
|
434
|
+
list: [Permissions.IsAdmin],
|
|
435
|
+
read: [Permissions.IsAdmin],
|
|
436
|
+
update: [Permissions.IsAdmin],
|
|
437
|
+
},
|
|
438
|
+
})
|
|
439
|
+
);
|
|
440
|
+
agent = await authAsUser(app, "admin");
|
|
441
|
+
|
|
442
|
+
const res = await agent.post(`/food/${apple._id}/name`).send({name: "Pear"}).expect(400);
|
|
443
|
+
assert.equal(res.body.code, "array-operation-field-not-array");
|
|
444
|
+
assert.equal(res.body.title, "Array operation field is not an array");
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
it("runs preUpdate before rejecting a non-array field", async () => {
|
|
448
|
+
app.use(
|
|
449
|
+
"/food",
|
|
450
|
+
modelRouter(FoodModel, {
|
|
451
|
+
allowAnonymous: true,
|
|
452
|
+
permissions: {
|
|
453
|
+
create: [Permissions.IsAdmin],
|
|
454
|
+
delete: [Permissions.IsAdmin],
|
|
455
|
+
list: [Permissions.IsAdmin],
|
|
456
|
+
read: [Permissions.IsAdmin],
|
|
457
|
+
update: [Permissions.IsAdmin],
|
|
458
|
+
},
|
|
459
|
+
preUpdate: () => null,
|
|
460
|
+
})
|
|
461
|
+
);
|
|
462
|
+
agent = await authAsUser(app, "admin");
|
|
463
|
+
|
|
464
|
+
const res = await agent.post(`/food/${apple._id}/name`).send({name: "Pear"}).expect(403);
|
|
465
|
+
assert.equal(res.body.code, "update-not-allowed");
|
|
466
|
+
assert.equal(res.body.title, "Update not allowed");
|
|
467
|
+
});
|
|
468
|
+
|
|
425
469
|
it("array operation preUpdate returning undefined throws error", async () => {
|
|
426
470
|
app.use(
|
|
427
471
|
"/food",
|
package/src/api.ts
CHANGED
|
@@ -1152,6 +1152,57 @@ const _buildModelRouter = <T>(
|
|
|
1152
1152
|
})
|
|
1153
1153
|
);
|
|
1154
1154
|
|
|
1155
|
+
/**
|
|
1156
|
+
* Applies consumer update transforms and authorization hooks for array subroutes.
|
|
1157
|
+
* Keeping this pipeline shared ensures invalid field requests cannot bypass consumer
|
|
1158
|
+
* authorization before modelRouter returns its own validation error.
|
|
1159
|
+
*/
|
|
1160
|
+
const applyArrayOperationUpdateHooks = async (
|
|
1161
|
+
inputBody: Partial<T>,
|
|
1162
|
+
req: Request
|
|
1163
|
+
): Promise<Partial<T>> => {
|
|
1164
|
+
let body: Partial<T> | null = inputBody;
|
|
1165
|
+
|
|
1166
|
+
try {
|
|
1167
|
+
body = transform<T>(options, body, "update", req.user) as Partial<T>;
|
|
1168
|
+
} catch (error: unknown) {
|
|
1169
|
+
throw passthroughOrWrap(error, {
|
|
1170
|
+
code: "transform-error",
|
|
1171
|
+
status: 403,
|
|
1172
|
+
title: "Transform error",
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
if (options.preUpdate) {
|
|
1177
|
+
try {
|
|
1178
|
+
body = await options.preUpdate(body, req);
|
|
1179
|
+
} catch (error: unknown) {
|
|
1180
|
+
throw passthroughOrWrap(error, {
|
|
1181
|
+
code: "pre-update-hook-error",
|
|
1182
|
+
detail: `preUpdate hook error on ${req.params.id}: ${errorDetail(error)}`,
|
|
1183
|
+
status: 400,
|
|
1184
|
+
title: "preUpdate hook error",
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
if (body === undefined) {
|
|
1188
|
+
throw new ForbiddenError({
|
|
1189
|
+
code: "update-not-allowed",
|
|
1190
|
+
detail: "A body must be returned from preUpdate",
|
|
1191
|
+
title: "Update not allowed",
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
if (body === null) {
|
|
1195
|
+
throw new ForbiddenError({
|
|
1196
|
+
code: "update-not-allowed",
|
|
1197
|
+
detail: `preUpdate hook on ${req.params.id} returned null`,
|
|
1198
|
+
title: "Update not allowed",
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
return body;
|
|
1204
|
+
};
|
|
1205
|
+
|
|
1155
1206
|
const arrayOperation = async (
|
|
1156
1207
|
req: Request,
|
|
1157
1208
|
res: Response,
|
|
@@ -1206,7 +1257,18 @@ const _buildModelRouter = <T>(
|
|
|
1206
1257
|
});
|
|
1207
1258
|
}
|
|
1208
1259
|
|
|
1209
|
-
const
|
|
1260
|
+
const currentFieldValue = (doc as unknown as Record<string, unknown>)[field];
|
|
1261
|
+
if (!Array.isArray(currentFieldValue)) {
|
|
1262
|
+
await applyArrayOperationUpdateHooks(req.body as Partial<T>, req);
|
|
1263
|
+
throw new BadRequestError({
|
|
1264
|
+
code: "array-operation-field-not-array",
|
|
1265
|
+
detail: `Cannot apply an array operation to non-array field ${field}`,
|
|
1266
|
+
meta: {field},
|
|
1267
|
+
title: "Array operation field is not an array",
|
|
1268
|
+
});
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
const array = [...currentFieldValue];
|
|
1210
1272
|
if (operation === "POST") {
|
|
1211
1273
|
array.push(req.body[field]);
|
|
1212
1274
|
} else if (operation === "PATCH" || operation === "DELETE") {
|
|
@@ -1243,44 +1305,7 @@ const _buildModelRouter = <T>(
|
|
|
1243
1305
|
title: "Invalid array operation",
|
|
1244
1306
|
});
|
|
1245
1307
|
}
|
|
1246
|
-
let body
|
|
1247
|
-
|
|
1248
|
-
try {
|
|
1249
|
-
body = transform<T>(options, body, "update", req.user) as Partial<T>;
|
|
1250
|
-
} catch (error: unknown) {
|
|
1251
|
-
throw passthroughOrWrap(error, {
|
|
1252
|
-
code: "transform-error",
|
|
1253
|
-
status: 403,
|
|
1254
|
-
title: "Transform error",
|
|
1255
|
-
});
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
if (options.preUpdate) {
|
|
1259
|
-
try {
|
|
1260
|
-
body = await options.preUpdate(body, req);
|
|
1261
|
-
} catch (error: unknown) {
|
|
1262
|
-
throw passthroughOrWrap(error, {
|
|
1263
|
-
code: "pre-update-hook-error",
|
|
1264
|
-
detail: `preUpdate hook error on ${req.params.id}: ${errorDetail(error)}`,
|
|
1265
|
-
status: 400,
|
|
1266
|
-
title: "preUpdate hook error",
|
|
1267
|
-
});
|
|
1268
|
-
}
|
|
1269
|
-
if (body === undefined) {
|
|
1270
|
-
throw new ForbiddenError({
|
|
1271
|
-
code: "update-not-allowed",
|
|
1272
|
-
detail: "A body must be returned from preUpdate",
|
|
1273
|
-
title: "Update not allowed",
|
|
1274
|
-
});
|
|
1275
|
-
}
|
|
1276
|
-
if (body === null) {
|
|
1277
|
-
throw new ForbiddenError({
|
|
1278
|
-
code: "update-not-allowed",
|
|
1279
|
-
detail: `preUpdate hook on ${req.params.id} returned null`,
|
|
1280
|
-
title: "Update not allowed",
|
|
1281
|
-
});
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1308
|
+
let body = await applyArrayOperationUpdateHooks({[field]: array} as unknown as Partial<T>, req);
|
|
1284
1309
|
|
|
1285
1310
|
body = omitUserRolesFromWriteBody(
|
|
1286
1311
|
model.modelName,
|