Skip to content

Commit 2547749

Browse files
ajdavishanumantmk
authored andcommitted
CDRIVER-576: Validate port number in URI.
Closes #202
1 parent e48ab08 commit 2547749

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/mongoc/mongoc-uri.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717

1818
#include <ctype.h>
19+
#include <stdlib.h>
1920
#include <string.h>
2021
#include <sys/types.h>
22+
#include <math.h>
2123

2224
#include "mongoc-host-list.h"
2325
#include "mongoc-host-list-private.h"
@@ -203,6 +205,23 @@ mongoc_uri_parse_userpass (mongoc_uri_t *uri,
203205
return ret;
204206
}
205207

208+
static bool
209+
mongoc_uri_parse_port (uint16_t *port,
210+
const char *str)
211+
{
212+
unsigned long ul_port;
213+
214+
ul_port = strtoul (str, NULL, 10);
215+
216+
if (ul_port == 0 || ul_port > UINT16_MAX) {
217+
/* Parse error or port number out of range. mongod prohibits port 0. */
218+
return false;
219+
}
220+
221+
*port = (uint16_t)ul_port;
222+
return true;
223+
}
224+
206225

207226
static bool
208227
mongoc_uri_parse_host6 (mongoc_uri_t *uri,
@@ -214,11 +233,9 @@ mongoc_uri_parse_host6 (mongoc_uri_t *uri,
214233
char *hostname;
215234

216235
if ((portstr = strrchr (str, ':')) && !strstr (portstr, "]")) {
217-
#ifdef _MSC_VER
218-
sscanf_s (portstr, ":%hu", &port);
219-
#else
220-
sscanf (portstr, ":%hu", &port);
221-
#endif
236+
if (!mongoc_uri_parse_port(&port, portstr + 1)) {
237+
return false;
238+
}
222239
}
223240

224241
hostname = scan_to_unichar (str + 1, ']', "", &end_host);
@@ -245,15 +262,10 @@ mongoc_uri_parse_host (mongoc_uri_t *uri,
245262

246263
if ((hostname = scan_to_unichar(str, ':', "?/,", &end_host))) {
247264
end_host++;
248-
if (!isdigit(*end_host)) {
249-
bson_free(hostname);
265+
if (!mongoc_uri_parse_port(&port, end_host)) {
266+
bson_free (hostname);
250267
return false;
251268
}
252-
#ifdef _MSC_VER
253-
sscanf_s (end_host, "%hu", &port);
254-
#else
255-
sscanf (end_host, "%hu", &port);
256-
#endif
257269
} else {
258270
hostname = bson_strdup(str);
259271
port = MONGOC_DEFAULT_PORT;

tests/test-mongoc-uri.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include "TestSuite.h"
66

7-
87
static void
98
test_mongoc_uri_new (void)
109
{
@@ -27,6 +26,16 @@ test_mongoc_uri_new (void)
2726
ASSERT(!mongoc_uri_new("mongodb://localhost::27017"));
2827
ASSERT(!mongoc_uri_new("mongodb://localhost::27017/"));
2928
ASSERT(!mongoc_uri_new("mongodb://localhost::27017,abc"));
29+
ASSERT(!mongoc_uri_new("mongodb://localhost:-1"));
30+
ASSERT(!mongoc_uri_new("mongodb://localhost:65536"));
31+
ASSERT(!mongoc_uri_new("mongodb://localhost:foo"));
32+
ASSERT(!mongoc_uri_new("mongodb://localhost:65536/"));
33+
ASSERT(!mongoc_uri_new("mongodb://localhost:0/"));
34+
ASSERT(!mongoc_uri_new("mongodb://[::1]:-1"));
35+
ASSERT(!mongoc_uri_new("mongodb://[::1]:foo"));
36+
ASSERT(!mongoc_uri_new("mongodb://[::1]:65536"));
37+
ASSERT(!mongoc_uri_new("mongodb://[::1]:65536/"));
38+
ASSERT(!mongoc_uri_new("mongodb://[::1]:0/"));
3039

3140
uri = mongoc_uri_new("mongodb://[::1]:27888,[::2]:27999/?ipv6=true&safe=true");
3241
assert (uri);

0 commit comments

Comments
 (0)