Skip to content

Commit ab44b0c

Browse files
committed
PHPC-1647: Use mongoc_client_new_from_uri_with_error
This allows PHPC to defer entirely to libmongoc for cross-option URI validation Bump libmongoc to 1.21-dev Build changes are ported from upstream CMake changes (see: CDRIVER-4249)
1 parent 968958f commit ab44b0c

10 files changed

+56
-175
lines changed

config.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ if test "$PHP_MONGODB" != "no"; then
294294
PHP_MONGODB_BUNDLED_CFLAGS="$STD_CFLAGS -DBSON_COMPILATION -DMONGOC_COMPILATION"
295295
dnl TODO: MONGOCRYPT-219 makes the -std argument obsolete
296296
PHP_MONGODB_LIBMONGOCRYPT_CFLAGS="-DKMS_MSG_STATIC -std=gnu99"
297+
PHP_MONGODB_ZLIB_CFLAGS=""
297298

298299
dnl M4 doesn't know if we're building statically or as a shared module, so
299300
dnl attempt to include both paths while ignoring errors. If neither path
@@ -430,8 +431,7 @@ if test "$PHP_MONGODB" != "no"; then
430431
])
431432

432433
if test "x$bundled_zlib" = "xyes"; then
433-
PHP_MONGODB_ZLIB_CFLAGS="$PHP_MONGODB_BUNDLED_CFLAGS"
434-
AC_CHECK_HEADER([unistd.h], [PHP_MONGODB_ZLIB_CFLAGS="$PHP_MONGODB_ZLIB_CFLAGS -DHAVE_UNISTD_H=1"], [])
434+
PHP_MONGODB_ZLIB_CFLAGS="$PHP_MONGODB_BUNDLED_CFLAGS $PHP_MONGODB_ZLIB_CFLAGS"
435435
PHP_MONGODB_ADD_SOURCES([src/libmongoc/src/zlib-1.2.11/], $PHP_MONGODB_ZLIB_SOURCES, $PHP_MONGODB_ZLIB_CFLAGS)
436436
PHP_MONGODB_ADD_INCLUDE([src/libmongoc/src/zlib-1.2.11/])
437437
PHP_MONGODB_ADD_BUILD_DIR([src/libmongoc/src/zlib-1.2.11/])

config.w32

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ if (PHP_MONGODB != "no") {
147147
BSON_HAVE_SYSCALL_TID: 0,
148148
BSON_HAVE_DECIMAL128: 0,
149149
BSON_HAVE_GMTIME_R: 0,
150-
BSON_HAVE_RAND_R: 0
150+
BSON_HAVE_RAND_R: 0,
151+
BSON_HAVE_ARC4RANDOM_BUF: 0
151152
};
152153

153154
if (CHECK_FUNC_IN_HEADER("stdio.h", "_set_output_format")) {

php_phongo.c

Lines changed: 9 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,126 +1435,6 @@ static const char* php_phongo_bson_type_to_string(bson_type_t type) /* {{{ */
14351435
bson_iter_key(&(iter)), \
14361436
php_phongo_bson_type_to_string(bson_iter_type(&(iter))))
14371437

