Skip to content

Commit 8ca5db7

Browse files
committed
CDRIVER-700 unchecked errs from mongoc_uri_do_unescape
1 parent c35aea0 commit 8ca5db7

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/mongoc/mongoc-uri.c

+14
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ mongoc_uri_parse_host (mongoc_uri_t *uri,
273273
}
274274

275275
mongoc_uri_do_unescape(&hostname);
276+
if (!hostname) {
277+
/* invalid */
278+
bson_free (hostname);
279+
return false;
280+
}
281+
276282
mongoc_uri_append_host(uri, hostname, port);
277283
bson_free(hostname);
278284

@@ -400,6 +406,10 @@ mongoc_uri_parse_database (mongoc_uri_t *uri,
400406
}
401407

402408
mongoc_uri_do_unescape(&uri->database);
409+
if (!uri->database) {
410+
/* invalid */
411+
return false;
412+
}
403413

404414
return true;
405415
}
@@ -489,6 +499,10 @@ mongoc_uri_parse_option (mongoc_uri_t *uri,
489499

490500
value = bson_strdup(end_key + 1);
491501
mongoc_uri_do_unescape(&value);
502+
if (!value) {
503+
/* do_unescape detected invalid UTF-8 and freed value */
504+
return false;
505+
}
492506

493507
if (!strcasecmp(key, "connecttimeoutms") ||
494508
!strcasecmp(key, "sockettimeoutms") ||

tests/test-mongoc-uri.c

+24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
#include "test-libmongoc.h"
88

9+
#define ASSERT_SUPPRESS(x) \
10+
do { \
11+
suppress_one_message (); \
12+
ASSERT (x); \
13+
} while (0)
14+
915
static void
1016
test_mongoc_uri_new (void)
1117
{
@@ -19,6 +25,22 @@ test_mongoc_uri_new (void)
1925

2026
/* bad uris */
2127
ASSERT(!mongoc_uri_new("mongodb://"));
28+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://\x80"));
29+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://localhost/\x80"));
30+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://localhost:\x80/"));
31+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://localhost/?ipv6=\x80"));
32+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://localhost/?foo=\x80"));
33+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://localhost/?\x80=bar"));
34+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://\x80:pass@localhost"));
35+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://user:\x80@localhost"));
36+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://user%40DOMAIN.COM:password@localhost/?"
37+
"authMechanism=\x80"));
38+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://user%40DOMAIN.COM:password@localhost/?"
39+
"authMechanism=GSSAPI"
40+
"&authMechanismProperties=SERVICE_NAME:\x80"));
41+
ASSERT_SUPPRESS(!mongoc_uri_new("mongodb://user%40DOMAIN.COM:password@localhost/?"
42+
"authMechanism=GSSAPI"
43+
"&authMechanismProperties=\x80:mongodb"));
2244
ASSERT(!mongoc_uri_new("mongodb://::"));
2345
ASSERT(!mongoc_uri_new("mongodb://localhost::27017"));
2446
ASSERT(!mongoc_uri_new("mongodb://localhost,localhost::"));
@@ -347,6 +369,8 @@ test_mongoc_uri_new (void)
347369
mongoc_uri_destroy(uri);
348370
}
349371

372+
#undef ASSERT_SUPPRESS
373+
350374
static void
351375
test_mongoc_host_list_from_string (void)
352376
{

0 commit comments

Comments
 (0)