Skip to content

CDRIVER-5550 fix crash when password is empty #1586

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

Merged
merged 2 commits into from
May 1, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-scram.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ _mongoc_scram_start (
goto FAIL;
}

if (!scram->pass) {
// Apply an empty string as a default.
scram->pass = bson_strdup ("");
}

/* auth message is as big as the outbuf just because */
scram->auth_message = (uint8_t *) bson_malloc (outbufmax);
scram->auth_messagemax = outbufmax;
Expand Down Expand Up @@ -994,6 +999,7 @@ _mongoc_scram_step (mongoc_scram_t *scram,
bool
_mongoc_sasl_prep_required (const char *str)
{
BSON_ASSERT_PARAM (str);
unsigned char c;
while (*str) {
c = (unsigned char) *str;
Expand Down
27 changes: 27 additions & 0 deletions src/libmongoc/tests/test-mongoc-scram.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,25 @@ test_mongoc_saslprep_auth (void *ctx)
_drop_saslprep_users ();
}

// `test_mongoc_scram_empty_password` is a regression test for CDRIVER-5550.
static void
test_mongoc_scram_empty_password (void *ctx)
{
BSON_UNUSED (ctx);
char *user = test_framework_get_admin_user ();
char *uri_str = test_framework_get_uri_str_no_auth ("admin");
mongoc_uri_t *uri = mongoc_uri_new (uri_str);
mongoc_uri_set_username (uri, user);

// Expect an auth failure (not a crash):
_try_auth_from_uri (false /* pooled */, uri, MONGOC_TEST_AUTH_ERROR);
_try_auth_from_uri (true /* pooled */, uri, MONGOC_TEST_AUTH_ERROR);

mongoc_uri_destroy (uri);
bson_free (uri_str);
bson_free (user);
}

void
test_scram_install (TestSuite *suite)
{
Expand Down Expand Up @@ -751,4 +770,12 @@ test_scram_install (TestSuite *suite)
test_framework_skip_if_no_auth,
_skip_if_no_sha256,
TestSuite_CheckLive);
TestSuite_AddFull (suite,
"/scram/empty_password",
test_mongoc_scram_empty_password,
NULL /* dtor */,
NULL /* ctx */,
test_framework_skip_if_no_auth,
_skip_if_no_sha256,
TestSuite_CheckLive);
}