1438-
static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri) /* {{{ */
1439-
{
1440-
const bson_t* credentials = mongoc_uri_get_credentials(uri);
1441-
bson_iter_t iter;
1442-
const char* source = NULL;
1443-
const char* username = mongoc_uri_get_username(uri);
1444-
bool require_auth = username != NULL;
1445-
1446-
if (bson_iter_init_find_case(&iter, credentials, MONGOC_URI_AUTHSOURCE)) {
1447-
source = bson_iter_utf8(&iter, NULL);
1448-
require_auth = true;
1449-
}
1450-
1451-
/* authSource with GSSAPI or X509 should always be external */
1452-
if (mongoc_uri_get_auth_mechanism(uri)) {
1453-
if (!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "GSSAPI") ||
1454-
!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1455-
1456-
if (source) {
1457-
if (strcasecmp(source, "$external")) {
1458-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: GSSAPI and X509 require \"$external\" authSource.");
1459-
return false;
1460-
}
1461-
} else {
1462-
mongoc_uri_set_auth_source(uri, "$external");
1463-
}
1464-
}
1465-
1466-
/* Mechanisms other than MONGODB-X509 and MONGODB-AWS require a username */
1467-
if (strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509") &&
1468-
strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-AWS")) {
1469-
if (!mongoc_uri_get_username(uri) ||
1470-
!strcmp(mongoc_uri_get_username(uri), "")) {
1471-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: '%s' authentication mechanism requires username.", mongoc_uri_get_auth_mechanism(uri));
1472-
return false;
1473-
}
1474-
}
1475-
1476-
/* MONGODB-X509 errors if a password is supplied. */
1477-
if (!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1478-
if (mongoc_uri_get_password(uri)) {
1479-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: X509 authentication mechanism does not accept a password.");
1480-
return false;
1481-
}
1482-
}
1483-
} else if (require_auth) {
1484-
if (source && strcmp(source, "$external") != 0 && (!username || strcmp(username, "") == 0)) {
1485-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: Default authentication mechanism requires username.");
1486-
return false;
1487-
}
1488-
}
1489-
1490-
return true;
1491-
} /* }}} */
1492-
1493-
static bool php_phongo_uri_finalize_directconnection(mongoc_uri_t* uri) /* {{{ */
1494-
{
1495-
const mongoc_host_list_t* hosts;
1496-
1497-
if (!mongoc_uri_get_option_as_bool(uri, MONGOC_URI_DIRECTCONNECTION, false)) {
1498-
return true;
1499-
}
1500-
1501-
/* Per the URI options spec, directConnection conflicts with multiple hosts
1502-
* and SRV URIs, which may resolve to multiple hosts. */
1503-
if (!strncmp(mongoc_uri_get_string(uri), "mongodb+srv://", 14)) {
1504-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: SRV URI not allowed with directConnection option.");
1505-
return false;
1506-
}
1507-
1508-
hosts = mongoc_uri_get_hosts(uri);
1509-
1510-
if (hosts && hosts->next) {
1511-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: Multiple seeds not allowed with directConnection option.");
1512-
return false;
1513-
}
1514-
1515-
return true;
1516-
} /* }}} */
1517-
1518-
static bool php_phongo_uri_finalize_tls(mongoc_uri_t* uri) /* {{{ */
1519-
{
1520-
const bson_t* options;
1521-
bson_iter_t iter;
1522-
1523-
if (!(options = mongoc_uri_get_options(uri))) {
1524-
return true;
1525-
}
1526-
1527-
if (bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSINSECURE) &&
1528-
(bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES) ||
1529-
bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSALLOWINVALIDHOSTNAMES) ||
1530-
bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSDISABLEOCSPENDPOINTCHECK) ||
1531-
bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK))) {
1532-
phongo_throw_exception(
1533-
PHONGO_ERROR_INVALID_ARGUMENT,
1534-
"Failed to parse URI options: %s may not be combined with %s, %s, %s, or %s.",
1535-
MONGOC_URI_TLSINSECURE,
1536-
MONGOC_URI_TLSALLOWINVALIDCERTIFICATES,
1537-
MONGOC_URI_TLSALLOWINVALIDHOSTNAMES,
1538-
MONGOC_URI_TLSDISABLEOCSPENDPOINTCHECK,
1539-
MONGOC_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK);
1540-
return false;
1541-
}
1542-
1543-
if (bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSALLOWINVALIDCERTIFICATES) &&
1544-
(bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSDISABLEOCSPENDPOINTCHECK) ||
1545-
bson_iter_init_find_case(&iter, options, MONGOC_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK))) {
1546-
phongo_throw_exception(
1547-
PHONGO_ERROR_INVALID_ARGUMENT,
1548-
"Failed to parse URI options: %s may not be combined with %s or %s.",
1549-
MONGOC_URI_TLSALLOWINVALIDCERTIFICATES,
1550-
MONGOC_URI_TLSDISABLEOCSPENDPOINTCHECK,
1551-
MONGOC_URI_TLSDISABLECERTIFICATEREVOCATIONCHECK);
1552-
return false;
1553-
}
1554-
1555-
return true;
1556-
} /* }}} */
1557-
15581438
static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options) /* {{{ */
15591439
{
15601440
bson_iter_t iter;
@@ -1755,17 +1635,6 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options)
17551635
}
17561636
}
17571637

1758-
/* Validate any interactions between URI options */
1759-
if (!php_phongo_uri_finalize_auth(uri)) {
1760-
/* Exception should already have been thrown */
1761-
return false;
1762-
}
1763-
1764-
if (!php_phongo_uri_finalize_directconnection(uri)) {
1765-
/* Exception should already have been thrown */
1766-
return false;
1767-
}
1768-
17691638
return true;
17701639
} /* }}} */
17711640

@@ -2566,7 +2435,9 @@ static void php_phongo_set_handshake_data(zval* driverOptions)
25662435

