Skip to content

Commit 59bfc16

Browse files
bukkaremicollet
authored andcommitted
Fix GHSA-c5f2-jwm7-mmq2: stream HTTP fulluri CRLF injection
(cherry picked from commit 426a6d4539ebee34879ac5de857036bb6ff0e732) (cherry picked from commit bc1f192) (cherry picked from commit 8d130e1) (cherry picked from commit 494de65) (cherry picked from commit dcb89ed) (cherry picked from commit 1178705)
1 parent 351dee5 commit 59bfc16

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

ext/standard/http_fopen_wrapper.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
180180
return NULL;
181181
}
182182

183+
/* Should we send the entire path in the request line, default to no. */
184+
if (context && (tmpzval = php_stream_context_get_option(context, "http", "request_fulluri")) != NULL) {
185+
request_fulluri = zend_is_true(tmpzval);
186+
}
187+
183188
use_ssl = resource->scheme && (strlen(resource->scheme) > 4) && resource->scheme[4] == 's';
184189
/* choose default ports */
185190
if (use_ssl && resource->port == 0)
@@ -199,6 +204,13 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
199204
}
200205
}
201206

207+
if (request_fulluri && (strchr(path, '\n') != NULL || strchr(path, '\r') != NULL)) {
208+
php_stream_wrapper_log_error(wrapper, options, "HTTP wrapper full URI path does not allow CR or LF characters");
209+
php_url_free(resource);
210+
efree(transport_string);
211+
return NULL;
212+
}
213+
202214
if (context && (tmpzval = php_stream_context_get_option(context, wrapper->wops->label, "timeout")) != NULL) {
203215
double d = zval_get_double(tmpzval);
204216
#ifndef PHP_WIN32
@@ -379,12 +391,6 @@ php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
379391
smart_str_appends(&req_buf, "GET ");
380392
}
381393

382-
/* Should we send the entire path in the request line, default to no. */
383-
if (!request_fulluri && context &&
384-
(tmpzval = php_stream_context_get_option(context, "http", "request_fulluri")) != NULL) {
385-
request_fulluri = zend_is_true(tmpzval);
386-
}
387-
388394
if (request_fulluri) {
389395
/* Ask for everything */
390396
smart_str_appends(&req_buf, path);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
GHSA-c5f2-jwm7-mmq2 (Configuring a proxy in a stream context might allow for CRLF injection in URIs)
3+
--INI--
4+
allow_url_fopen=1
5+
--CONFLICTS--
6+
server
7+
--FILE--
8+
<?php
9+
$serverCode = <<<'CODE'
10+
echo $_SERVER['REQUEST_URI'];
11+
CODE;
12+
13+
include __DIR__."/../../../../sapi/cli/tests/php_cli_server.inc";
14+
php_cli_server_start($serverCode, null);
15+
16+
$host = PHP_CLI_SERVER_ADDRESS;
17+
$userinput = "index.php HTTP/1.1\r\nHost: $host\r\n\r\nGET /index2.php HTTP/1.1\r\nHost: $host\r\n\r\nGET /index.php";
18+
$context = stream_context_create(['http' => ['proxy' => 'tcp://' . $host, 'request_fulluri' => true]]);
19+
echo file_get_contents("http://$host/$userinput", false, $context);
20+
?>
21+
--EXPECTF--
22+
Warning: file_get_contents(http://localhost:%d/index.php HTTP/1.1
23+
Host: localhost:%d
24+
25+
GET /index2.php HTTP/1.1
26+
Host: localhost:%d
27+
28+
GET /index.php): failed to open stream: HTTP wrapper full URI path does not allow CR or LF characters in %s on line %d

0 commit comments

Comments
 (0)