@@ -840,19 +840,22 @@ function idIncluded(fields, idName) {
840
840
return true ;
841
841
}
842
842
843
- MongoDB . prototype . buildWhere = function ( model , where ) {
843
+ MongoDB . prototype . buildWhere = function ( model , where , options ) {
844
844
var self = this ;
845
845
var query = { } ;
846
846
if ( where === null || typeof where !== 'object' ) {
847
847
return query ;
848
848
}
849
+
850
+ where = sanitizeFilter ( where , options ) ;
851
+
849
852
var idName = self . idName ( model ) ;
850
853
Object . keys ( where ) . forEach ( function ( k ) {
851
854
var cond = where [ k ] ;
852
855
if ( k === 'and' || k === 'or' || k === 'nor' ) {
853
856
if ( Array . isArray ( cond ) ) {
854
857
cond = cond . map ( function ( c ) {
855
- return self . buildWhere ( model , c ) ;
858
+ return self . buildWhere ( model , c , options ) ;
856
859
} ) ;
857
860
}
858
861
query [ '$' + k ] = cond ;
@@ -961,6 +964,7 @@ MongoDB.prototype.buildSort = function(model, order, options) {
961
964
}
962
965
}
963
966
if ( order ) {
967
+ order = sanitizeFilter ( order , options ) ;
964
968
var keys = order ;
965
969
if ( typeof keys === 'string' ) {
966
970
keys = keys . split ( ',' ) ;
@@ -1217,7 +1221,7 @@ MongoDB.prototype.all = function all(model, filter, options, callback) {
1217
1221
var idName = self . idName ( model ) ;
1218
1222
var query = { } ;
1219
1223
if ( filter . where ) {
1220
- query = self . buildWhere ( model , filter . where ) ;
1224
+ query = self . buildWhere ( model , filter . where , options ) ;
1221
1225
}
1222
1226
var fields = filter . fields ;
1223
1227
@@ -1308,7 +1312,8 @@ MongoDB.prototype.destroyAll = function destroyAll(
1308
1312
callback = where ;
1309
1313
where = undefined ;
1310
1314
}
1311
- where = self . buildWhere ( model , where ) ;
1315
+ where = self . buildWhere ( model , where , options ) ;
1316
+
1312
1317
this . execute ( model , 'remove' , where || { } , function ( err , info ) {
1313
1318
if ( err ) return callback && callback ( err ) ;
1314
1319
@@ -1335,7 +1340,7 @@ MongoDB.prototype.count = function count(model, where, options, callback) {
1335
1340
if ( self . debug ) {
1336
1341
debug ( 'count' , model , where ) ;
1337
1342
}
1338
- where = self . buildWhere ( model , where ) ;
1343
+ where = self . buildWhere ( model , where , options ) ;
1339
1344
this . execute ( model , 'countDocuments' , where , function ( err , count ) {
1340
1345
if ( self . debug ) {
1341
1346
debug ( 'count.callback' , model , err , count ) ;
@@ -1506,9 +1511,9 @@ MongoDB.prototype.update = MongoDB.prototype.updateAll = function updateAll(
1506
1511
}
1507
1512
var idName = this . idName ( model ) ;
1508
1513
1509
- where = self . buildWhere ( model , where ) ;
1510
- delete data [ idName ] ;
1514
+ where = self . buildWhere ( model , where , options ) ;
1511
1515
1516
+ delete data [ idName ] ;
1512
1517
data = self . toDatabase ( model , data ) ;
1513
1518
1514
1519
// Check for other operators and sanitize the data obj
@@ -1798,6 +1803,23 @@ MongoDB.prototype.isObjectIDProperty = function(model, prop, value) {
1798
1803
}
1799
1804
} ;
1800
1805
1806
+ function sanitizeFilter ( filter , options ) {
1807
+ options = Object . assign ( { } , options ) ;
1808
+ if ( options && options . disableSanitization ) return filter ;
1809
+ if ( ! filter || typeof filter !== 'object' ) return filter ;
1810
+
1811
+ for ( const key in filter ) {
1812
+ if ( key === '$where' || key === 'mapReduce' ) {
1813
+ debug ( `sanitizeFilter: deleting ${ key } ` ) ;
1814
+ delete filter [ key ] ;
1815
+ }
1816
+ }
1817
+
1818
+ return filter ;
1819
+ }
1820
+
1821
+ exports . sanitizeFilter = sanitizeFilter ;
1822
+
1801
1823
/**
1802
1824
* Find a matching model instances by the filter or create a new instance
1803
1825
*
@@ -1808,12 +1830,14 @@ MongoDB.prototype.isObjectIDProperty = function(model, prop, value) {
1808
1830
* @param {Object } filter The filter
1809
1831
* @param {Function } [callback] The callback function
1810
1832
*/
1811
- function optimizedFindOrCreate ( model , filter , data , callback ) {
1833
+ function optimizedFindOrCreate ( model , filter , data , options , callback ) {
1812
1834
var self = this ;
1813
1835
if ( self . debug ) {
1814
1836
debug ( 'findOrCreate' , model , filter , data ) ;
1815
1837
}
1816
1838
1839
+ if ( ! callback ) callback = options ;
1840
+
1817
1841
var idValue = self . getIdValue ( model , data ) ;
1818
1842
var idName = self . idName ( model ) ;
1819
1843
@@ -1836,10 +1860,10 @@ function optimizedFindOrCreate(model, filter, data, callback) {
1836
1860
id = self . coerceId ( model , id ) ;
1837
1861
filter . where . _id = id ;
1838
1862
}
1839
- query = self . buildWhere ( model , filter . where ) ;
1863
+ query = self . buildWhere ( model , filter . where , options ) ;
1840
1864
}
1841
1865
1842
- var sort = self . buildSort ( model , filter . order ) ;
1866
+ var sort = self . buildSort ( model , filter . order , options ) ;
1843
1867
1844
1868
this . collection ( model ) . findOneAndUpdate (
1845
1869
query ,
0 commit comments