express-restify-mongoose 9.0.5 β†’ 9.0.13

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/detective.js CHANGED
@@ -18,6 +18,7 @@ function detective(model, path) {
18
18
  }
19
19
  schemaPath += keys[i];
20
20
  if (schema.path(schemaPath) && schema.path(schemaPath).schema) {
21
+ // @ts-expect-error this is fine 🐢πŸ”₯
21
22
  schema = schema.path(schemaPath).schema;
22
23
  }
23
24
  }
@@ -9,6 +9,48 @@ 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
56
  match: _zod.z.record(_zod.z.unknown()).optional(),
@@ -85,13 +127,11 @@ 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
- if (!allowRegex && `${value}`.toLowerCase().includes("$regex")) {
89
- throw new Error("regex_not_allowed");
90
- }
91
- if (typeof value !== "string") {
92
- return value;
93
- }
94
- return JSON.parse(value);
130
+ const query = typeof value === "string" ? JSON.parse(value) : value;
131
+ assertQueryIsSafe(query, {
132
+ allowRegex
133
+ });
134
+ return query;
95
135
  }, _zod.z.record(_zod.z.unknown())).transform((value)=>{
96
136
  return Object.fromEntries(Object.entries(value).map(([key, value])=>{
97
137
  if (Array.isArray(value) && !key.startsWith("$")) {
@@ -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: _zod.z.ZodIssueCode.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// @ts-expect-error this is fine 🐢πŸ”₯
25
- .findOne().and({
26
- [options.idProperty]: req.params.id
27
- }).lean(false).read(options.readPreference || "p").exec().then((doc)=>{
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
  }
@@ -48,13 +48,13 @@ function operations(model, options, filter) {
48
48
  return next();
49
49
  }
50
50
  options.contextFilter(contextModel, req, (filteredContext)=>{
51
- buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
52
- filteredContext.find(), req.erm.query).then((items)=>{
51
+ buildQuery(filteredContext.find(), req.erm.query).then((items)=>{
53
52
  req.erm.result = items;
54
53
  req.erm.statusCode = 200;
55
54
  if (options.totalCountHeader && !req.erm.query?.distinct) {
56
55
  options.contextFilter(contextModel, req, (countFilteredContext)=>{
57
- buildQuery(countFilteredContext.countDocuments(), {
56
+ buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
57
+ countFilteredContext.countDocuments(), {
58
58
  ...req.erm.query,
59
59
  skip: 0,
60
60
  limit: 0
@@ -75,7 +75,8 @@ function operations(model, options, filter) {
75
75
  return errorHandler(new Error('Model is undefined.'), req, res, next);
76
76
  }
77
77
  options.contextFilter(contextModel, req, (filteredContext)=>{
78
- buildQuery(filteredContext.countDocuments(), req.erm.query).then((count)=>{
78
+ buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
79
+ filteredContext.countDocuments(), req.erm.query).then((count)=>{
79
80
  req.erm.result = {
80
81
  count: count
81
82
  };
@@ -177,9 +178,7 @@ function operations(model, options, filter) {
177
178
  if (req.body._id === null) {
178
179
  delete req.body._id;
179
180
  }
180
- // @ts-expect-error this is fine 🐢πŸ”₯
181
181
  if (contextModel.schema.options.versionKey) {
182
- // @ts-expect-error this is fine 🐢πŸ”₯
183
182
  delete req.body[contextModel.schema.options.versionKey];
184
183
  }
185
184
  contextModel.create(req.body).then((item)=>{
@@ -203,9 +202,7 @@ function operations(model, options, filter) {
203
202
  populate: req.erm.query?.populate
204
203
  });
205
204
  delete req.body._id;
206
- // @ts-expect-error this is fine 🐢πŸ”₯
207
205
  if (contextModel.schema.options.versionKey) {
208
- // @ts-expect-error this is fine 🐢πŸ”₯
209
206
  delete req.body[contextModel.schema.options.versionKey];
210
207
  }
211
208
  function depopulate(src) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-restify-mongoose",
3
- "version": "9.0.5",
3
+ "version": "9.0.13",
4
4
  "description": "Easily create a flexible REST interface for mongoose models",
5
5
  "keywords": [
6
6
  "ReST",
@@ -35,14 +35,13 @@
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.2.1",
44
+ "mongoose": "^8.22.1",
46
45
  "serialize-error": "^8.1.0",
47
46
  "zod": "^3.19.1"
48
47
  },
@@ -63,7 +62,6 @@
63
62
  "npm-run-all": "^4.1.5",
64
63
  "prettier": "^2.7.1",
65
64
  "request": "^2.88.2",
66
- "restify": "^4.3.4",
67
65
  "sinon": "^14.0.1",
68
66
  "tsup": "^6.3.0",
69
67
  "typescript": "^4.8.4"