express-restify-mongoose 9.0.10 β 9.0.14
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 +1 -0
- package/dist/detective.js +1 -0
- package/dist/getQuerySchema.js +70 -14
- package/dist/middleware/filterAndFindById.js +6 -4
- package/dist/operations.js +15 -11
- package/dist/resource_filter.js +16 -3
- package/package.json +21 -18
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ npm install express-restify-mongoose --save
|
|
|
19
19
|
|
|
20
20
|
| This library | Mongoose | MongoDB | NodeJS
|
|
21
21
|
| ------------ | ----------- | ----------- | --------
|
|
22
|
+
| >= 10.0.0 | 8.x | 5.x - 8.x | >=22
|
|
22
23
|
| >= 9.0.0 | 6.x - 8.x | 3.6.x - 7.x | >=16
|
|
23
24
|
| >= 8.0.0 | 6.x | 3.6.x - 6.x | >=18
|
|
24
25
|
| >= 7.0.0 | 6.x | 3.6.x - 6.x | >=14
|
package/dist/detective.js
CHANGED
package/dist/getQuerySchema.js
CHANGED
|
@@ -9,12 +9,54 @@ Object.defineProperty(exports, "getQueryOptionsSchema", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _zod = require("zod");
|
|
12
|
+
// MongoDB operators that evaluate arbitrary server-side JavaScript. They have no
|
|
13
|
+
// legitimate use in a client-supplied REST query and enable remote code
|
|
14
|
+
// execution and denial of service (CWE-94 / CWE-943). We reject them wherever
|
|
15
|
+
// they appear in untrusted query input. See GHSA-5r4m-2pw8-7gvw.
|
|
16
|
+
//
|
|
17
|
+
// $expr is intentionally not blocked (it enables legitimate in-document field
|
|
18
|
+
// comparisons and cannot itself run JavaScript); the recursive walk below still
|
|
19
|
+
// rejects a $function/$accumulator nested inside an $expr.
|
|
20
|
+
const BLOCKED_OPERATORS = new Set([
|
|
21
|
+
"$where",
|
|
22
|
+
"$function",
|
|
23
|
+
"$accumulator"
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* Recursively rejects server-side JavaScript operators in untrusted query
|
|
27
|
+
* input. Also enforces `allowRegex` structurally (on the parsed object) so it
|
|
28
|
+
* cannot be bypassed by sending the query as nested/bracketed parameters
|
|
29
|
+
* instead of a JSON string.
|
|
30
|
+
*/ function assertQueryIsSafe(value, { allowRegex }) {
|
|
31
|
+
if (Array.isArray(value)) {
|
|
32
|
+
for (const item of value){
|
|
33
|
+
assertQueryIsSafe(item, {
|
|
34
|
+
allowRegex
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (value === null || typeof value !== "object") {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
for (const key of Object.keys(value)){
|
|
43
|
+
if (BLOCKED_OPERATORS.has(key)) {
|
|
44
|
+
throw new Error("operator_not_allowed");
|
|
45
|
+
}
|
|
46
|
+
if (!allowRegex && (key === "$regex" || key === "$options")) {
|
|
47
|
+
throw new Error("regex_not_allowed");
|
|
48
|
+
}
|
|
49
|
+
assertQueryIsSafe(value[key], {
|
|
50
|
+
allowRegex
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
12
54
|
const PopulateOptionsSchema = _zod.z.object({
|
|
13
55
|
path: _zod.z.string(),
|
|
14
|
-
match: _zod.z.record(_zod.z.unknown()).optional(),
|
|
15
|
-
options: _zod.z.record(_zod.z.unknown()).optional(),
|
|
56
|
+
match: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
|
|
57
|
+
options: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
|
|
16
58
|
select: _zod.z.string().optional(),
|
|
17
|
-
populate: _zod.z.record(_zod.z.unknown()).optional(),
|
|
59
|
+
populate: _zod.z.record(_zod.z.string(), _zod.z.unknown()).optional(),
|
|
18
60
|
// Configure populate query to not use strict populate to maintain
|
|
19
61
|
// behavior from Mongoose previous to v6 (unless already configured)
|
|
20
62
|
strictPopulate: _zod.z.boolean().optional().default(false)
|
|
@@ -59,7 +101,7 @@ const SelectSchema = _zod.z.preprocess((value)=>{
|
|
|
59
101
|
return Object.fromEntries(value.map(fieldToRecord));
|
|
60
102
|
}
|
|
61
103
|
return value;
|
|
62
|
-
}, _zod.z.record(_zod.z.number().min(0).max(1)));
|
|
104
|
+
}, _zod.z.record(_zod.z.string(), _zod.z.number().min(0).max(1)));
|
|
63
105
|
const SortSchema = _zod.z.preprocess((value)=>{
|
|
64
106
|
if (typeof value === "string" && value.startsWith("{")) {
|
|
65
107
|
return JSON.parse(value);
|
|
@@ -67,7 +109,7 @@ const SortSchema = _zod.z.preprocess((value)=>{
|
|
|
67
109
|
return value;
|
|
68
110
|
}, _zod.z.union([
|
|
69
111
|
_zod.z.string(),
|
|
70
|
-
_zod.z.record(_zod.z.enum([
|
|
112
|
+
_zod.z.record(_zod.z.string(), _zod.z.enum([
|
|
71
113
|
"asc",
|
|
72
114
|
"desc",
|
|
73
115
|
"ascending",
|
|
@@ -75,7 +117,7 @@ const SortSchema = _zod.z.preprocess((value)=>{
|
|
|
75
117
|
"-1",
|
|
76
118
|
"1"
|
|
77
119
|
])),
|
|
78
|
-
_zod.z.record(_zod.z.number().min(-1).max(1))
|
|
120
|
+
_zod.z.record(_zod.z.string(), _zod.z.number().min(-1).max(1))
|
|
79
121
|
]));
|
|
80
122
|
const LimitSkipSchema = _zod.z.preprocess((value)=>{
|
|
81
123
|
if (typeof value !== "string") {
|
|
@@ -85,14 +127,12 @@ const LimitSkipSchema = _zod.z.preprocess((value)=>{
|
|
|
85
127
|
}, _zod.z.number());
|
|
86
128
|
function getQueryOptionsSchema({ allowRegex }) {
|
|
87
129
|
const QuerySchema = _zod.z.preprocess((value)=>{
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
return JSON.parse(value);
|
|
95
|
-
}, _zod.z.record(_zod.z.unknown())).transform((value)=>{
|
|
130
|
+
const query = typeof value === "string" ? JSON.parse(value) : value;
|
|
131
|
+
assertQueryIsSafe(query, {
|
|
132
|
+
allowRegex
|
|
133
|
+
});
|
|
134
|
+
return query;
|
|
135
|
+
}, _zod.z.record(_zod.z.string(), _zod.z.unknown())).transform((value)=>{
|
|
96
136
|
return Object.fromEntries(Object.entries(value).map(([key, value])=>{
|
|
97
137
|
if (Array.isArray(value) && !key.startsWith("$")) {
|
|
98
138
|
return [
|
|
@@ -116,6 +156,22 @@ function getQueryOptionsSchema({ allowRegex }) {
|
|
|
116
156
|
limit: LimitSkipSchema.optional(),
|
|
117
157
|
skip: LimitSkipSchema.optional(),
|
|
118
158
|
distinct: _zod.z.string().optional()
|
|
159
|
+
}).superRefine((value, ctx)=>{
|
|
160
|
+
// `populate.match` (and nested populate) accept arbitrary query objects,
|
|
161
|
+
// so they are a second injection channel that must be guarded like `query`.
|
|
162
|
+
if (value.populate === undefined || typeof value.populate === "string") {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
try {
|
|
166
|
+
assertQueryIsSafe(value.populate, {
|
|
167
|
+
allowRegex
|
|
168
|
+
});
|
|
169
|
+
} catch {
|
|
170
|
+
ctx.addIssue({
|
|
171
|
+
code: "custom",
|
|
172
|
+
message: "operator_not_allowed"
|
|
173
|
+
});
|
|
174
|
+
}
|
|
119
175
|
}).transform((value)=>{
|
|
120
176
|
if (typeof value.populate === "undefined") {
|
|
121
177
|
return value;
|
|
@@ -21,10 +21,12 @@ function getFilterAndFindByIdHandler(options) {
|
|
|
21
21
|
return next();
|
|
22
22
|
}
|
|
23
23
|
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
24
|
-
filteredContext
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
filteredContext.findOne().and([
|
|
25
|
+
{
|
|
26
|
+
[options.idProperty]: req.params.id
|
|
27
|
+
}
|
|
28
|
+
]).lean(false).read(options.readPreference || "p").exec()// @ts-expect-error this is fine πΆπ₯
|
|
29
|
+
.then((doc)=>{
|
|
28
30
|
if (!doc) {
|
|
29
31
|
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
30
32
|
}
|
package/dist/operations.js
CHANGED
|
@@ -32,10 +32,19 @@ function operations(model, options, filter) {
|
|
|
32
32
|
if (!req.erm.query?.distinct) {
|
|
33
33
|
return false;
|
|
34
34
|
}
|
|
35
|
-
return filter.
|
|
35
|
+
return filter.isPathExcluded(req.erm.query.distinct, {
|
|
36
36
|
access: req.access,
|
|
37
37
|
modelName: model.modelName
|
|
38
|
-
})
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* distinct replaces the operation of the query it is applied to, it is only
|
|
42
|
+
* meant for getItems and getItem, where isDistinctExcluded guards it.
|
|
43
|
+
*/ function withoutDistinct(req) {
|
|
44
|
+
return req.erm.query && {
|
|
45
|
+
...req.erm.query,
|
|
46
|
+
distinct: undefined
|
|
47
|
+
};
|
|
39
48
|
}
|
|
40
49
|
const getItems = function(req, res, next) {
|
|
41
50
|
const contextModel = req.erm.model;
|
|
@@ -48,8 +57,7 @@ function operations(model, options, filter) {
|
|
|
48
57
|
return next();
|
|
49
58
|
}
|
|
50
59
|
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
51
|
-
buildQuery(
|
|
52
|
-
filteredContext.find(), req.erm.query).then((items)=>{
|
|
60
|
+
buildQuery(filteredContext.find(), req.erm.query).then((items)=>{
|
|
53
61
|
req.erm.result = items;
|
|
54
62
|
req.erm.statusCode = 200;
|
|
55
63
|
if (options.totalCountHeader && !req.erm.query?.distinct) {
|
|
@@ -75,7 +83,7 @@ function operations(model, options, filter) {
|
|
|
75
83
|
return errorHandler(new Error('Model is undefined.'), req, res, next);
|
|
76
84
|
}
|
|
77
85
|
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
78
|
-
buildQuery(filteredContext.countDocuments(), req
|
|
86
|
+
buildQuery(filteredContext.countDocuments(), withoutDistinct(req)).then((count)=>{
|
|
79
87
|
req.erm.result = {
|
|
80
88
|
count: count
|
|
81
89
|
};
|
|
@@ -91,7 +99,7 @@ function operations(model, options, filter) {
|
|
|
91
99
|
}
|
|
92
100
|
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
93
101
|
buildQuery(// @ts-expect-error this is fine πΆπ₯
|
|
94
|
-
findById(filteredContext, req.params.id), req
|
|
102
|
+
findById(filteredContext, req.params.id), withoutDistinct(req)).then((item)=>{
|
|
95
103
|
if (!item) {
|
|
96
104
|
return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
|
|
97
105
|
}
|
|
@@ -110,7 +118,7 @@ function operations(model, options, filter) {
|
|
|
110
118
|
return errorHandler(new Error('Model is undefined.'), req, res, next);
|
|
111
119
|
}
|
|
112
120
|
options.contextFilter(contextModel, req, (filteredContext)=>{
|
|
113
|
-
buildQuery(filteredContext.deleteMany(), req
|
|
121
|
+
buildQuery(filteredContext.deleteMany(), withoutDistinct(req)).then(()=>{
|
|
114
122
|
req.erm.statusCode = 204;
|
|
115
123
|
next();
|
|
116
124
|
}).catch((err)=>errorHandler(err, req, res, next));
|
|
@@ -177,9 +185,7 @@ function operations(model, options, filter) {
|
|
|
177
185
|
if (req.body._id === null) {
|
|
178
186
|
delete req.body._id;
|
|
179
187
|
}
|
|
180
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
181
188
|
if (contextModel.schema.options.versionKey) {
|
|
182
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
183
189
|
delete req.body[contextModel.schema.options.versionKey];
|
|
184
190
|
}
|
|
185
191
|
contextModel.create(req.body).then((item)=>{
|
|
@@ -203,9 +209,7 @@ function operations(model, options, filter) {
|
|
|
203
209
|
populate: req.erm.query?.populate
|
|
204
210
|
});
|
|
205
211
|
delete req.body._id;
|
|
206
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
207
212
|
if (contextModel.schema.options.versionKey) {
|
|
208
|
-
// @ts-expect-error this is fine πΆπ₯
|
|
209
213
|
delete req.body[contextModel.schema.options.versionKey];
|
|
210
214
|
}
|
|
211
215
|
function depopulate(src) {
|
package/dist/resource_filter.js
CHANGED
|
@@ -19,9 +19,7 @@ function _define_property(obj, key, value) {
|
|
|
19
19
|
configurable: true,
|
|
20
20
|
writable: true
|
|
21
21
|
});
|
|
22
|
-
} else
|
|
23
|
-
obj[key] = value;
|
|
24
|
-
}
|
|
22
|
+
} else obj[key] = value;
|
|
25
23
|
return obj;
|
|
26
24
|
}
|
|
27
25
|
function _interop_require_default(obj) {
|
|
@@ -59,6 +57,21 @@ class Filter {
|
|
|
59
57
|
return options.access === "protected" ? filteredKeys.private : filteredKeys.private.concat(filteredKeys.protected);
|
|
60
58
|
}
|
|
61
59
|
/**
|
|
60
|
+
* Tells whether reading a path would disclose an excluded key.
|
|
61
|
+
*
|
|
62
|
+
* A path is excluded when it is an excluded key, a parent of one (the value
|
|
63
|
+
* would embed the excluded key) or a child of one (the value is part of the
|
|
64
|
+
* excluded key). Array positions are ignored, `purchases.0.number` is the
|
|
65
|
+
* same key as `purchases.number`.
|
|
66
|
+
*/ isPathExcluded(path, options) {
|
|
67
|
+
const normalize = (value)=>value.split(".").filter((key)=>!/^\d+$/.test(key)).join(".");
|
|
68
|
+
const normalizedPath = normalize(path);
|
|
69
|
+
return this.getExcluded(options).some((excluded)=>{
|
|
70
|
+
const normalizedExcluded = normalize(excluded);
|
|
71
|
+
return normalizedPath === normalizedExcluded || normalizedPath.startsWith(`${normalizedExcluded}.`) || normalizedExcluded.startsWith(`${normalizedPath}.`);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
62
75
|
* Removes excluded keys from a document.
|
|
63
76
|
*/ filterItem(item, excluded) {
|
|
64
77
|
if (!item) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-restify-mongoose",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.14",
|
|
4
4
|
"description": "Easily create a flexible REST interface for mongoose models",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ReST",
|
|
@@ -29,46 +29,49 @@
|
|
|
29
29
|
],
|
|
30
30
|
"scripts": {
|
|
31
31
|
"build": "run-p build:*",
|
|
32
|
-
"build:cjs": "swc src --config module.type=commonjs --out-dir dist",
|
|
32
|
+
"build:cjs": "swc src --config module.type=commonjs --strip-leading-paths --out-dir dist",
|
|
33
33
|
"build:dts": "tsup src/express-restify-mongoose.ts --dts-only",
|
|
34
34
|
"lint": "eslint . --ext .js,.ts",
|
|
35
35
|
"test": "run-s test:*",
|
|
36
36
|
"test:express": "mocha -R spec ./test/express.mjs --timeout 10s",
|
|
37
37
|
"test:filter": "mocha -R spec ./test/integration/resource_filter.mjs --timeout 10s",
|
|
38
|
-
"test:restify": "mocha -R spec ./test/restify.mjs --timeout 10s",
|
|
39
38
|
"test:unit": "mocha -R spec ./test/unit.mjs",
|
|
40
39
|
"tsc": "tsc --noEmit"
|
|
41
40
|
},
|
|
42
41
|
"dependencies": {
|
|
43
42
|
"dot-prop": "^6.0.0",
|
|
44
43
|
"lodash.isplainobject": "~4.0.6",
|
|
45
|
-
"mongoose": "^8.
|
|
44
|
+
"mongoose": "^8.22.1",
|
|
46
45
|
"serialize-error": "^8.1.0",
|
|
47
|
-
"zod": "^
|
|
46
|
+
"zod": "^4.6.6"
|
|
48
47
|
},
|
|
49
48
|
"devDependencies": {
|
|
50
|
-
"@swc/cli": "^0.
|
|
51
|
-
"@swc/core": "^1.
|
|
49
|
+
"@swc/cli": "^0.8.2",
|
|
50
|
+
"@swc/core": "^1.16.22",
|
|
52
51
|
"@swc/register": "^0.1.10",
|
|
53
52
|
"@types/express": "^4.17.14",
|
|
53
|
+
"@types/node": "^22",
|
|
54
54
|
"@typescript-eslint/eslint-plugin": "^5.40.0",
|
|
55
55
|
"@typescript-eslint/parser": "^5.40.0",
|
|
56
|
-
"body-parser": "^1.
|
|
57
|
-
"esbuild": "^0.
|
|
56
|
+
"body-parser": "^1.20.9",
|
|
57
|
+
"esbuild": "^0.28.2",
|
|
58
58
|
"eslint": "^8.25.0",
|
|
59
59
|
"eslint-plugin-mocha": "10.3.0",
|
|
60
|
-
"express": "^4.
|
|
60
|
+
"express": "^4.22.3",
|
|
61
61
|
"method-override": "^3.0.0",
|
|
62
|
-
"mocha": "^
|
|
63
|
-
"npm-run-
|
|
62
|
+
"mocha": "^12.0.3",
|
|
63
|
+
"npm-run-all2": "^9.0.4",
|
|
64
64
|
"prettier": "^2.7.1",
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
"sinon": "^22.1.0",
|
|
66
|
+
"tsup": "^9.0.0",
|
|
67
|
+
"typescript": "^5.9.3"
|
|
68
|
+
},
|
|
69
|
+
"resolutions": {
|
|
70
|
+
"mocha/glob": "^14.0.4",
|
|
71
|
+
"mocha/minimatch": "^11.0.5",
|
|
72
|
+
"@swc/cli/minimatch": "^11.0.5"
|
|
70
73
|
},
|
|
71
74
|
"engines": {
|
|
72
|
-
"node": ">=
|
|
75
|
+
"node": ">=22"
|
|
73
76
|
}
|
|
74
77
|
}
|