Skip to content

Commit 5959f64

Browse files
authored
CDRIVER-5550 fix crash when password is empty (#1586)
* add regression test * CDRIVER-5550 fix crash when password is empty Default `scram->pass` to empty string
1 parent 4059140 commit 5959f64

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/libmongoc/src/mongoc/mongoc-scram.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ _mongoc_scram_start (
341341
goto FAIL;
342342
}
343343

344+
if (!scram->pass) {
345+
// Apply an empty string as a default.
346+
scram->pass = bson_strdup ("");
347+
}
348+
344349
/* auth message is as big as the outbuf just because */
345350
scram->auth_message = (uint8_t *) bson_malloc (outbufmax);
346351
scram->auth_messagemax = outbufmax;
@@ -994,6 +999,7 @@ _mongoc_scram_step (mongoc_scram_t *scram,
994999
bool
9951000
_mongoc_sasl_prep_required (const char *str)
9961001
{
1002+
BSON_ASSERT_PARAM (str);
9971003
unsigned char c;
9981004
while (*str) {
9991005
c = (unsigned char) *str;

src/libmongoc/tests/test-mongoc-scram.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,25 @@ test_mongoc_saslprep_auth (void *ctx)
718718
_drop_saslprep_users ();
719719
}
720720

721+
// `test_mongoc_scram_empty_password` is a regression test for CDRIVER-5550.
722+
static void
723+
test_mongoc_scram_empty_password (void *ctx)
724+
{
725+
BSON_UNUSED (ctx);
726+
char *user = test_framework_get_admin_user ();
727+
char *uri_str = test_framework_get_uri_str_no_auth ("admin");
728+
mongoc_uri_t *uri = mongoc_uri_new (uri_str);
729+
mongoc_uri_set_username (uri, user);
730+
731+
// Expect an auth failure (not a crash):
732+
_try_auth_from_uri (false /* pooled */, uri, MONGOC_TEST_AUTH_ERROR);
733+
_try_auth_from_uri (true /* pooled */, uri, MONGOC_TEST_AUTH_ERROR);
734+
735+
mongoc_uri_destroy (uri);
736+
bson_free (uri_str);
737+
bson_free (user);
738+
}
739+
721740
void
722741
test_scram_install (TestSuite *suite)
723742
{
@@ -751,4 +770,12 @@ test_scram_install (TestSuite *suite)
751770
test_framework_skip_if_no_auth,
752771
_skip_if_no_sha256,
753772
TestSuite_CheckLive);
773+
TestSuite_AddFull (suite,
774+
"/scram/empty_password",
775+
test_mongoc_scram_empty_password,
776+
NULL /* dtor */,
777+
NULL /* ctx */,
778+
test_framework_skip_if_no_auth,
779+
_skip_if_no_sha256,
780+
TestSuite_CheckLive);
754781
}

0 commit comments

Comments
 (0)