diff options
author | Jonathan Reams <jbreams@mongodb.com> | 2018-08-21 16:54:19 -0400 |
---|---|---|
committer | Jonathan Reams <jbreams@mongodb.com> | 2018-09-24 11:47:29 -0400 |
commit | 59c05dba8c5de6798a110b812af1166e57a5c5f4 (patch) | |
tree | 01477f615ca5f2ae8cd5d8d3706dd9a3596f3d76 | |
parent | 0c3cdb2142f79b2be95e3fd389d3d0fc5a83daac (diff) | |
download | mongo-59c05dba8c5de6798a110b812af1166e57a5c5f4.tar.gz |
SERVER-36744 Fix options parsing for --password without argument
(cherry picked from commit d87156cc8f0650b9059aea1eb1aca135db59bbbc)
-rw-r--r-- | src/mongo/shell/shell_options.cpp | 10 | ||||
-rw-r--r-- | src/mongo/shell/shell_options_test.cpp | 4 |
2 files changed, 12 insertions, 2 deletions
diff --git a/src/mongo/shell/shell_options.cpp b/src/mongo/shell/shell_options.cpp index 68c3888cbd2..48d868f9299 100644 --- a/src/mongo/shell/shell_options.cpp +++ b/src/mongo/shell/shell_options.cpp @@ -512,16 +512,22 @@ void redactPasswordOptions(int argc, char** argv) { } } if (arg.startsWith(kLongPasswordOption)) { - char* toRedact; + char* toRedact = nullptr; // Handle --password password if ((arg == kLongPasswordOption) && (i + 1 < argc)) { toRedact = argv[++i]; // Handle --password=password - } else { + } else if (arg.size() != kLongPasswordOption.size()) { toRedact = argv[i] + kLongPasswordOption.size(); // It's not valid to do --passwordpassword, make sure there's an = separator invariant(*(toRedact++) == '='); } + + // If there's nothing to redact, just exit + if (!toRedact) { + continue; + } + // The arg should be null-terminated, replace everything up to \0 to 'x' while (*toRedact) { *toRedact++ = 'x'; diff --git a/src/mongo/shell/shell_options_test.cpp b/src/mongo/shell/shell_options_test.cpp index 994e8a07060..df278112a7a 100644 --- a/src/mongo/shell/shell_options_test.cpp +++ b/src/mongo/shell/shell_options_test.cpp @@ -61,6 +61,10 @@ TEST(ShellOptions, RedactPasswords) { {"--password=xxx"_sd}}, // NOLINT {{"-ppassword"}, // NOLINT {"-pxxxxxxxx"}}, // NOLINT + // Having --password at the end of the parameters list should do nothing since it means + // prompt for password + {{"mongodb://admin@localhost/admin", "--password"}, // NOLINT + {"mongodb://admin@localhost/admin"_sd, "--password"_sd}}, // NOLINT }; for (auto& testCase : testData) { |