Skip to content

Commit 929e2ca

Browse files
committed
Merge pull request #1012
2 parents e65d114 + 2f94d95 commit 929e2ca

File tree

5 files changed

+112
-17
lines changed

5 files changed

+112
-17
lines changed

php_phongo.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,45 @@ static const char* php_phongo_bson_type_to_string(bson_type_t type) /* {{{ */
14611461
bson_iter_key(&(iter)), \
14621462
php_phongo_bson_type_to_string(bson_iter_type(&(iter))))
14631463

1464+
static bool php_phongo_uri_finalize_auth(mongoc_uri_t* uri TSRMLS_DC) /* {{{ */
1465+
{
1466+
/* authSource with GSSAPI or X509 should always be external */
1467+
if (mongoc_uri_get_auth_mechanism(uri)) {
1468+
if (!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "GSSAPI") ||
1469+
!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1470+
const char *source = mongoc_uri_get_auth_source(uri);
1471+
1472+
if (source) {
1473+
if (strcasecmp(source, "$external")) {
1474+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Failed to parse URI options: GSSAPI and X509 require \"$external\" authSource.");
1475+
return false;
1476+
}
1477+
} else {
1478+
mongoc_uri_set_auth_source(uri, "$external");
1479+
}
1480+
}
1481+
1482+
/* MONGODB-X509 is the only mechanism that doesn't require username */
1483+
if (strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1484+
if (!mongoc_uri_get_username(uri) ||
1485+
!strcmp(mongoc_uri_get_username(uri), "")) {
1486+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Failed to parse URI options: '%s' authentication mechanism requires username.", mongoc_uri_get_auth_mechanism(uri));
1487+
return false;
1488+
}
1489+
}
1490+
1491+
/* MONGODB-X509 errors if a password is supplied. */
1492+
if (!strcasecmp(mongoc_uri_get_auth_mechanism(uri), "MONGODB-X509")) {
1493+
if (mongoc_uri_get_password(uri)) {
1494+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Failed to parse URI options: X509 authentication mechanism does not accept a password.");
1495+
return false;
1496+
}
1497+
}
1498+
}
1499+
1500+
return true;
1501+
} /* }}} */
1502+
14641503
static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options TSRMLS_DC) /* {{{ */
14651504
{
14661505
bson_iter_t iter;
@@ -1657,6 +1696,12 @@ static bool php_phongo_apply_options_to_uri(mongoc_uri_t* uri, bson_t* options T
16571696
}
16581697
}
16591698

1699+
// Finalize auth options
1700+
if (!php_phongo_uri_finalize_auth(uri TSRMLS_CC)) {
1701+
/* Exception should already have been thrown */
1702+
return false;
1703+
}
1704+
16601705
return true;
16611706
} /* }}} */
16621707

tests/connect/bug1045.phpt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22
PHPC-1045: Segfault if username is not provided for SCRAM-SHA-1 authMechanism
33
--SKIPIF--
44
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5-
<?php skip_if_not_libmongoc_ssl(); ?>
6-
<?php skip_if_not_live(); ?>
7-
<?php skip_if_auth(); ?>
8-
<?php skip_if_not_clean(); ?>
5+
<?php skip_if_not_libmongoc_crypto(); ?>
96
--FILE--
107
<?php
118

129
require_once __DIR__ . "/../utils/basic.inc";
1310

