Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ PHP NEWS
. Fixed a use-after-free when a user stream wrapper unregisters itself from
its constructor, destructor or __set() during unlink(), rename(), mkdir(),
rmdir(), touch(), chmod() or stat(). (Ilia Alshanetsky)
. Fixed bug GH-25880 (HTTP wrapper resolves a network-path reference
"//host/path" in the Location header against the original host).
(Matyapiro31)

- Standard:
. Made extension names in ini_get_all() case-insensitive. (Weilin Du)
Expand Down
7 changes: 6 additions & 1 deletion ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,12 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,

char *new_path = NULL;

if (header_info.location_len < 8 ||
if (header_info.location_len >= 2 && header_info.location[0] == '/' && header_info.location[1] == '/') {
/* Network-path reference (RFC 3986 section 4.2): keep the scheme, replace authority and path. */
spprintf(&new_path, 0, "%s:%s", ZSTR_VAL(resource->scheme), header_info.location);
efree(header_info.location);
header_info.location = NULL;
} else if (header_info.location_len < 8 ||
(strncasecmp(header_info.location, "http://", sizeof("http://")-1) &&
strncasecmp(header_info.location, "https://", sizeof("https://")-1) &&
strncasecmp(header_info.location, "ftp://", sizeof("ftp://")-1) &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Network-path reference Location header ("//host/path") is resolved against the current scheme
--DESCRIPTION--
RFC 3986 section 4.2 / RFC 9110 section 10.2.2: a Location value starting with "//"
replaces authority and path and keeps the scheme. It must not be treated as an absolute
path on the original host.
--FILE--
<?php
$serverCode = <<<'CODE'
$server = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr);
phpt_notify_server_start($server);
$addr = stream_socket_get_name($server, false);

for ($n = 0; $n < 2; $n++) {
$conn = stream_socket_accept($server, 10);
if (!$conn) {
break;
}
$req = fgets($conn);
$host = '';
while (($line = trim(fgets($conn))) !== '') {
if (stripos($line, 'Host:') === 0) {
$host = trim(substr($line, 5));
}
}
$uri = explode(' ', $req)[1];
if ($n < 1) {
fwrite($conn, "HTTP/1.1 302 Found\r\nLocation: //$addr/target\r\nContent-Length: 0\r\n\r\n");
} else {
$body = "uri=$uri host_is_server=" . ($host === $addr ? 'yes' : 'no');
fwrite($conn, "HTTP/1.1 200 OK\r\nContent-Length: " . strlen($body) . "\r\n\r\n$body");
}
fclose($conn);
}
CODE;

$clientCode = <<<'CODE'
$ctx = stream_context_create(['http' => ['follow_location' => 1]]);
echo @file_get_contents("http://{{ ADDR }}/a/b", false, $ctx), "\n";
CODE;

include sprintf("%s/../../../openssl/tests/ServerClientTestCase.inc", __DIR__);
ServerClientTestCase::getInstance()->run($clientCode, $serverCode);
?>
--EXPECT--
uri=/target host_is_server=yes
Loading