Skip to content

Commit 9b21b78

Browse files
committed
add test
1 parent 4ab9663 commit 9b21b78

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

sapi/cli/php_cli_server.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,19 +1678,20 @@ static int php_cli_server_client_read_request_on_fragment(php_http_parser *parse
16781678

16791679
static void php_cli_server_client_save_header(php_cli_server_client *client)
16801680
{
1681-
zval *entry;
16821681
/* Wrap header value in a zval to add is to the HashTable which acts as an array */
16831682
zval tmp;
16841683
/* strip off the colon */
16851684
zend_string *lc_header_name = zend_string_tolower_ex(client->current_header_name, /* persistent */ true);
16861685
GC_MAKE_PERSISTENT_LOCAL(lc_header_name);
16871686

1687+
zval *entry = zend_hash_find(&client->request.headers, lc_header_name);
1688+
bool is_forwarded_header = strstr(ZSTR_VAL(lc_header_name), "forwarded");
1689+
16881690
/**
16891691
* **Forwarded** HTTP family headers can have 1 or more values separated by a comma while still
16901692
* possibly be set separately by the client.
16911693
**/
1692-
if (!strstr(ZSTR_VAL(lc_header_name), "forwarded") ||
1693-
(entry = zend_hash_find(&client->request.headers, lc_header_name)) == NULL) {
1694+
if ((entry == NULL && is_forwarded_header) || !is_forwarded_header) {
16941695
ZVAL_STR(&tmp, client->current_header_value);
16951696

16961697
/* Add the wrapped zend_string to the HashTable */

sapi/cli/tests/gh16137.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug GH-16137 duplicate *Forwarded* HTTP headers values.
3+
--INI--
4+
allow_url_fopen=1
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
?>
9+
--FILE--
10+
<?php
11+
include "php_cli_server.inc";
12+
php_cli_server_start("echo \$_SERVER['HTTP_X_FORWARDED_FOR'];");
13+
$ctx = stream_context_create(array('http' => array (
14+
'method' => 'POST',
15+
'header' => array('x-forwarded-for: 127.0.0.1', 'x-forwarded-for: 192.168.1.254')
16+
)));
17+
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, true, $ctx));
18+
?>
19+
--EXPECT--
20+
string(23) "127.0.0.1,192.168.1.254"

0 commit comments

Comments
 (0)