-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix GH-16137: "Deduplicate" *Forwarded* http headers values. #16154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1680,14 +1680,33 @@ static void php_cli_server_client_save_header(php_cli_server_client *client) | |
{ | ||
/* Wrap header value in a zval to add is to the HashTable which acts as an array */ | ||
zval tmp; | ||
ZVAL_STR(&tmp, client->current_header_value); | ||
/* strip off the colon */ | ||
zend_string *lc_header_name = zend_string_tolower_ex(client->current_header_name, /* persistent */ true); | ||
GC_MAKE_PERSISTENT_LOCAL(lc_header_name); | ||
|
||
/* Add the wrapped zend_string to the HashTable */ | ||
zend_hash_add(&client->request.headers, lc_header_name, &tmp); | ||
zend_hash_add(&client->request.headers_original_case, client->current_header_name, &tmp); | ||
zval *entry = zend_hash_find(&client->request.headers, lc_header_name); | ||
bool with_comma = !zend_string_equals_literal(lc_header_name, "set-cookie"); | ||
|
||
/** | ||
* `Set-Cookie` HTTP header being the exception, they can have 1 or more values separated | ||
* by a comma while still possibly be set separately by the client. | ||
**/ | ||
if (!with_comma || entry == NULL) { | ||
ZVAL_STR(&tmp, client->current_header_value); | ||
} else { | ||
zend_string *curval = Z_STR_P(entry); | ||
zend_string *newval = zend_string_safe_alloc(1, ZSTR_LEN(curval), ZSTR_LEN(client->current_header_value) + 2, /* persistent */true); | ||
|
||
memcpy(ZSTR_VAL(newval), ZSTR_VAL(curval), ZSTR_LEN(curval)); | ||
memcpy(ZSTR_VAL(newval) + ZSTR_LEN(curval), ", ", 2); | ||
memcpy(ZSTR_VAL(newval) + ZSTR_LEN(curval) + 2, ZSTR_VAL(client->current_header_value), ZSTR_LEN(client->current_header_value) + 1); | ||
|
||
ZVAL_STR(&tmp, newval); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use |
||
} | ||
|
||
/* Add/Update the wrapped zend_string to the HashTable */ | ||
zend_hash_update(&client->request.headers, lc_header_name, &tmp); | ||
zend_hash_update(&client->request.headers_original_case, client->current_header_name, &tmp); | ||
|
||
zend_string_release_ex(lc_header_name, /* persistent */ true); | ||
zend_string_release_ex(client->current_header_name, /* persistent */ true); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
Bug GH-16137 duplicate *Forwarded* HTTP headers values. | ||
--INI-- | ||
allow_url_fopen=1 | ||
--SKIPIF-- | ||
<?php | ||
include "skipif.inc"; | ||
?> | ||
--FILE-- | ||
<?php | ||
include "php_cli_server.inc"; | ||
php_cli_server_start("echo \$_SERVER['HTTP_X_FORWARDED_FOR'];"); | ||
$ctx = stream_context_create(array('http' => array ( | ||
'method' => 'POST', | ||
'header' => array('x-forwarded-for: 127.0.0.1', 'x-forwarded-for: 192.168.1.254') | ||
))); | ||
var_dump(file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS, true, $ctx)); | ||
?> | ||
--EXPECT-- | ||
string(24) "127.0.0.1, 192.168.1.254" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.