Skip to content

Commit b67907e

Browse files
committed
PHPC-720: Do not persist SSL streams to avoid SSL reinitialization errors
1 parent f7d1b4b commit b67907e

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

php_phongo.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11331133
struct timeval timeout = {0, 0};
11341134
struct timeval *timeoutp = NULL;
11351135
char *uniqid;
1136+
const char *persistent_id;
11361137
phongo_char *errmsg = NULL;
11371138
int errcode;
11381139
char *dsn;
@@ -1178,9 +1179,13 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
11781179

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

1182+
/* Do not persist SSL streams to avoid errors attempting to reinitialize SSL
1183+
* on subsequent requests (see: PHPC-720) */
1184+
persistent_id = mongoc_uri_get_ssl(uri) ? NULL : uniqid;
1185+
11811186
MONGOC_DEBUG("Connecting to '%s'", uniqid);
11821187
zend_replace_error_handling(EH_SUPPRESS, NULL, &error_handling TSRMLS_CC);
1183-
stream = php_stream_xport_create(dsn, dsn_len, 0, STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, uniqid, timeoutp, (php_stream_context *)user_data, &errmsg, &errcode);
1188+
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);
11841189
zend_restore_error_handling(&error_handling TSRMLS_CC);
11851190

11861191
if (!stream) {
@@ -1200,7 +1205,7 @@ mongoc_stream_t* phongo_stream_initiator(const mongoc_uri_t *uri, const mongoc_h
12001205
if (mongoc_uri_get_ssl(uri)) {
12011206
zend_replace_error_handling(EH_THROW, php_phongo_sslconnectionexception_ce, &error_handling TSRMLS_CC);
12021207

1203-
MONGOC_DEBUG("Enabling SSL");
1208+
MONGOC_DEBUG("Enabling SSL (stream will not be persisted)");
12041209

12051210
/* Capture the server certificate so we can do further verification */
12061211
if (PHP_STREAM_CONTEXT(stream)) {

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)