diff --git a/sapi/cgi/cgi_main.c b/sapi/cgi/cgi_main.c index d617b1d0c038..69e98990417f 100644 --- a/sapi/cgi/cgi_main.c +++ b/sapi/cgi/cgi_main.c @@ -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"); diff --git a/sapi/cgi/tests/negative_content_length.phpt b/sapi/cgi/tests/negative_content_length.phpt new file mode 100644 index 000000000000..1673e2c301cd --- /dev/null +++ b/sapi/cgi/tests/negative_content_length.phpt @@ -0,0 +1,48 @@ +--TEST-- +php-cgi clamps negative CONTENT_LENGTH to 0 without aborting or underflowing read length +--SKIPIF-- + +--FILE-- +'); + +$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 diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index 56796c327f91..17f594b1282a 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -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");