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
3 changes: 2 additions & 1 deletion sapi/cgi/cgi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,8 @@ static void init_request_info(fcgi_request *request)
/* FIXME - Work out proto_num here */
SG(request_info).query_string = CGI_GETENV("QUERY_STRING");
SG(request_info).content_type = (content_type ? content_type : "" );
SG(request_info).content_length = (content_length ? atol(content_length) : 0);
zend_long parsed_content_length = (content_length ? ZEND_ATOL(content_length) : 0);
SG(request_info).content_length = (parsed_content_length > 0 ? parsed_content_length : 0);

/* The CGI RFC allows servers to pass on unvalidated Authorization data */
auth = CGI_GETENV("HTTP_AUTHORIZATION");
Expand Down
48 changes: 48 additions & 0 deletions sapi/cgi/tests/negative_content_length.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
php-cgi clamps negative CONTENT_LENGTH to 0 without aborting or underflowing read length
--SKIPIF--
<?php include "skipif.inc"; ?>
--FILE--
<?php
include "include.inc";

$cgi = get_cgi_path();
reset_env_vars();

$tmp = __DIR__ . '/negative_content_length_' . getmypid() . '.php';
file_put_contents($tmp, '<?php echo "OK:" . strlen(file_get_contents("php://input")) . ":" . json_encode($_POST) . "\n"; ?>');

$env = [
'REDIRECT_STATUS' => '200',
'REQUEST_METHOD' => 'POST',
'SCRIPT_FILENAME' => $tmp,
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'CONTENT_LENGTH' => '-1',
'GATEWAY_INTERFACE' => 'CGI/1.1',
];

$spec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w'],
];

$proc = proc_open([$cgi, '-n'], $spec, $pipes, null, $env);
fwrite($pipes[0], 'a=1');
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$code = proc_close($proc);

@unlink($tmp);

echo "EXIT=" . $code . "\n";
echo "BODY=" . (preg_match('/OK:\d+:[^\r\n]+/', $stdout, $m) ? $m[0] : 'MISSING') . "\n";
echo "STDERR_EMPTY=" . ($stderr === '' ? 'YES' : 'NO:' . trim($stderr)) . "\n";
?>
--EXPECT--
EXIT=0
BODY=OK:0:[]
STDERR_EMPTY=YES
3 changes: 2 additions & 1 deletion sapi/fpm/fpm/fpm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,8 @@ static void init_request_info(void)
/* FIXME - Work out proto_num here */
SG(request_info).query_string = FCGI_GETENV(request, "QUERY_STRING");
SG(request_info).content_type = (content_type ? content_type : "" );
SG(request_info).content_length = (content_length ? atol(content_length) : 0);
zend_long parsed_content_length = (content_length ? ZEND_ATOL(content_length) : 0);
SG(request_info).content_length = (parsed_content_length > 0 ? parsed_content_length : 0);

/* The CGI RFC allows servers to pass on unvalidated Authorization data */
auth = FCGI_GETENV(request, "HTTP_AUTHORIZATION");
Expand Down
Loading