Skip to content

Commit f59e0f8

Browse files
committed
Merge branch 'v1.1'
2 parents df07ec5 + 9599e28 commit f59e0f8

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

php_phongo.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11801180
struct timeval timeout = {0, 0};
11811181
struct timeval *timeoutp = NULL;
11821182
char *uniqid;
1183+
const char *persistent_id;
11831184
phongo_char *errmsg = NULL;
11841185
int errcode;
11851186
char *dsn;
@@ -1225,9 +1226,13 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
12251226

12261227
spprintf(&uniqid, 0, "%s:%d[%s]", host->host, host->port, mongoc_uri_get_string(uri));
12271228

1229+
/* Do not persist SSL streams to avoid errors attempting to reinitialize SSL
1230+
* on subsequent requests (see: PHPC-720) */
1231+
persistent_id = mongoc_uri_get_ssl(uri) ? NULL : uniqid;
1232+
12281233
MONGOC_DEBUG("Connecting to '%s'", uniqid);
12291234
zend_replace_error_handling(EH_SUPPRESS, NULL, &error_handling TSRMLS_CC);
1230-
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, uniqid, timeoutp, (php_stream_context *)user_data, &errmsg, &errcode);
1235+
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, persistent_id, timeoutp, (php_stream_context *)user_data, &errmsg, &errcode);
12311236
zend_restore_error_handling(&error_handling TSRMLS_CC);
12321237

12331238
if (!stream) {
@@ -1247,7 +1252,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
12471252
if (mongoc_uri_get_ssl(uri)) {
12481253
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
12491254

1250-
MONGOC_DEBUG("Enabling SSL");
1255+
MONGOC_DEBUG("Enabling SSL (stream will not be persisted)");
12511256

12521257
/* Capture the server certificate so we can do further verification */
12531258
if (PHP_STREAM_CONTEXT(stream)) {
@@ -1261,22 +1266,22 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
12611266
php_stream_free(stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
12621267
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_INVALID_TYPE, "Failed to setup crypto, is the OpenSSL extension loaded?");
12631268
efree(dsn);
1264-
return NULL;
1269+
RETURN(NULL);
12651270
}
12661271

12671272
if (php_stream_xport_crypto_enable(stream, 1 TSRMLS_CC) < 0) {
12681273
zend_restore_error_handling(&error_handling TSRMLS_CC);
12691274
php_stream_free(stream, PHP_STREAM_FREE_CLOSE_PERSISTENT | PHP_STREAM_FREE_RSRC_DTOR);
12701275
bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_INVALID_TYPE, "Failed to setup crypto, is the server running with SSL?");
12711276
efree(dsn);
1272-
return NULL;
1277+
RETURN(NULL);
12731278
}
12741279

12751280
if (!php_phongo_ssl_verify(stream, host->host, error TSRMLS_CC)) {
12761281
zend_restore_error_handling(&error_handling TSRMLS_CC);
12771282
php_stream_pclose(stream);
12781283
efree(dsn);
1279-
return NULL;
1284+
RETURN(NULL);
12801285
}
12811286

12821287
zend_restore_error_handling(&error_handling TSRMLS_CC);

tests/connect/bug0720.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
PHPC-720: Do not persist SSL streams to avoid SSL reinitialization errors
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_SSL"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
'peer_name' => 'server',
13+
'verify_peer' => true,
14+
'verify_peer_name' => true,
15+
'allow_self_signed' => false,
16+
'cafile' => $SSL_DIR . '/ca.pem', /* Defaults to openssl.cafile */
17+
];
18+
19+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
20+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
21+
var_dump($cursor->toArray()[0]);
22+
23+
unset($manager, $cursor);
24+
25+
$manager = new MongoDB\Driver\Manager(STANDALONE_SSL, ['ssl' => true], $driverOptions);
26+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
27+
var_dump($cursor->toArray()[0]);
28+
29+
?>
30+
===DONE===
31+
<?php exit(0); ?>
32+
--EXPECTF--
33+
object(stdClass)#%d (%d) {
34+
["ok"]=>
35+
float(1)
36+
}
37+
object(stdClass)#%d (%d) {
38+
["ok"]=>
39+
float(1)
40+
}
41+
===DONE===

0 commit comments

Comments
 (0)