Skip to content

Commit 36d3e70

Browse files
committed
PHPC-1648: Validate directConnection in URI options array
1 parent 2bc52f9 commit 36d3e70

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

php_phongo.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,31 @@ static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri) /* {{{ */
14351435
return true;
14361436
} /* }}} */
14371437

1438+
static bool php_phongo_uri_finalize_directconnection(mongoc_uri_t* uri) /* {{{ */
1439+
{
1440+
const mongoc_host_list_t* hosts;
1441+
1442+
if (!mongoc_uri_get_option_as_bool(uri, MONGOC_URI_DIRECTCONNECTION, false)) {
1443+
return true;
1444+
}
1445+
1446+
/* Per the URI options spec, directConnection conflicts with multiple hosts
1447+
* and SRV URIs, which may resolve to multiple hosts. */
1448+
if (!strncmp(mongoc_uri_get_string(uri), "mongodb+srv://", 14)) {
1449+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: SRV URI not allowed with directConnection option.");
1450+
return false;
1451+
}
1452+
1453+
hosts = mongoc_uri_get_hosts(uri);
1454+
1455+
if (hosts && hosts->next) {
1456+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: Multiple seeds not allowed with directConnection option.");
1457+
return false;
1458+
}
1459+
1460+
return true;
1461+
} /* }}} */
1462+
14381463
static bool php_phongo_uri_finalize_tls(mongoc_uri_t* uri) /* {{{ */
14391464
{
14401465
const bson_t* options;
@@ -1676,12 +1701,17 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options)
16761701
}
16771702
}
16781703

1679-
// Finalize auth options
1704+
/* Validate any interactions between URI options */
16801705
if (!php_phongo_uri_finalize_auth(uri)) {
16811706
/* Exception should already have been thrown */
16821707
return false;
16831708
}
16841709

1710+
if (!php_phongo_uri_finalize_directconnection(uri)) {
1711+
/* Exception should already have been thrown */
1712+
return false;
1713+
}
1714+
16851715
return true;
16861716
} /* }}} */
16871717

tests/manager/manager-ctor-directconnection-error-001.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
99
$manager = new \MongoDB\Driver\Manager('mongodb://a.example.com,b.example.com/?directConnection=true');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n\n";
11+
12+
echo throws(function() {
13+
$manager = new \MongoDB\Driver\Manager('mongodb://a.example.com,b.example.com', ['directConnection' => true]);
1014
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1115

1216
?>
@@ -15,4 +19,7 @@ echo throws(function() {
1519
--EXPECT--
1620
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1721
Failed to parse MongoDB URI: 'mongodb://a.example.com,b.example.com/?directConnection=true'. Multiple seeds not allowed with directConnection option.
22+
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Failed to parse URI options: Multiple seeds not allowed with directConnection option.
1825
===DONE===

tests/manager/manager-ctor-directconnection-error-002.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ require_once __DIR__ . '/../utils/tools.php';
77

88
echo throws(function() {
99
$manager = new \MongoDB\Driver\Manager('mongodb+srv://a.example.com/?directConnection=true');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n\n";
11+
12+
echo throws(function() {
13+
$manager = new \MongoDB\Driver\Manager('mongodb+srv://a.example.com', ['directConnection' => true]);
1014
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
1115

1216
?>
@@ -15,4 +19,7 @@ echo throws(function() {
1519
--EXPECT--
1620
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
1721
Failed to parse MongoDB URI: 'mongodb+srv://a.example.com/?directConnection=true'. SRV URI not allowed with directConnection option.
22+
23+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24+
Failed to parse URI options: SRV URI not allowed with directConnection option.
1825
===DONE===

0 commit comments

Comments
 (0)