Skip to content

Commit 2b81560

Browse files
author
Christian Hergert
committed
client: only try to create wrapper SSL stream if base_stream is valid.
This fixes a bug where the method guards would protect against the creation of the wrapper stream. It can be verified with: ./example-client 'mongodb://invalid-hostname/?ssl=true'
1 parent 4dcc78c commit 2b81560

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/mongoc/mongoc-client.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -306,32 +306,34 @@ mongoc_client_default_stream_initiator (const mongoc_uri_t *uri,
306306
}
307307

308308
#ifdef MONGOC_ENABLE_SSL
309-
options = mongoc_uri_get_options (uri);
310-
mechanism = mongoc_uri_get_auth_mechanism (uri);
311-
312-
if ((bson_iter_init_find_case (&iter, options, "ssl") &&
313-
bson_iter_as_bool (&iter)) ||
314-
(mechanism && (0 == strcmp (mechanism, "MONGODB-X509")))) {
315-
base_stream = mongoc_stream_tls_new (base_stream, &client->ssl_opts,
316-
true);
317-
318-
if (!base_stream) {
319-
bson_set_error (error,
320-
MONGOC_ERROR_STREAM,
321-
MONGOC_ERROR_STREAM_SOCKET,
322-
"Failed initialize TLS state.");
323-
return NULL;
324-
}
309+
if (base_stream) {
310+
options = mongoc_uri_get_options (uri);
311+
mechanism = mongoc_uri_get_auth_mechanism (uri);
312+
313+
if ((bson_iter_init_find_case (&iter, options, "ssl") &&
314+
bson_iter_as_bool (&iter)) ||
315+
(mechanism && (0 == strcmp (mechanism, "MONGODB-X509")))) {
316+
base_stream = mongoc_stream_tls_new (base_stream, &client->ssl_opts,
317+
true);
318+
319+
if (!base_stream) {
320+
bson_set_error (error,
321+
MONGOC_ERROR_STREAM,
322+
MONGOC_ERROR_STREAM_SOCKET,
323+
"Failed initialize TLS state.");
324+
return NULL;
325+
}
325326

326-
if (!mongoc_stream_tls_do_handshake (base_stream, -1) ||
327-
!mongoc_stream_tls_check_cert (base_stream, host->host)) {
328-
bson_set_error (error,
329-
MONGOC_ERROR_STREAM,
330-
MONGOC_ERROR_STREAM_SOCKET,
331-
"Failed to handshake and validate TLS certificate.");
332-
mongoc_stream_destroy (base_stream);
333-
base_stream = NULL;
334-
return NULL;
327+
if (!mongoc_stream_tls_do_handshake (base_stream, -1) ||
328+
!mongoc_stream_tls_check_cert (base_stream, host->host)) {
329+
bson_set_error (error,
330+
MONGOC_ERROR_STREAM,
331+
MONGOC_ERROR_STREAM_SOCKET,
332+
"Failed to handshake and validate TLS certificate.");
333+
mongoc_stream_destroy (base_stream);
334+
base_stream = NULL;
335+
return NULL;
336+
}
335337
}
336338
}
337339
#endif

0 commit comments

Comments
 (0)