diff --git a/HttpUtils.php b/HttpUtils.php index 6a1cb945..a4e03713 100644 --- a/HttpUtils.php +++ b/HttpUtils.php @@ -148,7 +148,9 @@ public function checkRequestPath(Request $request, string $path) */ public function generateUri(Request $request, string $path) { - if (str_starts_with($path, 'http') || !$path) { + $url = parse_url($path); + + if ('' === $path || isset($url['scheme'], $url['host'])) { return $path; } diff --git a/Tests/HttpUtilsTest.php b/Tests/HttpUtilsTest.php index 4a1ba23a..7686a296 100644 --- a/Tests/HttpUtilsTest.php +++ b/Tests/HttpUtilsTest.php @@ -58,6 +58,54 @@ public function testCreateRedirectResponseWithRequestsDomain() $this->assertTrue($response->isRedirect('http://localhost/blog')); } + /** + * @dataProvider validRequestDomainUrls + */ + public function testCreateRedirectResponse(?string $domainRegexp, string $path, string $expectedRedirectUri) + { + $utils = new HttpUtils($this->getUrlGenerator(), null, $domainRegexp); + $response = $utils->createRedirectResponse($this->getRequest(), $path); + + $this->assertTrue($response->isRedirect($expectedRedirectUri)); + $this->assertEquals(302, $response->getStatusCode()); + } + + public static function validRequestDomainUrls() + { + return [ + '/foobar' => [ + null, + '/foobar', + 'http://localhost/foobar', + ], + 'http://symfony.com/ without domain regex' => [ + null, + 'http://symfony.com/', + 'http://symfony.com/', + ], + 'http://localhost/blog with #^https?://symfony\.com$#i' => [ + '#^https?://symfony\.com$#i', + 'http://symfony.com/blog', + 'http://symfony.com/blog', + ], + 'http://localhost/blog with #^https?://%s$#i' => [ + '#^https?://%s$#i', + 'http://localhost/blog', + 'http://localhost/blog', + ], + 'custom scheme' => [ + null, + 'android-app://com.google.android.gm/', + 'android-app://com.google.android.gm/', + ], + 'custom scheme with all URL components' => [ + null, + 'android-app://foo:bar@www.example.com:8080/software/index.html?lite=true#section1', + 'android-app://foo:bar@www.example.com:8080/software/index.html?lite=true#section1', + ], + ]; + } + /** * @dataProvider badRequestDomainUrls */ @@ -77,6 +125,7 @@ public static function badRequestDomainUrls() ['http:/\\pirate.net/foo'], ['http:\\/pirate.net/foo'], ['http://////pirate.net/foo'], + ['http:///foo'], ]; }