From d4d8631affa3d5c133fc8855575094c5288c0921 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 8 Nov 2014 17:29:33 -0500 Subject: [PATCH] Fix LearnBoost/mongoose#2313: don't let user accidentally clobber geoNear params --- lib/mongodb/collection/geo.js | 10 ++++++-- test/tests/functional/geo_tests.js | 38 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/mongodb/collection/geo.js b/lib/mongodb/collection/geo.js index 185b4568848..a4138474b81 100644 --- a/lib/mongodb/collection/geo.js +++ b/lib/mongodb/collection/geo.js @@ -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) { diff --git a/test/tests/functional/geo_tests.js b/test/tests/functional/geo_tests.js index f44aeabff08..4c11012a354 100644 --- a/test/tests/functional/geo_tests.js +++ b/test/tests/functional/geo_tests.js @@ -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 * @@ -66,4 +102,4 @@ exports.shouldCorrectlyPerformSimpleGeoHaystackSearchCommand = function(configur }); }); // DOC_END -} \ No newline at end of file +}