Skip to content

Commit 61bb8ef

Browse files
committed
Fix GHSA-pcmh-g36c-qc44: http headers without colon
The header line must contain colon otherwise it is invalid and it needs to fail. Reviewed-by: Tim Düsterhus <[email protected]>
1 parent 9fe4966 commit 61bb8ef

File tree

5 files changed

+154
-25
lines changed

5 files changed

+154
-25
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ static zend_result php_stream_handle_proxy_authorization_header(const char *s, s
146146
typedef struct _php_stream_http_response_header_info {
147147
php_stream_filter *transfer_encoding;
148148
size_t file_size;
149+
bool error;
149150
bool follow_location;
150151
char location[HTTP_HEADER_BLOCK_SIZE];
151152
} php_stream_http_response_header_info;
@@ -155,6 +156,7 @@ static void php_stream_http_response_header_info_init(
155156
{
156157
header_info->transfer_encoding = NULL;
157158
header_info->file_size = 0;
159+
header_info->error = false;
158160
header_info->follow_location = 1;
159161
header_info->location[0] = '\0';
160162
}
@@ -192,10 +194,11 @@ static bool php_stream_http_response_header_trim(char *http_header_line,
192194
/* Process folding headers of the current line and if there are none, parse last full response
193195
* header line. It returns NULL if the last header is finished, otherwise it returns updated
194196
* last header line. */
195-
static zend_string *php_stream_http_response_headers_parse(php_stream *stream,
196-
php_stream_context *context, int options, zend_string *last_header_line_str,
197-
char *header_line, size_t *header_line_length, int response_code,
198-
zval *response_header, php_stream_http_response_header_info *header_info)
197+
static zend_string *php_stream_http_response_headers_parse(php_stream_wrapper *wrapper,
198+
php_stream *stream, php_stream_context *context, int options,
199+
zend_string *last_header_line_str, char *header_line, size_t *header_line_length,
200+
int response_code, zval *response_header,
201+
php_stream_http_response_header_info *header_info)
199202
{
200203
char *last_header_line = ZSTR_VAL(last_header_line_str);
201204
size_t last_header_line_length = ZSTR_LEN(last_header_line_str);
@@ -237,6 +240,19 @@ static zend_string *php_stream_http_response_headers_parse(php_stream *stream,
237240
/* Find header separator position. */
238241
char *last_header_value = memchr(last_header_line, ':', last_header_line_length);
239242
if (last_header_value) {
243+
/* Verify there is no space in header name */
244+
char *last_header_name = last_header_line + 1;
245+
while (last_header_name < last_header_value) {
246+
if (*last_header_name == ' ' || *last_header_name == '\t') {
247+
header_info->error = true;
248+
php_stream_wrapper_log_error(wrapper, options,
249+
"HTTP invalid response format (space in header name)!");
250+
zend_string_efree(last_header_line_str);
251+
return NULL;
252+
}
253+
++last_header_name;
254+
}
255+
240256
last_header_value++; /* Skip ':'. */
241257

242258
/* Strip leading whitespace. */
@@ -245,9 +261,12 @@ static zend_string *php_stream_http_response_headers_parse(php_stream *stream,
245261
last_header_value++;
246262
}
247263
} else {
248-
/* There is no colon. Set the value to the end of the header line, which is effectively
249-
* an empty string. */
250-
last_header_value = last_header_line_end;
264+
/* There is no colon which means invalid response so error. */
265+
header_info->error = true;
266+
php_stream_wrapper_log_error(wrapper, options,
267+
"HTTP invalid response format (no colon in header line)!");
268+
zend_string_efree(last_header_line_str);
269+
return NULL;
251270
}
252271

253272
bool store_header = true;
@@ -944,10 +963,16 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
944963

945964
if (last_header_line_str != NULL) {
946965
/* Parse last header line. */
947-
last_header_line_str = php_stream_http_response_headers_parse(stream, context,
948-
options, last_header_line_str, http_header_line, &http_header_line_length,
949-
response_code, response_header, &header_info);
950-
if (last_header_line_str != NULL) {
966+
last_header_line_str = php_stream_http_response_headers_parse(wrapper, stream,
967+
context, options, last_header_line_str, http_header_line,
968+
&http_header_line_length, response_code, response_header, &header_info);
969+
if (EXPECTED(last_header_line_str == NULL)) {
970+
if (UNEXPECTED(header_info.error)) {
971+
php_stream_close(stream);
972+
stream = NULL;
973+
goto out;
974+
}
975+
} else {
951976
/* Folding header present so continue. */
952977
continue;
953978
}
@@ -977,8 +1002,8 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
9771002

9781003
/* If the stream was closed early, we still want to process the last line to keep BC. */
9791004
if (last_header_line_str != NULL) {
980-
php_stream_http_response_headers_parse(stream, context, options, last_header_line_str,
981-
NULL, NULL, response_code, response_header, &header_info);
1005+
php_stream_http_response_headers_parse(wrapper, stream, context, options,
1006+
last_header_line_str, NULL, NULL, response_code, response_header, &header_info);
9821007
}
9831008

9841009
if (!reqok || (header_info.location[0] != '\0' && header_info.follow_location)) {

ext/standard/tests/http/bug47021.phpt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,27 @@ do_test(1, true);
7070
echo "\n";
7171

7272
?>
73-
--EXPECT--
73+
--EXPECTF--
74+
7475
Type='text/plain'
7576
Hello
76-
Size=5
77-
World
77+
78+
Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP invalid response format (no colon in header line)! in %s
79+
7880

7981
Type='text/plain'
8082
Hello
81-
Size=5
82-
World
83+
84+
Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP invalid response format (no colon in header line)! in %s
85+
8386

8487
Type='text/plain'
8588
Hello
86-
Size=5
87-
World
89+
90+
Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP invalid response format (no colon in header line)! in %s
91+
8892

8993
Type='text/plain'
9094
Hello
91-
Size=5
92-
World
95+
96+
Warning: file_get_contents(http://%s:%d): Failed to open stream: HTTP invalid response format (no colon in header line)! in %s

ext/standard/tests/http/bug75535.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ http_server_kill($pid);
2626
--EXPECT--
2727
NULL
2828
string(0) ""
29-
array(2) {
29+
array(1) {
3030
[0]=>
3131
string(15) "HTTP/1.0 200 Ok"
32-
[1]=>
33-
string(14) "Content-Length"
3432
}
3533
array(2) {
3634
[0]=>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
GHSA-pcmh-g36c-qc44: Header parser of http stream wrapper does not verify header name and colon (colon)
3+
--FILE--
4+
<?php
5+
$serverCode = <<<'CODE'
6+
$ctxt = stream_context_create([
7+
"socket" => [
8+
"tcp_nodelay" => true
9+
]
10+
]);
11+
12+
$server = stream_socket_server(
13+
"tcp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctxt);
14+
phpt_notify_server_start($server);
15+
16+
$conn = stream_socket_accept($server);
17+
18+
phpt_notify(message:"server-accepted");
19+
20+
fwrite($conn, "HTTP/1.0 200 Ok\r\nContent-Type: text/html\r\nWrong-Header\r\nGood-Header: test\r\n\r\nbody\r\n");
21+
CODE;
22+
23+
$clientCode = <<<'CODE'
24+
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
25+
switch($notification_code) {
26+
case STREAM_NOTIFY_MIME_TYPE_IS:
27+
echo "Found the mime-type: ", $message, PHP_EOL;
28+
break;
29+
}
30+
}
31+
32+
$ctx = stream_context_create();
33+
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
34+
var_dump(file_get_contents("http://{{ ADDR }}", false, $ctx));
35+
var_dump($http_response_header);
36+
CODE;
37+
38+
include sprintf("%s/../../../openssl/tests/ServerClientTestCase.inc", __DIR__);
39+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
40+
?>
41+
--EXPECTF--
42+
Found the mime-type: text/html
43+
44+
Warning: file_get_contents(http://127.0.0.1:%d): Failed to open stream: HTTP invalid response format (no colon in header line)! in %s
45+
bool(false)
46+
array(2) {
47+
[0]=>
48+
string(15) "HTTP/1.0 200 Ok"
49+
[1]=>
50+
string(23) "Content-Type: text/html"
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
GHSA-pcmh-g36c-qc44: Header parser of http stream wrapper does not verify header name and colon (name)
3+
--FILE--
4+
<?php
5+
$serverCode = <<<'CODE'
6+
$ctxt = stream_context_create([
7+
"socket" => [
8+
"tcp_nodelay" => true
9+
]
10+
]);
11+
12+
$server = stream_socket_server(
13+
"tcp://127.0.0.1:0", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $ctxt);
14+
phpt_notify_server_start($server);
15+
16+
$conn = stream_socket_accept($server);
17+
18+
phpt_notify(message:"server-accepted");
19+
20+
fwrite($conn, "HTTP/1.0 200 Ok\r\nContent-Type: text/html\r\nWrong-Header : test\r\nGood-Header: test\r\n\r\nbody\r\n");
21+
CODE;
22+
23+
$clientCode = <<<'CODE'
24+
function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
25+
switch($notification_code) {
26+
case STREAM_NOTIFY_MIME_TYPE_IS:
27+
echo "Found the mime-type: ", $message, PHP_EOL;
28+
break;
29+
}
30+
}
31+
32+
$ctx = stream_context_create();
33+
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));
34+
var_dump(file_get_contents("http://{{ ADDR }}", false, $ctx));
35+
var_dump($http_response_header);
36+
CODE;
37+
38+
include sprintf("%s/../../../openssl/tests/ServerClientTestCase.inc", __DIR__);
39+
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
40+
?>
41+
--EXPECTF--
42+
Found the mime-type: text/html
43+
44+
Warning: file_get_contents(http://127.0.0.1:%d): Failed to open stream: HTTP invalid response format (space in header name)! in %s
45+
bool(false)
46+
array(2) {
47+
[0]=>
48+
string(15) "HTTP/1.0 200 Ok"
49+
[1]=>
50+
string(23) "Content-Type: text/html"
51+
}

0 commit comments

Comments
 (0)