14-
// URI may or may not support auth, but that is not necessary for the test
15-
$m = new MongoDB\Driver\Manager(URI, ['authMechanism' => 'SCRAM-SHA-1']);
16-
17-
// Execute a basic ping command to trigger connection initialization
18-
echo throws(function() use ($m) {
19-
$m->executeCommand('admin', new MongoDB\Driver\Command(['ping'=>1]));
20-
}, 'MongoDB\Driver\Exception\RuntimeException'), "\n";
11+
echo throws(function() {
12+
// URI may or may not support auth, but that is not necessary for the test
13+
new MongoDB\Driver\Manager('mongodb://127.0.0.1/', ['authMechanism' => 'SCRAM-SHA-1']);
14+
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
2115

2216
?>
2317
===DONE===
2418
<?php exit(0); ?>
2519
--EXPECTF--
26-
OK: Got MongoDB\Driver\Exception\RuntimeException
27-
SCRAM Failure: username is not set
20+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
21+
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires username.
2822
===DONE===

tests/manager/manager-ctor-auth_mechanism-001.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ MongoDB\Driver\Manager::__construct(): authMechanism option
55

66
$tests = [
77
['mongodb://[email protected]/?authMechanism=MONGODB-X509', []],
8+
['mongodb://127.0.0.1/?authMechanism=MONGODB-X509', []],
89
['mongodb://[email protected]/?authMechanism=GSSAPI', []],
10+
[null, ['authMechanism' => 'MONGODB-X509', 'username' => 'username']],
911
[null, ['authMechanism' => 'MONGODB-X509']],
10-
[null, ['authMechanism' => 'GSSAPI']],
12+
[null, ['authMechanism' => 'GSSAPI', 'username' => 'username']],
1113
];
1214

1315
foreach ($tests as $test) {

tests/manager/manager-ctor-auth_mechanism-002.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ MongoDB\Driver\Manager::__construct(): authMechanismProperties option
55

66
$tests = [
77
['mongodb://[email protected]/?authMechanism=GSSAPI&authMechanismProperties=CANONICALIZE_HOST_NAME:true,SERVICE_NAME:foo,SERVICE_REALM:bar', []],
8-
[null, ['authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['CANONICALIZE_HOST_NAME' => 'true', 'SERVICE_NAME' => 'foo', 'SERVICE_REALM' => 'bar']]],
8+
[null, ['username' => 'username', 'authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['CANONICALIZE_HOST_NAME' => 'true', 'SERVICE_NAME' => 'foo', 'SERVICE_REALM' => 'bar']]],
99
// Options are case-insensitive
1010
['mongodb://[email protected]/?authMechanism=GSSAPI&authMechanismProperties=canonicalize_host_name:TRUE,service_name:foo,service_realm:bar', []],
11-
[null, ['authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => 'TRUE', 'service_name' => 'foo', 'service_realm' => 'bar']]],
11+
[null, ['username' => 'username', 'authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => 'TRUE', 'service_name' => 'foo', 'service_realm' => 'bar']]],
1212
// Boolean true "CANONICALIZE_HOST_NAME" value is converted to "true"
13-
[null, ['authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => true]]],
13+
[null, ['username' => 'username', 'authMechanism' => 'GSSAPI', 'authMechanismProperties' => ['canonicalize_host_name' => true]]],
1414
];
1515

1616
foreach ($tests as $test) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::__construct(): authentication options are validated
3+
--FILE--
4+
<?php
5+
6+
require_once __DIR__ . '/../utils/tools.php';
7+
8+
echo throws(function() {
9+
new MongoDB\Driver\Manager('mongodb://localhost:27017/?authMechanism=GSSAPI&authSource=admin');
10+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
11+
12+
echo throws(function() {
13+
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['authMechanism' => 'GSSAPI', 'authSource' => 'admin']);
14+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
15+
16+
echo throws(function() {
17+
new MongoDB\Driver\Manager('mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin');
18+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
19+
20+
echo throws(function() {
21+
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['authMechanism' => 'MONGODB-X509', 'authSource' => 'admin']);
22+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
23+
24+
echo throws(function() {
25+
new MongoDB\Driver\Manager('mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1');
26+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
27+
28+
echo throws(function() {
29+
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['username' => '', 'authMechanism' => 'SCRAM-SHA-1']);
30+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
31+
32+
echo throws(function() {
33+
new MongoDB\Driver\Manager('mongodb://localhost:27017/', ['password' => 'password', 'authMechanism' => 'MONGODB-X509']);
34+
}, "MongoDB\Driver\Exception\InvalidArgumentException"), "\n";
35+
36+
?>
37+
===DONE===
38+
<?php exit(0); ?>
39+
--EXPECT--
40+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
41+
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=GSSAPI&authSource=admin'. GSSAPI and X509 require "$external" authSource.
42+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
43+
Failed to parse URI options: GSSAPI and X509 require "$external" authSource.
44+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
45+
Failed to parse MongoDB URI: 'mongodb://localhost:27017/?authMechanism=MONGODB-X509&authSource=admin'. GSSAPI and X509 require "$external" authSource.
46+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
47+
Failed to parse URI options: GSSAPI and X509 require "$external" authSource.
48+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
49+
Failed to parse MongoDB URI: 'mongodb://@localhost:27017/?authMechanism=SCRAM-SHA-1'. 'SCRAM-SHA-1' authentication mechanism requires username.
50+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
51+
Failed to parse URI options: 'SCRAM-SHA-1' authentication mechanism requires username.
52+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
53+
Failed to parse URI options: X509 authentication mechanism does not accept a password.
54+
===DONE===

0 commit comments

Comments
 (0)