Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions ext/openssl/tests/https_notification_fclose_stream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--TEST--
Stream notification callback cannot close the TLS stream it is dispatched from
--EXTENSIONS--
openssl
--SKIPIF--
<?php
if (!function_exists("proc_open")) die("skip no proc_open");
?>
--INI--
allow_url_fopen=1
--FILE--
<?php
$certFile = __DIR__ . DIRECTORY_SEPARATOR . 'https_notification_fclose_stream.pem.tmp';

$serverCode = <<<'CODE'
$serverCtx = stream_context_create(['ssl' => ['local_cert' => '%s']]);
$server = stream_socket_server("tls://127.0.0.1:0", $errno, $errstr,
STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $serverCtx);
phpt_notify_server_start($server);

/* Send the body only after the client write. */
$conn = stream_socket_accept($server);
while (($line = fgets($conn)) !== false && $line !== "\r\n");
fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\n");
fread($conn, 2);
fwrite($conn, "body");
fclose($conn);
CODE;
$serverCode = sprintf($serverCode, $certFile);

$clientCode = <<<'CODE'
$stream = null;
$clientCtx = stream_context_create(
['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]],
['notification' => function (int $code) use (&$stream) {
if (!is_resource($stream)) {
return;
}
if ($code === STREAM_NOTIFY_PROGRESS || $code === STREAM_NOTIFY_COMPLETED) {
echo $code === STREAM_NOTIFY_PROGRESS ? "progress\n" : "completed\n";
var_dump(fclose($stream));
}
}]
);

$stream = fopen("https://{{ ADDR }}/", 'r', false, $clientCtx);
var_dump(fwrite($stream, "go"));
var_dump(fread($stream, 8192));
var_dump(fread($stream, 8192));
var_dump(fclose($stream));
CODE;

include 'CertificateGenerator.inc';
(new CertificateGenerator())->saveNewCertAsFileWithKey('https_notification_fclose_stream', $certFile);

include 'ServerClientTestCase.inc';
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--CLEAN--
<?php
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'https_notification_fclose_stream.pem.tmp');
?>
--EXPECTF--
progress

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
int(2)
progress

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
string(4) "body"
completed

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
string(0) ""
bool(true)
2 changes: 1 addition & 1 deletion ext/openssl/xp_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ static ssize_t php_openssl_sockop_io(int read, php_stream *stream, char *buf, si

/* Tell PHP if we read / wrote bytes. */
if (nr_bytes > 0) {
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0);
php_stream_notify_progress_increment_no_fclose(stream, nr_bytes);
}

/* And if we were originally supposed to be blocking, let's reset the socket to that. */
Expand Down
62 changes: 62 additions & 0 deletions ext/standard/tests/http/notification_fclose_stream.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
Stream notification callback cannot close the stream it is dispatched from
--SKIPIF--
<?php
require 'server.inc';
http_server_skipif();
?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$server = http_server_init();
if (is_resource($server)) {
/* Send the body only after the client write. */
$conn = stream_socket_accept($server);
while (($line = fgets($conn)) !== false && $line !== "\r\n");
fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nConnection: close\r\n\r\n");
fread($conn, 2);
fwrite($conn, "body");
fclose($conn);
exit(0);
}
['pid' => $pid, 'uri' => $uri] = $server;

$stream = null;
$context = stream_context_create([], ['notification' => function (int $code) use (&$stream) {
if (!is_resource($stream)) {
return;
}
if ($code === STREAM_NOTIFY_PROGRESS || $code === STREAM_NOTIFY_COMPLETED) {
echo $code === STREAM_NOTIFY_PROGRESS ? "progress\n" : "completed\n";
var_dump(fclose($stream));
}
}]);

$stream = fopen($uri, 'r', false, $context);
var_dump(fwrite($stream, "go"));
var_dump(fread($stream, 8192));
var_dump(fread($stream, 8192));
var_dump(fclose($stream));

http_server_kill($pid);
?>
--EXPECTF--
progress

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
int(2)
progress

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
string(4) "body"
completed

Warning: fclose(): %d is not a valid stream resource in %s on line %d
bool(false)
string(0) ""
bool(true)
28 changes: 28 additions & 0 deletions main/streams/php_streams_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,31 @@ void php_stream_mode_sanitize_fdopen_fopencookie(php_stream *stream, char *resul
/* Escapes NUL and backslash bytes so a host containing them cannot truncate or
* collide the persistent stream hash key. */
zend_string *php_stream_escape_persistent_key(const char *host, size_t hostlen);

/* Make sure the stream is not closed while the notifier callback executes. */
static zend_always_inline void php_stream_notify_progress_increment_no_fclose(
php_stream *stream, size_t dsofar)
{
php_stream_context *context = PHP_STREAM_CONTEXT(stream);

if (context && context->notifier) {
uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
php_stream_notify_progress_increment(context, dsofar, 0);
stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= orig_no_fclose;
}
}

static zend_always_inline void php_stream_notify_completed_no_fclose(php_stream *stream)
{
php_stream_context *context = PHP_STREAM_CONTEXT(stream);

if (context && context->notifier) {
uint32_t orig_no_fclose = stream->flags & PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
php_stream_notify_completed(context);
stream->flags &= ~PHP_STREAM_FLAG_NO_FCLOSE;
stream->flags |= orig_no_fclose;
}
}
4 changes: 2 additions & 2 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
out_check_eof:
if (old_eof != stream->eof) {
out_is_eof:
php_stream_notify_completed(PHP_STREAM_CONTEXT(stream));
php_stream_notify_completed_no_fclose(stream);
}
return retval;
}
Expand Down Expand Up @@ -1259,7 +1259,7 @@ static ssize_t _php_stream_write_buffer(php_stream *stream, const char *buf, siz

out:
if (old_eof != stream->eof) {
php_stream_notify_completed(PHP_STREAM_CONTEXT(stream));
php_stream_notify_completed_no_fclose(stream);
}
return retval;
}
Expand Down
4 changes: 2 additions & 2 deletions main/streams/xp_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static ssize_t php_sockop_write(php_stream *stream, const char *buf, size_t coun
}

if (didwrite > 0) {
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), didwrite, 0);
php_stream_notify_progress_increment_no_fclose(stream, didwrite);
}

return didwrite;
Expand Down Expand Up @@ -202,7 +202,7 @@ static ssize_t php_sockop_read(php_stream *stream, char *buf, size_t count)
}

if (nr_bytes > 0) {
php_stream_notify_progress_increment(PHP_STREAM_CONTEXT(stream), nr_bytes, 0);
php_stream_notify_progress_increment_no_fclose(stream, nr_bytes);
}

return nr_bytes;
Expand Down
Loading