8000 Work around `parse_url()` bug by nicolas-grekas · Pull Request #58218 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Work around parse_url() bug #58218

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

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,19 @@ public function urlRedirectAction(Request $request, string $path, bool $permanen
$statusCode = $permanent ? 301 : 302;
}

if (null === $scheme) {
$scheme = $request->getScheme();
}

if (str_starts_with($path, '//')) {
$path = $scheme.':'.$path;
}

// redirect if the path is a full URL
if (parse_url($path, \PHP_URL_SCHEME)) {
return new RedirectResponse($path, $statusCode);
}

if (null === $scheme) {
$scheme = $request->getScheme();
}

if ($qs = $request->server->get('QUERY_STRING') ?: $request->getQueryString()) {
if (!str_contains($path, '?')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ public function testFullURLWithMethodKeep()
$this->assertEquals(307, $returnResponse->getStatusCode());
}

public function testProtocolRelative()
{
$request = new Request();
$controller = new RedirectController();

$returnResponse = $controller->urlRedirectAction($request, '//foo.bar/');
$this->assertRedirectUrl($returnResponse, 'http://foo.bar/');
$this->assertSame(302, $returnResponse->getStatusCode());

$returnResponse = $controller->urlRedirectAction($request, '//foo.bar/', false, 'https');
$this->assertRedirectUrl($returnResponse, 'https://foo.bar/');
$this->assertSame(302, $returnResponse->getStatusCode());
}

public function testUrlRedirectDefaultPorts()
{
$host = 'www.example.com';
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/Tests/UriResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function provideResolverTests()
['foo', 'http://localhost#bar', 'http://localhost/foo'],

['http://', 'http://localhost', 'http://'],
['/foo:123', 'http://localhost', 'http://localhost/foo:123'],
];
}
}
6 changes: 5 additions & 1 deletion src/Symfony/Component/DomCrawler/UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public static function resolve(string $uri, ?string $baseUri): string
{
$uri = trim($uri);

if (false === ($scheme = parse_url($uri, \PHP_URL_SCHEME)) && '/' === ($uri[0] ?? '')) {
$scheme = parse_url($uri.'#', \PHP_URL_SCHEME);
}

// absolute URL?
if (null !== parse_url($uri, \PHP_URL_SCHEME)) {
if (null !== $scheme) {
return $uri;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,10 @@ private static function resolveUrl(array $url, ?array $base, array $queryDefault
private static function parseUrl(string $url, array $query = [], array $allowedSchemes = ['http' => 80, 'https' => 443]): array
{
if (false === $parts = parse_url($url)) {
throw new InvalidArgumentException(sprintf('Malformed URL "%s".', $url));
if ('/' !== ($url[0] ?? '') || false === $parts = parse_url($url.'#')) {
throw new InvalidArgumentException(sprintf('Malformed URL "%s".', $url));
}
unset($parts['fragment']);
}

if ($query) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/NativeHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,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)) {
if (false !== (parse_url($location.'#', \PHP_URL_HOST) ?? false)) {
// 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;
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,11 @@ public static function create(string $uri, string $method = 'GET', array $parame
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);

$components = parse_url($uri);
if (false === ($components = parse_url($uri)) && '/' === ($uri[0] ?? '')) {
$components = parse_url($uri.'#');
unset($components['fragment']);
}

if (isset($components['host'])) {
$server['SERVER_NAME'] = $components['host'];
$server['HTTP_HOST'] = $components['host'];
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public function testCreate()
// Fragment should not be included in the URI
$request = Request::create('http://test.com/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());

$request = Request::create('/foo:123');
$this->assertEquals('http://localhost/foo:123', $request->getUri());
}

public function testCreateWithRequestUri()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function buildExitPath(?string $targetUri = null): string
$targetUri = $request->getRequestUri();
}

$targetUri .= (parse_url($targetUri, \PHP_URL_QUERY) ? '&' : '?').http_build_query([$switchUserConfig['parameter'] => SwitchUserListener::EXIT_VALUE], '', '&');
$targetUri .= (str_contains($targetUri, '?') ? '&' : '?').http_build_query([$switchUserConfig['parameter'] => SwitchUserListener::EXIT_VALUE], '', '&');

return $targetUri;
}
Expand Down
Loading
0