Skip to content

Commit 72da418

Browse files
committed
Fix GH-9316: $http_response_header is wrong for long status line
While the reason-phrase in a HTTP response status line is usually short, there is no actual limit specified by the RFCs. As such, we must not assume that the line fits into the buffer (which is currently 128 bytes large). Since there is no real need to present the complete status line, we simply read and discard the rest of a long line. Co-authored-by: Tim Düsterhus <[email protected]> Closes GH-9319.
1 parent 84dcf57 commit 72da418

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2022, PHP 8.0.24
44

5+
- Streams:
6+
. Fixed bug GH-9316 ($http_response_header is wrong for long status line).
7+
(cmb, timwolla)
58

69
01 Sep 2022, PHP 8.0.23
710

ext/standard/http_fopen_wrapper.c

+4
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
717717
if (tmp_line_len >= 1 &&tmp_line[tmp_line_len - 1] == '\r') {
718718
--tmp_line_len;
719719
}
720+
} else {
721+
// read and discard rest of status line
722+
char *line = php_stream_get_line(stream, NULL, 0, NULL);
723+
efree(line);
720724
}
721725
ZVAL_STRINGL(&http_response, tmp_line, tmp_line_len);
722726
zend_hash_next_index_insert(Z_ARRVAL_P(response_header), &http_response);

ext/standard/tests/http/gh9316.phpt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Bug GH-9316 ($http_response_header is wrong for long status line)
3+
--SKIPIF--
4+
<?php require 'server.inc'; http_server_skipif(); ?>
5+
--INI--
6+
allow_url_fopen=1
7+
--FILE--
8+
<?php
9+
require 'server.inc';
10+
11+
$responses = array(
12+
"data://text/plain,HTTP/1.1 200 Some very long reason-phrase to test that this is properly handled by our code without adding a new header like Bad: Header\r\nGood: Header\r\n\r\nBody",
13+
"data://text/plain,HTTP/1.1 200 \r\nGood: Header\r\n\r\nBody",
14+
);
15+
16+
['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);
17+
18+
for ($i = 0; $i < count($responses); ++$i) {
19+
$f = @fopen($uri, "r");
20+
var_dump($http_response_header);
21+
fclose($f);
22+
}
23+
24+
http_server_kill($pid);
25+
26+
--EXPECT--
27+
array(2) {
28+
[0]=>
29+
string(126) "HTTP/1.1 200 Some very long reason-phrase to test that this is properly handled by our code without adding a new header like "
30+
[1]=>
31+
string(12) "Good: Header"
32+
}
33+
array(2) {
34+
[0]=>
35+
string(13) "HTTP/1.1 200 "
36+
[1]=>
37+
string(12) "Good: Header"
38+
}

0 commit comments

Comments
 (0)