Skip to content

Fix #78719: http wrapper silently ignores long Location headers #6676

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

#include "php_fopen_wrappers.h"

#define HTTP_HEADER_BLOCK_SIZE 1024
#define HTTP_HEADER_BLOCK_SIZE 2024
#define PHP_URL_REDIRECT_MAX 20
#define HTTP_HEADER_USER_AGENT 1
#define HTTP_HEADER_HOST 2
Expand Down Expand Up @@ -741,7 +741,15 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
char *e = http_header_line + http_header_line_length - 1;
char *http_header_value;
if (*e != '\n') {
do { /* partial header */
/* fail on overlong Location header */
if (!strncasecmp(http_header_line, "Location:", sizeof("Location:")-1)) {
php_stream_close(stream);
stream = NULL;
php_stream_wrapper_log_error(wrapper, options, "Location header too long");
goto out;
}
/* silently discard other overlong headers */
do {
if (php_stream_get_line(stream, http_header_line, HTTP_HEADER_BLOCK_SIZE, &http_header_line_length) == NULL) {
php_stream_wrapper_log_error(wrapper, options, "Failed to read HTTP headers");
goto out;
Expand Down
33 changes: 33 additions & 0 deletions ext/standard/tests/http/bug78719.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
Bug #78719 (http wrapper silently ignores long Location headers)
--SKIPIF--
<?php require 'server.inc'; http_server_skipif('tcp://127.0.0.1:12342'); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$url = str_repeat('*', 1024);
$responses = array(
"data://text/plain,HTTP/1.0 302 Ok\r\nLocation: $url\r\n\r\nBody",
"data://text/plain,HTTP/1.0 302 Ok\r\nLocation: $url$url\r\n\r\nBody",
);

$pid = http_server("tcp://127.0.0.1:12342", $responses, $output);

function test() {
$context = stream_context_create(['http' => ['follow_location' => 0]]);
$f = file_get_contents('http://127.0.0.1:12342/', false, $context);
var_dump($f);
}
test();
test();

http_server_kill($pid);
?>
--EXPECTF--
string(4) "Body"

Warning: file_get_contents(http://127.0.0.1:12342/): failed to open stream: Location header too long in %s on line %d
bool(false)