25672436
static mongoc_client_t* php_phongo_make_mongo_client(const mongoc_uri_t* uri, zval* driverOptions) /* {{{ */
25682437
{
2569-
const char *mongoc_version, *bson_version;
2438+
const char * mongoc_version, *bson_version;
2439+
mongoc_client_t* client;
2440+
bson_error_t error = { 0 };
25702441

25712442
#ifdef HAVE_SYSTEM_LIBMONGOC
25722443
mongoc_version = mongoc_get_version();
@@ -2592,7 +2463,11 @@ static mongoc_client_t* php_phongo_make_mongo_client(const mongoc_uri_t* uri, zv
25922463

25932464
php_phongo_set_handshake_data(driverOptions);
25942465

2595-
return mongoc_client_new_from_uri(uri);
2466+
if (!(client = mongoc_client_new_from_uri_with_error(uri, &error))) {
2467+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Failed to parse URI options: %s", error.message);
2468+
}
2469+
2470+
return client;
25962471
} /* }}} */
25972472

25982473
/* Adds a client to the appropriate registry. Persistent and request-scoped
@@ -3321,22 +3196,12 @@ void phongo_manager_init(php_phongo_manager_t* manager, const char* uri_string,
33213196
if (EG(exception)) {
33223197
goto cleanup;
33233198
}
3324-
3325-
if (!php_phongo_uri_finalize_tls(uri)) {
3326-
/* Exception should already have been thrown */
3327-
goto cleanup;
3328-
}
3329-
#else
3330-
if (mongoc_uri_get_tls(uri)) {
3331-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Cannot create SSL client. SSL is not enabled in this build.");
3332-
goto cleanup;
3333-
}
33343199
#endif
33353200

33363201
manager->client = php_phongo_make_mongo_client(uri, driverOptions);
33373202

33383203
if (!manager->client) {
3339-
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "Failed to create Manager from URI: '%s'", uri_string);
3204+
/* Exception should already have been thrown */
33403205
goto cleanup;
33413206
}
33423207

scripts/autotools/libbson/FindDependencies.m4

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
if test "$os_win32" != "yes"; then
2+
PHP_MONGODB_BUNDLED_CFLAGS="$PHP_MONGODB_BUNDLED_CFLAGS -D_DEFAULT_SOURCE"
3+
fi
4+
5+
# Enable macOS extensions for strlcpy and arc4random
6+
if test "$os_darwin" = "yes"; then
7+
PHP_MONGODB_BUNDLED_CFLAGS="$PHP_MONGODB_BUNDLED_CFLAGS -D_DARWIN_C_SOURCE=1"
8+
fi
9+
110
# Check for strnlen()
211
dnl AC_CHECK_FUNC isn't properly respecting _XOPEN_SOURCE for strnlen for unknown reason
312
AC_SUBST(BSON_HAVE_STRNLEN, 0)
@@ -79,6 +88,10 @@ AC_CHECK_FUNC(gmtime_r, [AC_SUBST(BSON_HAVE_GMTIME_R, 1)])
7988
AC_SUBST(BSON_HAVE_RAND_R, 0)
8089
AC_CHECK_FUNC(rand_r, [AC_SUBST(BSON_HAVE_RAND_R, 1)])
8190

91+
# Check for arc4random_buf()
92+
AC_SUBST(BSON_HAVE_ARC4RANDOM_BUF, 0)
93+
AC_CHECK_FUNC(arc4random_buf, [AC_SUBST(BSON_HAVE_ARC4RANDOM_BUF, 1)])
94+
8295
# Check for pthreads. We might need to make this better to handle mingw,
8396
# but I actually think it is okay to just check for it even though we will
8497
# use win32 primatives.

scripts/autotools/libmongoc/CheckCompression.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ PKG_CHECK_MODULES([PHP_MONGODB_ZLIB],[zlib],[
4545

4646
dnl If zlib was not found, use libmongoc's bundled version
4747
AS_IF([test "$found_zlib" != "yes"],[
48+
AC_CHECK_HEADER([unistd.h],[PHP_MONGODB_ZLIB_CFLAGS="$PHP_MONGODB_ZLIB_CFLAGS -DHAVE_UNISTD_H"])
49+
AC_CHECK_HEADER([stdarg.h],[PHP_MONGODB_ZLIB_CFLAGS="$PHP_MONGODB_ZLIB_CFLAGS -DHAVE_STDARG_H"])
4850
bundled_zlib="yes"
4951
])
5052

src/LIBMONGOC_VERSION_CURRENT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.20.0
1+
1.11.1-20220104+gite7e15002d6

src/libmongoc

Submodule libmongoc updated 133 files

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2121
Failed to parse MongoDB URI: 'mongodb://a.example.com,b.example.com/?directConnection=true'. Multiple seeds not allowed with directConnection option.
2222

2323
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24-
Failed to parse URI options: Multiple seeds not allowed with directConnection option.
24+
Failed to parse URI options: Multiple seeds not allowed with directConnection option
2525
===DONE===

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ OK: Got MongoDB\Driver\Exception\InvalidArgumentException
2121
Failed to parse MongoDB URI: 'mongodb+srv://a.example.com/?directConnection=true'. SRV URI not allowed with directConnection option.
2222

2323
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24-
Failed to parse URI options: SRV URI not allowed with directConnection option.
24+
Failed to parse URI options: SRV URI not allowed with directConnection option
2525
===DONE===

0 commit comments

Comments
 (0)