Skip to content

Commit f681fa6

Browse files
committed
Fix Automattic/mongoose#2313: don't let user accidentally clobber geoNear params
1 parent db21f1c commit f681fa6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

HISTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Bumped mongodb-core to 1.1.1 to take advantage of the prototype based refactorings.
55
- Implemented missing aspects of the CRUD specification.
66
- Fixed documentation issues.
7+
- Fixed global leak REFERENCE_BY_ID in gridfs grid_store (Issue #1225, https://github.com/j)
8+
- Fix LearnBoost/mongoose#2313: don't let user accidentally clobber geoNear params (Issue #1223, https://github.com/vkarpov15)
79

810
2.0.5 2014-10-29
911
----------------

lib/collection.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,8 +1559,16 @@ Collection.prototype.geoNear = function(x, y, options, callback) {
15591559
// Ensure we have the right read preference inheritance
15601560
options = getReadPreference(this, options, this.s.db, this);
15611561

1562-
// Remove read preference from hash if it exists
1563-
commandObject = decorateCommand(commandObject, options, {readPreference: true});
1562+
// Exclude readPreference and existing options to prevent user from
1563+
// shooting themselves in the foot
1564+
var exclude = {
1565+
readPreference: true,
1566+
geoNear: true,
1567+
near: true
1568+
};
1569+
1570+
// Filter out any excluded objects
1571+
commandObject = decorateCommand(commandObject, options, exclude);
15641572

15651573
// Execute the command
15661574
this.s.db.command(commandObject, options, function (err, res) {

test/functional/readpreference_tests.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,48 @@ exports['Should correctly apply collection level read Preference to group'] = {
6969
}
7070
}
7171

72+
73+
/**
74+
* Make sure user can't clobber geoNear options
75+
*
76+
* @_class collection
77+
* @_function geoNear
78+
* @ignore
79+
*/
80+
exports['shouldNotAllowUserToClobberGeoNearWithOptions'] = {
81+
metadata: { requires: { topology: ['single', 'ssl'] } },
82+
83+
// The actual test we wish to run
84+
test: function(configuration, test) {
85+
var db = configuration.newDbInstance({w:1}, {poolSize:1});
86+
87+
// Establish connection to db
88+
db.open(function(err, db) {
89+
90+
// Fetch the collection
91+
var collection = db.collection("simple_geo_near_command");
92+
93+
// Add a location based index
94+
collection.ensureIndex({loc:"2d"}, function(err, result) {
95+
96+
// Save a new location tagged document
97+
collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {w:1}, function(err, result) {
98+
// Try to intentionally clobber the underlying geoNear option
99+
var options = {query:{a:1}, num:1, geoNear: 'bacon', near: 'butter' };
100+
101+
// Use geoNear command to find document
102+
collection.geoNear(50, 50, options, function(err, docs) {
103+
test.equal(1, docs.results.length);
104+
105+
db.close();
106+
test.done();
107+
});
108+
});
109+
});
110+
});
111+
}
112+
};
113+
72114
/**
73115
* @ignore
74116
*/

0 commit comments

Comments
 (0)