Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LearnBoost/mongoose#2313: don't let user accidentally clobber geoNear params #1223

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/mongodb/collection/geo.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ var geoNear = function geoNear(x, y, options, callback) {
// Ensure we have the right read preference inheritance
options.readPreference = shared._getReadConcern(this, options);

// Remove read preference from hash if it exists
commandObject = utils.decorateCommand(commandObject, options, {readPreference: true});
// Exclude readPreference and existing options to prevent user from
// shooting themselves in the foot
var exclude = {
readPreference: true,
geoNear: true,
near: true
};
commandObject = utils.decorateCommand(commandObject, options, exclude);

// Execute the command
this.db.command(commandObject, options, function (err, res) {
Expand Down
38 changes: 37 additions & 1 deletion test/tests/functional/geo_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ exports.shouldCorrectlyPerformSimpleGeoNearCommand = function(configuration, tes
});
}

/**
* Make sure user can't clobber geoNear options
*
* @_class collection
* @_function geoNear
* @ignore
*/
exports.shouldNotAllowUserToClobberGeoNearWithOptions = function(configuration, test) {
var db = configuration.newDbInstance({w:0}, {poolSize:1});

// Establish connection to db
db.open(function(err, db) {

// Fetch the collection
var collection = db.collection("simple_geo_near_command");

// Add a location based index
collection.ensureIndex({loc:"2d"}, function(err, result) {

// Save a new location tagged document
collection.insert([{a:1, loc:[50, 30]}, {a:1, loc:[30, 50]}], {w:1}, function(err, result) {
// Try to intentionally clobber the underlying geoNear option
var options = {query:{a:1}, num:1, geoNear: 'bacon', near: 'butter' };

// Use geoNear command to find document
collection.geoNear(50, 50, options, function(err, docs) {
test.equal(1, docs.results.length);

db.close();
test.done();
});
});
});
});
};

/**
* Example of a simple geoHaystackSearch query across some documents
*
Expand Down Expand Up @@ -66,4 +102,4 @@ exports.shouldCorrectlyPerformSimpleGeoHaystackSearchCommand = function(configur
});
});
// DOC_END
}
}