Skip to content

CDRIVER-4499 Set bson_error when hello response is malformed #1129

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 8 commits into from
Oct 21, 2022
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
61 changes: 34 additions & 27 deletions src/libmongoc/src/mongoc/mongoc-server-description.c
Original file line number Diff line number Diff line change
Expand Up @@ -601,33 +601,33 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
* MUST treat this an authentication error." */
sd->error.domain = MONGOC_ERROR_CLIENT;
sd->error.code = MONGOC_ERROR_CLIENT_AUTHENTICATE;
goto failure;
GOTO (authfailure);
}
} else if (strcmp ("isWritablePrimary", bson_iter_key (&iter)) == 0 ||
strcmp (HANDSHAKE_RESPONSE_LEGACY_HELLO,
bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_BOOL (&iter))
goto failure;
GOTO (typefailure);
is_primary = bson_iter_bool (&iter);
} else if (strcmp ("helloOk", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_BOOL (&iter))
goto failure;
GOTO (typefailure);
sd->hello_ok = bson_iter_bool (&iter);
} else if (strcmp ("me", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_UTF8 (&iter))
goto failure;
GOTO (typefailure);
sd->me = bson_iter_utf8 (&iter, NULL);
} else if (strcmp ("maxMessageSizeBytes", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT32 (&iter))
goto failure;
GOTO (typefailure);
sd->max_msg_size = bson_iter_int32 (&iter);
} else if (strcmp ("maxBsonObjectSize", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT32 (&iter))
goto failure;
GOTO (typefailure);
sd->max_bson_obj_size = bson_iter_int32 (&iter);
} else if (strcmp ("maxWriteBatchSize", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT32 (&iter))
goto failure;
GOTO (typefailure);
sd->max_write_batch_size = bson_iter_int32 (&iter);
} else if (strcmp ("logicalSessionTimeoutMinutes",
bson_iter_key (&iter)) == 0) {
Expand All @@ -637,72 +637,72 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
/* this arises executing standard JSON tests */
sd->session_timeout_minutes = MONGOC_NO_SESSIONS;
} else {
goto failure;
GOTO (typefailure);
}
} else if (strcmp ("minWireVersion", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT32 (&iter))
goto failure;
GOTO (typefailure);
sd->min_wire_version = bson_iter_int32 (&iter);
} else if (strcmp ("maxWireVersion", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT32 (&iter))
goto failure;
GOTO (typefailure);
sd->max_wire_version = bson_iter_int32 (&iter);
} else if (strcmp ("msg", bson_iter_key (&iter)) == 0) {
const char *msg;
if (!BSON_ITER_HOLDS_UTF8 (&iter))
goto failure;
GOTO (typefailure);
msg = bson_iter_utf8 (&iter, NULL);
if (msg && 0 == strcmp (msg, "isdbgrid")) {
is_shard = true;
}
} else if (strcmp ("setName", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_UTF8 (&iter))
goto failure;
GOTO (typefailure);
sd->set_name = bson_iter_utf8 (&iter, NULL);
} else if (strcmp ("setVersion", bson_iter_key (&iter)) == 0) {
mongoc_server_description_set_set_version (sd,
bson_iter_as_int64 (&iter));
} else if (strcmp ("electionId", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_OID (&iter))
goto failure;
GOTO (typefailure);
mongoc_server_description_set_election_id (sd, bson_iter_oid (&iter));
} else if (strcmp ("secondary", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_BOOL (&iter))
goto failure;
GOTO (typefailure);
is_secondary = bson_iter_bool (&iter);
} else if (strcmp ("hosts", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_ARRAY (&iter))
goto failure;
GOTO (typefailure);
bson_iter_array (&iter, &len, &bytes);
bson_destroy (&sd->hosts);
BSON_ASSERT (bson_init_static (&sd->hosts, bytes, len));
} else if (strcmp ("passives", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_ARRAY (&iter))
goto failure;
GOTO (typefailure);
bson_iter_array (&iter, &len, &bytes);
bson_destroy (&sd->passives);
BSON_ASSERT (bson_init_static (&sd->passives, bytes, len));
} else if (strcmp ("arbiters", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_ARRAY (&iter))
goto failure;
GOTO (typefailure);
bson_iter_array (&iter, &len, &bytes);
bson_destroy (&sd->arbiters);
BSON_ASSERT (bson_init_static (&sd->arbiters, bytes, len));
} else if (strcmp ("primary", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_UTF8 (&iter))
goto failure;
GOTO (typefailure);
sd->current_primary = bson_iter_utf8 (&iter, NULL);
} else if (strcmp ("arbiterOnly", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_BOOL (&iter))
goto failure;
GOTO (typefailure);
is_arbiter = bson_iter_bool (&iter);
} else if (strcmp ("isreplicaset", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_BOOL (&iter))
goto failure;
GOTO (typefailure);
is_replicaset = bson_iter_bool (&iter);
} else if (strcmp ("tags", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_DOCUMENT (&iter))
goto failure;
GOTO (typefailure);
bson_iter_document (&iter, &len, &bytes);
bson_destroy (&sd->tags);
BSON_ASSERT (bson_init_static (&sd->tags, bytes, len));
Expand All @@ -713,21 +713,21 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
!bson_iter_recurse (&iter, &child) ||
!bson_iter_find (&child, "lastWriteDate") ||
!BSON_ITER_HOLDS_DATE_TIME (&child)) {
goto failure;
GOTO (typefailure);
}

