Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea5dfcf

Browse files
committed
Allow URL and URN to be used as redirection URIs
1 parent b4128fd commit ea5dfcf

File tree

2 files changed

+55
-26
lines changed

2 files changed

+55
-26
lines changed

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ public function checkRequestPath(Request $request, string $path)
148148
*/
149149
public function generateUri(Request $request, string $path)
150150
{
151-
if (str_starts_with($path, 'http') || !$path) {
151+
$parsedUrl = parse_url($path);
152+
$uriScheme = $parsedUrl['scheme'] ?? null;
153+
$uriHost = $parsedUrl['host'] ?? null;
154+
$uriPath = $parsedUrl['path'] ?? '';
155+
if (($uriScheme === 'urn' && $uriHost === null && $uriPath !== null) || ($uriScheme !== null && $uriHost !== null)) {
152156
return $path;
153157
}
154158

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,62 @@
2525

2626
class HttpUtilsTest extends TestCase
2727
{
28-
public function testCreateRedirectResponseWithPath()
28+
/**
29+
* @dataProvider validRequestDomainUrls
30+
*/
31+
public function testCreateRedirectResponseWithPath(?string $domainRegexp, string $path, string $expectedRedirectUri)
2932
{
30-
$utils = new HttpUtils($this->getUrlGenerator());
31-
$response = $utils->createRedirectResponse($this->getRequest(), '/foobar');
33+
$utils = new HttpUtils($this->getUrlGenerator(), null, $domainRegexp);
34+
$response = $utils->createRedirectResponse($this->getRequest(), $path);
3235

33-
$this->assertTrue($response->isRedirect('http://localhost/foobar'));
36+
$this->assertTrue($response->isRedirect($expectedRedirectUri));
3437
$this->assertEquals(302, $response->getStatusCode());
3538
}
3639

37-
public function testCreateRedirectResponseWithAbsoluteUrl()
38-
{
39-
$utils = new HttpUtils($this->getUrlGenerator());
40-
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/');
41-
42-
$this->assertTrue($response->isRedirect('http://symfony.com/'));
43-
}
44-
45-
public function testCreateRedirectResponseWithDomainRegexp()
40+
public static function validRequestDomainUrls()
4641
{
47-
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://symfony\.com$#i');
48-
$response = $utils->createRedirectResponse($this->getRequest(), 'http://symfony.com/blog');
49-
50-
$this->assertTrue($response->isRedirect('http://symfony.com/blog'));
51-
}
52-
53-
public function testCreateRedirectResponseWithRequestsDomain()
54-
{
55-
$utils = new HttpUtils($this->getUrlGenerator(), null, '#^https?://%s$#i');
56-
$response = $utils->createRedirectResponse($this->getRequest(), 'http://localhost/blog');
57-
58-
$this->assertTrue($response->isRedirect('http://localhost/blog'));
42+
return [
43+
'/foobar' => [
44+
null,
45+
'/foobar',
46+
'http://localhost/foobar',
47+
],
48+
'http://symfony.com/ without domain regex' => [
49+
null,
50+
'http://symfony.com/',
51+
'http://symfony.com/',
52+
],
53+
'http://localhost/blog with #^https?://symfony\.com$#i' => [
54+
'#^https?://symfony\.com$#i',
55+
'http://symfony.com/blog',
56+
'http://symfony.com/blog',
57+
],
58+
'http://localhost/blog with #^https?://%s$#i' => [
59+
'#^https?://%s$#i',
60+
'http://localhost/blog',
61+
'http://localhost/blog',
62+
],
63+
'custom scheme' => [
64+
null,
65+
'android-app://com.google.android.gm/',
66+
'android-app://com.google.android.gm/',
67+
],
68+
'custom scheme with all URL components' => [
69+
null,
70+
'android-app://foo:[email protected]:8080/software/index.html?lite=true#section1',
71+
'android-app://foo:[email protected]:8080/software/index.html?lite=true#section1',
72+
],
73+
'URN #1' => [
74+
null,
75+
'urn:ietf:rfc:8141',
76+
'urn:ietf:rfc:8141',
77+
],
78+
'URN #2' => [
79+
null,
80+
'urn:EXAMPLE:a123,z456',
81+
'urn:EXAMPLE:a123,z456',
82+
],
83+
];
5984
}
6085

6186
/**

0 commit comments

Comments
 (0)