mongoose 8.24.8 → 8.24.10
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/browser.umd.js +1 -1
- package/lib/cast.js +5 -0
- package/lib/cursor/queryCursor.js +8 -1
- package/lib/helpers/processConnectionOptions.js +6 -1
- package/lib/helpers/query/sanitizeFilter.js +1 -1
- package/lib/helpers/query/sanitizeProjection.js +18 -1
- package/lib/query.js +41 -21
- package/lib/queryHelpers.js +62 -10
- package/package.json +1 -1
package/lib/cast.js
CHANGED
|
@@ -111,6 +111,11 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
111
111
|
} else if (path === '$comment' && !Object.hasOwn(schema.paths, '$comment')) {
|
|
112
112
|
val = castString(val, path);
|
|
113
113
|
obj[path] = val;
|
|
114
|
+
} else if (isOperator(path)) {
|
|
115
|
+
// Pass through other top-level operators, like `$jsonSchema`, without casting.
|
|
116
|
+
// MongoDB doesn't allow querying top-level `$` fields, so these can't be paths
|
|
117
|
+
// and `strict` or `strictQuery` shouldn't strip them.
|
|
118
|
+
continue;
|
|
114
119
|
} else {
|
|
115
120
|
if (!schema) {
|
|
116
121
|
// no casting for Mixed types
|
|
@@ -72,7 +72,14 @@ function QueryCursor(query) {
|
|
|
72
72
|
this.listeners('error').length > 0 && this.emit('error', err);
|
|
73
73
|
return;
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
// `_optionsForExec()` can throw, for example if casting the projection fails
|
|
76
|
+
try {
|
|
77
|
+
Object.assign(this.options, query._optionsForExec());
|
|
78
|
+
} catch (err) {
|
|
79
|
+
this._markError(err);
|
|
80
|
+
this.listeners('error').length > 0 && this.emit('error', err);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
76
83
|
this._transforms = this._transforms.concat(query._transforms.slice());
|
|
77
84
|
if (this.options.transform) {
|
|
78
85
|
this._transforms.push(this.options.transform);
|
|
@@ -6,7 +6,7 @@ const MongooseError = require('../error/index');
|
|
|
6
6
|
function processConnectionOptions(uri, options) {
|
|
7
7
|
const opts = options ? options : {};
|
|
8
8
|
const readPreference = opts.readPreference
|
|
9
|
-
? opts.readPreference
|
|
9
|
+
? getReadPreferenceMode(opts.readPreference)
|
|
10
10
|
: getUriReadPreference(uri);
|
|
11
11
|
|
|
12
12
|
const clonedOpts = clone(opts);
|
|
@@ -40,6 +40,11 @@ function setsSecondaryRead(prefString) {
|
|
|
40
40
|
return !!(prefString === 'secondary' || prefString === 'secondaryPreferred');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
function getReadPreferenceMode(readPreference) {
|
|
44
|
+
// the MongoDB driver also accepts a `ReadPreference` instance or a `{ mode }` object
|
|
45
|
+
return typeof readPreference === 'string' ? readPreference : readPreference.mode;
|
|
46
|
+
}
|
|
47
|
+
|
|
43
48
|
function getUriReadPreference(connectionString) {
|
|
44
49
|
const exp = /(?:&|\?)readPreference=(\w+)(?:&|$)/;
|
|
45
50
|
const match = exp.exec(connectionString);
|
|
@@ -24,7 +24,7 @@ module.exports = function sanitizeFilter(filter) {
|
|
|
24
24
|
if (key === '$and' || key === '$or' || key === '$nor') {
|
|
25
25
|
sanitizeFilter(value);
|
|
26
26
|
continue;
|
|
27
|
-
} else if (key === '$
|
|
27
|
+
} else if (key[0] === '$') {
|
|
28
28
|
throw new MongooseError(key + ' is not allowed with sanitizeFilter');
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -7,7 +7,24 @@ module.exports = function sanitizeProjection(projection) {
|
|
|
7
7
|
|
|
8
8
|
const keys = Object.keys(projection);
|
|
9
9
|
for (let i = 0; i < keys.length; ++i) {
|
|
10
|
-
|
|
10
|
+
const value = projection[keys[i]];
|
|
11
|
+
if (typeof value === 'string') {
|
|
12
|
+
projection[keys[i]] = 1;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (value == null || typeof value !== 'object') {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
// Only allow `$meta: 'textScore'`, `$elemMatch`, and numeric `$slice`. Nested sub-projections, arrays, and
|
|
19
|
+
// expressions like `{ name: { $concat: ['$password'] } }` become `1`. Note that
|
|
20
|
+
// `{ $slice: ['$otherArray', 1] }` is an expression that can read another field, and
|
|
21
|
+
// `$meta: 'indexKey'` and `$meta: 'sortKey'` can return values of `select: false` paths.
|
|
22
|
+
const slice = value.$slice;
|
|
23
|
+
const isAllowedProjectionOperator = value.$meta === 'textScore' ||
|
|
24
|
+
value.$elemMatch != null ||
|
|
25
|
+
typeof slice === 'number' ||
|
|
26
|
+
(Array.isArray(slice) && slice.every(v => typeof v === 'number'));
|
|
27
|
+
if (!isAllowedProjectionOperator) {
|
|
11
28
|
projection[keys[i]] = 1;
|
|
12
29
|
}
|
|
13
30
|
}
|
package/lib/query.js
CHANGED
|
@@ -165,23 +165,31 @@ function Query(conditions, options, model, collection) {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
//
|
|
168
|
+
// Returns true if `obj` matches every document: every key is a no-op like `$comment`
|
|
169
|
+
// or `$alwaysTrue`, or a `$and` / `$or` that matches every document.
|
|
169
170
|
function isEmptyFilter(obj) {
|
|
170
171
|
if (obj == null) return true;
|
|
171
172
|
if (typeof obj !== 'object') return true;
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
173
|
+
|
|
174
|
+
for (const key of Object.keys(obj)) {
|
|
175
|
+
const val = obj[key];
|
|
176
|
+
if (key === '$comment' || key === '$alwaysTrue') {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (!Array.isArray(val)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
// `$nor: [{}]` matches no documents, but treat it as empty for backwards compatibility
|
|
183
|
+
if ((key === '$and' || key === '$nor') && val.every(isEmptyFilter)) {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (key === '$or' && (val.length === 0 || val.some(isEmptyFilter))) {
|
|
187
|
+
continue;
|
|
181
188
|
}
|
|
189
|
+
return false;
|
|
182
190
|
}
|
|
183
191
|
|
|
184
|
-
return
|
|
192
|
+
return true;
|
|
185
193
|
}
|
|
186
194
|
|
|
187
195
|
// Helper function to check for empty/invalid filter
|
|
@@ -1148,6 +1156,11 @@ Query.prototype.select = function select() {
|
|
|
1148
1156
|
}
|
|
1149
1157
|
arg = parseProjection(arg, true); // we want to keep the minus and pluses, so add boolean arg.
|
|
1150
1158
|
if (utils.isObject(arg)) {
|
|
1159
|
+
// `fields['__proto__'] = value` would re-point the projection's prototype
|
|
1160
|
+
if (Object.hasOwn(arg, '__proto__')) {
|
|
1161
|
+
arg = { ...arg };
|
|
1162
|
+
delete arg['__proto__'];
|
|
1163
|
+
}
|
|
1151
1164
|
if (this.selectedInclusively()) {
|
|
1152
1165
|
Object.entries(arg).forEach(([key, value]) => {
|
|
1153
1166
|
if (value) {
|
|
@@ -5108,11 +5121,7 @@ Query.prototype._castFields = function _castFields(fields) {
|
|
|
5108
5121
|
|
|
5109
5122
|
if (selected) {
|
|
5110
5123
|
// they passed $elemMatch, cast em
|
|
5111
|
-
|
|
5112
|
-
out = this.cast(this.model, selected);
|
|
5113
|
-
} catch (err) {
|
|
5114
|
-
return err;
|
|
5115
|
-
}
|
|
5124
|
+
out = this.cast(this.model, selected);
|
|
5116
5125
|
|
|
5117
5126
|
// apply the casted field args
|
|
5118
5127
|
for (let i = 0; i < elemMatchKeys.length; ++i) {
|
|
@@ -5135,19 +5144,30 @@ Query.prototype._applyPaths = function applyPaths() {
|
|
|
5135
5144
|
}
|
|
5136
5145
|
this._fields = this._fields || {};
|
|
5137
5146
|
|
|
5138
|
-
let
|
|
5147
|
+
let shouldSanitizeProjection = undefined;
|
|
5139
5148
|
if (this.model != null && utils.hasUserDefinedProperty(this.model.db.options, 'sanitizeProjection')) {
|
|
5140
|
-
|
|
5149
|
+
shouldSanitizeProjection = this.model.db.options.sanitizeProjection;
|
|
5141
5150
|
} else if (this.model != null && utils.hasUserDefinedProperty(this.model.base.options, 'sanitizeProjection')) {
|
|
5142
|
-
|
|
5151
|
+
shouldSanitizeProjection = this.model.base.options.sanitizeProjection;
|
|
5143
5152
|
} else {
|
|
5144
|
-
|
|
5153
|
+
shouldSanitizeProjection = this._mongooseOptions.sanitizeProjection;
|
|
5154
|
+
}
|
|
5155
|
+
|
|
5156
|
+
// Even with `schemaLevelProjections` disabled, `sanitizeProjection` should strip expressions
|
|
5157
|
+
if (shouldSanitizeProjection) {
|
|
5158
|
+
sanitizeProjection(this._fields);
|
|
5145
5159
|
}
|
|
5146
5160
|
|
|
5147
5161
|
const schemaLevelProjections = this._mongooseOptions.schemaLevelProjections ?? true;
|
|
5148
5162
|
|
|
5149
5163
|
if (schemaLevelProjections) {
|
|
5150
|
-
|
|
5164
|
+
// If the filter has a discriminator key, apply that discriminator's schema-level projections
|
|
5165
|
+
let schema = this.model.schema;
|
|
5166
|
+
const discriminatorKey = schema.options.discriminatorKey;
|
|
5167
|
+
if (this._conditions != null && Object.hasOwn(this._conditions, discriminatorKey)) {
|
|
5168
|
+
schema = getDiscriminatorByValue(this.model.discriminators, this._conditions[discriminatorKey])?.schema ?? schema;
|
|
5169
|
+
}
|
|
5170
|
+
helpers.applyPaths(this._fields, schema, shouldSanitizeProjection);
|
|
5151
5171
|
}
|
|
5152
5172
|
|
|
5153
5173
|
let _selectPopulatedPaths = true;
|
package/lib/queryHelpers.js
CHANGED
|
@@ -12,6 +12,8 @@ const get = require('./helpers/get');
|
|
|
12
12
|
const getDiscriminatorByValue =
|
|
13
13
|
require('./helpers/discriminator/getDiscriminatorByValue');
|
|
14
14
|
const isDefiningProjection = require('./helpers/projection/isDefiningProjection');
|
|
15
|
+
const isExclusive = require('./helpers/projection/isExclusive');
|
|
16
|
+
const isPOJO = require('./helpers/isPOJO');
|
|
15
17
|
const clone = require('./helpers/clone');
|
|
16
18
|
const isPathSelectedInclusive = require('./helpers/projection/isPathSelectedInclusive');
|
|
17
19
|
|
|
@@ -138,6 +140,16 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
138
140
|
const minusPathsToSkip = new Set();
|
|
139
141
|
|
|
140
142
|
if (fields) {
|
|
143
|
+
// `+path` can't override schema-level `select: false` with `sanitizeProjection`. Also remove
|
|
144
|
+
// `-+path`, because collapsing minus paths below would turn it into `+path`.
|
|
145
|
+
if (sanitizeProjection) {
|
|
146
|
+
for (const key of Object.keys(fields)) {
|
|
147
|
+
if (key[0] === '+' || key.startsWith('-+')) {
|
|
148
|
+
delete fields[key];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
141
153
|
keys = Object.keys(fields);
|
|
142
154
|
|
|
143
155
|
// Collapse minus paths
|
|
@@ -157,6 +169,7 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
157
169
|
}
|
|
158
170
|
|
|
159
171
|
keys = Object.keys(fields);
|
|
172
|
+
let hasElemMatch = false;
|
|
160
173
|
for (let keyIndex = 0; keyIndex < keys.length; ++keyIndex) {
|
|
161
174
|
if (keys[keyIndex][0] === '+') {
|
|
162
175
|
continue;
|
|
@@ -166,15 +179,32 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
166
179
|
if (!isDefiningProjection(field)) {
|
|
167
180
|
continue;
|
|
168
181
|
}
|
|
182
|
+
// `$elemMatch` is inclusive on its own, but MongoDB treats `{ arr: { $elemMatch }, name: 0 }` as exclusive
|
|
183
|
+
if (field?.$elemMatch != null) {
|
|
184
|
+
hasElemMatch = true;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
169
187
|
if (keys[keyIndex] === '_id' && keys.length > 1) {
|
|
170
188
|
continue;
|
|
171
189
|
}
|
|
172
190
|
if (keys[keyIndex] === schema.options.discriminatorKey && keys.length > 1 && field != null && !field) {
|
|
173
191
|
continue;
|
|
174
192
|
}
|
|
193
|
+
// Nested sub-projections like `{ profile: { bio: 0 } }` are exclusive or inclusive based on their
|
|
194
|
+
// nested values, unlike expressions like `{ $concat: [...] }`, which are always inclusive
|
|
195
|
+
if (isPOJO(field) && !Object.keys(field).some(key => key.startsWith('$'))) {
|
|
196
|
+
exclude = isExclusive(field) ?? undefined;
|
|
197
|
+
if (exclude === undefined) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
175
202
|
exclude = !field;
|
|
176
203
|
break;
|
|
177
204
|
}
|
|
205
|
+
if (exclude === undefined && hasElemMatch) {
|
|
206
|
+
exclude = false;
|
|
207
|
+
}
|
|
178
208
|
|
|
179
209
|
// Potentially add back minus paths based on schema-level path config
|
|
180
210
|
// and whether the projection is inclusive
|
|
@@ -202,6 +232,13 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
202
232
|
const stack = [];
|
|
203
233
|
|
|
204
234
|
analyzeSchema(schema);
|
|
235
|
+
// If `sanitizeProjection` removed every inclusion, the projection is now exclusive
|
|
236
|
+
if (sanitizeProjection && exclude === false) {
|
|
237
|
+
const keys = Object.keys(fields);
|
|
238
|
+
if (!keys.some(key => (key !== '_id' || keys.length === 1) && isDefiningProjection(fields[key]) && fields[key])) {
|
|
239
|
+
exclude = true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
205
242
|
switch (exclude) {
|
|
206
243
|
case true:
|
|
207
244
|
for (const fieldName of excluded) {
|
|
@@ -272,7 +309,10 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
272
309
|
if (type.$isSchemaMap || path.endsWith('.$*')) {
|
|
273
310
|
const plusPath = '+' + path;
|
|
274
311
|
const hasPlusPath = fields && plusPath in fields;
|
|
275
|
-
if (type.options
|
|
312
|
+
if (type.options?.select === false && sanitizeProjection) {
|
|
313
|
+
removeInclusions(path);
|
|
314
|
+
excluded.push(path);
|
|
315
|
+
} else if (type.options?.select === false && !hasPlusPath) {
|
|
276
316
|
excluded.push(path);
|
|
277
317
|
}
|
|
278
318
|
return;
|
|
@@ -312,12 +352,11 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
312
352
|
return;
|
|
313
353
|
}
|
|
314
354
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
355
|
+
if (type.selected === false && sanitizeProjection) {
|
|
356
|
+
// Fall through so `path` is excluded
|
|
357
|
+
removeInclusions(path);
|
|
358
|
+
} else if (type.selected === false && fields[path]) {
|
|
359
|
+
// User overwriting default exclusion
|
|
321
360
|
return;
|
|
322
361
|
}
|
|
323
362
|
|
|
@@ -342,10 +381,8 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
342
381
|
|
|
343
382
|
// if there are other fields being included, add this one
|
|
344
383
|
// if no other included fields, leave this out (implied inclusion)
|
|
345
|
-
if (exclude === false && keys.length > 1 && !~keys.indexOf(path)
|
|
384
|
+
if (exclude === false && keys.length > 1 && !~keys.indexOf(path)) {
|
|
346
385
|
fields[path] = 1;
|
|
347
|
-
} else if (exclude == null && sanitizeProjection && type.selected === false) {
|
|
348
|
-
fields[path] = 0;
|
|
349
386
|
}
|
|
350
387
|
|
|
351
388
|
return;
|
|
@@ -379,6 +416,21 @@ exports.applyPaths = function applyPaths(fields, schema, sanitizeProjection) {
|
|
|
379
416
|
return path;
|
|
380
417
|
}
|
|
381
418
|
|
|
419
|
+
// With `sanitizeProjection`, remove anything other than an exclusion that would project in
|
|
420
|
+
// `path`: `path` itself, a parent like `profile` for `profile.ssn`, or a child path.
|
|
421
|
+
function removeInclusions(path) {
|
|
422
|
+
for (const key of Object.keys(fields)) {
|
|
423
|
+
if (isDefiningProjection(fields[key]) && !fields[key]) {
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
// Positional projections like `comments.$` project in the whole array element
|
|
427
|
+
const keyPath = key.endsWith('.$') ? key.slice(0, -2) : key;
|
|
428
|
+
if (keyPath === path || keyPath.startsWith(path + '.') || path.startsWith(keyPath + '.')) {
|
|
429
|
+
delete fields[key];
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
382
434
|
function hasExcludedAncestor(fields, path) {
|
|
383
435
|
let i = -1;
|
|
384
436
|
while ((i = path.indexOf('.', i + 1)) !== -1) {
|