-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Work around parse_url()
bug (bis)
#58836
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -389,6 +389,7 @@ private static function createRedirectResolver(array $options, string $host, ?ar | |
return null; | ||
} | ||
|
||
$locationHasHost = isset($url['authority']); | ||
$url = self::resolveUrl($url, $info['url']); | ||
$info['redirect_url'] = implode('', $url); | ||
|
||
|
@@ -424,7 +425,7 @@ private static function createRedirectResolver(array $options, string $host, ?ar | |
|
||
[$host, $port] = self::parseHostPort($url, $info); | ||
|
||
if (false !== (parse_url($location.'#', \PHP_URL_HOST) ?? false)) { | ||
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. stop using parse_url on user-input |
||
if ($locationHasHost) { | ||
// Authorization and Cookie headers MUST NOT follow except for the initial host name | ||
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth']; | ||
$requestHeaders[] = 'Host: '.$host.$port; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,17 +436,18 @@ private static function parseHeaderLine($ch, string $data, array &$info, array & | |
$info['http_method'] = 'HEAD' === $info['http_method'] ? 'HEAD' : 'GET'; | ||
curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, $info['http_method']); | ||
} | ||
$locationHasHost = false; | ||
|
||
if (null === $info['redirect_url'] = $resolveRedirect($ch, $location, $noContent)) { | ||
if (null === $info['redirect_url'] = $resolveRedirect($ch, $location, $noContent, $locationHasHost)) { | ||
$options['max_redirects'] = curl_getinfo($ch, \CURLINFO_REDIRECT_COUNT); | ||
curl_setopt($ch, \CURLOPT_FOLLOWLOCATION, false); | ||
curl_setopt($ch, \CURLOPT_MAXREDIRS, $options['max_redirects']); | ||
} else { | ||
$url = parse_url($location ?? ':'); | ||
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. stop using parse_url on user-input here also |
||
} elseif ($locationHasHost) { | ||
$url = parse_url($info['redirect_url']); | ||
|
||
if (isset($url['host']) && null !== $ip = $multi->dnsCache->hostnames[$url['host'] = strtolower($url['host'])] ?? null) { | ||
if (null !== $ip = $multi->dnsCache->hostnames[$url['host'] = strtolower($url['host'])] ?? null) { | ||
// Populate DNS cache for redirects if needed | ||
$port = $url['port'] ?? ('http' === ($url['scheme'] ?? parse_url(curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL), \PHP_URL_SCHEME)) ? 80 : 443); | ||
$port = $url['port'] ?? ('http' === $url['scheme'] ? 80 : 443); | ||
curl_setopt($ch, \CURLOPT_RESOLVE, ["{$url['host']}:$port:$ip"]); | ||
$multi->dnsCache->removals["-{$url['host']}:$port"] = "-{$url['host']}:$port"; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,12 +358,7 @@ public static function create(string $uri, string $method = 'GET', array $parame | |
$server['PATH_INFO'] = ''; | ||
$server['REQUEST_METHOD'] = strtoupper($method); | ||
|
||
if (false === ($components = parse_url($uri)) && '/' === ($uri[0] ?? '')) { | ||
$components = parse_url($uri.'#'); | ||
unset($components['fragment']); | ||
} | ||
|
||
if (false === $components) { | ||
if (false === $components = parse_url(\strlen($uri) !== strcspn($uri, '?#') ? $uri : $uri.'#')) { | ||
throw new BadRequestException('Invalid URI.'); | ||
} | ||
|
||
|
@@ -386,9 +381,11 @@ public static function create(string $uri, string $method = 'GET', array $parame | |
if ('https' === $components['scheme']) { | ||
$server['HTTPS'] = 'on'; | ||
$server['SERVER_PORT'] = 443; | ||
} else { | ||
} elseif ('http' === $components['scheme']) { | ||
unset($server['HTTPS']); | ||
$server['SERVER_PORT'] = 80; | ||
} else { | ||
throw new BadRequestException('Invalid URI: http(s) scheme expected.'); | ||
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. looks like a missing check here, |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wondered if we could add the same logic to Request::create() but we have a test case that ensure
Request::create("test.com:80")
is parsed as host+port (which is not how the URL spec would parse it)