diff options
author | Ben Caimano <ben.caimano@mongodb.com> | 2020-01-08 21:05:40 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2020-01-08 21:05:40 +0000 |
commit | 11911e624ececb7546350f79c30f2abd45af5d92 (patch) | |
tree | 6fd07e5920fe093b1d602e3848b4a102fb005c53 | |
parent | a1a1e4f0c14ab71a5fc8bc7913af25571089b65f (diff) | |
download | mongo-11911e624ececb7546350f79c30f2abd45af5d92.tar.gz |
SERVER-45361 Sanitize random seeds for mongo shell
-rw-r--r-- | src/mongo/dbtests/jstests.cpp | 38 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils.cpp | 29 |
2 files changed, 9 insertions, 58 deletions
diff --git a/src/mongo/dbtests/jstests.cpp b/src/mongo/dbtests/jstests.cpp index 635fbd3088b..5efcb146d5e 100644 --- a/src/mongo/dbtests/jstests.cpp +++ b/src/mongo/dbtests/jstests.cpp @@ -1452,34 +1452,13 @@ public: ASSERT_EQUALS(scopeShardKey, trueShardKey); } - void checkWithSeed(shared_ptr<Scope> s, const mongo::BSONObj& o, int seed) { - s->setObject("o", o, true); - s->setNumber("seed", seed); - s->invoke("return convertShardKeyToHashed(o, seed);", nullptr, nullptr); - const auto scopeShardKey = s->getNumber("__returnValue"); - - // Wrapping to form a proper element - const auto wrapO = BSON("" << o); - const auto e = wrapO[""]; - const auto trueShardKey = mongo::BSONElementHasher::hash64(e, seed); - - ASSERT_EQUALS(scopeShardKey, trueShardKey); - } - void checkNoArgs(shared_ptr<Scope> s) { s->invoke("return convertShardKeyToHashed();", nullptr, nullptr); } void checkWithExtraArg(shared_ptr<Scope> s, const mongo::BSONObj& o, int seed) { s->setObject("o", o, true); - s->setNumber("seed", seed); - s->invoke("return convertShardKeyToHashed(o, seed, 1);", nullptr, nullptr); - } - - void checkWithBadSeed(shared_ptr<Scope> s, const mongo::BSONObj& o) { - s->setObject("o", o, true); - s->setString("seed", "sunflower"); - s->invoke("return convertShardKeyToHashed(o, seed);", nullptr, nullptr); + s->invoke("return convertShardKeyToHashed(o, 1);", nullptr, nullptr); } void run() { @@ -1499,23 +1478,8 @@ public: BSON("A" << 1 << "B" << "Shardy")); - // Check a few different seeds - checkWithSeed(s, - BSON("" - << "Shardy"), - mongo::BSONElementHasher::DEFAULT_HASH_SEED); - checkWithSeed(s, - BSON("" - << "Shardy"), - 0); - checkWithSeed(s, - BSON("" - << "Shardy"), - -1); - ASSERT_THROWS(checkNoArgs(s), mongo::DBException); ASSERT_THROWS(checkWithExtraArg(s, BSON("" << 10.0), 0), mongo::DBException); - ASSERT_THROWS(checkWithBadSeed(s, BSON("" << 1)), mongo::DBException); } }; diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp index b9e6b9fa7a2..faa1d52362c 100644 --- a/src/mongo/shell/shell_utils.cpp +++ b/src/mongo/shell/shell_utils.cpp @@ -306,13 +306,13 @@ BSONObj JSGetMemInfo(const BSONObj& args, void* data) { thread_local auto _prng = PseudoRandom(0); BSONObj JSSrand(const BSONObj& a, void* data) { - unsigned int seed; + int64_t seed; // grab the least significant bits of either the supplied argument or // a random number from SecureRandom. - if (a.nFields() == 1 && a.firstElement().isNumber()) - seed = static_cast<unsigned int>(a.firstElement().numberLong()); - else { - seed = static_cast<unsigned int>(SecureRandom().nextInt64()); + if (a.nFields() == 1 && a.firstElement().isNumber()) { + seed = a.firstElement().safeNumberLong(); + } else { + seed = SecureRandom().nextInt64(); } _prng = PseudoRandom(seed); return BSON("" << static_cast<double>(seed)); @@ -400,23 +400,10 @@ BSONObj computeSHA256Block(const BSONObj& a, void* data) { * > convertShardKeyToHashed("Whatever key") */ BSONObj convertShardKeyToHashed(const BSONObj& a, void* data) { - const auto& objEl = a[0]; + uassert(10151, "convertShardKeyToHashed accepts 1 argument", a.nFields() == 1); + const auto& objEl = a.firstElement(); - uassert(10151, - "convertShardKeyToHashed accepts either 1 or 2 arguments", - a.nFields() >= 1 && a.nFields() <= 2); - - // It looks like the seed is always default right now. - // But no reason not to allow for the future - auto seed = BSONElementHasher::DEFAULT_HASH_SEED; - if (a.nFields() > 1) { - auto seedEl = a[1]; - - uassert(10159, "convertShardKeyToHashed seed value should be a number", seedEl.isNumber()); - seed = seedEl.numberInt(); - } - - auto key = BSONElementHasher::hash64(objEl, seed); + auto key = BSONElementHasher::hash64(objEl, BSONElementHasher::DEFAULT_HASH_SEED); return BSON("" << key); } |