sd->last_write_date_ms = bson_iter_date_time (&child);
} else if (strcmp ("compression", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_ARRAY (&iter))
goto failure;
GOTO (typefailure);
bson_iter_array (&iter, &len, &bytes);
bson_destroy (&sd->compressors);
BSON_ASSERT (bson_init_static (&sd->compressors, bytes, len));
} else if (strcmp ("topologyVersion", bson_iter_key (&iter)) == 0) {
bson_t incoming_topology_version;

if (!BSON_ITER_HOLDS_DOCUMENT (&iter)) {
goto failure;
GOTO (typefailure);
}

bson_iter_document (&iter, &len, &bytes);
Expand All @@ -737,11 +737,11 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
bson_destroy (&incoming_topology_version);
} else if (strcmp ("serviceId", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_OID (&iter))
goto failure;
GOTO (typefailure);
bson_oid_copy_unsafe (bson_iter_oid (&iter), &sd->service_id);
} else if (strcmp ("connectionId", bson_iter_key (&iter)) == 0) {
if (!BSON_ITER_HOLDS_INT (&iter))
goto failure;
GOTO (typefailure);
sd->server_connection_id = bson_iter_as_int64 (&iter);
}
}
Expand Down Expand Up @@ -787,7 +787,14 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,

EXIT;

failure:
typefailure:
bson_set_error (&sd->error,
MONGOC_ERROR_STREAM,
MONGOC_ERROR_STREAM_INVALID_TYPE,
"unexpected type %s for field %s in hello response",
_mongoc_bson_type_to_str (bson_iter_type (&iter)),
bson_iter_key (&iter));
authfailure:
sd->type = MONGOC_SERVER_UNKNOWN;
sd->round_trip_time_msec = MONGOC_RTT_UNSET;

Expand Down
34 changes: 34 additions & 0 deletions src/libmongoc/tests/test-mongoc-server-description.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,37 @@ test_server_description_connection_id (void)
mongoc_server_description_cleanup (&sd);
}

static void
test_server_description_hello_type_error (void)
{
mongoc_server_description_t sd;
bson_error_t error;
const char *hello =
"{"
" 'ok' : { '$numberInt' : '1' },"
" 'ismaster' : true,"
" 'maxBsonObjectSize' : { '$numberInt' : '16777216' },"
" 'maxMessageSizeBytes' : { '$numberInt' : '48000000'},"
" 'maxWriteBatchSize' : { '$numberLong' : '565160423'},"
" 'logicalSessionTimeoutMinutes' : { '$numberInt' : '30'},"
" 'connectionId' : { '$numberLong' : '565160423'},"
" 'minWireVersion' : { '$numberInt' : '0'},"
" 'maxWireVersion' : { '$numberInt' : '15'},"
" 'readOnly' : true"
"}";
mongoc_server_description_init (&sd, "host:1234", 1);
memset (&error, 0, sizeof (bson_error_t));
mongoc_server_description_handle_hello (&sd, tmp_bson (hello), 0, &error);
BSON_ASSERT (sd.type == MONGOC_SERVER_UNKNOWN);
BSON_ASSERT (sd.error.code == MONGOC_ERROR_STREAM_INVALID_TYPE);
ASSERT_ERROR_CONTAINS (sd.error,
MONGOC_ERROR_STREAM,
MONGOC_ERROR_STREAM_INVALID_TYPE,
"unexpected type");

mongoc_server_description_cleanup (&sd);
}

void
test_server_description_install (TestSuite *suite)
{
Expand All @@ -442,4 +473,7 @@ test_server_description_install (TestSuite *suite)
TestSuite_Add (suite,
"/server_description/connection_id",
test_server_description_connection_id);
TestSuite_Add (suite,
"/server_description/hello_type_error",
test_server_description_hello_type_error);
}