Skip to content
Closed
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
3 changes: 2 additions & 1 deletion ext/soap/php_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,8 @@ static zend_string* get_http_body(php_stream *stream, int close, char *headers)
while (http_buf_size < header_length) {
ssize_t len_read = php_stream_read(stream, http_buf->val + http_buf_size, header_length - http_buf_size);
if (len_read <= 0) {
break;
zend_string_efree(http_buf);
return NULL;
}
http_buf_size += len_read;
}
Expand Down
88 changes: 88 additions & 0 deletions ext/soap/tests/http_content_length_truncated.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
--TEST--
SOAP client rejects a truncated Content-Length response
--EXTENSIONS--
soap
--SKIPIF--
<?php
if (!function_exists('proc_open')) {
die('skip proc_open() is not available');
}
?>
--FILE--
<?php
$serverCode = <<<'PHP'
$server = stream_socket_server('tcp://127.0.0.1:0', $errno, $errstr);
if (!$server) {
fwrite(STDERR, "could not start server: $errstr\n");
exit(1);
}
echo stream_socket_get_name($server, false), "\n";

$connection = stream_socket_accept($server, 10);
if (!$connection) {
exit(1);
}

$request = '';
while (!str_contains($request, "\r\n\r\n")) {
$chunk = fread($connection, 1);
if ($chunk === '') {
exit(1);
}
$request .= $chunk;
}
preg_match('/Content-Length:\s*(\d+)/i', $request, $matches);
$remaining = (int) $matches[1];
while ($remaining > 0) {
$chunk = fread($connection, $remaining);
if ($chunk === '') {
exit(1);
}
$remaining -= strlen($chunk);
}

$body = '<?xml version="1.0"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><testResponse xmlns="urn:test"/></SOAP-ENV:Body></SOAP-ENV:Envelope>';
fwrite($connection, "HTTP/1.1 200 OK\r\n"
. "Content-Type: text/xml; charset=utf-8\r\n"
. 'Content-Length: ' . (strlen($body) + 100) . "\r\n"
. "Connection: keep-alive\r\n"
. "\r\n"
. $body);
fclose($connection);
fclose($server);
PHP;

$process = proc_open([PHP_BINARY, '-n', '-r', $serverCode], [
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
], $pipes);
if (!is_resource($process)) {
die('could not start server process');
}

$address = fgets($pipes[1]);
if ($address === false) {
die(stream_get_contents($pipes[2]));
}

try {
$client = new SoapClient(null, [
'location' => 'http://' . trim($address),
'uri' => 'urn:test',
'keep_alive' => true,
]);

try {
$client->test();
echo "unexpected success\n";
} catch (SoapFault $e) {
echo $e->faultstring, "\n";
}
} finally {
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
?>
--EXPECT--
Error Fetching http body, No Content-Length, connection closed or chunked data
Loading